Skip to content

Commit 79a1277

Browse files
author
staaldraad
committed
Merge pull request #2 from Meatballs1/pr2107
Spacing and bugfixes
2 parents 3e1efbf + 13244ef commit 79a1277

File tree

2 files changed

+73
-75
lines changed

2 files changed

+73
-75
lines changed

lib/msf/core/post/windows/netapi.rb

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ module NetAPI
1111
SV_TYPE_DOMAIN_BAKCTRL = 10
1212
SV_TYPE_DOMAIN_CTRL = 4
1313

14+
ERROR_ACCESS_DENIED = 5
15+
ERROR_NOT_ENOUGH_MEMORY = 8
16+
ERROR_INVALID_PARAMETER = 87
17+
ERROR_INVALID_LEVEL = 124
18+
ERROR_MORE_DATA = 234
19+
ERROR_NO_BROWSER_SERVERS_FOUND = 6118
20+
21+
NERR_ClientNameNotFound = 2312
22+
NERR_InvalidComputer = 2351
23+
NERR_UserNotFound = 2221
24+
1425
def UnicodeByteStringToAscii(str)
1526
length = (str.index "\0\0\0") + 1
1627
Rex::Text.to_ascii(str[0..length])
@@ -23,6 +34,8 @@ def netapi_buffer_free(ptr)
2334
end
2435

2536
def net_server_enum(server_type=SV_TYPE_ALL, domain=nil)
37+
hosts = []
38+
2639
result = client.railgun.netapi32.NetServerEnum(
2740
nil, # servername
2841
100, # level (100/101)
@@ -36,38 +49,22 @@ def net_server_enum(server_type=SV_TYPE_ALL, domain=nil)
3649
)
3750

3851
case result['return']
39-
when 5
40-
vprint_error("Access Denied when trying to enum hosts.")
41-
return nil
42-
when 6118
43-
vprint_error("No Browser servers found.")
44-
return nil
45-
when 50
46-
vprint_error("Request not supported.")
47-
return nil
48-
when 2184
49-
vprint_error("Service not installed.")
50-
return nil
5152
when 0
52-
vprint_status("Success.")
53-
when 87
54-
vprint_error ("Invalid parameter.")
53+
hosts = read_server_structs(result['bufptr'], result['totalentries'])
54+
when ERROR_NO_BROWSER_SERVERS_FOUND
55+
print_error("ERROR_NO_BROWSER_SERVERS_FOUND")
56+
return nil
57+
when ERROR_MORE_DATA
58+
vprint_error("ERROR_MORE_DATA")
5559
return nil
56-
else
57-
if result['return'] != 234
58-
vprint_status("Unaccounted for error code: #{result['return']}")
59-
return nil
60-
end
6160
end
6261

63-
hosts = read_server_structs(result['bufptr'], result['totalentries'])
64-
6562
netapi_buffer_free(result['bufptr'])
6663

6764
return hosts
6865
end
6966

70-
def read_server_structs(start_ptr, count)
67+
def read_server_structs(start_ptr, count, domain, server_type)
7168
base = 0
7269
struct_size = 8
7370
hosts = []
@@ -85,21 +82,26 @@ def read_server_structs(start_ptr, count)
8582
return hosts
8683
end
8784

88-
def getSessions(hostname, username)
85+
def net_session_enum(hostname, username)
86+
sessions = []
87+
8988
result = client.railgun.netapi32.NetSessionEnum(
90-
hostname,
91-
nil,
92-
username,
93-
10,
94-
4,
95-
MAX_PREFERRED_LENGTH,
96-
4,
97-
4,
98-
nil
89+
hostname, # servername
90+
nil, # UncClientName
91+
username, # username
92+
10, # level
93+
4, # bufptr
94+
MAX_PREFERRED_LENGTH, # prefmaxlen
95+
4, # entriesread
96+
4, # totalentries
97+
nil # resume_handle
9998
)
10099

