PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/nselib/unpwdb.lua

https://github.com/prakashgamit/nmap
Lua | 329 lines | 166 code | 41 blank | 122 comment | 31 complexity | 41bc9ff339acdecd85922c57b1be26b9 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. ---
  2. -- Username/password database library.
  3. --
  4. -- The <code>usernames</code> and <code>passwords</code> functions return
  5. -- multiple values for use with exception handling via
  6. -- <code>nmap.new_try</code>. The first value is the Boolean success
  7. -- indicator, the second value is the closure.
  8. --
  9. -- The closures can take an argument of <code>"reset"</code> to rewind the list
  10. -- to the beginning.
  11. --
  12. -- To avoid taking a long time against slow services, the closures will
  13. -- stop returning values (start returning <code>nil</code>) after a
  14. -- certain time. The time depends on the timing template level, and is
  15. -- * <code>-T3</code> or less: 10 minutes
  16. -- * <code>-T4</code>: 5 minutes
  17. -- * <code>-T5</code>: 3 minutes
  18. -- Time limits are increased by 50% if a custom username or password
  19. -- database is used with the <code>userdb</code> or <code>passdb</code>
  20. -- script arguments. You can control the time limit directly with the
  21. -- <code>unpwdb.timelimit</code> script argument. Use
  22. -- <code>unpwdb.timelimit=0</code> to disable the time limit.
  23. --
  24. -- You can select your own username and/or password database to read from with
  25. -- the script arguments <code>userdb</code> and <code>passdb</code>,
  26. -- respectively. Comments are allowed in these files, prefixed with
  27. -- <code>"#!comment:"</code>. Comments cannot be on the same line as a
  28. -- username or password because this leaves too much ambiguity, e.g. does the
  29. -- password in <code>"mypass #!comment: blah"</code> contain a space, two
  30. -- spaces, or do they just separate the password from the comment?
  31. --
  32. -- @usage
  33. -- require("unpwdb")
  34. --
  35. -- local usernames, passwords
  36. -- local try = nmap.new_try()
  37. --
  38. -- usernames = try(unpwdb.usernames())
  39. -- passwords = try(unpwdb.passwords())
  40. --
  41. -- for password in passwords do
  42. -- for username in usernames do
  43. -- -- Do something with username and password.
  44. -- end
  45. -- usernames("reset")
  46. -- end
  47. --
  48. -- @usage
  49. -- nmap --script-args userdb=/tmp/user.lst
  50. -- nmap --script-args unpwdb.timelimit=10m
  51. --
  52. -- @args userdb The filename of an alternate username database.
  53. -- @args passdb The filename of an alternate password database.
  54. -- @args unpwdb.userlimit The maximum number of usernames
  55. -- <code>usernames</code> will return (default unlimited).
  56. -- @args unpwdb.passlimit The maximum number of passwords
  57. -- <code>passwords</code> will return (default unlimited).
  58. -- @args unpwdb.timelimit The maximum amount of time that any iterator will run
  59. -- before stopping. The value is in seconds by default and you can follow it
  60. -- with <code>ms</code>, <code>s</code>, <code>m</code>, or <code>h</code> for
  61. -- milliseconds, seconds, minutes, or hours. For example,
  62. -- <code>unpwdb.timelimit=30m</code> or <code>unpwdb.timelimit=.5h</code> for
  63. -- 30 minutes. The default depends on the timing template level (see the module
  64. -- description). Use the value <code>0</code> to disable the time limit.
  65. -- @author Kris Katterjohn 06/2008
  66. -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html
  67. local io = require "io"
  68. local nmap = require "nmap"
  69. local os = require "os"
  70. local stdnse = require "stdnse"
  71. _ENV = stdnse.module("unpwdb", stdnse.seeall)
  72. local usertable = {}
  73. local passtable = {}
  74. local customdata = false
  75. -- So I don't have to type as much :)
  76. local args = nmap.registry.args
  77. local userfile = function()
  78. if args.userdb then
  79. customdata = true
  80. return args.userdb
  81. end
  82. return nmap.fetchfile("nselib/data/usernames.lst")
  83. end
  84. local passfile = function()
  85. if args.passdb then
  86. customdata = true
  87. return args.passdb
  88. end
  89. return nmap.fetchfile("nselib/data/passwords.lst")
  90. end
  91. local filltable = function(filename, table)
  92. if #table ~= 0 then
  93. return true
  94. end
  95. local file = io.open(filename, "r")
  96. if not file then
  97. return false
  98. end
  99. for l in file:lines() do
  100. -- Comments takes up a whole line
  101. if not l:match("#!comment:") then
  102. table[#table + 1] = l
  103. end
  104. end
  105. file:close()
  106. return true
  107. end
  108. table_iterator = function(table)
  109. local i = 1
  110. return function(cmd)
  111. if cmd == "reset" then
  112. i = 1
  113. return
  114. end
  115. local elem = table[i]
  116. if elem then i = i + 1 end
  117. return elem
  118. end
  119. end
  120. --- Returns the suggested number of seconds to attempt a brute force attack,
  121. -- based on the <code>unpwdb.timelimit</code> script argument, Nmap's timing
  122. -- values (<code>-T4</code> etc.) and whether or not a user-defined list is
  123. -- used.
  124. --
  125. -- You can use the script argument <code>notimelimit</code> to make this
  126. -- function return <code>nil</code>, which means the brute-force should run
  127. -- until the list is empty. If <code>notimelimit</code> is not used, be sure to
  128. -- still check for <code>nil</code> return values on the above two functions in
  129. -- case you finish before the time limit is up.
  130. timelimit = function()
  131. -- If we're reading from a user-defined username or password list,
  132. -- we'll give them a timeout 1.5x the default. If the "notimelimit"
  133. -- script argument is used, we return nil.
  134. local t = nmap.timing_level()
  135. -- Easy enough
  136. if args.notimelimit then
  137. return nil
  138. end
  139. if args["unpwdb.timelimit"] then
  140. local limit, err = stdnse.parse_timespec(args["unpwdb.timelimit"])
  141. if not limit then
  142. error(err)
  143. end
  144. return limit
  145. end
  146. if t <= 3 then
  147. return (customdata and 900) or 600
  148. elseif t == 4 then
  149. return (customdata and 450) or 300
  150. elseif t == 5 then
  151. return (customdata and 270) or 180
  152. end
  153. end
  154. --- Returns a function closure which returns a new username with every call
  155. -- until the username list is exhausted (in which case it returns
  156. -- <code>nil</code>).
  157. -- @return boolean Status.
  158. -- @return function The usernames iterator.
  159. local usernames_raw = function()
  160. local path = userfile()
  161. if not path then
  162. return false, "Cannot find username list"
  163. end
  164. if not filltable(path, usertable) then
  165. return false, "Error parsing username list"
  166. end
  167. return true, table_iterator(usertable)
  168. end
  169. --- Returns a function closure which returns a new password with every call
  170. -- until the password list is exhausted (in which case it returns
  171. -- <code>nil</code>).
  172. -- @return boolean Status.
  173. -- @return function The passwords iterator.
  174. local passwords_raw = function()
  175. local path = passfile()
  176. if not path then
  177. return false, "Cannot find password list"
  178. end
  179. if not filltable(path, passtable) then
  180. return false, "Error parsing password list"
  181. end
  182. return true, table_iterator(passtable)
  183. end
  184. --- Wraps time and count limits around an iterator. When either limit expires,
  185. -- starts returning <code>nil</code>. Calling the iterator with an argument of
  186. -- "reset" resets the count.
  187. -- @param time_limit Time limit in seconds. Use 0 or <code>nil</code> for no limit.
  188. -- @param count_limit Count limit in seconds. Use 0 or <code>nil</code> for no limit.
  189. -- @return boolean Status.
  190. -- @return function The wrapped iterator.
  191. limited_iterator = function(iterator, time_limit, count_limit)
  192. local start, count, elem
  193. time_limit = (time_limit and time_limit > 0) and time_limit
  194. count_limit = (count_limit and count_limit > 0) and count_limit
  195. start = os.time()
  196. count = 0
  197. return function(cmd)
  198. if cmd == "reset" then
  199. count = 0
  200. else
  201. count = count + 1
  202. end
  203. if count_limit and count > count_limit then
  204. return
  205. end
  206. if time_limit and os.time() - start >= time_limit then
  207. return
  208. end
  209. return iterator(cmd)
  210. end
  211. end
  212. --- Returns a function closure which returns a new password with every call
  213. -- until the username list is exhausted or either limit expires (in which cases
  214. -- it returns <code>nil</code>).
  215. -- @param time_limit Time limit in seconds. Use 0 for no limit.
  216. -- @param count_limit Count limit in seconds. Use 0 for no limit.
  217. -- @return boolean Status.
  218. -- @return function The usernames iterator.
  219. usernames = function(time_limit, count_limit)
  220. local status, iterator
  221. status, iterator = usernames_raw()
  222. if not status then
  223. return false, iterator
  224. end
  225. time_limit = time_limit or timelimit()
  226. if not count_limit and args["unpwdb.userlimit"] then
  227. count_limit = tonumber(args["unpwdb.userlimit"])
  228. end
  229. return true, limited_iterator(iterator, time_limit, count_limit)
  230. end
  231. --- Returns a function closure which returns a new password with every call
  232. -- until the password list is exhausted or either limit expires (in which cases
  233. -- it returns <code>nil</code>).
  234. -- @param time_limit Time limit in seconds. Use 0 for no limit.
  235. -- @param count_limit Count limit in seconds. Use 0 for no limit.
  236. -- @return boolean Status.
  237. -- @return function The passwords iterator.
  238. passwords = function(time_limit, count_limit)
  239. local status, iterator
  240. status, iterator = passwords_raw()
  241. if not status then
  242. return false, iterator
  243. end
  244. time_limit = time_limit or timelimit()
  245. if not count_limit and args["unpwdb.passlimit"] then
  246. count_limit = tonumber(args["unpwdb.passlimit"])
  247. end
  248. return true, limited_iterator(iterator, time_limit, count_limit)
  249. end
  250. --- Returns a new iterator that iterates trough it's consecutive iterators,
  251. -- basically concatenating them.
  252. -- @param iter1 First iterator to concatenate.
  253. -- @param iter2 Second iterator to concatenate.
  254. -- @return function The concatenated iterators.
  255. function concat_iterators (iter1, iter2)
  256. local function helper (next_iterator, command, first, ...)
  257. if first ~= nil then
  258. return first, ...
  259. elseif next_iterator ~= nil then
  260. return helper(nil, command, next_iterator(command))
  261. end
  262. end
  263. local function iterator (command)
  264. if command == "reset" then
  265. iter1 "reset"
  266. iter2 "reset"
  267. else
  268. return helper(iter2, command, iter1(command))
  269. end
  270. end
  271. return iterator
  272. end
  273. --- Returns a new iterator that filters it's results based on the filter.
  274. -- @param iterator Iterator that needs to be filtered
  275. -- @param filter Function that returns bool, which serves as a filter
  276. -- @return function The filtered iterator.
  277. function filter_iterator (iterator, filter)
  278. local function helper (...)
  279. if filter(...) then
  280. return ...
  281. end
  282. end
  283. local function filter (command)
  284. return helper(iterator(command))
  285. end
  286. return filter
  287. end
  288. return _ENV;