5 # Find right directory when running from source tree
6 sys
.path
.insert(0, "bin/python")
9 from optparse
import OptionParser
10 from pprint
import pprint
15 """Convert a swig object to a dictionary."""
19 for attr
in filter(lambda x
: type(x
) == str, dir(obj
)):
21 if attr
[:2] == '__' and attr
[-2:] == '__':
24 if attr
== 'this' or attr
== 'thisown':
27 result
[attr
] = getattr(obj
, attr
)
33 prompt
= 'rpcclient$ '
35 def __init__(self
, server
, cred
):
42 # Default for empty line is to repeat last command - yuck
46 def onecmd(self
, line
):
48 # Override the onecmd() method so we can trap error returns
51 Cmd
.onecmd(self
, line
)
52 except dcerpc
.NTSTATUS
as arg
:
53 print 'The command returned an error: %s' % arg
[1]
57 def do_help(self
, line
):
58 """Displays on-line help for rpcclient commands."""
59 Cmd
.do_help(self
, line
)
61 def do_shell(self
, line
):
63 status
= os
.system(line
)
65 if os
.WIFEXITED(status
):
66 if os
.WEXITSTATUS(status
) != 0:
67 print 'Command exited with code %d' % os
.WEXITSTATUS(status
)
69 print 'Command exited with signal %d' % os
.WTERMSIG(status
)
71 def do_EOF(self
, line
):
72 """Exits rpcclient."""
78 def do_SamrEnumDomains(self
, line
):
79 """Enumerate domain names."""
81 usage
= 'usage: SamrEnumDomains'
87 pipe
= dcerpc
.pipe_connect(
88 'ncacn_np:%s' % self
.server
,
89 dcerpc
.DCERPC_SAMR_UUID
, int(dcerpc
.DCERPC_SAMR_VERSION
),
92 connect_handle
= samr
.Connect(pipe
)
94 for i
in connect_handle
.EnumDomains():
97 def do_SamrLookupDomain(self
, line
):
98 """Return the SID for a domain."""
100 usage
= 'SamrLookupDomain DOMAIN'
102 parser
= OptionParser(usage
)
103 options
, args
= parser
.parse_args(string
.split(line
))
106 print 'usage:', usage
109 pipe
= dcerpc
.pipe_connect(
110 'ncacn_np:%s' % self
.server
,
111 dcerpc
.DCERPC_SAMR_UUID
, int(dcerpc
.DCERPC_SAMR_VERSION
),
114 connect_handle
= samr
.Connect(pipe
)
116 print connect_handle
.LookupDomain(args
[0])
118 def do_SamrQueryDomInfo(self
, line
):
119 """Return information about a domain designated by its SID."""
121 usage
= 'SamrQueryDomInfo DOMAIN_SID [info_level]'
123 parser
= OptionParser(usage
)
124 options
, args
= parser
.parse_args(string
.split(line
))
126 if (len(args
) == 0) or (len(args
) > 2):
127 print 'usage:', usage
130 pipe
= dcerpc
.pipe_connect(
131 'ncacn_np:%s' % self
.server
,
132 dcerpc
.DCERPC_SAMR_UUID
, int(dcerpc
.DCERPC_SAMR_VERSION
),
135 connect_handle
= samr
.Connect(pipe
)
136 domain_handle
= connect_handle
.OpenDomain(args
[0])
139 result
= domain_handle
.QueryDomainInfo(int(args
[1]))
141 result
= domain_handle
.QueryDomainInfo()
143 pprint(swig2dict(result
))
145 def do_SamrQueryDomInfo2(self
, line
):
146 """Return information about a domain designated by its SID.
147 (Windows 2000 and >)"""
149 usage
= 'SamrQueryDomInfo2 DOMAIN_SID [info_level] (Windows 2000 and >)'
150 parser
= OptionParser(usage
)
151 options
, args
= parser
.parse_args(string
.split(line
))
153 if len(args
) == 0 or len(args
) > 2:
154 print 'usage:', usage
157 pipe
= dcerpc
.pipe_connect(
158 'ncacn_np:%s' % self
.server
,
159 dcerpc
.DCERPC_SAMR_UUID
, int(dcerpc
.DCERPC_SAMR_VERSION
),
162 connect_handle
= samr
.Connect(pipe
)
163 domain_handle
= connect_handle
.OpenDomain(args
[0])
166 result
= domain_handle
.QueryDomainInfo2(int(args
[1]))
168 result
= domain_handle
.QueryDomainInfo2()
170 pprint(swig2dict(result
))
172 def do_SamrEnumDomainGroups(self
, line
):
173 """Return the list of groups of a domain designated by its SID."""
175 usage
= 'SamrEnumDomainGroups DOMAIN_SID'
177 parser
= OptionParser(usage
)
178 options
, args
= parser
.parse_args(string
.split(line
))
181 print 'usage:', usage
184 pipe
= dcerpc
.pipe_connect(
185 'ncacn_np:%s' % self
.server
,
186 dcerpc
.DCERPC_SAMR_UUID
, int(dcerpc
.DCERPC_SAMR_VERSION
),
189 connect_handle
= samr
.Connect(pipe
)
190 domain_handle
= connect_handle
.OpenDomain(args
[0])
192 result
= domain_handle
.EnumDomainGroups()
196 def do_SamrEnumDomainAliases(self
, line
):
197 """Return the list of aliases (local groups) of a domain designated
200 usage
= 'SamrEnumDomainAliases DOMAIN_SID'
202 parser
= OptionParser(usage
)
203 options
, args
= parser
.parse_args(string
.split(line
))
206 print 'usage:', usage
209 pipe
= dcerpc
.pipe_connect(
210 'ncacn_np:%s' % self
.server
,
211 dcerpc
.DCERPC_SAMR_UUID
, int(dcerpc
.DCERPC_SAMR_VERSION
),
214 connect_handle
= samr
.Connect(pipe
)
215 domain_handle
= connect_handle
.OpenDomain(args
[0])
217 result
= domain_handle
.EnumDomainAliases()
221 def do_SamrEnumDomainUsers(self
, line
):
222 """Return the list of users of a domain designated by its SID."""
224 usage
= 'SamrEnumDomainUsers DOMAIN_SID [user_account_flags]'
226 parser
= OptionParser(usage
)
227 options
, args
= parser
.parse_args(string
.split(line
))
229 if (len(args
) == 0) or (len(args
) > 2):
230 print 'usage:', usage
233 pipe
= dcerpc
.pipe_connect(
234 'ncacn_np:%s' % self
.server
,
235 dcerpc
.DCERPC_SAMR_UUID
, int(dcerpc
.DCERPC_SAMR_VERSION
),
238 connect_handle
= samr
.Connect(pipe
)
239 domain_handle
= connect_handle
.OpenDomain(args
[0])
242 result
= domain_handle
.EnumDomainUsers(int(args
[1]))
244 result
= domain_handle
.EnumDomainUsers()
248 if __name__
== '__main__':
252 usage
= 'rpcclient SERVER [options]'
254 if len(sys
.argv
) == 1:
261 parser
= OptionParser(usage
)
263 parser
.add_option('-U', '--username', action
='store', type='string',
264 help='Use given credentials when connecting',
265 metavar
='DOMAIN\\username%password',
268 parser
.add_option('-c', '--command', action
='store', type='string',
269 help='Execute COMMAND', dest
='command')
271 options
, args
= parser
.parse_args()
273 # Break --username up into domain, username and password
277 if not options
.username
:
278 options
.username
= '%'
281 if string
.find(options
.username
, '\\') != -1:
282 domain
, options
.username
= string
.split(options
.username
, '\\')
285 if string
.find(options
.username
, '%') != -1:
286 options
.username
, password
= string
.split(options
.username
, '%')
288 username
= options
.username
291 cred
= (domain
, username
, password
)
295 c
= rpcclient(server
, cred
)
298 c
.onecmd(options
.command
)
304 except KeyboardInterrupt:
305 print 'KeyboardInterrupt'