Class: RubySMB::Dcerpc::Samr::SamprEncryptedUserPasswordNew

Inherits:
BinData::Record
  • Object
show all
Defined in:
lib/ruby_smb/dcerpc/samr.rb

Overview

Class Method Summary collapse

Class Method Details

.encrypt_password(password, key) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/ruby_smb/dcerpc/samr.rb', line 340

def self.encrypt_password(password, key)
  # see: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-samr/5fe3c4c4-e71b-440d-b2fd-8448bfaf6e04
  password = password.encode('UTF-16LE').force_encoding('ASCII-8BIT')
  buffer = password.rjust(512, "\x00") + [ password.length ].pack('V')
  salt = SecureRandom.random_bytes(16)
  key = OpenSSL::Digest::MD5.new(salt + key).digest
  cipher = OpenSSL::Cipher.new('RC4').tap do |cipher|
    cipher.encrypt
    cipher.key = key
  end
  cipher.update(buffer) + salt
end