2005-03-27 00:23:10 +00:00
|
|
|
require 'openssl'
|
|
|
|
|
|
|
|
module Rex
|
2005-07-16 07:38:31 +00:00
|
|
|
module Proto
|
2005-03-27 00:23:10 +00:00
|
|
|
module SMB
|
2005-09-16 09:27:41 +00:00
|
|
|
class Crypt
|
2005-03-27 00:23:10 +00:00
|
|
|
|
2005-09-16 09:27:41 +00:00
|
|
|
def self.lanman_des(pass, chal)
|
2005-03-27 00:23:10 +00:00
|
|
|
e_p24( [ e_p16( [ pass.upcase()[0,14] ].pack('a14') ) ].pack('a21'), chal)
|
|
|
|
end
|
|
|
|
|
2005-09-16 09:27:41 +00:00
|
|
|
def self.e_p16(pass)
|
2005-03-27 00:23:10 +00:00
|
|
|
stat = "\x4b\x47\x53\x21\x40\x23\x24\x25"
|
|
|
|
des_hash(stat, pass[0,7]) << des_hash(stat, pass[7,7])
|
|
|
|
end
|
|
|
|
|
2005-09-16 09:27:41 +00:00
|
|
|
def self.e_p24(pass, chal)
|
2005-03-27 00:23:10 +00:00
|
|
|
des_hash(chal, pass[0,7]) << des_hash(chal, pass[7,7]) << des_hash(chal, pass[14,7])
|
|
|
|
end
|
|
|
|
|
2005-09-16 09:27:41 +00:00
|
|
|
def self.des_hash(data, ckey)
|
2005-03-27 00:23:10 +00:00
|
|
|
cipher = OpenSSL::Cipher::Cipher.new('des-ecb')
|
|
|
|
cipher.encrypt
|
|
|
|
cipher.key = des_56_to_64(ckey)
|
|
|
|
cipher.update(data)
|
|
|
|
end
|
|
|
|
|
2005-09-16 09:27:41 +00:00
|
|
|
def self.des_56_to_64(ckey56)
|
2005-03-27 00:23:10 +00:00
|
|
|
ckey64 = []
|
|
|
|
ckey64[0] = ckey56[0]
|
|
|
|
ckey64[1] = ((ckey56[0] << 7) & 0xFF) | (ckey56[1] >> 1)
|
|
|
|
ckey64[2] = ((ckey56[1] << 6) & 0xFF) | (ckey56[2] >> 2)
|
|
|
|
ckey64[3] = ((ckey56[2] << 5) & 0xFF) | (ckey56[3] >> 3)
|
|
|
|
ckey64[4] = ((ckey56[3] << 4) & 0xFF) | (ckey56[4] >> 4)
|
|
|
|
ckey64[5] = ((ckey56[4] << 3) & 0xFF) | (ckey56[5] >> 5)
|
|
|
|
ckey64[6] = ((ckey56[5] << 2) & 0xFF) | (ckey56[6] >> 6)
|
|
|
|
ckey64[7] = (ckey56[6] << 1) & 0xFF
|
|
|
|
ckey64.pack('C*')
|
|
|
|
end
|
|
|
|
|
2005-09-16 09:27:41 +00:00
|
|
|
def self.unicode (str)
|
2005-03-27 00:23:10 +00:00
|
|
|
str.unpack('C*').pack('v*')
|
|
|
|
end
|
|
|
|
|
2005-09-16 09:27:41 +00:00
|
|
|
def self.ntlm_md4(pass, chal)
|
2005-03-27 00:23:10 +00:00
|
|
|
e_p24( [ md4_hash(unicode(pass)) ].pack('a21'), chal)
|
|
|
|
end
|
|
|
|
|
2005-09-16 09:27:41 +00:00
|
|
|
def self.md4_hash(data)
|
2005-03-27 00:23:10 +00:00
|
|
|
digest = OpenSSL::Digest::Digest.digest('md4', data)
|
|
|
|
end
|
2005-09-19 23:35:51 +00:00
|
|
|
|
|
|
|
def self.md5_hash(data)
|
|
|
|
digest = OpenSSL::Digest::Digest.digest('md5', data)
|
|
|
|
end
|
2005-03-27 00:23:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-07-16 09:37:18 +00:00
|
|
|
end
|