1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
136
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
import dcerpc
def sid_to_string(sid):
"""Convert a Python dictionary SID to a string SID."""
result = 'S-%d' % sid['sid_rev_num']
ia = sid['id_auth']
result = result + '-%u' % (ia[5] + (ia[4] << 8) + (ia[3] << 16) + \
(ia[2] << 24))
for i in range(0, sid['num_auths']):
result = result + '-%u' % sid['sub_auths'][i]
return result
def string_to_sid(string):
"""Convert a string SID to a Python dictionary SID. Throws a
ValueError if the SID string was badly formed."""
if string[0] != 'S':
raise ValueError('Bad SID format')
string = string[1:]
import re
match = re.match('-\d+', string)
if not match:
raise ValueError('Bad SID format')
try:
sid_rev_num = int(string[match.start()+1:match.end()])
except ValueError:
raise ValueError('Bad SID format')
string = string[match.end():]
match = re.match('-\d+', string)
if not match:
raise ValueError('Bad SID format')
try:
ia = int(string[match.start()+1:match.end()])
except ValueError:
raise ValueError('Bad SID format')
string = string[match.end():]
id_auth = [0, 0, (ia >> 24) & 0xff, (ia >> 16) & 0xff,
(ia >> 8) & 0xff, ia & 0xff]
num_auths = 0
sub_auths = []
while len(string):
match = re.match('-\d+', string)
if not match:
raise ValueError('Bad SID format')
try:
sa = int(string[match.start() + 1 : match.end()])
except ValueError:
raise ValueError('Bad SID format')
num_auths = num_auths + 1
sub_auths.append(int(sa))
string = string[match.end():]
return {'sid_rev_num': sid_rev_num, 'id_auth': id_auth,
'num_auths': num_auths, 'sub_auths': sub_auths}
class SamrHandle:
def __init__(self, pipe, handle):
self.pipe = pipe
self.handle = handle
def __del__(self):
r = {}
r['handle'] = self.handle
dcerpc.samr_Close(self.pipe, r)
class ConnectHandle(SamrHandle):
def EnumDomains(self):
r = {}
r['connect_handle'] = self.handle
r['resume_handle'] = 0
r['buf_size'] = -1
domains = []
while 1:
result = dcerpc.samr_EnumDomains(self.pipe, r)
domains = domains + result['sam']['entries']
if result['result'] == dcerpc.STATUS_MORE_ENTRIES:
r['resume_handle'] = result['resume_handle']
continue
break
return map(lambda x: x['name']['name'], domains)
def LookupDomain(self, domain_name):
r = {}
r['connect_handle'] = self.handle
r['domain'] = {}
r['domain']['name_len'] = 0
r['domain']['name_size'] = 0
r['domain']['name'] = domain_name
result = dcerpc.samr_LookupDomain(self.pipe, r)
return sid_to_string(result['sid'])
def OpenDomain(self, domain_sid, access_mask = 0x02000000):
r = {}
r['connect_handle'] = self.handle
r['access_mask'] = access_mask
r['sid'] = string_to_sid(domain_sid)
result = dcerpc.samr_OpenDomain(self.pipe, r)
return DomainHandle(self.pipe, result['domain_handle'])
class DomainHandle(SamrHandle):
def QueryDomainInfo(self, level = 2):
r = {}
r['domain_handle'] = self.handle
r['level'] = level
result = dcerpc.samr_QueryDomainInfo(self.pipe, r)
return result
def QueryDomainInfo2(self, level = 2):
r = {}
r['domain_handle'] = self.handle
r['level'] = level
result = dcerpc.samr_QueryDomainInfo2(self.pipe, r)
return result
def EnumDomainGroups(self):
r = {}
r['domain_handle'] = self.handle
r['resume_handle'] = 0
r['max_size'] = 1000
result = dcerpc.samr_EnumDomainGroups(self.pipe, r)
return result
def EnumDomainAliases(self):
r = {}
r['domain_handle'] = self.handle
r['resume_handle'] = 0
# acct_flags in SamrEnumerateAliasesInDomain has probably
# no meaning so use 0xffffffff like W2K
r['acct_flags'] = 0xffffffff
result = dcerpc.samr_EnumDomainAliases(self.pipe, r)
return result
def EnumDomainUsers(self, user_account_flags = 16):
r = {}
r['domain_handle'] = self.handle
r['resume_handle'] = 0
r['acct_flags'] = user_account_flags
r['max_size'] = 1000
result = dcerpc.samr_EnumDomainUsers(self.pipe, r)
return result
def Connect(pipe, system_name = None, access_mask = 0x02000000):
"""Connect to the SAMR pipe."""
r = {}
r['system_name'] = system_name
r['access_mask'] = access_mask
result = dcerpc.samr_Connect2(pipe, r)
return ConnectHandle(pipe, result['connect_handle'])
|