Module: Msf::Post::Windows::NetAPI

Defined in:
lib/msf/core/post/windows/net_api.rb

Constant Summary collapse

MAX_PREFERRED_LENGTH =
-1
SV_TYPE_ALL =
0xFFFFFFFF
SV_TYPE_DOMAIN_ENUM =
0x80000000
SV_TYPE_DOMAIN_BAKCTRL =
0x00000010
SV_TYPE_DOMAIN_CTRL =
0x00000008
ERROR_ACCESS_DENIED =
5
ERROR_NOT_ENOUGH_MEMORY =
8
ERROR_INVALID_PARAMETER =
87
ERROR_INVALID_LEVEL =
124
ERROR_MORE_DATA =
234
ERROR_NO_BROWSER_SERVERS_FOUND =
6118
NERR_ClientNameNotFound =
2312
NERR_InvalidComputer =
2351
NERR_UserNotFound =
2221

Instance Method Summary collapse

Instance Method Details

#net_server_enum(server_type = SV_TYPE_ALL, domain = nil) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/msf/core/post/windows/net_api.rb', line 36

def net_server_enum(server_type=SV_TYPE_ALL, domain=nil)
  hosts = []

  result = client.railgun.netapi32.NetServerEnum(
      nil,    # servername
      100,    # level (100/101)
      4,      # bufptr
      MAX_PREFERRED_LENGTH, # prefmaxlen
      4,      # entries read
      4,      # total entries
      server_type, # server_type
      domain,    # domain
      nil     # resume handle
  )

  case result['return']
  when 0
    # Railgun assumes PDWORDS are pointers and returns 8 bytes for x64 architectures.
    # Therefore we need to truncate the result value to an actual
    # DWORD for entriesread or totalentries.
    hosts = read_server_structs(result['bufptr'], (result['entriesread'] % 4294967296), domain, server_type)
  when ERROR_NO_BROWSER_SERVERS_FOUND
    print_error("ERROR_NO_BROWSER_SERVERS_FOUND")
    return nil
  when ERROR_MORE_DATA
    vprint_error("ERROR_MORE_DATA")
    return nil
  end

  netapi_buffer_free(result['bufptr'])

  return hosts
end

#net_session_enum(hostname, username) ⇒ Object


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/msf/core/post/windows/net_api.rb', line 95

def net_session_enum(hostname, username)
  sessions = []

  result = client.railgun.netapi32.NetSessionEnum(
      hostname,   # servername
      nil,        # UncClientName
      username,   # username
      10,         # level
      4,          # bufptr
      MAX_PREFERRED_LENGTH, # prefmaxlen
      4,          # entriesread
      4,          # totalentries
      nil         # resume_handle
  )

  case result['return']
  when 0
    vprint_error("#{hostname} Session identified")
    sessions = read_session_structs(result['bufptr'], (result['entriesread'] % 4294967296), hostname)
  when ERROR_ACCESS_DENIED
    vprint_error("#{hostname} Access denied...")
    return nil
  when 53
    vprint_error("Host not found or did not respond: #{hostname}")
    return nil
  when 123
    vprint_error("Invalid host: #{hostname}")
    return nil
  when NERR_UserNotFound
    return nil
  when ERROR_MORE_DATA
    vprint_error("#{hostname} ERROR_MORE_DATA")
  else
    vprint_error("Unaccounted for error code: #{result['return']}")
    return nil
  end

  netapi_buffer_free(result['bufptr'])

  return sessions
end

#netapi_buffer_free(ptr) ⇒ Object


30
31
32
33
34
# File 'lib/msf/core/post/windows/net_api.rb', line 30

def netapi_buffer_free(ptr)
  # Free the buffer
  ret = client.railgun.netapi32.NetApiBufferFree(ptr)
  vprint_error("Unable to free buffer, Error Code: #{ret['return']}") unless ret['return'] == 0
end

#read_server_structs(start_ptr, count, domain, server_type) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/msf/core/post/windows/net_api.rb', line 70

def read_server_structs(start_ptr, count, domain, server_type)
  hosts = []
  return hosts if count <= 0

  ptr_size = client.railgun.util.pointer_size
  ptr = (ptr_size == 8) ? 'Q<' : 'V'

  base = 0
  # Struct -> Ptr, Ptr
  struct_size = ptr_size * 2

  mem = client.railgun.memread(start_ptr, struct_size*count)

  count.times do
    x = {}
    x[:version]= mem[(base + 0),ptr_size].unpack(ptr).first
    nameptr = mem[(base + ptr_size),ptr_size].unpack(ptr).first
    x[:name] = UnicodeByteStringToAscii(client.railgun.memread(nameptr, 255))
    hosts << x
    base += struct_size
  end

  hosts
end

#read_session_structs(start_ptr, count, hostname) ⇒ Object


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/msf/core/post/windows/net_api.rb', line 137

def read_session_structs(start_ptr, count, hostname)
  sessions = []
  return sessions if count <= 0

  ptr_size = client.railgun.util.pointer_size
  ptr = (ptr_size == 8) ? 'Q<' : 'V'

  base = 0
  # Struct -> Ptr, Ptr, Dword Dword
  struct_size = (ptr_size * 2) + 8
  mem = client.railgun.memread(start_ptr, struct_size*count)

  count.times do
    sess = {}
    cnameptr = mem[(base + 0),ptr_size].unpack(ptr).first
    usernameptr = mem[(base + ptr_size),ptr_size].unpack(ptr).first
    sess[:usetime] = mem[(base + (ptr_size * 2)),4].unpack('V').first
    sess[:idletime] = mem[(base + (ptr_size * 2) + 4),4].unpack('V').first
    sess[:cname] = UnicodeByteStringToAscii(client.railgun.memread(cnameptr,255))
    sess[:username] = UnicodeByteStringToAscii(client.railgun.memread(usernameptr,255))
    sess[:hostname] = hostname
    sessions << sess
    base = base + struct_size
  end

  sessions
end

#UnicodeByteStringToAscii(str) ⇒ Object


25
26
27
28
# File 'lib/msf/core/post/windows/net_api.rb', line 25

def UnicodeByteStringToAscii(str)
  length = (str.index "\0\0\0") + 1
  Rex::Text.to_ascii(str[0..length])
end