101100
case result['return']
102-
when 5
101+
when 0
102+
vprint_error("#{hostname} Session identified")
103+
sessions = read_session_structs(result['bufptr'], result['totalentries'], hostname)
104+
when ERROR_ACCESS_DENIED
103105
vprint_error("#{hostname} Access denied...")
104106
return nil
105107
when 53
@@ -108,19 +110,15 @@ def getSessions(hostname, username)
108110
when 123
109111
vprint_error("Invalid host: #{hostname}")
110112
return nil
111-
when 0
112-
vprint_status("#{hostname} Session identified")
113-
when 2221 #username not found
113+
when NERR_UserNotFound
114114
return nil
115+
when ERROR_MORE_DATA
116+
vprint_error("#{hostname} ERROR_MORE_DATA")
115117
else
116-
if result['return'] != 234
117-
vprint_error("Unaccounted for error code: #{result['return']}")
118-
return nil
119-
end
118+
vprint_error("Unaccounted for error code: #{result['return']}")
119+
return nil
120120
end
121121

122-
sessions = read_session_structs(result['bufptr'], result['totalentries'], hostname)
123-
124122
netapi_buffer_free(result['bufptr'])
125123

126124
return sessions

modules/post/windows/gather/enum_domain_users.rb

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def initialize(info={})
1414
super( update_info( info,
1515
'Name' => 'Windows Gather Enumerate Active Domain Users',
1616
'Description' => %q{
17-
This module will enumerate computers included in the primary Domain and attempt
18-
to list all locations the targeted user has sessions on. If a the HOST option is specified
19-
the module will target only that host. If the HOST is specified and USER is set to nil, all users
20-
logged into that host will be returned.'
17+
This module will enumerate computers included in the primary Domain and attempt
18+
to list all locations the targeted user has sessions on. If a the HOST option is specified
19+
the module will target only that host. If the HOST is specified and USER is set to nil, all users
20+
logged into that host will be returned.'
2121
},
2222
'License' => MSF_LICENSE,
2323
'Author' => [ 'Etienne Stalmans <etienne[at]sensepost.com>'],
@@ -32,50 +32,50 @@ def initialize(info={})
3232
end
3333

3434
def run
35-
sessions = []
36-
user = datastore['USER']
37-
host = datastore['HOST']
35+
sessions = []
36+
user = datastore['USER']
37+
host = datastore['HOST']
3838

39-
if host
40-
if user
41-
print_status("Attempting to identify #{user} on #{host}...")
42-
else
43-
print_status("Attempting to get all logged in users on #{host}...")
44-
end
45-
sessions = getSessions(host, user)
46-
47-
elsif user
48-
domain = getdomain
39+
if host
40+
if user
41+
print_status("Attempting to identify #{user} on #{host}...")
42+
else
43+
print_status("Attempting to get all logged in users on #{host}...")
44+
end
45+
sessions = net_session_enum(host, user)
46+
elsif user
47+
domain = getdomain
4948

50-
unless domain.empty?
51-
print_status ("Using domain: #{domain}")
52-
print_status ("Getting list of domain hosts...")
53-
hosts = net_server_enum(SV_TYPE_ALL, domain)
49+
unless domain.empty?
50+
print_status ("Using domain: #{domain}")
51+
print_status ("Getting list of domain hosts...")
52+
end
5453

55-
if hosts
56-
len = hosts.count
57-
print_status("#{len} host(s) found")
54+
hosts = net_server_enum(SV_TYPE_ALL, domain)
5855

59-
hosts.each do |host|
60-
sessions << getSessions(host[:name], user)
61-
end
56+
if hosts
57+
len = hosts.count
58+
print_status("#{len} host(s) found")
6259

63-
sessions.flatten!
60+
hosts.each do |host|
61+
sessions << net_session_enum(host[:name], user)
6462
end
6563
end
64+
65+
sessions.flatten!
6666
else
67-
print_error("Invalid options, either HOST or USER must be specified.")
68-
return
67+
print_error("Invalid options, either HOST or USER must be specified.")
68+
return
6969
end
7070

71-
if sessions.count == 0
71+
if sessions.nil? or sessions.count == 0
7272
print_error("No sessions found")
7373
return
7474
else
7575
print_status("#{sessions.count} session(s) identified")
7676
end
7777

78-
if sessions and sessions.count > 0
78+
if sessions and sessions.count > 0
7979
sessions.each do |s|
8080
if s
8181
print_good("#{s[:username]} logged in at #{s[:hostname]} and has been idle for #{s[:idletime]} seconds")

0 commit comments

Comments
 (0)