lib/net/ntlm.rb in rubyntlm-0.6.3 vs lib/net/ntlm.rb in rubyntlm-0.6.4
- old
+ new
@@ -57,10 +57,12 @@
require 'net/ntlm/message/type1'
require 'net/ntlm/message/type2'
require 'net/ntlm/message/type3'
require 'net/ntlm/encode_util'
+require 'net/ntlm/md4'
+require 'net/ntlm/rc4'
require 'net/ntlm/client'
require 'net/ntlm/channel_binding'
require 'net/ntlm/target_info'
@@ -92,14 +94,14 @@
else
false
end
end
- # Conver the value to a 64-Bit Little Endian Int
+ # Convert the value to a 64-bit little-endian integer
# @param [String] val The string to convert
def pack_int64le(val)
- [val & 0x00000000ffffffff, val >> 32].pack("V2")
+ [val & 0x00000000ffffffff, val >> 32].pack("V2")
end
# Builds an array of strings that are 7 characters long
# @param [String] str The string to split
# @api private
@@ -109,11 +111,12 @@
(ret ||= []).push s.slice!(0, 7)
end
ret
end
- # Not sure what this is doing
+ # Each byte of a DES key contains seven bits of key material and one odd-parity bit.
+ # The parity bit should be set so that there are an odd number of 1 bits in each byte.
# @param [String] str String to generate keys for
# @api private
def gen_keys(str)
split7(str).map{ |str7|
bits = split7(str7.unpack("B*")[0]).inject('')\
@@ -121,40 +124,42 @@
[bits].pack("B*")
}
end
def apply_des(plain, keys)
- dec = OpenSSL::Cipher.new("des-cbc").encrypt
- dec.padding = 0
keys.map {|k|
- dec.key = k
+ # Spec requires des-cbc, but openssl 3 does not support single des
+ # by default, so just do triple DES (EDE) with the same key
+ dec = OpenSSL::Cipher.new("des-ede-cbc").encrypt
+ dec.padding = 0
+ dec.key = k + k
dec.update(plain) + dec.final
}
end
- # Generates a Lan Manager Hash
+ # Generates a {https://en.wikipedia.org/wiki/LAN_Manager LAN Manager Hash}
# @param [String] password The password to base the hash on
def lm_hash(password)
keys = gen_keys password.upcase.ljust(14, "\0")
apply_des(LM_MAGIC, keys).join
end
- # Generate a NTLM Hash
+ # Generate an NTLM Hash
# @param [String] password The password to base the hash on
# @option opt :unicode (false) Unicode encode the password
def ntlm_hash(password, opt = {})
pwd = password.dup
unless opt[:unicode]
pwd = EncodeUtil.encode_utf16le(pwd)
end
- OpenSSL::Digest::MD4.digest pwd
+ Net::NTLM::Md4.digest pwd
end
# Generate a NTLMv2 Hash
# @param [String] user The username
# @param [String] password The password
# @param [String] target The domain or workstation to authenticate to
- # @option opt :unicode (false) Unicode encode the domain
+ # @option [Boolean] opt :unicode (false) Unicode encode the domain.
def ntlmv2_hash(user, password, target, opt={})
if is_ntlm_hash? password
decoded_password = EncodeUtil.decode_utf16le(password)
ntlmhash = [decoded_password.upcase[33,65]].pack('H32')
else