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



323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/ruby_smb/dcerpc/samr.rb', line 323

def self.encrypt_password(password, key)
  # see: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-samr/5fe3c4c4-e71b-440d-b2fd-8448bfaf6e04
  password = RubySMB::Utils.safe_encode(password, '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