Path: blob/master/lib/msf/util/windows_crypto_helpers.rb
21537 views
module Msf1module Util2module WindowsCryptoHelpers34EMPTY_LM = "\xaa\xd3\xb4\x35\xb5\x14\x04\xee\xaa\xd3\xb4\x35\xb5\x14\x04\xee".b5EMPTY_NT = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89\xc0".b67#class Error < RuntimeError; end8#class Unknown < Error; end910# Converts DES 56 key to DES 64 key11#12# See [2.2.11.1.2 Encrypting a 64-Bit Block with a 7-Byte Key](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-samr/ebdb15df-8d0d-4347-9d62-082e6eccac40)13#14# @param kstr [String] The key to convert15# @return [String] The converted key16def convert_des_56_to_64(kstr)17des_odd_parity = [181, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14,1916, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,2032, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,2149, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,2264, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,2381, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,2497, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,25112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,26128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,27145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,28161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,29176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,30193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,31208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,32224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,33241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,25434]3536key = []37str = kstr.unpack("C*")3839key[0] = str[0] >> 140key[1] = ((str[0] & 0x01) << 6) | (str[1] >> 2)41key[2] = ((str[1] & 0x03) << 5) | (str[2] >> 3)42key[3] = ((str[2] & 0x07) << 4) | (str[3] >> 4)43key[4] = ((str[3] & 0x0F) << 3) | (str[4] >> 5)44key[5] = ((str[4] & 0x1F) << 2) | (str[5] >> 6)45key[6] = ((str[5] & 0x3F) << 1) | (str[6] >> 7)46key[7] = str[6] & 0x7F47480.upto(7) do |i|49key[i] = ( key[i] << 1)50key[i] = des_odd_parity[key[i]]51end52return key.pack("C*")53end5455# Decrypts "Secret" encrypted data56#57# Ruby implementation of SystemFunction005. The original python code58# has been taken from Credump59#60# @param secret [String] The secret to decrypt61# @param key [String] The key to decrypt the secret62# @return [String] The decrypted data63def decrypt_secret_data(secret, key)6465j = 066decrypted_data = ''6768for i in (0...secret.length).step(8)69enc_block = secret[i..i+7]70block_key = key[j..j+6]71des_key = convert_des_56_to_64(block_key)72d1 = OpenSSL::Cipher.new('des-ecb')73d1.decrypt74d1.padding = 075d1.key = des_key76d1o = d1.update(enc_block)77d1o << d1.final78decrypted_data += d1o79j += 780if (key[j..j+7].length < 7 )81j = key[j..j+7].length82end83end84dec_data_len = decrypted_data[0,4].unpack('L<').first8586return decrypted_data[8, dec_data_len]8788end8990# Decrypts LSA encrypted data91#92# @param policy_secret [String] The encrypted data stored in the registry93# @param lsa_key [String] The LSA key94# @return [String] The decrypted data95def decrypt_lsa_data(policy_secret, lsa_key)9697sha256x = Digest::SHA256.new()98sha256x << lsa_key991000.times do100sha256x << policy_secret[28,32]101end102103aes = OpenSSL::Cipher.new("aes-256-cbc")104aes.decrypt105aes.key = sha256x.digest106107# vprint_status("digest #{sha256x.digest.unpack("H*")[0]}")108109decrypted_data = ''110111(60...policy_secret.length).step(16) do |i|112aes.reset113aes.padding = 0114aes.iv = "\x00" * 16115decrypted_data << aes.update(policy_secret[i,16])116end117118return decrypted_data119end120121# Derive DES Key1 and Key2 from user RID.122#123# @param rid [String] The user RID124# @return [Array] A two element array containing Key1 and Key2, in this order125def rid_to_key(rid)126# See [2.2.11.1.3 Deriving Key1 and Key2 from a Little-Endian, Unsigned Integer Key](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-samr/b1b0094f-2546-431f-b06d-582158a9f2bb)127s1 = [rid].pack('V')128s1 << s1[0, 3]129130s2b = [rid].pack('V').unpack('C4')131s2 = [s2b[3], s2b[0], s2b[1], s2b[2]].pack('C4')132s2 << s2[0, 3]133134[convert_des_56_to_64(s1), convert_des_56_to_64(s2)]135end136137# This decrypt an encrypted NT or LM hash.138# See [2.2.11.1.1 Encrypting an NT or LM Hash Value with a Specified Key](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-samr/a5252e8c-25e7-4616-a375-55ced086b19b)139#140# @param rid [String] The user RID141# @param hboot_key [String] The hashedBootKey142# @param enc_hash [String] The encrypted hash143# @param pass [String] The password used for revision 1 hashes144# @param default [String] The default hash to return if something goes wrong145# @return [String] The decrypted NT or LM hash146def decrypt_user_hash(rid, hboot_key, enc_hash, pass, default)147revision = enc_hash[2, 2]&.unpack('v')&.first148149case revision150when 1151return default if enc_hash.length < 20152153md5 = Digest::MD5.new154md5.update(hboot_key[0, 16] + [rid].pack('V') + pass)155156rc4 = OpenSSL::Cipher.new('rc4')157rc4.decrypt158rc4.key = md5.digest159okey = rc4.update(enc_hash[4, 16])160when 2161return default if enc_hash.length < 40162163aes = OpenSSL::Cipher.new('aes-128-cbc')164aes.decrypt165aes.key = hboot_key[0, 16]166aes.padding = 0167aes.iv = enc_hash[8, 16]168okey = aes.update(enc_hash[24, 16]) # we need only 16 bytes169else170elog("decrypt_user_hash: Unknown user hash revision: #{revision}, returning default")171return default172end173174des_k1, des_k2 = rid_to_key(rid)175176d1 = OpenSSL::Cipher.new('des-ecb')177d1.decrypt178d1.padding = 0179d1.key = des_k1180181d2 = OpenSSL::Cipher.new('des-ecb')182d2.decrypt183d2.padding = 0184d2.key = des_k2185186d1o = d1.update(okey[0, 8])187d1o << d1.final188189d2o = d2.update(okey[8, 8])190d1o << d2.final191d1o + d2o192end193194# Decrypts the user V key value and return the NT amd LM hashes. The V value195# can be found under the196# HKEY_LOCAL_MACHINE\SAM\SAM\Domains\Account\Users\<RID> registry key.197#198# @param hboot_key [String] The hashedBootKey199# @param user_v [String] The user V value200# @param rid [String] The user RID201# @return [Array] Array with the first and second element containing the NT and LM hashes respectively202def decrypt_user_key(hboot_key, user_v, rid)203sam_lmpass = "LMPASSWORD\x00"204sam_ntpass = "NTPASSWORD\x00"205206# TODO: use a proper structure for V data, instead of unpacking directly207hashlm_off = user_v[0x9c, 4]&.unpack('V')&.first208hashlm_len = user_v[0xa0, 4]&.unpack('V')&.first209if hashlm_off && hashlm_len210hashlm_enc = user_v[hashlm_off + 0xcc, hashlm_len]211hashlm = decrypt_user_hash(rid, hboot_key, hashlm_enc, sam_lmpass, EMPTY_LM)212else213elog('decrypt_user_key: Unable to extract LM hash, using empty LM hash instead')214hashlm = EMPTY_LM215end216217hashnt_off = user_v[0xa8, 4]&.unpack('V')&.first218hashnt_len = user_v[0xac, 4]&.unpack('V')&.first219if hashnt_off && hashnt_len220hashnt_enc = user_v[hashnt_off + 0xcc, hashnt_len]221hashnt = decrypt_user_hash(rid, hboot_key, hashnt_enc, sam_ntpass, EMPTY_NT)222else223elog('decrypt_user_key: Unable to extract NT hash, using empty NT hash instead')224hashnt = EMPTY_NT225end226227[hashnt, hashlm]228end229230# Decrypt a cipher using AES in CBC mode. The key length is deduced from231# `key` argument length. The supported key length are 16, 24 and 32. Also, it232# will take care of padding the last block if the cipher length is not modulo233# 16.234#235# @param edata [String] The cipher to decrypt236# @param key [String] The key used to decrypt237# @param iv [String] The IV238# @return [String, nil] The decrypted plaintext or nil if the key size is not supported239def decrypt_aes(edata, key, iv)240cipher_str = case key.length241when 16242'aes-128-cbc'243when 24244'aes-192-cbc'245when 32246'aes-256-cbc'247else248elog("decrypt_aes: Unknown key length (#{key.length} bytes)")249return250end251aes = OpenSSL::Cipher.new(cipher_str)252aes.decrypt253aes.key = key254aes.padding = 0255aes.iv = iv256257decrypted = ''258(0...edata.length).step(aes.block_size) do |i|259block_str = edata[i, aes.block_size]260# Pad buffer with \x00 if needed261if block_str.length < aes.block_size262block_str << "\x00".b * (aes.block_size - block_str.length)263end264decrypted << aes.update(block_str)265end266267return decrypted268end269270# Decrypt encrypted cached entry from HKLM\Security\Cache\NL$XX271#272# @param edata [String] The encrypted hash entry to decrypt273# @param key [String] The key used to decrypt274# @param iv [String] The IV275# @return [String, nil] The decrypted plaintext or nil if the key size is not supported276def decrypt_hash(edata, key, iv)277rc4key = OpenSSL::HMAC.digest(OpenSSL::Digest.new('md5'), key, iv)278rc4 = OpenSSL::Cipher.new('rc4')279rc4.decrypt280rc4.key = rc4key281decrypted = rc4.update(edata)282decrypted << rc4.final283284return decrypted285end286287def add_parity(byte_str)288byte_str.map do |byte|289if byte.to_s(2).count('1').odd?290(byte << 1) & 0b11111110291else292(byte << 1) | 0b00000001293end294end295end296297def fix_parity(byte_str)298byte_str.map do |byte|299t = byte.to_s(2).rjust(8, '0')300if t[0, 7].count('1').odd?301("#{t[0, 7]}0").to_i(2).chr302else303("#{t[0, 7]}1").to_i(2).chr304end305end306end307308def weak_des_key?(key)309[310"\x01\x01\x01\x01\x01\x01\x01\x01",311"\xFE\xFE\xFE\xFE\xFE\xFE\xFE\xFE",312"\x1F\x1F\x1F\x1F\x0E\x0E\x0E\x0E",313"\xE0\xE0\xE0\xE0\xF1\xF1\xF1\xF1",314"\x01\xFE\x01\xFE\x01\xFE\x01\xFE",315"\xFE\x01\xFE\x01\xFE\x01\xFE\x01",316"\x1F\xE0\x1F\xE0\x0E\xF1\x0E\xF1",317"\xE0\x1F\xE0\x1F\xF1\x0E\xF1\x0E",318"\x01\xE0\x01\xE0\x01\xF1\x01\xF1",319"\xE0\x01\xE0\x01\xF1\x01\xF1\x01",320"\x1F\xFE\x1F\xFE\x0E\xFE\x0E\xFE",321"\xFE\x1F\xFE\x1F\xFE\x0E\xFE\x0E",322"\x01\x1F\x01\x1F\x01\x0E\x01\x0E",323"\x1F\x01\x1F\x01\x0E\x01\x0E\x01",324"\xE0\xFE\xE0\xFE\xF1\xFE\xF1\xFE",325"\xFE\xE0\xFE\xE0\xFE\xF1\xFE\xF1"326].include?(key)327end328329# Encrypt using MIT Kerberos des-cbc-md5330# http://web.mit.edu/kerberos/krb5-latest/doc/admin/enctypes.html?highlight=des#enctype-compatibility331#332# @param raw_secret [String] The data to encrypt333# @param key [String] The salt used by the encryption algorithm334# @return [String, nil] The encrypted data335def des_cbc_md5(raw_secret, salt)336odd = true337tmp_byte_str = [0, 0, 0, 0, 0, 0, 0, 0]338plaintext = raw_secret + salt339plaintext += "\x00".b * (8 - (plaintext.size % 8))340plaintext.bytes.each_slice(8) do |block|341tmp_56 = block.map { |byte| byte & 0b01111111 }342if !odd343# rubocop:disable Style/FormatString344tmp_56_str = tmp_56.map { |byte| '%07b' % byte }.join345# rubocop:enable Style/FormatString346tmp_56_str.reverse!347tmp_56 = tmp_56_str.bytes.each_slice(7).map do |bits7|348bits7.map(&:chr).join.to_i(2)349end350end351odd = !odd352tmp_byte_str = tmp_byte_str.zip(tmp_56).map { |a, b| a ^ b }353end354tempkey = add_parity(tmp_byte_str).map(&:chr).join355if weak_des_key?(tempkey)356tempkey[7] = (tempkey[7].ord ^ 0xF0).chr357end358cipher = OpenSSL::Cipher.new('DES-CBC')359cipher.encrypt360cipher.iv = tempkey361cipher.key = tempkey362chekcsumkey = cipher.update(plaintext)[-8..-1]363chekcsumkey = fix_parity(chekcsumkey.bytes).map(&:chr).join364if weak_des_key?(chekcsumkey)365chekcsumkey[7] = (chekcsumkey[7].ord ^ 0xF0).chr366end367chekcsumkey.unpack('H*')[0]368end369370# Encrypt using MIT Kerberos aesXXX-cts-hmac-sha1-96371# http://web.mit.edu/kerberos/krb5-latest/doc/admin/enctypes.html?highlight=des#enctype-compatibility372#373# @param algorithm [String] The AES algorithm to use (e.g. `128-CBC` or `256-CBC`)374# @param raw_secret [String] The data to encrypt375# @param key [String] The salt used by the encryption algorithm376# @return [String, nil] The encrypted data377def aes_cts_hmac_sha1_96(algorithm, raw_secret, salt)378iterations = 4096379cipher = OpenSSL::Cipher::AES.new(algorithm)380key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(raw_secret, salt, iterations, cipher.key_len)381plaintext = "kerberos\x7B\x9B\x5B\x2B\x93\x13\x2B\x93".b382rnd_seed = ''.b383loop do384cipher.reset385cipher.encrypt386cipher.iv = "\x00".b * 16387cipher.key = key388ciphertext = cipher.update(plaintext)389rnd_seed += ciphertext390break unless rnd_seed.size < cipher.key_len391392plaintext = ciphertext393end394rnd_seed395end396397# Encrypt using MIT Kerberos aes128-cts-hmac-sha1-96398# http://web.mit.edu/kerberos/krb5-latest/doc/admin/enctypes.html?highlight=des#enctype-compatibility399#400# @param raw_secret [String] The data to encrypt401# @param salt [String] The salt used by the encryption algorithm402# @return [String, nil] The encrypted data403def aes128_cts_hmac_sha1_96(raw_secret, salt)404aes_cts_hmac_sha1_96('128-CBC', raw_secret, salt)405end406407# Encrypt using MIT Kerberos aes256-cts-hmac-sha1-96408# http://web.mit.edu/kerberos/krb5-latest/doc/admin/enctypes.html?highlight=des#enctype-compatibility409#410# @param raw_secret [String] The data to encrypt411# @param salt [String] The salt used by the encryption algorithm412# @return [String, nil] The encrypted data413def aes256_cts_hmac_sha1_96(raw_secret, salt)414aes_cts_hmac_sha1_96('256-CBC', raw_secret, salt)415end416417# Encrypt using MIT Kerberos rc4_hmac418# http://web.mit.edu/kerberos/krb5-latest/doc/admin/enctypes.html?highlight=des#enctype-compatibility419#420# @param raw_secret [String] The data to encrypt421# @param salt [String] The salt used by the encryption algorithm422# @return [String, nil] The encrypted data423def rc4_hmac(raw_secret, salt = nil)424Rex::Proto::Kerberos::Crypto::Rc4Hmac.new.string_to_key(raw_secret, salt)425end426end427end428end429430431