halflm_second.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env ruby
  2. ##
  3. # This module requires Metasploit: https://metasploit.com/download
  4. # Current source: https://github.com/rapid7/metasploit-framework
  5. ##
  6. #
  7. # This script cracks a half-lm challenge/response hash that uses a
  8. # a static challenge key. The idea is you use rainbow tables to
  9. # crack the first 7 chars and this script to complete a few remaining.
  10. # If the password is longer than 10 characters, this script will fail.
  11. #
  12. msfbase = __FILE__
  13. while File.symlink?(msfbase)
  14. msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
  15. end
  16. $:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))
  17. require 'msfenv'
  18. $:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
  19. require 'rex'
  20. def usage
  21. $stderr.puts("\n" + " Usage: #{$0} <options>\n" + $args.usage)
  22. exit
  23. end
  24. def try(word,challenge)
  25. buf = ::Rex::Proto::NTLM::Crypt.lanman_des(word, challenge)
  26. buf.unpack("H*")[0]
  27. end
  28. hash = pass = chall = nil
  29. $args = Rex::Parser::Arguments.new(
  30. "-n" => [ true, "The encrypted LM hash to crack" ],
  31. "-p" => [ true, "The decrypted LANMAN password for bytes 1-7" ],
  32. "-s" => [ true, "The server challenge (default value 1122334455667788)" ],
  33. "-h" => [ false, "Display this help information" ])
  34. $args.parse(ARGV) { |opt, idx, val|
  35. case opt
  36. when "-n"
  37. hash = val
  38. when "-p"
  39. pass = val
  40. when "-s"
  41. chall = val
  42. when "-h"
  43. usage
  44. else
  45. usage
  46. end
  47. }
  48. if (not (hash and pass))
  49. usage
  50. end
  51. if (not chall)
  52. chall = ["1122334455667788"].pack("H*")
  53. else
  54. if not chall =~ /^([a-fA-F0-9]{16})$/
  55. $stderr.puts "[*] Server challenge must be exactly 16 bytes of hexadecimal"
  56. exit
  57. else
  58. chall = [chall].pack("H*")
  59. end
  60. end
  61. if(hash.length != 48)
  62. $stderr.puts "[*] LANMAN should be exactly 48 bytes of hexadecimal"
  63. exit
  64. end
  65. if(pass.length != 7)
  66. $stderr.puts "[*] Cracked LANMAN password should be exactly 7 characters"
  67. exit
  68. end
  69. pass = pass.upcase
  70. hash = hash.downcase
  71. cset = [*(1..255)].pack("C*").upcase.unpack("C*").uniq
  72. stime = Time.now.to_f
  73. puts "[*] Trying one character..."
  74. 0.upto(cset.length-1) do |c1|
  75. test = pass + cset[c1].chr
  76. if(try(test, chall) == hash)
  77. puts "[*] Cracked: #{test}"
  78. exit
  79. end
  80. end
  81. etime = Time.now.to_f - stime
  82. puts "[*] Trying two characters (eta: #{etime * cset.length} seconds)..."
  83. 0.upto(cset.length-1) do |c1|
  84. 0.upto(cset.length-1) do |c2|
  85. test = pass + cset[c1].chr + cset[c2].chr
  86. if(try(test, chall) == hash)
  87. puts "[*] Cracked: #{test}"
  88. exit
  89. end
  90. end
  91. end
  92. puts "[*] Trying three characters (eta: #{etime * cset.length * cset.length} seconds)..."
  93. 0.upto(cset.length-1) do |c1|
  94. 0.upto(cset.length-1) do |c2|
  95. 0.upto(cset.length-1) do |c3|
  96. test = pass + cset[c1].chr + cset[c2].chr + cset[c3].chr
  97. if(try(test, chall) == hash)
  98. puts "[*] Cracked: #{test}"
  99. exit
  100. end
  101. end
  102. end
  103. end
  104. puts "[*] Trying four characters (eta: #{etime * cset.length * cset.length * cset.length} seconds)..."
  105. 0.upto(cset.length-1) do |c1|
  106. 0.upto(cset.length-1) do |c2|
  107. 0.upto(cset.length-1) do |c3|
  108. 0.upto(cset.length-1) do |c4|
  109. test = pass + cset[c1].chr + cset[c2].chr + cset[c3].chr + cset[c4].chr
  110. if(try(test, chall) == hash)
  111. puts "[*] Cracked: #{test}"
  112. exit
  113. end
  114. end
  115. end
  116. end
  117. end