2005-11-26 02:34:39 +00:00
|
|
|
require 'rex/text'
|
|
|
|
require 'rex/proto/smb/constants'
|
|
|
|
|
2005-09-16 09:27:41 +00:00
|
|
|
module Rex
|
|
|
|
module Proto
|
|
|
|
module SMB
|
|
|
|
class Utils
|
|
|
|
|
2005-10-02 06:53:39 +00:00
|
|
|
CONST = Rex::Proto::SMB::Constants
|
2005-09-16 09:27:41 +00:00
|
|
|
|
2005-10-02 06:53:39 +00:00
|
|
|
# Creates an access mask for use with the CLIENT.open() call based on a string
|
|
|
|
def self.open_mode_to_access(str)
|
|
|
|
access = CONST::OPEN_ACCESS_READ | CONST::OPEN_SHARE_DENY_NONE
|
|
|
|
str.each_byte { |c|
|
|
|
|
case [c].pack('C').downcase
|
|
|
|
when 'w'
|
|
|
|
access |= CONST::OPEN_ACCESS_READWRITE
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return access
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a mode mask for use with the CLIENT.open() call based on a string
|
|
|
|
def self.open_mode_to_mode(str)
|
|
|
|
mode = 0
|
|
|
|
|
|
|
|
str.each_byte { |c|
|
|
|
|
case [c].pack('C').downcase
|
|
|
|
when 'x' # Fail if the file already exists
|
|
|
|
mode |= CONST::OPEN_MODE_EXCL
|
|
|
|
when 't' # Truncate the file if it already exists
|
|
|
|
mode |= CONST::OPEN_MODE_TRUNC
|
|
|
|
when 'c' # Create the file if it does not exist
|
|
|
|
mode |= CONST::OPEN_MODE_CREAT
|
|
|
|
when 'o' # Just open the file, clashes with x
|
|
|
|
mode |= CONST::OPEN_MODE_OPEN
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
return mode
|
|
|
|
end
|
2005-10-03 13:51:05 +00:00
|
|
|
|
|
|
|
# Returns a disposition value for smb.create based on permission string
|
|
|
|
def self.create_mode_to_disposition(str)
|
|
|
|
str.each_byte { |c|
|
|
|
|
case [c].pack('C').downcase
|
|
|
|
when 'c' # Create the file if it does not exist
|
|
|
|
return CONST::CREATE_ACCESS_OPENCREATE
|
|
|
|
when 'o' # Just open the file and fail if it does not exist
|
|
|
|
return CONST::CREATE_ACCESS_EXIST
|
|
|
|
end
|
|
|
|
}
|
2005-10-02 06:53:39 +00:00
|
|
|
|
2005-10-03 13:51:05 +00:00
|
|
|
return CONST::CREATE_ACCESS_OPENCREATE
|
|
|
|
end
|
2005-10-02 06:53:39 +00:00
|
|
|
|
2010-09-18 01:12:44 +00:00
|
|
|
# NOTE: the difference below came from: Time.utc("1970-1-1") - Time.utc("1601-1-1")
|
|
|
|
|
2005-10-02 06:53:39 +00:00
|
|
|
# Convert a 64-bit signed SMB time to a unix timestamp
|
2006-05-03 04:43:19 +00:00
|
|
|
def self.time_smb_to_unix(thi, tlo)
|
|
|
|
(((thi << 32) + tlo) / 10000000) - 11644473600
|
2005-10-02 06:53:39 +00:00
|
|
|
end
|
2006-05-03 04:43:19 +00:00
|
|
|
|
|
|
|
# Convert a unix timestamp to a 64-bit signed server time
|
|
|
|
def self.time_unix_to_smb(unix_time)
|
|
|
|
t64 = (unix_time + 11644473600) * 10000000
|
|
|
|
thi = (t64 & 0xffffffff00000000) >> 32
|
|
|
|
tlo = (t64 & 0x00000000ffffffff)
|
|
|
|
return [thi, tlo]
|
|
|
|
end
|
|
|
|
|
2005-09-16 09:27:41 +00:00
|
|
|
# Convert a name to its NetBIOS equivalent
|
2006-05-03 04:43:19 +00:00
|
|
|
def self.nbname_encode(str)
|
2005-09-16 09:27:41 +00:00
|
|
|
encoded = ''
|
|
|
|
for x in (0..15)
|
|
|
|
if (x >= str.length)
|
|
|
|
encoded << 'CA'
|
|
|
|
else
|
2009-06-20 02:42:00 +00:00
|
|
|
c = str[x, 1].upcase[0,1].unpack('C*')[0]
|
2005-09-16 09:27:41 +00:00
|
|
|
encoded << [ (c / 16) + 0x41, (c % 16) + 0x41 ].pack('CC')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return encoded
|
|
|
|
end
|
|
|
|
|
|
|
|
# Convert a name from its NetBIOS equivalent
|
2006-05-03 04:43:19 +00:00
|
|
|
def self.nbname_decode(str)
|
2005-09-16 09:27:41 +00:00
|
|
|
decoded = ''
|
|
|
|
str << 'A' if str.length % 2 != 0
|
|
|
|
while (str.length > 0)
|
2009-06-20 02:42:00 +00:00
|
|
|
two = str.slice!(0, 2).unpack('C*')
|
2005-09-16 09:27:41 +00:00
|
|
|
if (two.length == 2)
|
|
|
|
decoded << [ ((two[0] - 0x41) * 16) + two[1] - 0x41 ].pack('C')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return decoded
|
|
|
|
end
|
|
|
|
|
2007-07-16 01:13:25 +00:00
|
|
|
|
2005-09-16 09:27:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-20 02:42:00 +00:00
|
|
|
end
|