5 from optparse
import OptionParser
12 prompt
= 'rpcclient$ '
14 def __init__(self
, binding
, domain
, username
, password
):
16 self
.binding
= binding
18 self
.username
= username
19 self
.password
= password
23 # Default for empty line is to repeat last command - yuck
27 def onecmd(self
, line
):
29 # Override the onecmd() method so we can trap error returns
32 Cmd
.onecmd(self
, line
)
33 except dcerpc
.NTSTATUS
, arg
:
34 print 'The command returned an error: %s' % arg
[1]
38 def do_help(self
, line
):
39 """Displays on-line help for rpcclient commands."""
40 Cmd
.do_help(self
, line
)
42 def do_shell(self
, line
):
44 status
= os
.system(line
)
46 if os
.WIFEXITED(status
):
47 if os
.WEXITSTATUS(status
) != 0:
48 print 'Command exited with code %d' % os
.WEXITSTATUS(status
)
50 print 'Command exited with signal %d' % os
.WTERMSIG(status
)
52 def do_EOF(self
, line
):
53 """Exits rpcclient."""
59 def do_SamrEnumDomains(self
, line
):
60 """Enumerate domain names."""
62 usage
= 'usage: SamrEnumDomains'
68 pipe
= dcerpc
.pipe_connect(
70 dcerpc
.DCERPC_SAMR_UUID
, dcerpc
.DCERPC_SAMR_VERSION
,
71 self
.domain
, self
.username
, self
.password
)
73 connect_handle
= samr
.Connect(pipe
)
75 for i
in connect_handle
.EnumDomains():
78 def do_SamrLookupDomain(self
, line
):
79 """Return the SID for a domain."""
81 usage
= 'SamrLookupDomain DOMAIN'
83 parser
= OptionParser(usage
)
84 options
, args
= parser
.parse_args(string
.split(line
))
90 pipe
= dcerpc
.pipe_connect(
92 dcerpc
.DCERPC_SAMR_UUID
, dcerpc
.DCERPC_SAMR_VERSION
,
93 self
.domain
, self
.username
, self
.password
)
95 connect_handle
= samr
.Connect(pipe
)
97 print connect_handle
.LookupDomain(args
[0])
99 if __name__
== '__main__':
103 usage
= 'rpcclient BINDING [options]'
105 if len(sys
.argv
) == 1:
109 binding
= sys
.argv
[1]
112 if string
.find(binding
, ':') == -1:
113 binding
= 'ncacn_np:' + binding
115 parser
= OptionParser(usage
)
117 parser
.add_option('-U', '--username', action
='store', type='string',
118 help='Use given credentials when connecting',
119 metavar
='DOMAIN\\username%password',
122 parser
.add_option('-c', '--command', action
='store', type='string',
123 help='Execute COMMAND', dest
='command')
125 options
, args
= parser
.parse_args()
127 # Break --username up into domain, usernamd and password
129 if not options
.username
:
130 options
.username
= '%'
133 if string
.find(options
.username
, '\\') != -1:
134 domain
, options
.username
= string
.split(options
.username
, '\\')
137 if string
.find(options
.username
, '%') != -1:
138 options
.username
, password
= string
.split(options
.username
, '%')
140 username
= options
.username
144 c
= rpcclient(binding
, domain
, username
, password
)
147 c
.onecmd(options
.command
)
153 except KeyboardInterrupt:
154 print 'KeyboardInterrupt'