2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2011-03-07 19:57:53 +00:00
|
|
|
#
|
|
|
|
# An NTLM Authentication Library for Ruby
|
|
|
|
#
|
|
|
|
# This code is a derivative of "dbf2.rb" written by yrock
|
|
|
|
# and Minero Aoki. You can find original code here:
|
|
|
|
# http://jp.rubyist.net/magazine/?0013-CodeReview
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
# Copyright (c) 2005,2006 yrock
|
2012-05-24 23:10:26 +00:00
|
|
|
#
|
2011-03-07 19:57:53 +00:00
|
|
|
# This program is free software.
|
|
|
|
# You can distribute/modify this program under the terms of the
|
|
|
|
# Ruby License.
|
|
|
|
#
|
2011-03-09 05:04:42 +00:00
|
|
|
# 2011-03-08 improved through a code merge with Metasploit's SMB::Crypt
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
#
|
2011-03-07 19:57:53 +00:00
|
|
|
# 2011-02-23 refactored and improved by Alexandre Maloteaux for Metasploit Project
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# 2006-02-11 refactored by Minero Aoki
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# All protocol information used to write this code stems from
|
2012-05-24 23:10:26 +00:00
|
|
|
# "The NTLM Authentication Protocol" by Eric Glass. The author
|
|
|
|
# would thank to him for this tremendous work and making it
|
2011-03-07 19:57:53 +00:00
|
|
|
# available on the net.
|
|
|
|
# http://davenport.sourceforge.net/ntlm.html
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
# Copyright (c) 2003 Eric Glass
|
|
|
|
#
|
|
|
|
# Permission to use, copy, modify, and distribute this document
|
|
|
|
# for any purpose and without any fee is hereby granted,
|
|
|
|
# provided that the above copyright notice and this list of
|
2012-05-24 23:10:26 +00:00
|
|
|
# conditions appear in all copies.
|
2011-03-07 19:57:53 +00:00
|
|
|
# -------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# The author also looked Mozilla-Firefox-1.0.7 source code,
|
|
|
|
# namely, security/manager/ssl/src/nsNTLMAuthModule.cpp and
|
|
|
|
# Jonathan Bastien-Filiatrault's libntlm-ruby.
|
|
|
|
# "http://x2a.org/websvn/filedetails.php?
|
|
|
|
# repname=libntlm-ruby&path=%2Ftrunk%2Fntlm.rb&sc=1"
|
|
|
|
# The latter has a minor bug in its separate_keys function.
|
2012-05-24 23:10:26 +00:00
|
|
|
# The third key has to begin from the 14th character of the
|
2011-03-07 19:57:53 +00:00
|
|
|
# input string instead of 13th:)
|
|
|
|
#--
|
|
|
|
# $Id: ntlm.rb 11678 2011-01-30 19:26:35Z hdm $
|
|
|
|
#++
|
|
|
|
|
|
|
|
|
|
|
|
require 'rex/proto/ntlm/constants'
|
|
|
|
require 'rex/proto/ntlm/base'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Proto
|
|
|
|
module NTLM
|
|
|
|
class Crypt
|
|
|
|
|
|
|
|
CONST = Rex::Proto::NTLM::Constants
|
|
|
|
BASE = Rex::Proto::NTLM::Base
|
|
|
|
|
|
|
|
@@loaded_openssl = false
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-07 19:57:53 +00:00
|
|
|
begin
|
|
|
|
require 'openssl'
|
|
|
|
require 'openssl/digest'
|
|
|
|
@@loaded_openssl = true
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.gen_keys(str)
|
2011-03-09 05:04:42 +00:00
|
|
|
str.scan(/.{7}/).map{ |key| des_56_to_64(key) }
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
def self.des_56_to_64(ckey56s)
|
|
|
|
ckey64 = []
|
|
|
|
ckey56 = ckey56s.unpack('C*')
|
|
|
|
ckey64[0] = ckey56[0]
|
|
|
|
ckey64[1] = ((ckey56[0] << 7) & 0xFF) | (ckey56[1] >> 1)
|
|
|
|
ckey64[2] = ((ckey56[1] << 6) & 0xFF) | (ckey56[2] >> 2)
|
|
|
|
ckey64[3] = ((ckey56[2] << 5) & 0xFF) | (ckey56[3] >> 3)
|
|
|
|
ckey64[4] = ((ckey56[3] << 4) & 0xFF) | (ckey56[4] >> 4)
|
|
|
|
ckey64[5] = ((ckey56[4] << 3) & 0xFF) | (ckey56[5] >> 5)
|
|
|
|
ckey64[6] = ((ckey56[5] << 2) & 0xFF) | (ckey56[6] >> 6)
|
|
|
|
ckey64[7] = (ckey56[6] << 1) & 0xFF
|
|
|
|
ckey64.pack('C*')
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-07 19:57:53 +00:00
|
|
|
def self.apply_des(plain, keys)
|
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
|
|
|
dec = OpenSSL::Cipher::DES.new
|
2011-03-09 21:13:04 +00:00
|
|
|
keys.map do |k|
|
2011-03-07 19:57:53 +00:00
|
|
|
dec.key = k
|
|
|
|
dec.encrypt.update(plain)
|
2011-03-09 21:13:04 +00:00
|
|
|
end
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-07 19:57:53 +00:00
|
|
|
def self.lm_hash(password, half = false)
|
2011-03-09 05:04:42 +00:00
|
|
|
size = half ? 7 : 14
|
|
|
|
keys = gen_keys(password.upcase.ljust(size, "\0"))
|
2011-03-07 19:57:53 +00:00
|
|
|
apply_des(CONST::LM_MAGIC, keys).join
|
2012-05-24 23:10:26 +00:00
|
|
|
end
|
|
|
|
|
2011-03-07 19:57:53 +00:00
|
|
|
def self.ntlm_hash(password, opt = {})
|
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
|
|
|
pwd = password.dup
|
|
|
|
unless opt[:unicode]
|
|
|
|
pwd = Rex::Text.to_unicode(pwd)
|
|
|
|
end
|
2011-03-09 05:04:42 +00:00
|
|
|
OpenSSL::Digest::MD4.digest(pwd)
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
# This hash is used for lmv2/ntlmv2 response calculation
|
2011-03-07 19:57:53 +00:00
|
|
|
def self.ntlmv2_hash(user, password, domain, opt={})
|
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-09 02:41:50 +00:00
|
|
|
if opt[:pass_is_hash]
|
|
|
|
ntlmhash = password
|
|
|
|
else
|
|
|
|
ntlmhash = ntlm_hash(password, opt)
|
2012-05-24 23:10:26 +00:00
|
|
|
end
|
2011-03-09 05:04:42 +00:00
|
|
|
# With Win 7 and maybe other OSs we sometimes get the domain not uppercased
|
2011-03-07 19:57:53 +00:00
|
|
|
userdomain = user.upcase + domain
|
|
|
|
unless opt[:unicode]
|
|
|
|
userdomain = Rex::Text.to_unicode(userdomain)
|
|
|
|
end
|
|
|
|
OpenSSL::HMAC.digest(OpenSSL::Digest::MD5.new, ntlmhash, userdomain)
|
|
|
|
end
|
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
# Create the LANMAN response
|
2011-03-07 19:57:53 +00:00
|
|
|
def self.lm_response(arg, half = false)
|
|
|
|
begin
|
|
|
|
hash = arg[:lm_hash]
|
|
|
|
chal = arg[:challenge]
|
|
|
|
rescue
|
|
|
|
raise ArgumentError
|
|
|
|
end
|
|
|
|
chal = BASE::pack_int64le(chal) if chal.is_a?(Integer)
|
|
|
|
if half then size = 7 else size = 21 end
|
|
|
|
keys = gen_keys hash.ljust(size, "\0")
|
|
|
|
apply_des(chal, keys).join
|
|
|
|
end
|
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
# Synonym of lm_response for old compatibility with lib/rex/proto/smb/crypt
|
2012-05-24 23:10:26 +00:00
|
|
|
def self.lanman_des(password, challenge)
|
2011-03-09 05:04:42 +00:00
|
|
|
lm_response({
|
|
|
|
:lm_hash => self.lm_hash(password),
|
|
|
|
:challenge => challenge
|
|
|
|
})
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-07 19:57:53 +00:00
|
|
|
def self.ntlm_response(arg)
|
|
|
|
hash = arg[:ntlm_hash]
|
|
|
|
chal = arg[:challenge]
|
|
|
|
chal = BASE::pack_int64le(chal) if chal.is_a?(::Integer)
|
2011-03-09 05:04:42 +00:00
|
|
|
keys = gen_keys(hash.ljust(21, "\0"))
|
2011-03-07 19:57:53 +00:00
|
|
|
apply_des(chal, keys).join
|
|
|
|
end
|
|
|
|
|
|
|
|
#synonym of ntlm_response for old compatibility with lib/rex/proto/smb/crypt
|
|
|
|
def self.ntlm_md4(password, challenge)
|
2011-03-09 05:04:42 +00:00
|
|
|
ntlm_response({
|
2012-05-24 23:10:26 +00:00
|
|
|
:ntlm_hash => self.ntlm_hash(password),
|
2011-03-09 05:04:42 +00:00
|
|
|
:challenge => challenge
|
|
|
|
})
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.ntlmv2_response(arg, opt = {})
|
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
key, chal = arg[:ntlmv2_hash], arg[:challenge]
|
|
|
|
if not (key and chal)
|
2011-03-07 19:57:53 +00:00
|
|
|
raise ArgumentError , 'ntlmv2_hash and challenge are mandatory'
|
|
|
|
end
|
2011-03-11 20:24:49 +00:00
|
|
|
|
2011-03-07 19:57:53 +00:00
|
|
|
chal = BASE::pack_int64le(chal) if chal.is_a?(::Integer)
|
2011-03-09 05:04:42 +00:00
|
|
|
bb = nil
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-07 19:57:53 +00:00
|
|
|
if opt[:nt_client_challenge]
|
2011-03-11 20:24:49 +00:00
|
|
|
if opt[:nt_client_challenge].to_s.length <= 8
|
2012-05-24 23:10:26 +00:00
|
|
|
raise ArgumentError,"nt_client_challenge is not in a correct format "
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
|
|
|
bb = opt[:nt_client_challenge]
|
|
|
|
else
|
2011-03-09 05:04:42 +00:00
|
|
|
if not arg[:target_info]
|
2011-03-07 19:57:53 +00:00
|
|
|
raise ArgumentError, "target_info is mandatory in this case"
|
|
|
|
end
|
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
ti = arg[:target_info]
|
|
|
|
cc = opt[:client_challenge] || rand(CONST::MAX64)
|
|
|
|
cc = BASE::pack_int64le(cc) if cc.is_a?(::Integer)
|
|
|
|
|
|
|
|
ts = opt[:timestamp] || Time.now.to_i
|
|
|
|
|
|
|
|
# Convert the unix timestamp to windows format
|
|
|
|
# epoch -> milsec from Jan 1, 1601
|
2011-03-07 19:57:53 +00:00
|
|
|
ts = 10000000 * (ts + CONST::TIME_OFFSET)
|
|
|
|
|
|
|
|
blob = BASE::Blob.new
|
|
|
|
blob.timestamp = ts
|
|
|
|
blob.challenge = cc
|
|
|
|
blob.target_info = ti
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-07 19:57:53 +00:00
|
|
|
bb = blob.serialize
|
|
|
|
end
|
|
|
|
|
|
|
|
OpenSSL::HMAC.digest(OpenSSL::Digest::MD5.new, key, chal + bb) + bb
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-07 19:57:53 +00:00
|
|
|
def self.lmv2_response(arg, opt = {})
|
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
|
|
|
key = arg[:ntlmv2_hash]
|
|
|
|
chal = arg[:challenge]
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-07 19:57:53 +00:00
|
|
|
chal = BASE::pack_int64le(chal) if chal.is_a?(::Integer)
|
2011-03-09 05:04:42 +00:00
|
|
|
cc = opt[:client_challenge] || rand(CONST::MAX64)
|
|
|
|
cc = BASE::pack_int64le(cc) if cc.is_a?(::Integer)
|
2011-03-07 19:57:53 +00:00
|
|
|
|
|
|
|
OpenSSL::HMAC.digest(OpenSSL::Digest::MD5.new, key, chal + cc) + cc
|
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-07 19:57:53 +00:00
|
|
|
def self.ntlm2_session(arg, opt = {})
|
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
2011-03-09 05:04:42 +00:00
|
|
|
passwd_hash,chal = arg[:ntlm_hash],arg[:challenge]
|
2011-03-09 16:57:33 +00:00
|
|
|
if not (passwd_hash and chal)
|
2011-03-09 05:04:42 +00:00
|
|
|
raise RuntimeError, "ntlm_hash and challenge are required"
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
cc = opt[:client_challenge] || rand(CONST::MAX64)
|
2011-03-07 19:57:53 +00:00
|
|
|
cc = BASE::pack_int64le(cc) if cc.is_a?(Integer)
|
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
keys = gen_keys(passwd_hash.ljust(21, "\0"))
|
|
|
|
session_hash = OpenSSL::Digest::MD5.digest(chal + cc)[0,8]
|
2011-03-07 19:57:53 +00:00
|
|
|
response = apply_des(session_hash, keys).join
|
|
|
|
[cc.ljust(24, "\0"), response]
|
|
|
|
end
|
|
|
|
|
2012-05-24 23:10:26 +00:00
|
|
|
#this function will check if the net lm response provided correspond to en empty password
|
2011-03-11 20:24:49 +00:00
|
|
|
def self.is_hash_from_empty_pwd?(arg)
|
|
|
|
hash_type = arg[:type]
|
2012-05-24 23:10:26 +00:00
|
|
|
raise ArgumentError,"arg[:type] is mandatory" if not hash_type
|
2011-03-11 20:24:49 +00:00
|
|
|
raise ArgumentError,"arg[:type] must be lm or ntlm" if not hash_type =~ /^((lm)|(ntlm))$/
|
|
|
|
|
|
|
|
ntlm_ver = arg[:ntlm_ver]
|
|
|
|
raise ArgumentError,"arg[:ntlm_ver] is mandatory" if not ntlm_ver
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-11 20:24:49 +00:00
|
|
|
hash = arg[:hash]
|
|
|
|
raise ArgumentError,"arg[:hash] is mandatory" if not hash
|
|
|
|
|
2012-05-24 23:10:26 +00:00
|
|
|
srv_chall = arg[:srv_challenge]
|
|
|
|
raise ArgumentError,"arg[:srv_challenge] is mandatory" if not srv_chall
|
2011-03-11 20:24:49 +00:00
|
|
|
raise ArgumentError,"Server challenge length must be exactly 8 bytes" if srv_chall.length != 8
|
|
|
|
|
|
|
|
#calculate responses for empty pwd
|
|
|
|
case ntlm_ver
|
2012-05-24 23:10:26 +00:00
|
|
|
when CONST::NTLM_V1_RESPONSE
|
2011-03-11 20:24:49 +00:00
|
|
|
if hash.length != 24
|
|
|
|
raise ArgumentError,"hash length must be exactly 24 bytes "
|
|
|
|
end
|
|
|
|
case hash_type
|
2012-05-24 23:10:26 +00:00
|
|
|
when 'lm'
|
2011-03-11 20:24:49 +00:00
|
|
|
arglm = { :lm_hash => self.lm_hash(''),
|
|
|
|
:challenge => srv_chall}
|
|
|
|
calculatedhash = self.lm_response(arglm)
|
|
|
|
when 'ntlm'
|
2012-05-24 23:10:26 +00:00
|
|
|
argntlm = { :ntlm_hash => self.ntlm_hash(''),
|
2011-03-11 20:24:49 +00:00
|
|
|
:challenge => srv_chall }
|
|
|
|
calculatedhash = self.ntlm_response(argntlm)
|
|
|
|
end
|
|
|
|
when CONST::NTLM_V2_RESPONSE
|
|
|
|
raise ArgumentError,"hash length must be exactly 16 bytes " if hash.length != 16
|
2012-05-24 23:10:26 +00:00
|
|
|
cli_chall = arg[:cli_challenge]
|
2011-03-11 20:24:49 +00:00
|
|
|
raise ArgumentError,"arg[:cli_challenge] is mandatory in this case" if not cli_chall
|
2012-05-24 23:10:26 +00:00
|
|
|
user = arg[:user]
|
2011-03-11 20:24:49 +00:00
|
|
|
raise ArgumentError,"arg[:user] is mandatory in this case" if not user
|
|
|
|
domain = arg[:domain]
|
|
|
|
raise ArgumentError,"arg[:domain] is mandatory in this case" if not domain
|
|
|
|
|
|
|
|
case hash_type
|
|
|
|
when 'lm'
|
|
|
|
raise ArgumentError,"Client challenge length must be exactly 8 bytes " if cli_chall.length != 8
|
|
|
|
arglm = { :ntlmv2_hash => self.ntlmv2_hash(user,'', domain),
|
|
|
|
:challenge => srv_chall }
|
|
|
|
optlm = { :client_challenge => cli_chall}
|
|
|
|
calculatedhash = self.lmv2_response(arglm, optlm)[0,16]
|
|
|
|
when 'ntlm'
|
|
|
|
raise ArgumentError,"Client challenge length must be bigger then 8 bytes " if cli_chall.length <= 8
|
|
|
|
argntlm = { :ntlmv2_hash => self.ntlmv2_hash(user, '', domain),
|
|
|
|
:challenge => srv_chall }
|
|
|
|
optntlm = { :nt_client_challenge => cli_chall}
|
|
|
|
calculatedhash = self.ntlmv2_response(argntlm,optntlm)[0,16]
|
|
|
|
end
|
|
|
|
when CONST::NTLM_2_SESSION_RESPONSE
|
|
|
|
raise ArgumentError,"hash length must be exactly 16 bytes " if hash.length != 24
|
2012-05-24 23:10:26 +00:00
|
|
|
cli_chall = arg[:cli_challenge]
|
2011-03-11 20:24:49 +00:00
|
|
|
raise ArgumentError,"arg[:cli_challenge] is mandatory in this case" if not cli_chall
|
|
|
|
raise ArgumentError,"Client challenge length must be exactly 8 bytes " if cli_chall.length != 8
|
|
|
|
case hash_type
|
|
|
|
when 'lm'
|
|
|
|
raise ArgumentError, "ntlm2_session is incompatible with lm"
|
|
|
|
when 'ntlm'
|
2012-05-24 23:10:26 +00:00
|
|
|
argntlm = { :ntlm_hash => self.ntlm_hash(''),
|
2011-03-11 20:24:49 +00:00
|
|
|
:challenge => srv_chall }
|
|
|
|
optntlm = { :client_challenge => cli_chall}
|
|
|
|
end
|
|
|
|
calculatedhash = self.ntlm2_session(argntlm,optntlm).join[24,24]
|
|
|
|
else
|
|
|
|
raise ArgumentError,"ntlm_ver is of unknow type"
|
|
|
|
end
|
|
|
|
hash == calculatedhash
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
#
|
|
|
|
# Signing method added for metasploit project
|
|
|
|
#
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
# Used when only the LMv1 response is provided (i.e., with Win9x clients)
|
2011-03-09 21:13:04 +00:00
|
|
|
def self.lmv1_user_session_key(pass, opt = {})
|
|
|
|
if opt[:pass_is_hash]
|
|
|
|
usk = pass[0,8]
|
|
|
|
else
|
|
|
|
usk = self.lm_hash(pass.upcase[0,7],true)
|
|
|
|
end
|
|
|
|
usk.ljust(16,"\x00")
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
# This variant is used when the client sends the NTLMv1 response
|
2011-03-09 21:13:04 +00:00
|
|
|
def self.ntlmv1_user_session_key(pass, opt = {})
|
2011-03-07 19:57:53 +00:00
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
2011-03-09 21:13:04 +00:00
|
|
|
|
|
|
|
if opt[:pass_is_hash]
|
|
|
|
usk = pass
|
|
|
|
else
|
|
|
|
usk = self.ntlm_hash(pass)
|
|
|
|
end
|
|
|
|
OpenSSL::Digest::MD4.digest(usk)
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
# Used when NTLMv1 authentication is employed with NTLM2 session security
|
2011-03-09 21:13:04 +00:00
|
|
|
def self.ntlm2_session_user_session_key(pass, srv_chall, cli_chall, opt = {})
|
2011-03-07 19:57:53 +00:00
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
2011-03-09 21:13:04 +00:00
|
|
|
|
|
|
|
ntlm_key = self.ntlmv1_user_session_key(pass, opt )
|
2011-03-07 19:57:53 +00:00
|
|
|
session_chal = srv_chall + cli_chall
|
|
|
|
OpenSSL::HMAC.digest(OpenSSL::Digest::MD5.new, ntlm_key, session_chal)
|
|
|
|
end
|
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
# Used when the LMv2 response is sent
|
2011-03-09 21:13:04 +00:00
|
|
|
def self.lmv2_user_session_key(user, pass, domain, srv_chall, cli_chall, opt = {})
|
2011-03-07 19:57:53 +00:00
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
2011-03-09 21:13:04 +00:00
|
|
|
|
|
|
|
ntlmv2_key = self.ntlmv2_hash(user, pass, domain, opt)
|
2011-03-07 19:57:53 +00:00
|
|
|
hash1 = OpenSSL::HMAC.digest(OpenSSL::Digest::MD5.new, ntlmv2_key, srv_chall + cli_chall)
|
|
|
|
OpenSSL::HMAC.digest(OpenSSL::Digest::MD5.new, ntlmv2_key, hash1)
|
|
|
|
end
|
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
# Used when the NTLMv2 response is sent
|
|
|
|
class << self; alias_method :ntlmv2_user_session_key, :lmv2_user_session_key; end
|
2011-03-07 19:57:53 +00:00
|
|
|
|
2011-03-09 05:04:42 +00:00
|
|
|
# Used when LanMan Key flag is set
|
2011-03-09 21:13:04 +00:00
|
|
|
def self.lanman_session_key(pass, srvchall, opt = {})
|
|
|
|
if opt[:pass_is_hash]
|
|
|
|
halfhash = pass[0,8]
|
|
|
|
else
|
|
|
|
halfhash = lm_hash(pass.upcase[0,7],true)
|
|
|
|
end
|
2011-03-09 05:04:42 +00:00
|
|
|
plain = self.lm_response({
|
|
|
|
:lm_hash => halfhash[0,7],
|
|
|
|
:challenge => srvchall
|
|
|
|
}, true )
|
2011-03-07 19:57:53 +00:00
|
|
|
key = halfhash + ["bdbdbdbdbdbd"].pack("H*")
|
2011-03-09 05:04:42 +00:00
|
|
|
keys = self.gen_keys(key)
|
|
|
|
apply_des(plain, keys).join
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.encrypt_sessionkey(session_key, user_session_key)
|
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
|
|
|
cipher = OpenSSL::Cipher::Cipher.new('rc4')
|
|
|
|
cipher.encrypt
|
|
|
|
cipher.key = user_session_key
|
2012-05-24 23:10:26 +00:00
|
|
|
cipher.update(session_key)
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.decrypt_sessionkey(encrypted_session_key, user_session_key)
|
|
|
|
raise RuntimeError, "No OpenSSL support" if not @@loaded_openssl
|
|
|
|
cipher = OpenSSL::Cipher::Cipher.new('rc4')
|
|
|
|
cipher.decrypt
|
|
|
|
cipher.key = user_session_key
|
2012-05-24 23:10:26 +00:00
|
|
|
cipher.update(encrypted_session_key)
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.make_weak_sessionkey(session_key,key_size,lanman_key = false)
|
|
|
|
case key_size
|
|
|
|
when 40
|
|
|
|
if lanman_key
|
2012-05-24 23:10:26 +00:00
|
|
|
return session_key[0,5] + "\xe5\x38\xb0"
|
2011-03-07 19:57:53 +00:00
|
|
|
else
|
2012-05-24 23:10:26 +00:00
|
|
|
return session_key[0,5]
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
|
|
|
when 56
|
|
|
|
if lanman_key
|
|
|
|
return session_key[0,7] + "\xa0"
|
|
|
|
else
|
2012-05-24 23:10:26 +00:00
|
|
|
return session_key[0,7]
|
2011-03-07 19:57:53 +00:00
|
|
|
end
|
2012-05-24 23:10:26 +00:00
|
|
|
else #128
|
2011-03-07 19:57:53 +00:00
|
|
|
return session_key[0,16]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|