lib/net/ntlm.rb in rubyntlm-0.4.0 vs lib/net/ntlm.rb in rubyntlm-0.5.0
- old
+ new
@@ -55,16 +55,14 @@
require 'net/ntlm/message/type0'
require 'net/ntlm/message/type1'
require 'net/ntlm/message/type2'
require 'net/ntlm/message/type3'
-
require 'net/ntlm/encode_util'
+require 'net/ntlm/client'
-
-
module Net
module NTLM
LM_MAGIC = "KGS!@\#$%"
TIME_OFFSET = 11644473600
@@ -100,14 +98,15 @@
[bits].pack("B*")
}
end
def apply_des(plain, keys)
- dec = OpenSSL::Cipher::DES.new
+ dec = OpenSSL::Cipher::Cipher.new("des-cbc")
+ dec.padding = 0
keys.map {|k|
dec.key = k
- dec.encrypt.update(plain)
+ dec.encrypt.update(plain) + dec.final
}
end
# Generates a Lan Manager Hash
# @param [String] password The password to base the hash on
@@ -132,11 +131,11 @@
# @param [String] password The password
# @param [String] target The domain or workstaiton to authenticate to
# @option opt :unicode (false) Unicode encode the domain
def ntlmv2_hash(user, password, target, opt={})
ntlmhash = ntlm_hash(password, opt)
- userdomain = (user + target).upcase
+ userdomain = user.upcase + target
unless opt[:unicode]
userdomain = EncodeUtil.encode_utf16le(userdomain)
end
OpenSSL::HMAC.digest(OpenSSL::Digest::MD5.new, ntlmhash, userdomain)
end
@@ -182,18 +181,19 @@
ts = opt[:timestamp]
else
ts = Time.now.to_i
end
# epoch -> milsec from Jan 1, 1601
- ts = 10000000 * (ts + TIME_OFFSET)
+ ts = 10_000_000 * (ts + TIME_OFFSET)
blob = Blob.new
blob.timestamp = ts
blob.challenge = cc
blob.target_info = ti
bb = blob.serialize
+
OpenSSL::HMAC.digest(OpenSSL::Digest::MD5.new, key, chal + bb) + bb
end
def lmv2_response(arg, opt = {})
key = arg[:ntlmv2_hash]
@@ -216,18 +216,19 @@
passwd_hash = arg[:ntlm_hash]
chal = arg[:challenge]
rescue
raise ArgumentError
end
+ chal = NTLM::pack_int64le(chal) if chal.is_a?(Integer)
if opt[:client_challenge]
- cc = opt[:client_challenge]
+ cc = opt[:client_challenge]
else
cc = rand(MAX64)
end
cc = NTLM::pack_int64le(cc) if cc.is_a?(Integer)
- keys = gen_keys passwd_hash.ljust(21, "\0")
+ keys = gen_keys(passwd_hash.ljust(21, "\0"))
session_hash = OpenSSL::Digest::MD5.digest(chal + cc).slice(0, 8)
response = apply_des(session_hash, keys).join
[cc.ljust(24, "\0"), response]
end
end