Class: Rex::Proto::SMB::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/proto/smb/utils.rb

Constant Summary collapse

CONST =
Rex::Proto::SMB::Constants

Class Method Summary collapse

Class Method Details

.create_mode_to_disposition(str) ⇒ Object

Returns a disposition value for smb.create based on permission string



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rex/proto/smb/utils.rb', line 44

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
  }

  return CONST::CREATE_ACCESS_OPENCREATE
end

.nbname_decode(str) ⇒ Object

Convert a name from its NetBIOS equivalent



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rex/proto/smb/utils.rb', line 87

def self.nbname_decode(str)
  decoded = ''
  str << 'A' if str.length % 2 != 0
  while (str.length > 0)
    two = str.slice!(0, 2).unpack('C*')
    if (two.length == 2)
      decoded << [ ((two[0] - 0x41) * 16) + two[1] - 0x41 ].pack('C')
    end
  end
  return decoded
end

.nbname_encode(str) ⇒ Object

Convert a name to its NetBIOS equivalent



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rex/proto/smb/utils.rb', line 73

def self.nbname_encode(str)
  encoded = ''
  for x in (0..15)
    if (x >= str.length)
      encoded << 'CA'
    else
      c = str[x, 1].upcase[0,1].unpack('C*')[0]
      encoded << [ (c / 16) + 0x41, (c % 16) + 0x41 ].pack('CC')
    end
  end
  return encoded
end

.open_mode_to_access(str) ⇒ Object

Creates an access mask for use with the CLIENT.open() call based on a string



12
13
14
15
16
17
18
19
20
21
# File 'lib/rex/proto/smb/utils.rb', line 12

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

.open_mode_to_mode(str) ⇒ Object

Creates a mode mask for use with the CLIENT.open() call based on a string



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rex/proto/smb/utils.rb', line 24

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

.time_smb_to_unix(thi, tlo) ⇒ Object

Convert a 64-bit signed SMB time to a unix timestamp



60
61
62
# File 'lib/rex/proto/smb/utils.rb', line 60

def self.time_smb_to_unix(thi, tlo)
  (((thi << 32) + tlo) / 10000000) - 11644473600
end

.time_unix_to_smb(unix_time) ⇒ Object

Convert a unix timestamp to a 64-bit signed server time



65
66
67
68
69
70
# File 'lib/rex/proto/smb/utils.rb', line 65

def self.time_unix_to_smb(unix_time)
  t64 = (unix_time + 11644473600) * 10000000
  thi = (t64 & 0xffffffff00000000) >> 32
  tlo = (t64 & 0x00000000ffffffff)
  return [thi, tlo]
end