Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/util/windows_crypto_helpers.rb
Views: 11623
module Msf1module Util2module WindowsCryptoHelpers34#class Error < RuntimeError; end5#class Unknown < Error; end67# Converts DES 56 key to DES 64 key8#9# 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)10#11# @param kstr [String] The key to convert12# @return [String] The converted key13def convert_des_56_to_64(kstr)14des_odd_parity = [151, 1, 2, 2, 4, 4, 7, 7, 8, 8, 11, 11, 13, 13, 14, 14,1616, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,1732, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,1849, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,1964, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,2081, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,2197, 97, 98, 98,100,100,103,103,104,104,107,107,109,109,110,110,22112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,23128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,24145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,25161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,26176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,27193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,28208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,29224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,30241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,25431]3233key = []34str = kstr.unpack("C*")3536key[0] = str[0] >> 137key[1] = ((str[0] & 0x01) << 6) | (str[1] >> 2)38key[2] = ((str[1] & 0x03) << 5) | (str[2] >> 3)39key[3] = ((str[2] & 0x07) << 4) | (str[3] >> 4)40key[4] = ((str[3] & 0x0F) << 3) | (str[4] >> 5)41key[5] = ((str[4] & 0x1F) << 2) | (str[5] >> 6)42key[6] = ((str[5] & 0x3F) << 1) | (str[6] >> 7)43key[7] = str[6] & 0x7F44450.upto(7) do |i|46key[i] = ( key[i] << 1)47key[i] = des_odd_parity[key[i]]48end49return key.pack("C*")50end5152# Decrypts "Secret" encrypted data53#54# Ruby implementation of SystemFunction005. The original python code55# has been taken from Credump56#57# @param secret [String] The secret to decrypt58# @param key [String] The key to decrypt the secret59# @return [String] The decrypted data60def decrypt_secret_data(secret, key)6162j = 063decrypted_data = ''6465for i in (0...secret.length).step(8)66enc_block = secret[i..i+7]67block_key = key[j..j+6]68des_key = convert_des_56_to_64(block_key)69d1 = OpenSSL::Cipher.new('des-ecb')70d1.decrypt71d1.padding = 072d1.key = des_key73d1o = d1.update(enc_block)74d1o << d1.final75decrypted_data += d1o76j += 777if (key[j..j+7].length < 7 )78j = key[j..j+7].length79end80end81dec_data_len = decrypted_data[0,4].unpack('L<').first8283return decrypted_data[8, dec_data_len]8485end8687# Decrypts LSA encrypted data88#89# @param policy_secret [String] The encrypted data stored in the registry90# @param lsa_key [String] The LSA key91# @return [String] The decrypted data92def decrypt_lsa_data(policy_secret, lsa_key)9394sha256x = Digest::SHA256.new()95sha256x << lsa_key961000.times do97sha256x << policy_secret[28,32]98end99100aes = OpenSSL::Cipher.new("aes-256-cbc")101aes.decrypt102aes.key = sha256x.digest103104# vprint_status("digest #{sha256x.digest.unpack("H*")[0]}")105106decrypted_data = ''107108(60...policy_secret.length).step(16) do |i|109aes.reset110aes.padding = 0111aes.iv = "\x00" * 16112decrypted_data << aes.update(policy_secret[i,16])113end114115return decrypted_data116end117118# Derive DES Key1 and Key2 from user RID.119#120# @param rid [String] The user RID121# @return [Array] A two element array containing Key1 and Key2, in this order122def rid_to_key(rid)123# 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)124s1 = [rid].pack('V')125s1 << s1[0, 3]126127s2b = [rid].pack('V').unpack('C4')128s2 = [s2b[3], s2b[0], s2b[1], s2b[2]].pack('C4')129s2 << s2[0, 3]130131[convert_des_56_to_64(s1), convert_des_56_to_64(s2)]132end133134# This decrypt an encrypted NT or LM hash.135# 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)136#137# @param rid [String] The user RID138# @param hboot_key [String] The hashedBootKey139# @param enc_hash [String] The encrypted hash140# @param pass [String] The password used for revision 1 hashes141# @param default [String] The default hash to return if something goes wrong142# @return [String] The decrypted NT or LM hash143def decrypt_user_hash(rid, hboot_key, enc_hash, pass, default)144revision = enc_hash[2, 2]&.unpack('v')&.first145146case revision147when 1148return default if enc_hash.length < 20149150md5 = Digest::MD5.new151md5.update(hboot_key[0, 16] + [rid].pack('V') + pass)152153rc4 = OpenSSL::Cipher.new('rc4')154rc4.decrypt155rc4.key = md5.digest156okey = rc4.update(enc_hash[4, 16])157when 2158return default if enc_hash.length < 40159160aes = OpenSSL::Cipher.new('aes-128-cbc')161aes.decrypt162aes.key = hboot_key[0, 16]163aes.padding = 0164aes.iv = enc_hash[8, 16]165okey = aes.update(enc_hash[24, 16]) # we need only 16 bytes166else167elog("decrypt_user_hash: Unknown user hash revision: #{revision}, returning default")168return default169end170171des_k1, des_k2 = rid_to_key(rid)172173d1 = OpenSSL::Cipher.new('des-ecb')174d1.decrypt175d1.padding = 0176d1.key = des_k1177178d2 = OpenSSL::Cipher.new('des-ecb')179d2.decrypt180d2.padding = 0181d2.key = des_k2182183d1o = d1.update(okey[0, 8])184d1o << d1.final185186d2o = d2.update(okey[8, 8])187d1o << d2.final188d1o + d2o189end190191# Decrypts the user V key value and return the NT amd LM hashes. The V value192# can be found under the193# HKEY_LOCAL_MACHINE\SAM\SAM\Domains\Account\Users\<RID> registry key.194#195# @param hboot_key [String] The hashedBootKey196# @param user_v [String] The user V value197# @param rid [String] The user RID198# @return [Array] Array with the first and second element containing the NT and LM hashes respectively199def decrypt_user_key(hboot_key, user_v, rid)200sam_lmpass = "LMPASSWORD\x00"201sam_ntpass = "NTPASSWORD\x00"202sam_empty_lm = ['aad3b435b51404eeaad3b435b51404ee'].pack('H*')203sam_empty_nt = ['31d6cfe0d16ae931b73c59d7e0c089c0'].pack('H*')204205# TODO: use a proper structure for V data, instead of unpacking directly206hashlm_off = user_v[0x9c, 4]&.unpack('V')&.first207hashlm_len = user_v[0xa0, 4]&.unpack('V')&.first208if hashlm_off && hashlm_len209hashlm_enc = user_v[hashlm_off + 0xcc, hashlm_len]210hashlm = decrypt_user_hash(rid, hboot_key, hashlm_enc, sam_lmpass, sam_empty_lm)211else212elog('decrypt_user_key: Unable to extract LM hash, using empty LM hash instead')213hashlm = sam_empty_lm214end215216hashnt_off = user_v[0xa8, 4]&.unpack('V')&.first217hashnt_len = user_v[0xac, 4]&.unpack('V')&.first218if hashnt_off && hashnt_len219hashnt_enc = user_v[hashnt_off + 0xcc, hashnt_len]220hashnt = decrypt_user_hash(rid, hboot_key, hashnt_enc, sam_ntpass, sam_empty_nt)221else222elog('decrypt_user_key: Unable to extract NT hash, using empty NT hash instead')223hashnt = sam_empty_nt224end225226[hashnt, hashlm]227end228229# Decrypt a cipher using AES in CBC mode. The key length is deduced from230# `key` argument length. The supported key length are 16, 24 and 32. Also, it231# will take care of padding the last block if the cipher length is not modulo232# 16.233#234# @param edata [String] The cipher to decrypt235# @param key [String] The key used to decrypt236# @param iv [String] The IV237# @return [String, nil] The decrypted plaintext or nil if the key size is not supported238def decrypt_aes(edata, key, iv)239cipher_str = case key.length240when 16241'aes-128-cbc'242when 24243'aes-192-cbc'244when 32245'aes-256-cbc'246else247elog("decrypt_aes: Unknown key length (#{key.length} bytes)")248return249end250aes = OpenSSL::Cipher.new(cipher_str)251aes.decrypt252aes.key = key253aes.padding = 0254aes.iv = iv255256decrypted = ''257(0...edata.length).step(aes.block_size) do |i|258block_str = edata[i, aes.block_size]259# Pad buffer with \x00 if needed260if block_str.length < aes.block_size261block_str << "\x00".b * (aes.block_size - block_str.length)262end263decrypted << aes.update(block_str)264end265266return decrypted267end268269# Decrypt encrypted cached entry from HKLM\Security\Cache\NL$XX270#271# @param edata [String] The encrypted hash entry to decrypt272# @param key [String] The key used to decrypt273# @param iv [String] The IV274# @return [String, nil] The decrypted plaintext or nil if the key size is not supported275def decrypt_hash(edata, key, iv)276rc4key = OpenSSL::HMAC.digest(OpenSSL::Digest.new('md5'), key, iv)277rc4 = OpenSSL::Cipher.new('rc4')278rc4.decrypt279rc4.key = rc4key280decrypted = rc4.update(edata)281decrypted << rc4.final282283return decrypted284end285286def add_parity(byte_str)287byte_str.map do |byte|288if byte.to_s(2).count('1').odd?289(byte << 1) & 0b11111110290else291(byte << 1) | 0b00000001292end293end294end295296def fix_parity(byte_str)297byte_str.map do |byte|298t = byte.to_s(2).rjust(8, '0')299if t[0, 7].count('1').odd?300("#{t[0, 7]}0").to_i(2).chr301else302("#{t[0, 7]}1").to_i(2).chr303end304end305end306307def weak_des_key?(key)308[309"\x01\x01\x01\x01\x01\x01\x01\x01",310"\xFE\xFE\xFE\xFE\xFE\xFE\xFE\xFE",311"\x1F\x1F\x1F\x1F\x0E\x0E\x0E\x0E",312"\xE0\xE0\xE0\xE0\xF1\xF1\xF1\xF1",313"\x01\xFE\x01\xFE\x01\xFE\x01\xFE",314"\xFE\x01\xFE\x01\xFE\x01\xFE\x01",315"\x1F\xE0\x1F\xE0\x0E\xF1\x0E\xF1",316"\xE0\x1F\xE0\x1F\xF1\x0E\xF1\x0E",317"\x01\xE0\x01\xE0\x01\xF1\x01\xF1",318"\xE0\x01\xE0\x01\xF1\x01\xF1\x01",319"\x1F\xFE\x1F\xFE\x0E\xFE\x0E\xFE",320"\xFE\x1F\xFE\x1F\xFE\x0E\xFE\x0E",321"\x01\x1F\x01\x1F\x01\x0E\x01\x0E",322"\x1F\x01\x1F\x01\x0E\x01\x0E\x01",323"\xE0\xFE\xE0\xFE\xF1\xFE\xF1\xFE",324"\xFE\xE0\xFE\xE0\xFE\xF1\xFE\xF1"325].include?(key)326end327328# Encrypt using MIT Kerberos des-cbc-md5329# http://web.mit.edu/kerberos/krb5-latest/doc/admin/enctypes.html?highlight=des#enctype-compatibility330#331# @param raw_secret [String] The data to encrypt332# @param key [String] The salt used by the encryption algorithm333# @return [String, nil] The encrypted data334def des_cbc_md5(raw_secret, salt)335odd = true336tmp_byte_str = [0, 0, 0, 0, 0, 0, 0, 0]337plaintext = raw_secret + salt338plaintext += "\x00".b * (8 - (plaintext.size % 8))339plaintext.bytes.each_slice(8) do |block|340tmp_56 = block.map { |byte| byte & 0b01111111 }341if !odd342# rubocop:disable Style/FormatString343tmp_56_str = tmp_56.map { |byte| '%07b' % byte }.join344# rubocop:enable Style/FormatString345tmp_56_str.reverse!346tmp_56 = tmp_56_str.bytes.each_slice(7).map do |bits7|347bits7.map(&:chr).join.to_i(2)348end349end350odd = !odd351tmp_byte_str = tmp_byte_str.zip(tmp_56).map { |a, b| a ^ b }352end353tempkey = add_parity(tmp_byte_str).map(&:chr).join354if weak_des_key?(tempkey)355tempkey[7] = (tempkey[7].ord ^ 0xF0).chr356end357cipher = OpenSSL::Cipher.new('DES-CBC')358cipher.encrypt359cipher.iv = tempkey360cipher.key = tempkey361chekcsumkey = cipher.update(plaintext)[-8..-1]362chekcsumkey = fix_parity(chekcsumkey.bytes).map(&:chr).join363if weak_des_key?(chekcsumkey)364chekcsumkey[7] = (chekcsumkey[7].ord ^ 0xF0).chr365end366chekcsumkey.unpack('H*')[0]367end368369# Encrypt using MIT Kerberos aesXXX-cts-hmac-sha1-96370# http://web.mit.edu/kerberos/krb5-latest/doc/admin/enctypes.html?highlight=des#enctype-compatibility371#372# @param algorithm [String] The AES algorithm to use (e.g. `128-CBC` or `256-CBC`)373# @param raw_secret [String] The data to encrypt374# @param key [String] The salt used by the encryption algorithm375# @return [String, nil] The encrypted data376def aes_cts_hmac_sha1_96(algorithm, raw_secret, salt)377iterations = 4096378cipher = OpenSSL::Cipher::AES.new(algorithm)379key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(raw_secret, salt, iterations, cipher.key_len)380plaintext = "kerberos\x7B\x9B\x5B\x2B\x93\x13\x2B\x93".b381rnd_seed = ''.b382loop do383cipher.reset384cipher.encrypt385cipher.iv = "\x00".b * 16386cipher.key = key387ciphertext = cipher.update(plaintext)388rnd_seed += ciphertext389break unless rnd_seed.size < cipher.key_len390391plaintext = ciphertext392end393rnd_seed394end395396# Encrypt using MIT Kerberos aes128-cts-hmac-sha1-96397# http://web.mit.edu/kerberos/krb5-latest/doc/admin/enctypes.html?highlight=des#enctype-compatibility398#399# @param raw_secret [String] The data to encrypt400# @param salt [String] The salt used by the encryption algorithm401# @return [String, nil] The encrypted data402def aes128_cts_hmac_sha1_96(raw_secret, salt)403aes_cts_hmac_sha1_96('128-CBC', raw_secret, salt)404end405406# Encrypt using MIT Kerberos aes256-cts-hmac-sha1-96407# http://web.mit.edu/kerberos/krb5-latest/doc/admin/enctypes.html?highlight=des#enctype-compatibility408#409# @param raw_secret [String] The data to encrypt410# @param salt [String] The salt used by the encryption algorithm411# @return [String, nil] The encrypted data412def aes256_cts_hmac_sha1_96(raw_secret, salt)413aes_cts_hmac_sha1_96('256-CBC', raw_secret, salt)414end415416# Encrypt using MIT Kerberos rc4_hmac417# http://web.mit.edu/kerberos/krb5-latest/doc/admin/enctypes.html?highlight=des#enctype-compatibility418#419# @param raw_secret [String] The data to encrypt420# @param salt [String] The salt used by the encryption algorithm421# @return [String, nil] The encrypted data422def rc4_hmac(raw_secret, salt = nil)423Rex::Proto::Kerberos::Crypto::Rc4Hmac.new.string_to_key(raw_secret, salt)424end425end426end427end428429430