PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/linkedfs/usr/share/nmap/nselib/unpwdb.lua

https://bitbucket.org/harakiri/trk
Lua | 161 lines | 84 code | 30 blank | 47 comment | 18 complexity | 3dedf8d7a2689bda1bafb3629f0e3075 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. --- Username/password database library.
  2. --
  3. -- The <code>usernames</code> and <code>passwords</code> functions return
  4. -- multiple values for use with exception handling via
  5. -- <code>nmap.new_try</code>. The first value is the Boolean success
  6. -- indicator, the second value is the closure.
  7. --
  8. -- The closures can take an argument of <code>"reset"</code> to rewind the list
  9. -- to the beginning.
  10. --
  11. -- You can select your own username and/or password database to read from with
  12. -- the script arguments <code>userdb</code> and <code>passdb</code>,
  13. -- respectively. Comments are allowed in these files, prefixed with
  14. -- <code>"#!comment:"</code>. Comments cannot be on the same line as a
  15. -- username or password because this leaves too much ambiguity, e.g. does the
  16. -- password in <code>"mypass #!comment: blah"</code> contain a space, two
  17. -- spaces, or do they just separate the password from the comment?
  18. --
  19. -- @args userdb The filename of an alternate username database.
  20. -- @args passdb The filename of an alternate password database.
  21. -- @author Kris Katterjohn 06/2008
  22. -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html
  23. module(... or "unpwdb", package.seeall)
  24. local usertable = {}
  25. local passtable = {}
  26. local customdata = false
  27. -- So I don't have to type as much :)
  28. local args = nmap.registry.args
  29. local userfile = function()
  30. if args.userdb then
  31. customdata = true
  32. return args.userdb
  33. end
  34. return nmap.fetchfile("nselib/data/usernames.lst")
  35. end
  36. local passfile = function()
  37. if args.passdb then
  38. customdata = true
  39. return args.passdb
  40. end
  41. return nmap.fetchfile("nselib/data/passwords.lst")
  42. end
  43. local filltable = function(filename, table)
  44. if #table ~= 0 then
  45. return true
  46. end
  47. local file = io.open(filename, "r")
  48. if not file then
  49. return false
  50. end
  51. while true do
  52. local l = file:read()
  53. if not l then
  54. break
  55. end
  56. -- Comments takes up a whole line
  57. if not l:match("#!comment:") then
  58. table[#table + 1] = l
  59. end
  60. end
  61. file:close()
  62. return true
  63. end
  64. local closure = function(table)
  65. local i = 1
  66. return function(cmd)
  67. if cmd == "reset" then
  68. i = 1
  69. return
  70. end
  71. local elem = table[i]
  72. if elem then i = i + 1 end
  73. return elem
  74. end
  75. end
  76. --- Returns the suggested number of seconds to attempt a brute force attack,
  77. -- based on Nmap's timing values (<code>-T4</code> etc.) and whether or not a
  78. -- user-defined list is used.
  79. --
  80. -- You can use the script argument <code>notimelimit</code> to make this
  81. -- function return <code>nil</code>, which means the brute-force should run
  82. -- until the list is empty. If <code>notimelimit</code> is not used, be sure to
  83. -- still check for <code>nil</code> return values on the above two functions in
  84. -- case you finish before the time limit is up.
  85. timelimit = function()
  86. -- If we're reading from a user-defined username or password list,
  87. -- we'll give them a timeout 1.5x the default. If the "notimelimit"
  88. -- script argument is used, we return nil.
  89. local t = nmap.timing_level()
  90. -- Easy enough
  91. if args.notimelimit then
  92. return nil
  93. end
  94. if t <= 3 then
  95. return (customdata and 900) or 600
  96. elseif t == 4 then
  97. return (customdata and 450) or 300
  98. elseif t == 5 then
  99. return (customdata and 270) or 180
  100. end
  101. end
  102. --- Returns a function closure which returns a new username with every call
  103. -- until the username list is exhausted (in which case it returns
  104. -- <code>nil</code>).
  105. -- @return boolean Status.
  106. -- @return function The usernames iterator.
  107. usernames = function()
  108. local path = userfile()
  109. if not path then
  110. return false, "Cannot find username list"
  111. end
  112. if not filltable(path, usertable) then
  113. return false, "Error parsing username list"
  114. end
  115. return true, closure(usertable)
  116. end
  117. --- Returns a function closure which returns a new password with every call
  118. -- until the password list is exhausted (in which case it returns
  119. -- <code>nil</code>).
  120. -- @return boolean Status.
  121. -- @return function The passwords iterator.
  122. passwords = function()
  123. local path = passfile()
  124. if not path then
  125. return false, "Cannot find password list"
  126. end
  127. if not filltable(path, passtable) then
  128. return false, "Error parsing password list"
  129. end
  130. return true, closure(passtable)
  131. end