CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/windows/gather/ad_to_sqlite.rb
Views: 11655
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
require 'sqlite3'
7
require 'tempfile'
8
9
class MetasploitModule < Msf::Post
10
include Msf::Post::Windows::LDAP
11
12
def initialize(info = {})
13
super(
14
update_info(
15
info,
16
'Name' => 'AD Computer, Group and Recursive User Membership to Local SQLite DB',
17
'Description' => %q{
18
This module will gather a list of AD groups, identify the users (taking into account recursion)
19
and write this to a SQLite database for offline analysis and query using normal SQL syntax.
20
},
21
'License' => MSF_LICENSE,
22
'Author' => [
23
'Stuart Morgan <stuart.morgan[at]mwrinfosecurity.com>'
24
],
25
'Platform' => [ 'win' ],
26
'SessionTypes' => [ 'meterpreter' ]
27
)
28
)
29
30
register_options([
31
OptString.new('GROUP_FILTER', [false, 'Additional LDAP filters to use when searching for initial groups', '']),
32
OptBool.new('SHOW_USERGROUPS', [true, 'Show the user/group membership in a greppable form to the console.', false]),
33
OptBool.new('SHOW_COMPUTERS', [true, 'Show basic computer information in a greppable form to the console.', false]),
34
OptInt.new('THREADS', [true, 'Number of threads to spawn to gather membership of each group.', 20])
35
])
36
end
37
38
# Entry point
39
def run
40
max_search = datastore['MAX_SEARCH']
41
42
db, dbfile = create_sqlite_db
43
print_status "Temporary database created: #{dbfile.path}"
44
45
# Download the list of groups from Active Directory
46
vprint_status 'Retrieving AD Groups'
47
begin
48
group_fields = ['distinguishedName', 'objectSid', 'samAccountType', 'sAMAccountName', 'whenChanged', 'whenCreated', 'description', 'groupType', 'adminCount', 'comment', 'managedBy', 'cn']
49
if datastore['GROUP_FILTER'].nil? || datastore['GROUP_FILTER'].empty?
50
group_query = '(objectClass=group)'
51
else
52
group_query = "(&(objectClass=group)(#{datastore['GROUP_FILTER']}))"
53
end
54
groups = query(group_query, max_search, group_fields)
55
rescue ::RuntimeError, ::Rex::Post::Meterpreter::RequestError => e
56
print_error("Error(Group): #{e.message}")
57
return
58
end
59
60
# If no groups were downloaded, there's no point carrying on
61
if groups.nil? || groups[:results].empty?
62
print_error('No AD groups were discovered')
63
return
64
end
65
66
# Go through each of the groups and identify the individual users in each group
67
vprint_status "Groups retrieval completed: #{groups[:results].size} group(s)"
68
vprint_status 'Retrieving AD Group Membership'
69
users_fields = ['distinguishedName', 'objectSid', 'sAMAccountType', 'sAMAccountName', 'displayName', 'description', 'logonCount', 'userAccountControl', 'userPrincipalName', 'whenChanged', 'whenCreated', 'primaryGroupID', 'badPwdCount', 'comment', 'title', 'cn', 'adminCount', 'manager']
70
71
remaining_groups = groups[:results]
72
73
# If the number of threads exceeds the number of groups, reduce them down to the correct number
74
threadcount = remaining_groups.count < datastore['THREADS'] ? remaining_groups.count : datastore['THREADS']
75
76
# Loop through each of the groups, creating threads where necessary
77
while !remaining_groups.nil? && !remaining_groups.empty?
78
group_gather = []
79
1.upto(threadcount) do
80
group_gather << framework.threads.spawn("Module(#{refname})", false, remaining_groups.shift) do |individual_group|
81
next if !individual_group || individual_group.empty? || individual_group.nil?
82
83
# Get the Group RID
84
group_rid = get_rid(individual_group[1][:value]).to_i
85
86
# Perform the ADSI query to retrieve the effective users in each group (recursion)
87
vprint_status "Retrieving members of #{individual_group[3][:value]}"
88
users_filter = "(&(objectCategory=person)(objectClass=user)(|(memberOf:1.2.840.113556.1.4.1941:=#{individual_group[0][:value]})(primaryGroupID=#{group_rid})))"
89
users_in_group = query(users_filter, max_search, users_fields)
90
91
grouptype_int = individual_group[7][:value].to_i # Set this here because it is used a lot below
92
sat_int = individual_group[2][:value].to_i
93
94
# Add the group to the database
95
# groupType parameter interpretation: https://msdn.microsoft.com/en-us/library/windows/desktop/ms675935(v=vs.85).aspx
96
# Note that the conversions to UTF-8 are necessary because of the way SQLite detects column type affinity
97
# Turns out that the 'fix' is documented in https://github.com/rails/rails/issues/1965
98
sql_param_group = {
99
g_rid: group_rid,
100
g_distinguishedName: individual_group[0][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
101
g_sAMAccountType: sat_int,
102
g_sAMAccountName: individual_group[3][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
103
g_whenChanged: individual_group[4][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
104
g_whenCreated: individual_group[5][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
105
g_description: individual_group[6][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
106
g_groupType: grouptype_int,
107
g_adminCount: individual_group[8][:value].to_i,
108
g_comment: individual_group[9][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
109
g_managedBy: individual_group[10][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
110
g_cn: individual_group[11][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
111
# Specifies a group that is created by the system.
112
g_GT_GROUP_CREATED_BY_SYSTEM: (grouptype_int & 0x00000001).zero? ? 0 : 1,
113
# Specifies a group with global scope.
114
g_GT_GROUP_SCOPE_GLOBAL: (grouptype_int & 0x00000002).zero? ? 0 : 1,
115
# Specifies a group with local scope.
116
g_GT_GROUP_SCOPE_LOCAL: (grouptype_int & 0x00000004).zero? ? 0 : 1,
117
# Specifies a group with universal scope.
118
g_GT_GROUP_SCOPE_UNIVERSAL: (grouptype_int & 0x00000008).zero? ? 0 : 1,
119
# Specifies an APP_BASIC group for Windows Server Authorization Manager.
120
g_GT_GROUP_SAM_APP_BASIC: (grouptype_int & 0x00000010).zero? ? 0 : 1,
121
# Specifies an APP_QUERY group for Windows Server Authorization Manager.
122
g_GT_GROUP_SAM_APP_QUERY: (grouptype_int & 0x00000020).zero? ? 0 : 1,
123
# Specifies a security group. If this flag is not set, then the group is a distribution group.
124
g_GT_GROUP_SECURITY: (grouptype_int & 0x80000000).zero? ? 0 : 1,
125
# The inverse of the flag above. Technically GT_GROUP_SECURITY=0 makes it a distribution
126
# group so this is arguably redundant, but I have included it for ease. It makes a lot more sense
127
# to set DISTRIBUTION=1 in a query when your mind is on other things to remember that
128
# DISTRIBUTION is in fact the inverse of SECURITY...:)
129
g_GT_GROUP_DISTRIBUTION: (grouptype_int & 0x80000000).zero? ? 1 : 0,
130
# Now add sAMAccountType constants
131
g_SAM_DOMAIN_OBJECT: (sat_int == 0) ? 1 : 0,
132
g_SAM_GROUP_OBJECT: (sat_int == 0x10000000) ? 1 : 0,
133
g_SAM_NON_SECURITY_GROUP_OBJECT: (sat_int == 0x10000001) ? 1 : 0,
134
g_SAM_ALIAS_OBJECT: (sat_int == 0x20000000) ? 1 : 0,
135
g_SAM_NON_SECURITY_ALIAS_OBJECT: (sat_int == 0x20000001) ? 1 : 0,
136
g_SAM_NORMAL_USER_ACCOUNT: (sat_int == 0x30000000) ? 1 : 0,
137
g_SAM_MACHINE_ACCOUNT: (sat_int == 0x30000001) ? 1 : 0,
138
g_SAM_TRUST_ACCOUNT: (sat_int == 0x30000002) ? 1 : 0,
139
g_SAM_APP_BASIC_GROUP: (sat_int == 0x40000000) ? 1 : 0,
140
g_SAM_APP_QUERY_GROUP: (sat_int == 0x40000001) ? 1 : 0,
141
g_SAM_ACCOUNT_TYPE_MAX: (sat_int == 0x7fffffff) ? 1 : 0
142
}
143
run_sqlite_query(db, 'ad_groups', sql_param_group)
144
145
# Go through each group user
146
next if users_in_group[:results].empty?
147
148
users_in_group[:results].each do |group_user|
149
user_rid = get_rid(group_user[1][:value]).to_i
150
print_line "Group [#{individual_group[3][:value]}][#{group_rid}] has member [#{group_user[3][:value]}][#{user_rid}]" if datastore['SHOW_USERGROUPS']
151
152
uac_int = group_user[7][:value].to_i # Set this because it is used so frequently below
153
sat_int = group_user[2][:value].to_i
154
155
# Add the group to the database
156
# Also parse the ADF_ flags from userAccountControl: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680832(v=vs.85).aspx
157
sql_param_user = {
158
u_rid: user_rid,
159
u_distinguishedName: group_user[0][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
160
u_sAMAccountType: group_user[2][:value].to_i,
161
u_sAMAccountName: group_user[3][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
162
u_displayName: group_user[4][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
163
u_description: group_user[5][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
164
u_logonCount: group_user[6][:value].to_i,
165
u_userAccountControl: uac_int,
166
u_userPrincipalName: group_user[8][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
167
u_whenChanged: group_user[9][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
168
u_whenCreated: group_user[10][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
169
u_primaryGroupID: group_user[11][:value].to_i,
170
u_badPwdCount: group_user[12][:value].to_i,
171
u_comment: group_user[13][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
172
u_title: group_user[14][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
173
u_cn: group_user[15][:value].to_s.encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
174
# Indicates that a given object has had its ACLs changed to a more secure value by the
175
# system because it was a member of one of the administrative groups (directly or transitively).
176
u_adminCount: group_user[16][:value].to_i,
177
u_manager: group_user[17][:value].to_s.encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
178
# The login script is executed
179
u_ADS_UF_SCRIPT: (uac_int & 0x00000001).zero? ? 0 : 1,
180
# The user account is disabled.
181
u_ADS_UF_ACCOUNTDISABLE: (uac_int & 0x00000002).zero? ? 0 : 1,
182
# The home directory is required.
183
u_ADS_UF_HOMEDIR_REQUIRED: (uac_int & 0x00000008).zero? ? 0 : 1,
184
# The account is currently locked out.
185
u_ADS_UF_LOCKOUT: (uac_int & 0x00000010).zero? ? 0 : 1,
186
# No password is required.
187
u_ADS_UF_PASSWD_NOTREQD: (uac_int & 0x00000020).zero? ? 0 : 1,
188
# The user cannot change the password.
189
u_ADS_UF_PASSWD_CANT_CHANGE: (uac_int & 0x00000040).zero? ? 0 : 1,
190
# The user can send an encrypted password.
191
u_ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED: (uac_int & 0x00000080).zero? ? 0 : 1,
192
# This is an account for users whose primary account is in another domain. This account
193
# provides user access to this domain, but not to any domain that trusts this domain.
194
# Also known as a local user account.
195
u_ADS_UF_TEMP_DUPLICATE_ACCOUNT: (uac_int & 0x00000100).zero? ? 0 : 1,
196
# This is a default account type that represents a typical user.
197
u_ADS_UF_NORMAL_ACCOUNT: (uac_int & 0x00000200).zero? ? 0 : 1,
198
# This is a permit to trust account for a system domain that trusts other domains.
199
u_ADS_UF_INTERDOMAIN_TRUST_ACCOUNT: (uac_int & 0x00000800).zero? ? 0 : 1,
200
# This is a computer account for a computer that is a member of this domain.
201
u_ADS_UF_WORKSTATION_TRUST_ACCOUNT: (uac_int & 0x00001000).zero? ? 0 : 1,
202
# This is a computer account for a system backup domain controller that is a member of this domain.
203
u_ADS_UF_SERVER_TRUST_ACCOUNT: (uac_int & 0x00002000).zero? ? 0 : 1,
204
# The password for this account will never expire.
205
u_ADS_UF_DONT_EXPIRE_PASSWD: (uac_int & 0x00010000).zero? ? 0 : 1,
206
# This is an MNS logon account.
207
u_ADS_UF_MNS_LOGON_ACCOUNT: (uac_int & 0x00020000).zero? ? 0 : 1,
208
# The user must log on using a smart card.
209
u_ADS_UF_SMARTCARD_REQUIRED: (uac_int & 0x00040000).zero? ? 0 : 1,
210
# The service account (user or computer account), under which a service runs, is trusted for Kerberos delegation.
211
# Any such service can impersonate a client requesting the service.
212
u_ADS_UF_TRUSTED_FOR_DELEGATION: (uac_int & 0x00080000).zero? ? 0 : 1,
213
# The security context of the user will not be delegated to a service even if the service
214
# account is set as trusted for Kerberos delegation.
215
u_ADS_UF_NOT_DELEGATED: (uac_int & 0x00100000).zero? ? 0 : 1,
216
# Restrict this principal to use only Data #Encryption Standard (DES) encryption types for keys.
217
u_ADS_UF_USE_DES_KEY_ONLY: (uac_int & 0x00200000).zero? ? 0 : 1,
218
# This account does not require Kerberos pre-authentication for logon.
219
u_ADS_UF_DONT_REQUIRE_PREAUTH: (uac_int & 0x00400000).zero? ? 0 : 1,
220
# The password has expired
221
u_ADS_UF_PASSWORD_EXPIRED: (uac_int & 0x00800000).zero? ? 0 : 1,
222
# The account is enabled for delegation. This is a security-sensitive setting; accounts with
223
# this option enabled should be strictly controlled. This setting enables a service running
224
# under the account to assume a client identity and authenticate as that user to other remote
225
# servers on the network.
226
u_ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION: (uac_int & 0x01000000).zero? ? 0 : 1,
227
# Now add sAMAccountType constants
228
u_SAM_DOMAIN_OBJECT: (sat_int == 0) ? 1 : 0,
229
u_SAM_GROUP_OBJECT: (sat_int == 0x10000000) ? 1 : 0,
230
u_SAM_NON_SECURITY_GROUP_OBJECT: (sat_int == 0x10000001) ? 1 : 0,
231
u_SAM_ALIAS_OBJECT: (sat_int == 0x20000000) ? 1 : 0,
232
u_SAM_NON_SECURITY_ALIAS_OBJECT: (sat_int == 0x20000001) ? 1 : 0,
233
u_SAM_NORMAL_USER_ACCOUNT: (sat_int == 0x30000000) ? 1 : 0,
234
u_SAM_MACHINE_ACCOUNT: (sat_int == 0x30000001) ? 1 : 0,
235
u_SAM_TRUST_ACCOUNT: (sat_int == 0x30000002) ? 1 : 0,
236
u_SAM_APP_BASIC_GROUP: (sat_int == 0x40000000) ? 1 : 0,
237
u_SAM_APP_QUERY_GROUP: (sat_int == 0x40000001) ? 1 : 0,
238
u_SAM_ACCOUNT_TYPE_MAX: (sat_int == 0x7fffffff) ? 1 : 0
239
}
240
run_sqlite_query(db, 'ad_users', sql_param_user)
241
242
# Now associate the user with the group
243
sql_param_mapping = {
244
user_rid: user_rid,
245
group_rid: group_rid
246
}
247
run_sqlite_query(db, 'ad_mapping', sql_param_mapping)
248
end
249
rescue ::RuntimeError, ::Rex::Post::Meterpreter::RequestError => e
250
print_error("Error(Users): #{e.message}")
251
next
252
end
253
end
254
group_gather.map(&:join)
255
end
256
257
vprint_status 'Retrieving computers'
258
begin
259
computer_filter = '(objectClass=computer)'
260
computer_fields = ['distinguishedName', 'objectSid', 'cn', 'dNSHostName', 'sAMAccountType', 'sAMAccountName', 'displayName', 'logonCount', 'userAccountControl', 'whenChanged', 'whenCreated', 'primaryGroupID', 'badPwdCount', 'operatingSystem', 'operatingSystemServicePack', 'operatingSystemVersion', 'description', 'comment']
261
computers = query(computer_filter, max_search, computer_fields)
262
263
computers[:results].each do |comp|
264
computer_rid = get_rid(comp[1][:value]).to_i
265
266
uac_int = comp[8][:value].to_i # Set this because it is used so frequently below
267
sat_int = comp[4][:value].to_i
268
269
# Add the group to the database
270
# Also parse the ADF_ flags from userAccountControl: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680832(v=vs.85).aspx
271
# Note that userAccountControl is basically the same for a computer as a user; this is because a computer account is derived from a user account
272
# (if you look at the objectClass for a computer account, it includes 'user') and, for efficiency, we should really store it all in one
273
# table. However, the reality is that it will get annoying for users to have to remember to use the userAccountControl flags to work out whether
274
# its a user or a computer and so, for convenience and ease of use, I have put them in completely separate tables.
275
# Also add the sAMAccount type flags from https://msdn.microsoft.com/en-us/library/windows/desktop/ms679637(v=vs.85).aspx
276
sql_param_computer = {
277
c_rid: computer_rid,
278
c_distinguishedName: comp[0][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
279
c_cn: comp[2][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
280
c_dNSHostName: comp[3][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
281
c_sAMAccountType: sat_int,
282
c_sAMAccountName: comp[5][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
283
c_displayName: comp[6][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
284
c_logonCount: comp[7][:value].to_i,
285
c_userAccountControl: uac_int,
286
c_whenChanged: comp[9][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
287
c_whenCreated: comp[10][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
288
c_primaryGroupID: comp[11][:value].to_i,
289
c_badPwdCount: comp[12][:value].to_i,
290
c_operatingSystem: comp[13][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
291
c_operatingSystemServicePack: comp[14][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
292
c_operatingSystemVersion: comp[15][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
293
c_description: comp[16][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
294
c_comment: comp[17][:value].encode('UTF-16be', invalid: :replace, undef: :replace, replace: '?').encode('UTF-8', invalid: :replace, undef: :replace, replace: '?'),
295
# The login script is executed
296
c_ADS_UF_SCRIPT: (uac_int & 0x00000001).zero? ? 0 : 1,
297
# The user account is disabled.
298
c_ADS_UF_ACCOUNTDISABLE: (uac_int & 0x00000002).zero? ? 0 : 1,
299
# The home directory is required.
300
c_ADS_UF_HOMEDIR_REQUIRED: (uac_int & 0x00000008).zero? ? 0 : 1,
301
# The account is currently locked out.
302
c_ADS_UF_LOCKOUT: (uac_int & 0x00000010).zero? ? 0 : 1,
303
# No password is required.
304
c_ADS_UF_PASSWD_NOTREQD: (uac_int & 0x00000020).zero? ? 0 : 1,
305
# The user cannot change the password.
306
c_ADS_UF_PASSWD_CANT_CHANGE: (uac_int & 0x00000040).zero? ? 0 : 1,
307
# The user can send an encrypted password.
308
c_ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED: (uac_int & 0x00000080).zero? ? 0 : 1,
309
# This is an account for users whose primary account is in another domain. This account
310
# provides user access to this domain, but not to any domain that trusts this domain.
311
# Also known as a local user account.
312
c_ADS_UF_TEMP_DUPLICATE_ACCOUNT: (uac_int & 0x00000100).zero? ? 0 : 1,
313
# This is a default account type that represents a typical user.
314
c_ADS_UF_NORMAL_ACCOUNT: (uac_int & 0x00000200).zero? ? 0 : 1,
315
# This is a permit to trust account for a system domain that trusts other domains.
316
c_ADS_UF_INTERDOMAIN_TRUST_ACCOUNT: (uac_int & 0x00000800).zero? ? 0 : 1,
317
# This is a computer account for a computer that is a member of this domain.
318
c_ADS_UF_WORKSTATION_TRUST_ACCOUNT: (uac_int & 0x00001000).zero? ? 0 : 1,
319
# This is a computer account for a system backup domain controller that is a member of this domain.
320
c_ADS_UF_SERVER_TRUST_ACCOUNT: (uac_int & 0x00002000).zero? ? 0 : 1,
321
# The password for this account will never expire.
322
c_ADS_UF_DONT_EXPIRE_PASSWD: (uac_int & 0x00010000).zero? ? 0 : 1,
323
# This is an MNS logon account.
324
c_ADS_UF_MNS_LOGON_ACCOUNT: (uac_int & 0x00020000).zero? ? 0 : 1,
325
# The user must log on using a smart card.
326
c_ADS_UF_SMARTCARD_REQUIRED: (uac_int & 0x00040000).zero? ? 0 : 1,
327
# The service account (user or computer account), under which a service runs, is trusted for Kerberos delegation.
328
# Any such service can impersonate a client requesting the service.
329
c_ADS_UF_TRUSTED_FOR_DELEGATION: (uac_int & 0x00080000).zero? ? 0 : 1,
330
# The security context of the user will not be delegated to a service even if the service
331
# account is set as trusted for Kerberos delegation.
332
c_ADS_UF_NOT_DELEGATED: (uac_int & 0x00100000).zero? ? 0 : 1,
333
# Restrict this principal to use only Data #Encryption Standard (DES) encryption types for keys.
334
c_ADS_UF_USE_DES_KEY_ONLY: (uac_int & 0x00200000).zero? ? 0 : 1,
335
# This account does not require Kerberos pre-authentication for logon.
336
c_ADS_UF_DONT_REQUIRE_PREAUTH: (uac_int & 0x00400000).zero? ? 0 : 1,
337
# The password has expired
338
c_ADS_UF_PASSWORD_EXPIRED: (uac_int & 0x00800000).zero? ? 0 : 1,
339
# The account is enabled for delegation. This is a security-sensitive setting; accounts with
340
# this option enabled should be strictly controlled. This setting enables a service running
341
# under the account to assume a client identity and authenticate as that user to other remote
342
# servers on the network.
343
c_ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION: (uac_int & 0x01000000).zero? ? 0 : 1,
344
# Now add the sAMAccountType objects
345
c_SAM_DOMAIN_OBJECT: (sat_int == 0) ? 1 : 0,
346
c_SAM_GROUP_OBJECT: (sat_int == 0x10000000) ? 1 : 0,
347
c_SAM_NON_SECURITY_GROUP_OBJECT: (sat_int == 0x10000001) ? 1 : 0,
348
c_SAM_ALIAS_OBJECT: (sat_int == 0x20000000) ? 1 : 0,
349
c_SAM_NON_SECURITY_ALIAS_OBJECT: (sat_int == 0x20000001) ? 1 : 0,
350
c_SAM_NORMAL_USER_ACCOUNT: (sat_int == 0x30000000) ? 1 : 0,
351
c_SAM_MACHINE_ACCOUNT: (sat_int == 0x30000001) ? 1 : 0,
352
c_SAM_TRUST_ACCOUNT: (sat_int == 0x30000002) ? 1 : 0,
353
c_SAM_APP_BASIC_GROUP: (sat_int == 0x40000000) ? 1 : 0,
354
c_SAM_APP_QUERY_GROUP: (sat_int == 0x40000001) ? 1 : 0,
355
c_SAM_ACCOUNT_TYPE_MAX: (sat_int == 0x7fffffff) ? 1 : 0
356
}
357
run_sqlite_query(db, 'ad_computers', sql_param_computer)
358
print_line "Computer [#{sql_param_computer[:c_cn]}][#{sql_param_computer[:c_dNSHostName]}][#{sql_param_computer[:c_rid]}]" if datastore['SHOW_COMPUTERS']
359
end
360
rescue ::RuntimeError, ::Rex::Post::Meterpreter::RequestError => e
361
print_error("Error(Computers): #{e.message}")
362
return
363
end
364
365
loot_path = store_loot(
366
'host.ad_to_sqlite',
367
'application/x-sqlite3',
368
session,
369
File.binread(dbfile.path),
370
'ad_to_sqlite.db',
371
'AD Computer, Group and Recursive User Membership'
372
)
373
374
print_good("Sqlite extraction stored in file: #{loot_path}")
375
ensure
376
# Finished enumeration, cleanup resources
377
if db
378
db.close
379
end
380
381
if dbfile
382
dbfile.close
383
dbfile.unlink
384
end
385
end
386
387
# Run the parameterised SQL query
388
def run_sqlite_query(db, table_name, values)
389
sql_param_columns = values.keys
390
sql_param_bind_params = values.keys.map { |k| ":#{k}" }
391
db.execute("replace into #{table_name} (#{sql_param_columns.join(',')}) VALUES (#{sql_param_bind_params.join(',')})", values)
392
end
393
394
# Creat the SQLite Database
395
def create_sqlite_db
396
dbfile = Tempfile.new('ad_to_sqlite')
397
db = SQLite3::Database.new(dbfile.path)
398
db.type_translation = true
399
400
# Create the table for the AD Computers
401
db.execute('DROP TABLE IF EXISTS ad_computers')
402
sql_table_computers = 'CREATE TABLE ad_computers ('\
403
'c_rid INTEGER PRIMARY KEY NOT NULL,'\
404
'c_distinguishedName TEXT UNIQUE NOT NULL,'\
405
'c_cn TEXT,'\
406
'c_sAMAccountType INTEGER,'\
407
'c_sAMAccountName TEXT UNIQUE NOT NULL,'\
408
'c_dNSHostName TEXT,'\
409
'c_displayName TEXT,'\
410
'c_logonCount INTEGER,'\
411
'c_userAccountControl INTEGER,'\
412
'c_primaryGroupID INTEGER,'\
413
'c_badPwdCount INTEGER,'\
414
'c_description TEXT,'\
415
'c_comment TEXT,'\
416
'c_operatingSystem TEXT,'\
417
'c_operatingSystemServicePack TEXT,'\
418
'c_operatingSystemVersion TEXT,'\
419
'c_whenChanged TEXT,'\
420
'c_whenCreated TEXT,'\
421
'c_ADS_UF_SCRIPT INTEGER,'\
422
'c_ADS_UF_ACCOUNTDISABLE INTEGER,'\
423
'c_ADS_UF_HOMEDIR_REQUIRED INTEGER,'\
424
'c_ADS_UF_LOCKOUT INTEGER,'\
425
'c_ADS_UF_PASSWD_NOTREQD INTEGER,'\
426
'c_ADS_UF_PASSWD_CANT_CHANGE INTEGER,'\
427
'c_ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED INTEGER,'\
428
'c_ADS_UF_TEMP_DUPLICATE_ACCOUNT INTEGER,'\
429
'c_ADS_UF_NORMAL_ACCOUNT INTEGER,'\
430
'c_ADS_UF_INTERDOMAIN_TRUST_ACCOUNT INTEGER,'\
431
'c_ADS_UF_WORKSTATION_TRUST_ACCOUNT INTEGER,'\
432
'c_ADS_UF_SERVER_TRUST_ACCOUNT INTEGER,'\
433
'c_ADS_UF_DONT_EXPIRE_PASSWD INTEGER,'\
434
'c_ADS_UF_MNS_LOGON_ACCOUNT INTEGER,'\
435
'c_ADS_UF_SMARTCARD_REQUIRED INTEGER,'\
436
'c_ADS_UF_TRUSTED_FOR_DELEGATION INTEGER,'\
437
'c_ADS_UF_NOT_DELEGATED INTEGER,'\
438
'c_ADS_UF_USE_DES_KEY_ONLY INTEGER,'\
439
'c_ADS_UF_DONT_REQUIRE_PREAUTH INTEGER,'\
440
'c_ADS_UF_PASSWORD_EXPIRED INTEGER,'\
441
'c_ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION INTEGER,'\
442
'c_SAM_DOMAIN_OBJECT INTEGER,'\
443
'c_SAM_GROUP_OBJECT INTEGER,'\
444
'c_SAM_NON_SECURITY_GROUP_OBJECT INTEGER,'\
445
'c_SAM_ALIAS_OBJECT INTEGER,'\
446
'c_SAM_NON_SECURITY_ALIAS_OBJECT INTEGER,'\
447
'c_SAM_NORMAL_USER_ACCOUNT INTEGER,'\
448
'c_SAM_MACHINE_ACCOUNT INTEGER,'\
449
'c_SAM_TRUST_ACCOUNT INTEGER,'\
450
'c_SAM_APP_BASIC_GROUP INTEGER,'\
451
'c_SAM_APP_QUERY_GROUP INTEGER,'\
452
'c_SAM_ACCOUNT_TYPE_MAX INTEGER)'
453
db.execute(sql_table_computers)
454
455
# Create the table for the AD Groups
456
db.execute('DROP TABLE IF EXISTS ad_groups')
457
sql_table_group = 'CREATE TABLE ad_groups ('\
458
'g_rid INTEGER PRIMARY KEY NOT NULL,'\
459
'g_distinguishedName TEXT UNIQUE NOT NULL,'\
460
'g_sAMAccountType INTEGER,'\
461
'g_sAMAccountName TEXT UNIQUE NOT NULL,'\
462
'g_groupType INTEGER,'\
463
'g_adminCount INTEGER,'\
464
'g_description TEXT,'\
465
'g_comment TEXT,'\
466
'g_cn TEXT,'\
467
'g_managedBy TEXT,'\
468
'g_whenChanged TEXT,'\
469
'g_whenCreated TEXT,'\
470
'g_GT_GROUP_CREATED_BY_SYSTEM INTEGER,'\
471
'g_GT_GROUP_SCOPE_GLOBAL INTEGER,'\
472
'g_GT_GROUP_SCOPE_LOCAL INTEGER,'\
473
'g_GT_GROUP_SCOPE_UNIVERSAL INTEGER,'\
474
'g_GT_GROUP_SAM_APP_BASIC INTEGER,'\
475
'g_GT_GROUP_SAM_APP_QUERY INTEGER,'\
476
'g_GT_GROUP_SECURITY INTEGER,'\
477
'g_GT_GROUP_DISTRIBUTION INTEGER,'\
478
'g_SAM_DOMAIN_OBJECT INTEGER,'\
479
'g_SAM_GROUP_OBJECT INTEGER,'\
480
'g_SAM_NON_SECURITY_GROUP_OBJECT INTEGER,'\
481
'g_SAM_ALIAS_OBJECT INTEGER,'\
482
'g_SAM_NON_SECURITY_ALIAS_OBJECT INTEGER,'\
483
'g_SAM_NORMAL_USER_ACCOUNT INTEGER,'\
484
'g_SAM_MACHINE_ACCOUNT INTEGER,'\
485
'g_SAM_TRUST_ACCOUNT INTEGER,'\
486
'g_SAM_APP_BASIC_GROUP INTEGER,'\
487
'g_SAM_APP_QUERY_GROUP INTEGER,'\
488
'g_SAM_ACCOUNT_TYPE_MAX INTEGER)'
489
db.execute(sql_table_group)
490
491
# Create the table for the AD Users
492
db.execute('DROP TABLE IF EXISTS ad_users')
493
sql_table_users = 'CREATE TABLE ad_users ('\
494
'u_rid INTEGER PRIMARY KEY NOT NULL,'\
495
'u_distinguishedName TEXT UNIQUE NOT NULL,'\
496
'u_description TEXT,'\
497
'u_displayName TEXT,'\
498
'u_sAMAccountType INTEGER,'\
499
'u_sAMAccountName TEXT,'\
500
'u_logonCount INTEGER,'\
501
'u_userAccountControl INTEGER,'\
502
'u_primaryGroupID INTEGER,'\
503
'u_cn TEXT,'\
504
'u_adminCount INTEGER,'\
505
'u_badPwdCount INTEGER,'\
506
'u_userPrincipalName TEXT UNIQUE,'\
507
'u_comment TEXT,'\
508
'u_title TEXT,'\
509
'u_manager TEXT,'\
510
'u_whenCreated TEXT,'\
511
'u_whenChanged TEXT,'\
512
'u_ADS_UF_SCRIPT INTEGER,'\
513
'u_ADS_UF_ACCOUNTDISABLE INTEGER,'\
514
'u_ADS_UF_HOMEDIR_REQUIRED INTEGER,'\
515
'u_ADS_UF_LOCKOUT INTEGER,'\
516
'u_ADS_UF_PASSWD_NOTREQD INTEGER,'\
517
'u_ADS_UF_PASSWD_CANT_CHANGE INTEGER,'\
518
'u_ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED INTEGER,'\
519
'u_ADS_UF_TEMP_DUPLICATE_ACCOUNT INTEGER,'\
520
'u_ADS_UF_NORMAL_ACCOUNT INTEGER,'\
521
'u_ADS_UF_INTERDOMAIN_TRUST_ACCOUNT INTEGER,'\
522
'u_ADS_UF_WORKSTATION_TRUST_ACCOUNT INTEGER,'\
523
'u_ADS_UF_SERVER_TRUST_ACCOUNT INTEGER,'\
524
'u_ADS_UF_DONT_EXPIRE_PASSWD INTEGER,'\
525
'u_ADS_UF_MNS_LOGON_ACCOUNT INTEGER,'\
526
'u_ADS_UF_SMARTCARD_REQUIRED INTEGER,'\
527
'u_ADS_UF_TRUSTED_FOR_DELEGATION INTEGER,'\
528
'u_ADS_UF_NOT_DELEGATED INTEGER,'\
529
'u_ADS_UF_USE_DES_KEY_ONLY INTEGER,'\
530
'u_ADS_UF_DONT_REQUIRE_PREAUTH INTEGER,'\
531
'u_ADS_UF_PASSWORD_EXPIRED INTEGER,'\
532
'u_ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION INTEGER,'\
533
'u_SAM_DOMAIN_OBJECT INTEGER,'\
534
'u_SAM_GROUP_OBJECT INTEGER,'\
535
'u_SAM_NON_SECURITY_GROUP_OBJECT INTEGER,'\
536
'u_SAM_ALIAS_OBJECT INTEGER,'\
537
'u_SAM_NON_SECURITY_ALIAS_OBJECT INTEGER,'\
538
'u_SAM_NORMAL_USER_ACCOUNT INTEGER,'\
539
'u_SAM_MACHINE_ACCOUNT INTEGER,'\
540
'u_SAM_TRUST_ACCOUNT INTEGER,'\
541
'u_SAM_APP_BASIC_GROUP INTEGER,'\
542
'u_SAM_APP_QUERY_GROUP INTEGER,'\
543
'u_SAM_ACCOUNT_TYPE_MAX INTEGER)'
544
db.execute(sql_table_users)
545
546
# Create the table for the mapping between the two (membership)
547
db.execute('DROP TABLE IF EXISTS ad_mapping')
548
sql_table_mapping = 'CREATE TABLE ad_mapping ('\
549
'user_rid INTEGER NOT NULL,' \
550
'group_rid INTEGER NOT NULL,'\
551
'PRIMARY KEY (user_rid, group_rid),'\
552
'FOREIGN KEY(user_rid) REFERENCES ad_users(u_rid)'\
553
'FOREIGN KEY(group_rid) REFERENCES ad_groups(g_rid))'
554
db.execute(sql_table_mapping)
555
556
# Create the view for the AD User/Group membership
557
db.execute('DROP VIEW IF EXISTS view_mapping')
558
sql_view_mapping = 'CREATE VIEW view_mapping AS SELECT ad_groups.*,ad_users.* FROM ad_mapping '\
559
'INNER JOIN ad_groups ON ad_groups.g_rid = ad_mapping.group_rid '\
560
'INNER JOIN ad_users ON ad_users.u_rid = ad_mapping.user_rid'
561
db.execute(sql_view_mapping)
562
563
return db, dbfile
564
rescue SQLite3::Exception => e
565
print_error("Error(Database): #{e.message}")
566
return
567
end
568
569
def get_rid(data)
570
sid = data.unpack('bbbbbbbbV*')[8..]
571
sid[-1]
572
end
573
end
574
575