PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/lm2ntcrack.rb

https://bitbucket.org/technopunk2099/metasploit-framework
Ruby | 877 lines | 824 code | 41 blank | 12 comment | 181 complexity | bb5c5b393c6af61a0e819ffd6f24532e MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, LGPL-2.1, GPL-2.0, MIT
  1. #!/usr/bin/env ruby
  2. #
  3. # $Id$
  4. #
  5. # This script cracks any type of NTLM hash
  6. # Credit to -Yannick Hamon <yannick.hamon[at]xmcopartners.com> for the original idea/perl code
  7. # -Alexandre Maloteaux <a.maloteaux[at]gmail.com> for improvments
  8. # $Revision$
  9. #
  10. msfbase = __FILE__
  11. while File.symlink?(msfbase)
  12. msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
  13. end
  14. $:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', 'lib')))
  15. require 'fastlib'
  16. require 'msfenv'
  17. $:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
  18. require 'rex'
  19. require 'rex/proto/ntlm/crypt'
  20. CRYPT = Rex::Proto::NTLM::Crypt
  21. BRUTE_MODE = 1
  22. HASH_MODE = 2
  23. PASS_MODE = 3
  24. def usage
  25. $stderr.puts("\nUsage: #{$0} -t type <options>\n" + $args.usage)
  26. $stderr.puts("This tool can be use in 3 ways whatever type is choosen\n")
  27. $stderr.puts("-If only a password (-p) is provided, it will display the hash.\n")
  28. $stderr.puts("-If a password (-p) and an hash (-a) is provided, it will test the password against the hash.\n")
  29. $stderr.puts("-If a list of password (-l) is provided and an hash (-a), it will try to bruteforce the hash \n\n")
  30. exit
  31. end
  32. def permute_pw(pw)
  33. # fast permutation from http://stackoverflow.com/a/1398900
  34. perms = [""]
  35. if pw.nil?
  36. return perms
  37. end
  38. tail = pw.downcase
  39. while tail.length > 0 do
  40. head, tail, psize = tail[0..0], tail[1..-1], perms.size
  41. hu = head.upcase
  42. for i in (0...psize)
  43. tp = perms[i]
  44. perms[i] = tp + hu
  45. if hu != head
  46. perms.push(tp + head)
  47. end
  48. end
  49. end
  50. return perms
  51. end
  52. type = hash = pass = srvchal = clichal = calculatedhash = list = user = domain = nil
  53. $args = Rex::Parser::Arguments.new(
  54. "-t" => [ true, "The type of hash to crack : HALFLM/LM/NTLM/HALFNETLMv1/NETLMv1/NETNTLMv1/NETNTLM2_SESSION/NETLMv2/NETNTLMv2" ],
  55. "-a" => [ true, "The hash to crack" ],
  56. "-p" => [ true, "The password " ],
  57. "-l" => [ true, "The list of password to check against an hash" ],
  58. "-s" => [ true, "The LM/NTLM Server Challenge (NET* type only)" ],
  59. "-c" => [ true, "The LM/NTLM Client Challenge (NETNTLM2_SESSION/NETLMv2/NETNTLMv2/ type only)" ],
  60. "-u" => [ true, "The user name (NETLMv2/NETNTLMv2 type only)" ],
  61. "-d" => [ true, "The domain (machine) name (NETLMv2/NETNTLMv2 type only)" ],
  62. "-h" => [ false, "Display this help information" ])
  63. $args.parse(ARGV) { |opt, idx, val|
  64. case opt
  65. when "-t"
  66. type = val
  67. when "-a"
  68. hash = val
  69. when "-p"
  70. pass = val
  71. when "-l"
  72. list = val
  73. when "-s"
  74. srvchal = val
  75. when "-c"
  76. clichal = val
  77. when "-u"
  78. user = val
  79. when "-d"
  80. domain = val
  81. when "-h"
  82. usage
  83. else
  84. usage
  85. end
  86. }
  87. if not type
  88. usage
  89. else
  90. if pass and (not (hash or list))
  91. mode = HASH_MODE
  92. elsif pass and hash and not list
  93. mode = PASS_MODE
  94. elsif list and hash and not pass
  95. mode = BRUTE_MODE
  96. if not File.exist? list
  97. $stderr.puts "[*] The passwords list file does not exist"
  98. exit
  99. end
  100. if not File.file? list
  101. $stderr.puts "[*] The passwords list provided is not a file"
  102. exit
  103. end
  104. if not File.readable? list
  105. $stderr.puts "[*] The passwords list file is not readable"
  106. exit
  107. end
  108. else
  109. usage
  110. end
  111. end
  112. if type == "HALFLM" or type == "LM" or type == "NTLM" then
  113. if srvchal != nil or clichal != nil or user != nil or domain != nil then
  114. $stderr.puts "[*] No challenge, user or domain must be provided with this type"
  115. exit
  116. end
  117. elsif type == "HALFNETLMv1" or type == "NETLMv1" or type == "NETNTLMv1" then
  118. if clichal != nil or user != nil or domain != nil then
  119. $stderr.puts "[*] Client challenge, user or domain must not be provided with this type"
  120. exit
  121. end
  122. elsif type == "NETNTLM2_SESSION" then
  123. if user != nil or domain != nil then
  124. $stderr.puts "[*] User or domain must not be provided with this type"
  125. exit
  126. end
  127. end
  128. case type
  129. when "HALFLM"
  130. case mode
  131. when BRUTE_MODE
  132. if not hash =~ /^([a-fA-F0-9]{16})$/
  133. $stderr.puts "[*] HALFLM HASH must be exactly 16 bytes of hexadecimal"
  134. exit
  135. end
  136. File.open(list,"rb") do |password_list|
  137. password_list.each_line do |line|
  138. password = line.gsub("\r\n",'').gsub("\n",'')
  139. if password =~ /^.{1,7}$/
  140. puts password
  141. calculatedhash = CRYPT::lm_hash(password,true).unpack("H*")[0].upcase
  142. if calculatedhash == hash.upcase
  143. puts "[*] Correct password found : #{password.upcase}"
  144. exit
  145. end
  146. end
  147. end
  148. end
  149. puts "[*] No password found"
  150. exit
  151. when HASH_MODE
  152. if not pass =~ /^.{0,7}$/
  153. $stderr.puts "[*] LM password can not be bigger then 7 characters"
  154. exit
  155. end
  156. calculatedhash = CRYPT::lm_hash(pass,true).unpack("H*")[0].upcase
  157. puts "[*] The LM hash for #{pass.upcase} is : #{calculatedhash}"
  158. exit
  159. when PASS_MODE
  160. if not pass =~ /^.{0,7}$/
  161. $stderr.puts "[*] LM password can not be bigger then 7 characters"
  162. exit
  163. end
  164. if not hash =~ /^([a-fA-F0-9]{16})$/
  165. $stderr.puts "[*] LM HASH must be exactly 16 bytes of hexadecimal"
  166. exit
  167. end
  168. calculatedhash = CRYPT::lm_hash(pass,true).unpack("H*")[0].upcase
  169. if hash.upcase == calculatedhash
  170. puts "[*] Correct password provided : #{pass.upcase}"
  171. exit
  172. else
  173. puts "[*] Incorrect password provided : #{pass.upcase}"
  174. exit
  175. end
  176. end
  177. when "LM"
  178. case mode
  179. when BRUTE_MODE
  180. if not hash =~ /^([a-fA-F0-9]{32})$/
  181. $stderr.puts "[*] LM HASH must be exactly 32 bytes of hexadecimal"
  182. exit
  183. end
  184. File.open(list,"rb") do |password_list|
  185. password_list.each_line do |line|
  186. password = line.gsub("\r\n",'').gsub("\n",'')
  187. if password =~ /^.{1,14}$/
  188. puts password
  189. calculatedhash = CRYPT::lm_hash(password.upcase).unpack("H*")[0].upcase
  190. if calculatedhash == hash.upcase
  191. puts "[*] Correct password found : #{password.upcase}"
  192. exit
  193. end
  194. end
  195. end
  196. end
  197. puts "[*] No password found"
  198. exit
  199. when HASH_MODE
  200. if not pass =~ /^.{0,14}$/
  201. $stderr.puts "[*] LM password can not be bigger then 14 characters"
  202. exit
  203. end
  204. calculatedhash = CRYPT::lm_hash(pass.upcase).unpack("H*")[0].upcase
  205. puts "[*] The LM hash for #{pass.upcase} is : #{calculatedhash}"
  206. exit
  207. when PASS_MODE
  208. if not pass =~ /^.{0,14}$/
  209. $stderr.puts "[*] LM password can not be bigger then 14 characters"
  210. exit
  211. end
  212. if not hash =~ /^([a-fA-F0-9]{32})$/
  213. $stderr.puts "[*] LM HASH must be exactly 32 bytes of hexadecimal"
  214. exit
  215. end
  216. calculatedhash = CRYPT::lm_hash(pass.upcase).unpack("H*")[0].upcase
  217. if hash.upcase == calculatedhash
  218. puts "[*] Correct password provided : #{pass.upcase}"
  219. exit
  220. else
  221. puts "[*] Incorrect password provided : #{pass.upcase}"
  222. exit
  223. end
  224. end
  225. when "NTLM"
  226. case mode
  227. when BRUTE_MODE
  228. if not hash =~ /^([a-fA-F0-9]{32})$/
  229. $stderr.puts "[*] NTLM HASH must be exactly 32 bytes of hexadecimal"
  230. exit
  231. end
  232. File.open(list,"rb") do |password_list|
  233. password_list.each_line do |line|
  234. password = line.gsub("\r\n",'').gsub("\n",'')
  235. for permutedpw in permute_pw(password)
  236. puts permutedpw
  237. calculatedhash = CRYPT::ntlm_hash(permutedpw).unpack("H*")[0].upcase
  238. if calculatedhash == hash.upcase
  239. puts "[*] Correct password found : #{permutedpw}"
  240. exit
  241. end
  242. end
  243. end
  244. end
  245. puts "[*] No password found"
  246. exit
  247. when HASH_MODE
  248. calculatedhash = CRYPT::ntlm_hash(pass).unpack("H*")[0].upcase
  249. puts "[*] The NTLM hash for #{pass} is : #{calculatedhash}"
  250. exit
  251. when PASS_MODE
  252. if not hash =~ /^([a-fA-F0-9]{32})$/
  253. $stderr.puts "[*] NTLM HASH must be exactly 32 bytes of hexadecimal"
  254. exit
  255. end
  256. for permutedpw in permute_pw(pass)
  257. calculatedhash = CRYPT::ntlm_hash(permutedpw).unpack("H*")[0].upcase
  258. if hash.upcase == calculatedhash
  259. puts "[*] Correct password provided : #{permutedpw}"
  260. exit
  261. end
  262. end
  263. puts "[*] Incorrect password provided : #{pass}"
  264. end
  265. when "HALFNETLMv1"
  266. case mode
  267. when BRUTE_MODE
  268. if not hash =~ /^([a-fA-F0-9]{16})$/
  269. $stderr.puts "[*] NETLMv1 HASH must be exactly 16 bytes of hexadecimal"
  270. exit
  271. end
  272. if not srvchal
  273. $stderr.puts "[*] Server challenge must be provided with this type"
  274. exit
  275. end
  276. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  277. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  278. exit
  279. end
  280. File.open(list,"rb") do |password_list|
  281. password_list.each_line do |line|
  282. password = line.gsub("\r\n",'').gsub("\n",'')
  283. if password =~ /^.{1,7}$/
  284. puts password
  285. #Rem : cause of the [0,7] there is only 1/256 chance that the guessed password will be the good one
  286. arglm = { :lm_hash => CRYPT::lm_hash(password,true)[0,7],
  287. :challenge => [ srvchal ].pack("H*") }
  288. calculatedhash = CRYPT::lm_response(arglm,true).unpack("H*")[0].upcase
  289. if calculatedhash == hash.upcase
  290. puts "[*] Correct password found : #{password.upcase}"
  291. exit
  292. end
  293. end
  294. end
  295. end
  296. puts "[*] No password found"
  297. exit
  298. when HASH_MODE
  299. if not pass =~ /^.{0,7}$/
  300. $stderr.puts "[*] HALFNETLMv1 password can not be bigger then 7 characters"
  301. exit
  302. end
  303. if not srvchal
  304. $stderr.puts "[*] Server challenge must be provided with this type"
  305. exit
  306. end
  307. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  308. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  309. exit
  310. end
  311. arglm = { :lm_hash => CRYPT::lm_hash(pass,true)[0,7],
  312. :challenge => [ srvchal ].pack("H*") }
  313. calculatedhash = CRYPT::lm_response(arglm,true).unpack("H*")[0].upcase
  314. puts "[*] The HALFNETLMv1 hash for #{pass.upcase} is : #{calculatedhash}"
  315. exit
  316. when PASS_MODE
  317. if not pass =~ /^.{0,7}$/
  318. $stderr.puts "[*] HALFNETLMv1 password can not be bigger then 7 characters"
  319. exit
  320. end
  321. if not hash =~ /^([a-fA-F0-9]{16})$/
  322. $stderr.puts "[*] HALFNETLMv1 HASH must be exactly 16 bytes of hexadecimal"
  323. exit
  324. end
  325. if not srvchal
  326. $stderr.puts "[*] Server challenge must be provided with this type"
  327. exit
  328. end
  329. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  330. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  331. exit
  332. end
  333. #Rem : cause of the [0,7] there is only 1/256 chance that the guessed password will be the good one
  334. arglm = { :lm_hash => CRYPT::lm_hash(pass,true)[0,7],
  335. :challenge => [ srvchal ].pack("H*") }
  336. calculatedhash = CRYPT::lm_response(arglm,true).unpack("H*")[0].upcase
  337. if hash.upcase == calculatedhash
  338. puts "[*] Correct password provided : #{pass.upcase}"
  339. exit
  340. else
  341. puts "[*] Incorrect password provided : #{pass.upcase}"
  342. exit
  343. end
  344. end
  345. when "NETLMv1"
  346. case mode
  347. when BRUTE_MODE
  348. if not hash =~ /^([a-fA-F0-9]{48})$/
  349. $stderr.puts "[*] NETLMv1 HASH must be exactly 48 bytes of hexadecimal"
  350. exit
  351. end
  352. if not srvchal
  353. $stderr.puts "[*] Server challenge must be provided with this type"
  354. exit
  355. end
  356. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  357. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  358. exit
  359. end
  360. File.open(list,"rb") do |password_list|
  361. password_list.each_line do |line|
  362. password = line.gsub("\r\n",'').gsub("\n",'')
  363. if password =~ /^.{1,14}$/
  364. puts password
  365. arglm = { :lm_hash => CRYPT::lm_hash(password),
  366. :challenge => [ srvchal ].pack("H*") }
  367. calculatedhash = CRYPT::lm_response(arglm).unpack("H*")[0].upcase
  368. if calculatedhash == hash.upcase
  369. puts "[*] Correct password found : #{password.upcase}"
  370. exit
  371. end
  372. end
  373. end
  374. end
  375. puts "[*] No password found"
  376. exit
  377. when HASH_MODE
  378. if not pass =~ /^.{1,14}$/
  379. $stderr.puts "[*] NETLMv1 password can not be bigger then 14 characters"
  380. exit
  381. end
  382. if not srvchal
  383. $stderr.puts "[*] Server challenge must be provided with this type"
  384. exit
  385. end
  386. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  387. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  388. exit
  389. end
  390. arglm = { :lm_hash => CRYPT::lm_hash(pass),
  391. :challenge => [ srvchal ].pack("H*") }
  392. calculatedhash = CRYPT::lm_response(arglm).unpack("H*")[0].upcase
  393. puts "[*] The NETLMv1 hash for #{pass.upcase} is : #{calculatedhash}"
  394. exit
  395. when PASS_MODE
  396. if not pass =~ /^.{1,14}$/
  397. $stderr.puts "[*] NETLMv1 password can not be bigger then 14 characters"
  398. exit
  399. end
  400. if not hash =~ /^([a-fA-F0-9]{48})$/
  401. $stderr.puts "[*] NETLMv1 HASH must be exactly 48 bytes of hexadecimal"
  402. exit
  403. end
  404. if not srvchal
  405. $stderr.puts "[*] Server challenge must be provided with this type"
  406. exit
  407. end
  408. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  409. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  410. exit
  411. end
  412. arglm = { :lm_hash => CRYPT::lm_hash(pass),
  413. :challenge => [ srvchal ].pack("H*") }
  414. calculatedhash = CRYPT::lm_response(arglm).unpack("H*")[0].upcase
  415. if hash.upcase == calculatedhash
  416. puts "[*] Correct password provided : #{pass.upcase}"
  417. exit
  418. else
  419. puts "[*] Incorrect password provided : #{pass.upcase}"
  420. exit
  421. end
  422. end
  423. when "NETNTLMv1"
  424. case mode
  425. when BRUTE_MODE
  426. if not hash =~ /^([a-fA-F0-9]{48})$/
  427. $stderr.puts "[*] NETNTLMv1 HASH must be exactly 48 bytes of hexadecimal"
  428. exit
  429. end
  430. if not srvchal
  431. $stderr.puts "[*] Server challenge must be provided with this type"
  432. exit
  433. end
  434. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  435. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  436. exit
  437. end
  438. File.open(list,"rb") do |password_list|
  439. password_list.each_line do |line|
  440. password = line.gsub("\r\n",'').gsub("\n",'')
  441. for permutedpw in permute_pw(password)
  442. puts permutedpw
  443. argntlm = { :ntlm_hash => CRYPT::ntlm_hash(permutedpw),
  444. :challenge => [ srvchal ].pack("H*") }
  445. calculatedhash = CRYPT::ntlm_response(argntlm).unpack("H*")[0].upcase
  446. if calculatedhash == hash.upcase
  447. puts "[*] Correct password found : #{permutedpw}"
  448. exit
  449. end
  450. end
  451. end
  452. end
  453. puts "[*] No password found"
  454. exit
  455. when HASH_MODE
  456. if not srvchal
  457. $stderr.puts "[*] Server challenge must be provided with this type"
  458. exit
  459. end
  460. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  461. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  462. exit
  463. end
  464. argntlm = { :ntlm_hash => CRYPT::ntlm_hash(pass),
  465. :challenge => [ srvchal ].pack("H*") }
  466. calculatedhash = CRYPT::ntlm_response(argntlm).unpack("H*")[0].upcase
  467. puts "[*] The NETNTLMv1 hash for #{pass} is : #{calculatedhash}"
  468. exit
  469. when PASS_MODE
  470. if not hash =~ /^([a-fA-F0-9]{48})$/
  471. $stderr.puts "[*] NETNTLMv1 HASH must be exactly 48 bytes of hexadecimal"
  472. exit
  473. end
  474. if not srvchal
  475. $stderr.puts "[*] Server challenge must be provided with this type"
  476. exit
  477. end
  478. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  479. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  480. exit
  481. end
  482. for permutedpw in permute_pw(pass)
  483. argntlm = { :ntlm_hash => CRYPT::ntlm_hash(permutedpw),
  484. :challenge => [ srvchal ].pack("H*") }
  485. calculatedhash = CRYPT::ntlm_response(argntlm).unpack("H*")[0].upcase
  486. if hash.upcase == calculatedhash
  487. puts "[*] Correct password provided : #{permutedpw}"
  488. exit
  489. end
  490. end
  491. puts "[*] Incorrect password provided : #{pass}"
  492. exit
  493. end
  494. when "NETNTLM2_SESSION"
  495. case mode
  496. when BRUTE_MODE
  497. if not hash =~ /^([a-fA-F0-9]{48})$/
  498. $stderr.puts "[*] NETNTLM2_SESSION HASH must be exactly 48 bytes of hexadecimal"
  499. exit
  500. end
  501. if not srvchal
  502. $stderr.puts "[*] Server challenge must be provided with this type"
  503. exit
  504. end
  505. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  506. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  507. exit
  508. end
  509. if not clichal
  510. $stderr.puts "[*] Client challenge must be provided with this type"
  511. exit
  512. end
  513. if not clichal =~ /^([a-fA-F0-9]{16})$/
  514. $stderr.puts "[*] Client challenge must be exactly 16 bytes of hexadecimal"
  515. exit
  516. end
  517. File.open(list,"rb") do |password_list|
  518. password_list.each_line do |line|
  519. password = line.gsub("\r\n",'').gsub("\n",'')
  520. for permutedpw in permute_pw(password)
  521. puts permutedpw
  522. argntlm = { :ntlm_hash => CRYPT::ntlm_hash(permutedpw),
  523. :challenge => [ srvchal ].pack("H*") }
  524. optntlm = { :client_challenge => [ clichal ].pack("H*")}
  525. calculatedhash = CRYPT::ntlm2_session(argntlm,optntlm).join[24,24].unpack("H*")[0].upcase
  526. if calculatedhash == hash.upcase
  527. puts "[*] Correct password found : #{permutedpw}"
  528. exit
  529. end
  530. end
  531. end
  532. end
  533. puts "[*] No password found"
  534. exit
  535. when HASH_MODE
  536. if not srvchal
  537. $stderr.puts "[*] Server challenge must be provided with this type"
  538. exit
  539. end
  540. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  541. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  542. exit
  543. end
  544. if not clichal
  545. $stderr.puts "[*] Client challenge must be provided with this type"
  546. exit
  547. end
  548. if not clichal =~ /^([a-fA-F0-9]{16})$/
  549. $stderr.puts "[*] Client challenge must be exactly 16 bytes of hexadecimal"
  550. exit
  551. end
  552. argntlm = { :ntlm_hash => CRYPT::ntlm_hash(pass),
  553. :challenge => [ srvchal ].pack("H*") }
  554. optntlm = { :client_challenge => [ clichal ].pack("H*")}
  555. calculatedhash = CRYPT::ntlm2_session(argntlm,optntlm).join[24,24].unpack("H*")[0].upcase
  556. puts "[*] The NETNTLM2_SESSION hash for #{pass} is : #{calculatedhash}"
  557. exit
  558. when PASS_MODE
  559. if not hash =~ /^([a-fA-F0-9]{48})$/
  560. $stderr.puts "[*] NETNTLM2_SESSION HASH must be exactly 48 bytes of hexadecimal"
  561. exit
  562. end
  563. if not srvchal
  564. $stderr.puts "[*] Server challenge must be provided with this type"
  565. exit
  566. end
  567. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  568. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  569. exit
  570. end
  571. if not clichal
  572. $stderr.puts "[*] Client challenge must be provided with this type"
  573. exit
  574. end
  575. if not clichal =~ /^([a-fA-F0-9]{16})$/
  576. $stderr.puts "[*] Client challenge must be exactly 16 bytes of hexadecimal"
  577. exit
  578. end
  579. for permutedpw in permute_pw(pass)
  580. argntlm = { :ntlm_hash => CRYPT::ntlm_hash(permutedpw),
  581. :challenge => [ srvchal ].pack("H*") }
  582. optntlm = { :client_challenge => [ clichal ].pack("H*")}
  583. calculatedhash = CRYPT::ntlm2_session(argntlm,optntlm).join[24,24].unpack("H*")[0].upcase
  584. if hash.upcase == calculatedhash
  585. puts "[*] Correct password provided : #{permutedpw}"
  586. exit
  587. end
  588. end
  589. puts "[*] Incorrect password provided : #{pass}"
  590. exit
  591. end
  592. when "NETLMv2"
  593. case mode
  594. when BRUTE_MODE
  595. if not hash =~ /^([a-fA-F0-9]{32})$/
  596. $stderr.puts "[*] NETLMv2 HASH must be exactly 32 bytes of hexadecimal"
  597. exit
  598. end
  599. if not srvchal
  600. $stderr.puts "[*] Server challenge must be provided with this type"
  601. exit
  602. end
  603. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  604. $stderr.puts "[*] Server challenge mus be exactly 16 bytes of hexadecimal"
  605. exit
  606. end
  607. if not clichal
  608. $stderr.puts "[*] Client challenge must be provided with this type"
  609. exit
  610. end
  611. if not clichal =~ /^([a-fA-F0-9]{16})$/
  612. $stderr.puts "[*] Client challenge must be exactly 16 bytes of hexadecimal"
  613. exit
  614. end
  615. if not user
  616. $stderr.puts "[*] User name must be provided with this type"
  617. exit
  618. end
  619. if not domain
  620. $stderr.puts "[*] Domain name must be provided with this type"
  621. exit
  622. end
  623. File.open(list,"rb") do |password_list|
  624. password_list.each_line do |line|
  625. password = line.gsub("\r\n",'').gsub("\n",'')
  626. puts password
  627. arglm = { :ntlmv2_hash => CRYPT::ntlmv2_hash(user,password, domain),
  628. :challenge => [ srvchal ].pack("H*") }
  629. optlm = { :client_challenge => [ clichal ].pack("H*")}
  630. calculatedhash = CRYPT::lmv2_response(arglm, optlm).unpack("H*")[0].upcase
  631. if calculatedhash.slice(0,32) == hash.upcase
  632. puts "[*] Correct password found : #{password}"
  633. exit
  634. end
  635. end
  636. end
  637. puts "[*] No password found"
  638. exit
  639. when HASH_MODE
  640. if not srvchal
  641. $stderr.puts "[*] Server challenge must be provided with this type"
  642. exit
  643. end
  644. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  645. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  646. exit
  647. end
  648. if not clichal
  649. $stderr.puts "[*] Client challenge must be provided with this type"
  650. exit
  651. end
  652. if not clichal =~ /^([a-fA-F0-9]{16})$/
  653. $stderr.puts "[*] Client challenge must be exactly 16 bytes of hexadecimal"
  654. exit
  655. end
  656. if not user
  657. $stderr.puts "[*] User name must be provided with this type"
  658. exit
  659. end
  660. if not domain
  661. $stderr.puts "[*] Domain name must be provided with this type"
  662. exit
  663. end
  664. arglm = { :ntlmv2_hash => CRYPT::ntlmv2_hash(user,pass, domain),
  665. :challenge => [ srvchal ].pack("H*") }
  666. optlm = { :client_challenge => [ clichal ].pack("H*")}
  667. calculatedhash = CRYPT::lmv2_response(arglm, optlm).unpack("H*")[0].upcase
  668. puts "[*] The NETLMv2 hash for #{pass} is : #{calculatedhash.slice(0,32)}"
  669. exit
  670. when PASS_MODE
  671. if not hash =~ /^([a-fA-F0-9]{32})$/
  672. $stderr.puts "[*] NETLMv2 HASH must be exactly 32 bytes of hexadecimal"
  673. exit
  674. end
  675. if not srvchal
  676. $stderr.puts "[*] Server challenge must be provided with this type"
  677. exit
  678. end
  679. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  680. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  681. exit
  682. end
  683. if not clichal
  684. $stderr.puts "[*] Client challenge must be provided with this type"
  685. exit
  686. end
  687. if not clichal =~ /^([a-fA-F0-9]{16})$/
  688. $stderr.puts "[*] Client challenge must be exactly 16 bytes of hexadecimal"
  689. exit
  690. end
  691. if not user
  692. $stderr.puts "[*] User name must be provided with this type"
  693. exit
  694. end
  695. if not domain
  696. $stderr.puts "[*] Domain name must be provided with this type"
  697. exit
  698. end
  699. arglm = { :ntlmv2_hash => CRYPT::ntlmv2_hash(user,pass, domain),
  700. :challenge => [ srvchal ].pack("H*") }
  701. optlm = { :client_challenge => [ clichal ].pack("H*")}
  702. calculatedhash = CRYPT::lmv2_response(arglm, optlm).unpack("H*")[0].upcase
  703. if hash.upcase == calculatedhash.slice(0,32)
  704. puts "[*] Correct password provided : #{pass}"
  705. exit
  706. else
  707. puts "[*] Incorrect password provided : #{pass}"
  708. exit
  709. end
  710. end
  711. when "NETNTLMv2"
  712. case mode
  713. when BRUTE_MODE
  714. if not hash =~ /^([a-fA-F0-9]{32})$/
  715. $stderr.puts "[*] NETNTLMv2 HASH must be exactly 32 bytes of hexadecimal"
  716. exit
  717. end
  718. if not srvchal
  719. $stderr.puts "[*] Server challenge must be provided with this type"
  720. exit
  721. end
  722. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  723. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  724. exit
  725. end
  726. if not clichal
  727. $stderr.puts "[*] Client challenge must be provided with this type"
  728. exit
  729. end
  730. if not clichal =~ /^([a-fA-F0-9]{17,})$/
  731. $stderr.puts "[*] Client challenge must be bigger then 16 bytes of hexadecimal"
  732. exit
  733. end
  734. if not user
  735. $stderr.puts "[*] User name must be provided with this type"
  736. exit
  737. end
  738. if not domain
  739. $stderr.puts "[*] Domain name must be provided with this type"
  740. exit
  741. end
  742. File.open(list,"rb") do |password_list|
  743. password_list.each_line do |line|
  744. password = line.gsub("\r\n",'').gsub("\n",'')
  745. for permutedpw in permute_pw(password)
  746. puts permutedpw
  747. argntlm = { :ntlmv2_hash => CRYPT::ntlmv2_hash(user, permutedpw, domain),
  748. :challenge => [ srvchal ].pack("H*") }
  749. optntlm = { :nt_client_challenge => [ clichal ].pack("H*")}
  750. calculatedhash = CRYPT::ntlmv2_response(argntlm,optntlm).unpack("H*")[0].upcase
  751. if calculatedhash.slice(0,32) == hash.upcase
  752. puts "[*] Correct password found : #{password}"
  753. exit
  754. end
  755. end
  756. end
  757. end
  758. puts "[*] No password found"
  759. exit
  760. when HASH_MODE
  761. if not srvchal
  762. $stderr.puts "[*] Server challenge must be provided with this type"
  763. exit
  764. end
  765. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  766. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  767. exit
  768. end
  769. if not clichal
  770. $stderr.puts "[*] Client challenge must be provided with this type"
  771. exit
  772. end
  773. if not clichal =~ /^([a-fA-F0-9]{17,})$/
  774. $stderr.puts "[*] Client challenge must be bigger then 16 bytes of hexadecimal"
  775. exit
  776. end
  777. if not user
  778. $stderr.puts "[*] User name must be provided with this type"
  779. exit
  780. end
  781. if not domain
  782. $stderr.puts "[*] Domain name must be provided with this type"
  783. exit
  784. end
  785. argntlm = { :ntlmv2_hash => CRYPT::ntlmv2_hash(user, pass, domain),
  786. :challenge => [ srvchal ].pack("H*") }
  787. optntlm = { :nt_client_challenge => [ clichal ].pack("H*")}
  788. calculatedhash = CRYPT::ntlmv2_response(argntlm,optntlm).unpack("H*")[0].upcase
  789. puts "[*] The NETNTLMv2 hash for #{pass} is : #{calculatedhash.slice(0,32)}"
  790. exit
  791. when PASS_MODE
  792. if not hash =~ /^([a-fA-F0-9]{32})$/
  793. $stderr.puts "[*] NETNTLMv2 HASH must be exactly 32 bytes of hexadecimal"
  794. exit
  795. end
  796. if not srvchal
  797. $stderr.puts "[*] Server challenge must be provided with this type"
  798. exit
  799. end
  800. if not srvchal =~ /^([a-fA-F0-9]{16})$/
  801. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  802. exit
  803. end
  804. if not clichal
  805. $stderr.puts "[*] Client challenge must be provided with this type"
  806. exit
  807. end
  808. if not clichal =~ /^([a-fA-F0-9]{17,})$/
  809. $stderr.puts "[*] Client challenge must be bigger then 16 bytes of hexadecimal"
  810. exit
  811. end
  812. if not user
  813. $stderr.puts "[*] User name must be provided with this type"
  814. exit
  815. end
  816. if not domain
  817. $stderr.puts "[*] Domain name must be provided with this type"
  818. exit
  819. end
  820. for permutedpw in permute_pw(password)
  821. argntlm = { :ntlmv2_hash => CRYPT::ntlmv2_hash(user, permutedpw, domain),
  822. :challenge => [ srvchal ].pack("H*") }
  823. optntlm = { :nt_client_challenge => [ clichal ].pack("H*")}
  824. calculatedhash = CRYPT::ntlmv2_response(argntlm,optntlm).unpack("H*")[0].upcase
  825. if hash.upcase == calculatedhash.slice(0,32)
  826. puts "[*] Correct password provided : #{permutedpw}"
  827. exit
  828. end
  829. end
  830. puts "[*] Incorrect password provided : #{pass}"
  831. exit
  832. end
  833. else
  834. $stderr.puts "type must be of type : HALFLM/LM/NTLM/HALFNETLMv1/NETLMv1/NETNTLMv1/NETNTLM2_SESSION/NETLMv2/NETNTLMv2"
  835. exit
  836. end