PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/harakiri/trk
Lua | 736 lines | 370 code | 108 blank | 258 comment | 70 complexity | ebff30a69e028416173a8d22106d5ee5 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. ---This module takes care of the authentication used in SMB (LM, NTLM, LMv2, NTLMv2).
  2. -- There is a lot to this functionality, so if you're interested in how it works, read
  3. -- on.
  4. --
  5. -- In SMB authentication, there are two distinct concepts. Each will be dealt with
  6. -- separately. There are:
  7. -- * Stored hashes
  8. -- * Authentication
  9. --
  10. -- What's confusing is that the same names are used for each of those.
  11. --
  12. -- Stored Hashes
  13. -- Windows stores two types of hashes: Lanman and NT Lanman (or NTLM). Vista and later
  14. -- store NTLM only. Lanman passwords are divided into two 7-character passwords and
  15. -- used as a key in DES, while NTLM is converted to unicode and MD4ed.
  16. --
  17. -- The stored hashes can be dumped in a variety of ways (pwdump6, fgdump, metasploit's
  18. -- priv module, smb-pwdump.nse, etc). Generally, two hashes are dumped together
  19. -- (generally, Lanman:NTLM). Sometimes, Lanman is empty and only NTLM is given. Lanman
  20. -- is never required.
  21. --
  22. -- The password hashes can be given instead of passwords when supplying credentials;
  23. -- this is done by using the <code>smbhash</code> argument. Either a pair of hashes
  24. -- can be passed, in the form of Lanman:NTLM, or a single hash, which is assumed to
  25. -- be NTLM.
  26. --
  27. -- Authentication
  28. -- There are four types of authentication. Confusingly, these have the same names as
  29. -- stored hashes, but only slight relationships. The four types are Lanmanv1, NTLMv1,
  30. -- Lanmanv2, and NTLMv2. By default, Lanmanv1 and NTLMv1 are used together in most
  31. -- applications. These Nmap scripts default to NTLMv1 alone, except in special cases,
  32. -- but it can be overridden by the user.
  33. --
  34. -- Lanmanv1 and NTLMv1 both use DES for their response. The DES mixes a server challenge
  35. -- with the hash (Lanman hash for Lanmanv1 response and NTLMv1 hash for NTLM response).
  36. -- The way the challenge is DESed with the hashes is identical for Lanmanv1 and NTLMv1,
  37. -- the only difference is the starting hash (Lanman vs NTLM).
  38. --
  39. -- Lanmanv2 and NTLMv2 both use HMAC-MD5 for their response. The HMAC-MD5 mixes a
  40. -- server challenge and a client challenge with the NTLM hash, in both cases. The
  41. -- difference between Lanmanv2 and NTLMv2 is the length of the client challenge;
  42. -- Lanmanv2 has a maximum client challenge of 8 bytes, whereas NTLMv2 doesn't limit
  43. -- the length of the client challenge.
  44. --
  45. -- The primary advantage to the 'v2' protocols is the client challenge -- by
  46. -- incorporating a client challenge, a malicious server can't use a precomputation
  47. -- attack.
  48. --
  49. -- In addition to hashing the passwords, messages are also signed, by default, if a
  50. -- v1 protocol is being used (I (Ron Bowes) couldn't get signatures to work on v2
  51. -- protocols; if anybody knows how I'd love to implement it).
  52. --
  53. --@args smbusername The SMB username to log in with. The forms "DOMAIN\username" and "username@DOMAIN"
  54. -- are not understood. To set a domain, use the <code>smbdomain</code> argument.
  55. --@args smbdomain The domain to log in with. If you aren't in a domained environment, then anything
  56. -- will (should?) be accepted by the server.
  57. --@args smbpassword The password to connect with. Be cautious with this, since some servers will lock
  58. -- accounts if the incorrect password is given. Although it's rare that the
  59. -- Administrator account can be locked out, in the off chance that it can, you could
  60. -- get yourself in trouble. To use a blank password, leave this parameter off
  61. -- altogether.
  62. --@args smbhash A password hash to use when logging in. This is given as a single hex string (32
  63. -- characters) or a pair of hex strings (both 32 characters, optionally separated by a
  64. -- single character). These hashes are the LanMan or NTLM hash of the user's password,
  65. -- and are stored on disk or in memory. They can be retrieved from memory
  66. -- using the fgdump or pwdump tools.
  67. --@args smbtype The type of SMB authentication to use. These are the possible options:
  68. -- * <code>v1</code>: Sends LMv1 and NTLMv1.
  69. -- * <code>LMv1</code>: Sends LMv1 only.
  70. -- * <code>NTLMv1</code>: Sends NTLMv1 only (default).
  71. -- * <code>v2</code>: Sends LMv2 and NTLMv2.
  72. -- * <code>LMv2</code>: Sends LMv2 only.
  73. -- * <code>NTLMv2</code>: Doesn't exist; the protocol doesn't support NTLMv2 alone.
  74. -- The default, <code>NTLMv1</code>, is a pretty decent compromise between security and
  75. -- compatibility. If you are paranoid, you might want to use <code>v2</code> or
  76. -- <code>lmv2</code> for this. (Actually, if you're paranoid, you should be avoiding this
  77. -- protocol altogether!). If you're using an extremely old system, you might need to set
  78. -- this to <code>v1</code> or <code>lm</code>, which are less secure but more compatible.
  79. -- For information, see <code>smbauth.lua</code>.
  80. module(... or "smbauth", package.seeall)
  81. require 'bit'
  82. require 'bin'
  83. require 'netbios'
  84. require 'stdnse'
  85. have_ssl = (nmap.have_ssl() and pcall(require, "openssl"))
  86. -- Constants
  87. local NTLMSSP_NEGOTIATE = 0x00000001
  88. local NTLMSSP_CHALLENGE = 0x00000002
  89. local NTLMSSP_AUTH = 0x00000003
  90. local session_key = string.rep(string.char(0x00), 16)
  91. -- Types of accounts (ordered by how useful they are
  92. local ACCOUNT_TYPES = {
  93. ANONYMOUS = 0,
  94. GUEST = 1,
  95. USER = 2,
  96. ADMIN = 3
  97. }
  98. local function account_exists(host, username, domain)
  99. if(nmap.registry[host.ip] == nil or nmap.registry[host.ip]['smbaccounts'] == nil) then
  100. return false
  101. end
  102. for i, j in pairs(nmap.registry[host.ip]['smbaccounts']) do
  103. if(j['username'] == username and j['domain'] == domain) then
  104. return true
  105. end
  106. end
  107. return false
  108. end
  109. function next_account(host, num)
  110. if(num == nil) then
  111. if(nmap.registry[host.ip]['smbindex'] == nil) then
  112. nmap.registry[host.ip]['smbindex'] = 1
  113. else
  114. nmap.registry[host.ip]['smbindex'] = nmap.registry[host.ip]['smbindex'] + 1
  115. end
  116. else
  117. nmap.registry[host.ip]['smbindex'] = num
  118. end
  119. end
  120. ---Writes the given account to the registry. There are several places where accounts are stored:
  121. -- * registry['usernames'][username] => true
  122. -- * registry['smbaccounts'][username] => password
  123. -- * registry[ip]['smbaccounts'] => array of table containing 'username', 'password', and 'is_admin'
  124. --
  125. -- The final place, 'smbaccount', is reserved for the "best" account. This is an administrator
  126. -- account, if one's found; otherwise, it's the first account discovered that isn't <code>guest</code>.
  127. --
  128. -- This has to be called while no SMB connections are made, since it potentially makes its own connection.
  129. --
  130. --@param host The host object.
  131. --@param username The username to add.
  132. --@param domain The domain to add.
  133. --@param password The password to add.
  134. --@param password_hash The password hash to add.
  135. --@param hash_type The hash type to use.
  136. --@param is_admin [optional] Set to 'true' the account is known to be an administrator.
  137. function add_account(host, username, domain, password, password_hash, hash_type, is_admin)
  138. -- Save the username in a global list -- TODO: restore this
  139. -- if(nmap.registry.usernames == nil) then
  140. -- nmap.registry.usernames = {}
  141. -- end
  142. -- nmap.registry.usernames[username] = true
  143. --
  144. -- -- Save the username/password pair in a global list
  145. -- if(nmap.registry.smbaccounts == nil) then
  146. -- nmap.registry.smbaccounts = {}
  147. -- end
  148. -- nmap.registry.smbaccounts[username] = password
  149. -- Check if we've already recorded this account
  150. if(account_exists(host, username, domain)) then
  151. return
  152. end
  153. if(nmap.registry[host.ip] == nil) then
  154. nmap.registry[host.ip] = {}
  155. end
  156. if(nmap.registry[host.ip]['smbaccounts'] == nil) then
  157. nmap.registry[host.ip]['smbaccounts'] = {}
  158. end
  159. -- Determine the type of account, if it wasn't given
  160. local account_type = nil
  161. if(is_admin) then
  162. account_type = ACCOUNT_TYPES.ADMIN
  163. else
  164. if(username == '') then
  165. -- Anonymous account
  166. account_type = ACCOUNT_TYPES.ANONYMOUS
  167. elseif(string.lower(username) == 'guest') then
  168. -- Guest account
  169. account_type = ACCOUNT_TYPES.GUEST
  170. else
  171. -- We have to assume it's a user-level account (we just can't call any SMB functions from inside here)
  172. account_type = ACCOUNT_TYPES.USER
  173. end
  174. end
  175. -- Set some defaults
  176. if(hash_type == nil) then
  177. hash_type = 'ntlm'
  178. end
  179. -- Save the new account if this is our first one, or our other account isn't an admin
  180. local new_entry = {}
  181. new_entry['username'] = username
  182. new_entry['domain'] = domain
  183. new_entry['password'] = password
  184. new_entry['password_hash'] = password_hash
  185. new_entry['hash_type'] = string.lower(hash_type)
  186. new_entry['account_type'] = account_type
  187. -- Insert the new entry into the table
  188. table.insert(nmap.registry[host.ip]['smbaccounts'], new_entry)
  189. -- Sort the table based on the account type (we want anonymous at the end, administrator at the front)
  190. table.sort(nmap.registry[host.ip]['smbaccounts'], function(a,b) return a['account_type'] > b['account_type'] end)
  191. -- Print a debug message
  192. stdnse.print_debug(1, "SMB: Added account '%s' to account list", username)
  193. -- Reset the credentials
  194. next_account(host, 1)
  195. -- io.write("\n\n" .. nsedebug.tostr(nmap.registry[host.ip]['smbaccounts']) .. "\n\n")
  196. end
  197. ---Retrieve the current set of credentials set in the registry. If these fail, <code>next_credentials</code> should be
  198. -- called.
  199. --
  200. --@param host The host object.
  201. --@return (result, username, domain, password, password_hash, hash_type) If result is false, username is an error message. Otherwise, username and password are
  202. -- the current username and password that should be used.
  203. function get_account(host)
  204. if(nmap.registry[host.ip]['smbindex'] == nil) then
  205. nmap.registry[host.ip]['smbindex'] = 1
  206. end
  207. local index = nmap.registry[host.ip]['smbindex']
  208. local account = nmap.registry[host.ip]['smbaccounts'][index]
  209. if(account == nil) then
  210. return false, "No accounts left to try"
  211. end
  212. return true, account['username'], account['domain'], account['password'], account['password_hash'], account['hash_type']
  213. end
  214. ---Create the account table with the anonymous and guest users, as well as the user given in the script's
  215. -- arguments, if there is one.
  216. --
  217. --@param host The host object.
  218. function init_account(host)
  219. -- Create the key if it exists
  220. if(nmap.registry[host.ip] == nil) then
  221. nmap.registry[host.ip] = {}
  222. end
  223. -- Don't run this more than once for each host
  224. if(nmap.registry[host.ip]['smbaccounts'] ~= nil) then
  225. return
  226. end
  227. -- Create the list
  228. nmap.registry[host.ip]['smbaccounts'] = {}
  229. -- Add the anonymous/guest accounts
  230. add_account(host, '', '', '', nil, 'none')
  231. add_account(host, 'guest', '', '', nil, 'ntlm')
  232. -- Add the account given on the commandline (TODO: allow more than one?)
  233. local args = nmap.registry.args
  234. local username = nil
  235. local domain = ''
  236. local password = nil
  237. local password_hash = nil
  238. local hash_type = 'ntlm'
  239. -- Do the username first
  240. if(args.smbusername ~= nil) then
  241. username = args.smbusername
  242. elseif(args.smbuser ~= nil) then
  243. username = args.smbuser
  244. end
  245. -- If the username exists, do everything else
  246. if(username ~= nil) then
  247. -- Domain
  248. if(args.smbdomain ~= nil) then
  249. domain = args.smbdomain
  250. end
  251. -- Type
  252. if(args.smbtype ~= nil) then
  253. hash_type = args.smbtype
  254. end
  255. -- Do the password
  256. if(args.smbpassword ~= nil) then
  257. password = args.smbpassword
  258. elseif(args.smbpass ~= nil) then
  259. password = args.smbpass
  260. end
  261. -- Only use the hash if there's no password
  262. if(password == nil) then
  263. password_hash = args.smbhash
  264. end
  265. -- Add the account, if we got a password
  266. if(password == nil and password_hash == nil) then
  267. stdnse.print_debug(1, "SMB: Either smbpass, smbpassword, or smbhash have to be passed as script arguments to use an account")
  268. else
  269. add_account(host, username, domain, password, password_hash, hash_type)
  270. end
  271. end
  272. end
  273. local function to_unicode(str)
  274. local unicode = ""
  275. for i = 1, #str, 1 do
  276. unicode = unicode .. bin.pack("<S", string.byte(str, i))
  277. end
  278. return unicode
  279. end
  280. ---Generate the Lanman v1 hash (LMv1). The generated hash is incredibly easy to reverse, because the input
  281. -- is padded or truncated to 14 characters, then split into two 7-character strings. Each of these strings
  282. -- are used as a key to encrypt the string, "KGS!@#$%" in DES. Because the keys are no longer than
  283. -- 7-characters long, it's pretty trivial to bruteforce them.
  284. --
  285. --@param password the password to hash
  286. --@return (status, hash) If status is true, the hash is returned; otherwise, an error message is returned.
  287. local function lm_create_hash(password)
  288. if(have_ssl ~= true) then
  289. return false, "SMB: OpenSSL not present"
  290. end
  291. local str1, str2
  292. local key1, key2
  293. local result
  294. -- Convert the password to uppercase
  295. password = string.upper(password)
  296. -- If password is under 14 characters, pad it to 14
  297. if(#password < 14) then
  298. password = password .. string.rep(string.char(0), 14 - #password)
  299. end
  300. -- Take the first and second half of the password (note that if it's longer than 14 characters, it's truncated)
  301. str1 = string.sub(password, 1, 7)
  302. str2 = string.sub(password, 8, 14)
  303. -- Generate the keys
  304. key1 = openssl.DES_string_to_key(str1)
  305. key2 = openssl.DES_string_to_key(str2)
  306. -- Encrypt the string "KGS!@#$%" with each half, and concatenate it
  307. result = openssl.encrypt("DES", key1, nil, "KGS!@#$%") .. openssl.encrypt("DES", key2, nil, "KGS!@#$%")
  308. return true, result
  309. end
  310. ---Generate the NTLMv1 hash. This hash is quite a bit better than LMv1, and is far easier to generate. Basically,
  311. -- it's the MD4 of the Unicode password.
  312. --
  313. --@param password the password to hash
  314. --@return (status, hash) If status is true, the hash is returned; otherwise, an error message is returned.
  315. function ntlm_create_hash(password)
  316. if(have_ssl ~= true) then
  317. return false, "SMB: OpenSSL not present"
  318. end
  319. return true, openssl.md4(to_unicode(password))
  320. end
  321. ---Create the Lanman response to send back to the server. To do this, the Lanman password is padded to 21
  322. -- characters and split into three 7-character strings. Each of those strings is used as a key to encrypt
  323. -- the server challenge. The three encrypted strings are concatenated and returned.
  324. --
  325. --@param lanman The LMv1 hash
  326. --@param challenge The server's challenge.
  327. --@return (status, response) If status is true, the response is returned; otherwise, an error message is returned.
  328. function lm_create_response(lanman, challenge)
  329. if(have_ssl ~= true) then
  330. return false, "SMB: OpenSSL not present"
  331. end
  332. local str1, str2, str3
  333. local key1, key2, key3
  334. local result
  335. -- Pad the hash to 21 characters
  336. lanman = lanman .. string.rep(string.char(0), 21 - #lanman)
  337. -- Take the first and second half of the password (note that if it's longer than 14 characters, it's truncated)
  338. str1 = string.sub(lanman, 1, 7)
  339. str2 = string.sub(lanman, 8, 14)
  340. str3 = string.sub(lanman, 15, 21)
  341. -- Generate the keys
  342. key1 = openssl.DES_string_to_key(str1)
  343. key2 = openssl.DES_string_to_key(str2)
  344. key3 = openssl.DES_string_to_key(str3)
  345. -- Print a warning message if a blank challenge is received, and create a phony challenge. A blank challenge is
  346. -- invalid in the protocol, and causes some versions of OpenSSL to abort with no possible error handling.
  347. if(challenge == "") then
  348. stdnse.print_debug(1, "SMB: ERROR: Server returned invalid (blank) challenge value (should be 8 bytes); failing login to avoid OpenSSL crash.")
  349. challenge = "AAAAAAAA"
  350. end
  351. -- Encrypt the challenge with each key
  352. result = openssl.encrypt("DES", key1, nil, challenge) .. openssl.encrypt("DES", key2, nil, challenge) .. openssl.encrypt("DES", key3, nil, challenge)
  353. return true, result
  354. end
  355. ---Create the NTLM response to send back to the server. This is actually done the exact same way as the Lanman hash,
  356. -- so I call the <code>Lanman</code> function.
  357. --
  358. --@param ntlm The NTLMv1 hash
  359. --@param challenge The server's challenge.
  360. --@return (status, response) If status is true, the response is returned; otherwise, an error message is returned.
  361. function ntlm_create_response(ntlm, challenge)
  362. if(have_ssl ~= true) then
  363. return false, "SMB: OpenSSL not present"
  364. end
  365. return lm_create_response(ntlm, challenge)
  366. end
  367. ---Create the NTLM mac key, which is used for message signing. For basic authentication, this is the md4 of the
  368. -- NTLM hash, concatenated with the response hash; for extended authentication, this is just the md4 of the NTLM
  369. -- hash.
  370. --@param ntlm_hash The NTLM hash.
  371. --@param ntlm_response The NTLM response.
  372. --@param is_extended Should be set if extended security negotiations are being used.
  373. function ntlm_create_mac_key(ntlm_hash, ntlm_response, is_extended)
  374. if(have_ssl ~= true) then
  375. return false, "SMB: OpenSSL not present"
  376. end
  377. if(is_extended) then
  378. return openssl.md4(ntlm_hash)
  379. else
  380. return openssl.md4(ntlm_hash) .. ntlm_response
  381. end
  382. end
  383. ---Create the LM mac key, which is used for message signing. For basic authentication, it's the first 8 bytes
  384. -- of the lanman hash, followed by 8 null bytes, followed by the lanman response; for extended authentication,
  385. -- this is just the first 8 bytes of the lanman hash followed by 8 null bytes.
  386. --@param ntlm_hash The NTLM hash.
  387. --@param ntlm_response The NTLM response.
  388. --@param is_extended Should be set if extended security negotiations are being used.
  389. function lm_create_mac_key(lm_hash, lm_response, is_extended)
  390. if(have_ssl ~= true) then
  391. return false, "SMB: OpenSSL not present"
  392. end
  393. if(is_extended) then
  394. return string.sub(lm_hash, 1, 8) .. string.rep(string.char(0), 8)
  395. else
  396. return string.sub(lm_hash, 1, 8) .. string.rep(string.char(0), 8) .. lm_response
  397. end
  398. end
  399. ---Create the NTLMv2 hash, which is based on the NTLMv1 hash (for easy upgrading), the username, and the domain.
  400. -- Essentially, the NTLM hash is used as a HMAC-MD5 key, which is used to hash the unicode domain concatenated
  401. -- with the unicode username.
  402. --
  403. --@param ntlm The NTLMv1 hash.
  404. --@param username The username we're using.
  405. --@param domain The domain.
  406. --@return (status, response) If status is true, the response is returned; otherwise, an error message is returned.
  407. function ntlmv2_create_hash(ntlm, username, domain)
  408. if(have_ssl ~= true) then
  409. return false, "SMB: OpenSSL not present"
  410. end
  411. local unicode = ""
  412. username = to_unicode(string.upper(username))
  413. domain = to_unicode(string.upper(domain))
  414. return true, openssl.hmac("MD5", ntlm, username .. domain)
  415. end
  416. ---Create the LMv2 response, which can be sent back to the server. This is identical to the <code>NTLMv2</code> function,
  417. -- except that it uses an 8-byte client challenge.
  418. --
  419. -- The reason for LMv2 is a long and twisted story. Well, not really. The reason is basically that the v1 hashes
  420. -- are always 24-bytes, and some servers expect 24 bytes, but the NTLMv2 hash is more than 24 bytes. So, the only
  421. -- way to keep pass-through compatibility was to have a v2-hash that was guaranteed to be 24 bytes. So LMv1 was
  422. -- born -- it has a 16-byte hash followed by the 8-byte client challenge, for a total of 24 bytes. And now you've
  423. -- learned something
  424. --
  425. --@param ntlm The NVLMv1 hash.
  426. --@param username The username we're using.
  427. --@param domain The domain.
  428. --@param challenge The server challenge.
  429. --@return (status, response) If status is true, the response is returned; otherwise, an error message is returned.
  430. function lmv2_create_response(ntlm, username, domain, challenge)
  431. if(have_ssl ~= true) then
  432. return false, "SMB: OpenSSL not present"
  433. end
  434. return ntlmv2_create_response(ntlm, username, domain, challenge, 8)
  435. end
  436. ---Create the NTLMv2 response, which can be sent back to the server. This is done by using the HMAC-MD5 algorithm
  437. -- with the NTLMv2 hash as a key, and the server challenge concatenated with the client challenge for the data.
  438. -- The resulting hash is concatenated with the client challenge and returned.
  439. --
  440. -- The "proper" implementation for this uses a certain structure for the client challenge, involving the time
  441. -- and computer name and stuff (if you don't do this, Wireshark tells you it's a malformed packet). In my tests,
  442. -- however, I couldn't get Vista to recognize a client challenge longer than 24 bytes, and this structure was
  443. -- guaranteed to be much longer than 24 bytes. So, I just use a random string generated by OpenSSL. I've tested
  444. -- it on every Windows system from Windows 2000 to Windows Vista, and it has always worked.
  445. function ntlmv2_create_response(ntlm, username, domain, challenge, client_challenge_length)
  446. if(have_ssl ~= true) then
  447. return false, "SMB: OpenSSL not present"
  448. end
  449. local client_challenge = openssl.rand_bytes(client_challenge_length)
  450. local status, ntlmv2_hash = ntlmv2_create_hash(ntlm, username, domain)
  451. return true, openssl.hmac("MD5", ntlmv2_hash, challenge .. client_challenge) .. client_challenge
  452. end
  453. ---Generate the Lanman and NTLM password hashes. The password itself is taken from the function parameters,
  454. -- the nmap arguments, and the registry (in that order). If no password is set, then the password hash
  455. -- is used (which is read from all the usual places). If neither is set, then a blank password is used.
  456. --
  457. -- The output passwords are hashed based on the hash type.
  458. --
  459. --@param ip The ip address of the host, used for registry lookups.
  460. --@param username The username, which is used for v2 passwords.
  461. --@param domain The username, which is used for v2 passwords.
  462. --@param password [optional] The overriding password.
  463. --@param password_hash [optional] The overriding password hash. Shouldn't be set if password is set.
  464. --@param challenge The server challenge.
  465. --@param hash_type The way in which to hash the password.
  466. --@param is_extended Set to 'true' if extended security negotiations are being used (this has to be known for the
  467. -- message-signing key to be generated properly).
  468. --@return (lm_response, ntlm_response, mac_key) The two strings that can be sent directly back to the server,
  469. -- and the mac_key, which is used for message signing.
  470. function get_password_response(ip, username, domain, password, password_hash, hash_type, challenge, is_extended)
  471. local status
  472. local lm_hash = nil
  473. local ntlm_hash = nil
  474. local mac_key = nil
  475. local lm_response, ntlm_response
  476. -- Check for a blank password
  477. if(password == nil and password_hash == nil) then
  478. stdnse.print_debug(2, "SMB: Couldn't find password or hash to use (assuming blank)")
  479. password = ""
  480. end
  481. -- The anonymous user requires a single 0-byte instead of a LANMAN hash (don't ask me why, but it doesn't work without)
  482. if(hash_type == 'none') then
  483. return string.char(0), '', nil
  484. end
  485. -- If we got a password, hash it
  486. if(password ~= nil) then
  487. status, lm_hash = lm_create_hash(password)
  488. status, ntlm_hash = ntlm_create_hash(password)
  489. else
  490. if(password_hash ~= nil) then
  491. if(string.find(password_hash, "^" .. string.rep("%x%x", 16) .. "$")) then
  492. stdnse.print_debug(2, "SMB: Found a 16-byte hex string")
  493. lm_hash = bin.pack("H", password_hash:sub(1, 32))
  494. ntlm_hash = bin.pack("H", password_hash:sub(1, 32))
  495. elseif(string.find(password_hash, "^" .. string.rep("%x%x", 32) .. "$")) then
  496. stdnse.print_debug(2, "SMB: Found a 32-byte hex string")
  497. lm_hash = bin.pack("H", password_hash:sub(1, 32))
  498. ntlm_hash = bin.pack("H", password_hash:sub(33, 64))
  499. elseif(string.find(password_hash, "^" .. string.rep("%x%x", 16) .. "." .. string.rep("%x%x", 16) .. "$")) then
  500. stdnse.print_debug(2, "SMB: Found two 16-byte hex strings")
  501. lm_hash = bin.pack("H", password_hash:sub(1, 32))
  502. ntlm_hash = bin.pack("H", password_hash:sub(34, 65))
  503. else
  504. stdnse.print_debug(1, "SMB: ERROR: Hash(es) provided in an invalid format (should be 32, 64, or 65 hex characters)")
  505. lm_hash = nil
  506. ntlm_hash = nil
  507. end
  508. end
  509. end
  510. -- At this point, we should have a good lm_hash and ntlm_hash if we're getting one
  511. if(lm_hash == nil or ntlm_hash == nil) then
  512. stdnse.print_debug(2, "SMB: Couldn't determine which password to use, using a blank one")
  513. return "", ""
  514. end
  515. -- Output what we've got so far
  516. stdnse.print_debug(2, "SMB: Lanman hash: %s", stdnse.tohex(lm_hash))
  517. stdnse.print_debug(2, "SMB: NTLM hash: %s", stdnse.tohex(ntlm_hash))
  518. -- Hash the password the way the user wants
  519. if(hash_type == "v1") then
  520. -- LM and NTLM are hashed with their respective algorithms
  521. stdnse.print_debug(2, "SMB: Creating v1 response")
  522. status, lm_response = lm_create_response(lm_hash, challenge)
  523. status, ntlm_response = ntlm_create_response(ntlm_hash, challenge)
  524. mac_key = ntlm_create_mac_key(ntlm_hash, ntlm_response, is_extended)
  525. elseif(hash_type == "lm") then
  526. -- LM is hashed with its algorithm, NTLM is blank
  527. stdnse.print_debug(2, "SMB: Creating LMv1 response")
  528. status, lm_response = lm_create_response(lm_hash, challenge)
  529. ntlm_response = ""
  530. mac_key = lm_create_mac_key(lm_hash, lm_response, is_extended)
  531. elseif(hash_type == "ntlm") then
  532. -- LM and NTLM both use the NTLM algorithm
  533. stdnse.print_debug(2, "SMB: Creating NTLMv1 response")
  534. status, lm_response = ntlm_create_response(ntlm_hash, challenge)
  535. status, ntlm_response = ntlm_create_response(ntlm_hash, challenge)
  536. mac_key = ntlm_create_mac_key(ntlm_hash, ntlm_response, is_extended)
  537. elseif(hash_type == "v2") then
  538. -- LM and NTLM are hashed with their respective v2 algorithms
  539. stdnse.print_debug(2, "SMB: Creating v2 response")
  540. status, lm_response = lmv2_create_response(ntlm_hash, username, domain, challenge)
  541. status, ntlm_response = ntlmv2_create_response(ntlm_hash, username, domain, challenge, 24)
  542. elseif(hash_type == "lmv2") then
  543. -- LM is hashed with its v2 algorithm, NTLM is blank
  544. stdnse.print_debug(2, "SMB: Creating LMv2 response")
  545. status, lm_response = lmv2_create_response(ntlm_hash, username, domain, challenge)
  546. ntlm_response = ""
  547. else
  548. -- Default to NTLMv1
  549. if(hash_type ~= nil) then
  550. stdnse.print_debug(1, "SMB: Invalid login type specified ('%s'), using default (NTLM)", hash_type)
  551. else
  552. stdnse.print_debug(1, "SMB: No login type specified, using default (NTLM)")
  553. end
  554. status, lm_response = ntlm_create_response(ntlm_hash, challenge)
  555. status, ntlm_response = ntlm_create_response(ntlm_hash, challenge)
  556. end
  557. stdnse.print_debug(2, "SMB: Lanman response: %s", stdnse.tohex(lm_response))
  558. stdnse.print_debug(2, "SMB: NTLM response: %s", stdnse.tohex(ntlm_response))
  559. return lm_response, ntlm_response, mac_key
  560. end
  561. function get_security_blob(security_blob, ip, username, domain, password, password_hash, hash_type)
  562. local pos = 1
  563. local new_blob
  564. local flags = 0x00008211 -- (NEGOTIATE_SIGN_ALWAYS | NEGOTIATE_NTLM | NEGOTIATE_SIGN | NEGOTIATE_UNICODE)
  565. if(security_blob == nil) then
  566. -- If security_blob is nil, this is the initial packet
  567. new_blob = bin.pack("<zIILL",
  568. "NTLMSSP", -- Identifier
  569. NTLMSSP_NEGOTIATE, -- Type
  570. flags, -- Flags
  571. 0, -- Calling workstation domain
  572. 0 -- Calling workstation name
  573. )
  574. return true, new_blob, "", ""
  575. else
  576. local identifier, message_type, domain_length, domain_max, domain_offset, server_flags, challenge, reserved
  577. -- Parse the old security blob
  578. pos, identifier, message_type, domain_length, domain_max, domain_offset, server_flags, challenge, reserved = bin.unpack("<LISSIIA8A8", security_blob, 1)
  579. -- Get the information for the current login
  580. local lanman, ntlm, mac_key = get_password_response(ip, username, domain, password, password_hash, hash_type, challenge, true)
  581. -- Convert the username and domain to unicode (TODO: Disable the unicode flag, evaluate if that'll work)
  582. username = to_unicode(username)
  583. domain = to_unicode(domain)
  584. new_blob = bin.pack("<zISSISSISSISSISSISSII",
  585. "NTLMSSP", -- Identifier
  586. NTLMSSP_AUTH, -- Type
  587. #lanman, -- Lanman (length, max, offset)
  588. #lanman, --
  589. 0x40, --
  590. #ntlm, -- NTLM (length, max, offset)
  591. #ntlm, --
  592. 0x40 + #lanman, --
  593. #domain, -- Domain (length, max, offset)
  594. #domain, --
  595. 0x40 + #lanman + #ntlm,--
  596. #username, -- Username (length, max, offset)
  597. #username, --
  598. 0x40 + #lanman + #ntlm + #domain,
  599. #domain, -- Hostname (length, max, offset)
  600. #domain, --
  601. 0x40 + #lanman + #ntlm + #domain + #username,
  602. #session_key, -- Session key (length, max, offset)
  603. #session_key, --
  604. 0x40 + #lanman + #ntlm + #domain + #username + #domain,
  605. flags -- Flags
  606. )
  607. new_blob = new_blob .. bin.pack("AAAAAA", lanman, ntlm, domain, username, domain, session_key)
  608. return true, new_blob, mac_key
  609. end
  610. end
  611. ---Create an 8-byte message signature that's sent with all SMB packets.
  612. --
  613. --@param mac_key The key used for authentication. It's the concatination of the session key and the
  614. -- response hash.
  615. --@param data The packet to generate the signature for. This should be the packet that's about to be
  616. -- sent, except with the signature slot replaced with the sequence number.
  617. --@return The 8-byte signature. The signature is equal to the first eight bytes of md5(mac_key .. smb_data)
  618. function calculate_signature(mac_key, data)
  619. if(have_ssl) then
  620. return string.sub(openssl.md5(mac_key .. data), 1, 8)
  621. else
  622. return string.rep(string.char(0), 8)
  623. end
  624. end