-
Notifications
You must be signed in to change notification settings - Fork 8
/
commandlineInterface.py
332 lines (273 loc) · 9.73 KB
/
commandlineInterface.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#! /usr/bin/env python
#Proprietary CANVAS source code - use only under the license agreement
#specified in LICENSE.txt in your CANVAS distribution
#Copyright Immunity, Inc, 2002-2006
#http://www.immunityinc.com/CANVAS/ for more information
"""
commandlineInterface.py
Use this before running the exploit modules
"""
import getopt,sys,os,socket,time
#covers both angles
if "." not in sys.path: sys.path.append(".")
from internal.debug import debug_enabled
from internal.utils import setup_logging
setup_logging(enable_debug=debug_enabled)
from exploitutils import *
try:
from linuxNode import linuxNode
from solarisNode import solarisNode
from bsdNode import bsdNode
from osxNode import osxNode
from ScriptNode import ScriptNode
from aixNode import aixNode
except:
logging.warning("Not loading linux or solaris for CRI")
from unixShellNode import unixShellNode
from localNode import localNode
old_tech=0
if old_tech:
from solarissparcsyscallserver import solarissparc
#for Unixshell Nodes
from libs.ctelnetlib import Telnet
from shelllistener import shelllistener
from shelllistener import shellfromtelnet
from MOSDEFShellServer import MosdefShellServer
import canvasengine
class commandline_logger:
def __init__(self, *fd):
self.fileobjects = fd
def write(self, string):
for fileobject in self.fileobjects:
fileobject.write(string)
def flush( self ):
for fileobject in self.fileobjects:
fileobject.flush()
class interactiveServer:
def __init__(self):
port=""
self.type = ""
self.mode = "interactive"
self.callback = None
self.command = ""
self.uploadfiles= []
self.debug = 0
self.targets= [] #initialized from engine
self.argsDict = {}
self.client = None
self.engine = None
# used for secondary callbacks...
self.localport = None
self.localhost = None
self.infile = None
self.ipv6 = 0
self.initstring = ""
return
def loadTargets(self):
"""
Loads a list of potential targets from our engine.
"""
import canvasengine
self.targets = canvasengine.getAllListenerOptions()
return
def log(self,buf):
"""stub that prints out a string: buf"""
#print buf
self.engine.log(buf)
def setMode(self,mode):
self.mode=mode
def setType(self,type):
# see targets list for valid types
self.type = type
return
def setConnectionCallback(self,callback):
"""
register a callback - used by the engine to get statistics and stuff
"""
self.callback=callback
return
def setPort(self,port):
self.port=int(port)
return
def getPort(self):
return self.port
def doBind(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.set_timeout(None)
listenhost=""
listenport=self.port
self.log("Binding to %s:%d"%(listenhost,listenport))
s.bind((listenhost, listenport))
s.listen(5)
self.s=s
return
def doBind6(self):
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
listenhost="::"
listenport=self.port
self.log("Binding to %s:%d"%(listenhost,listenport))
s.bind((listenhost, listenport))
s.listen(5)
self.s=s
return
def getArgs(self):
"""This function will get arguments from some global places..."""
#not finished
if not self.localhost:
self.localhost=getLocalIP()
logging.info("Localhost set to %s" % self.localhost)
return
def run(self):
self.getArgs()
filename="listener-%d" % self.port
try:
os.unlink(filename)
except :
pass
# start the proxy and change the type and port to regular win32 mosdef
if self.type == canvasengine.HTTPMOSDEF:
self.engine.set_callback_interface(self.localhost)
if not self.engine.start_http_mosdef(self.port):
logging.critical("commandline http_proxy failed")
sys.exit(0)
self.port += 1
self.type = canvasengine.UNIVERSAL_MOSDEF
# start the proxy and change the type and port to regular win64 mosdef
#elif self.type == canvasengine.HTTPMOSDEF64:
# self.engine.set_callback_interface(self.localhost)
# if not self.engine.start_http_mosdef(self.port):
# print "XXX: commandline http_proxy failed ..."
# sys.exit(0)
# self.port += 1
# self.type = canvasengine.WIN64MOSDEF_INTEL
# start the proxy and change the type and port to regular win64 mosdef
#elif self.type == canvasengine.HTTPMOSDEF64_SSL:
# self.engine.set_callback_interface(self.localhost)
# if not self.engine.start_http_mosdef(self.port, ssl=True):
# print "XXX: commandline http_proxy failed ..."
# sys.exit(0)
# self.port += 1
# self.type = canvasengine.WIN64MOSDEF_INTEL
# start the proxy and change the type and port to regular win32 mosdef
elif self.type == canvasengine.HTTPMOSDEF_SSL:
self.engine.set_callback_interface(self.localhost)
if not self.engine.start_http_mosdef(self.port, ssl=True):
logging.critical("commandline http_proxy failed")
sys.exit(0)
self.port += 1
self.type = canvasengine.UNIVERSAL_MOSDEF
# start the dns proxy and change the type and port to regular win32 mosdef
elif self.type == canvasengine.DNSMOSDEF:
self.engine.set_callback_interface(self.localhost)
if not self.engine.start_dns_mosdef(self.localhost, self.port):
logging.critical("commandline dns_proxy failed")
sys.exit(0)
self.type = canvasengine.WIN32MOSDEF
if self.ipv6:
logging.info("Switching MOSDEF into IPv6 mode")
self.doBind6()
else:
self.doBind()
while 1:
#Here we write our listener-5555 file
#this tells other commandline exploits that we succeeded.
#AKA. If the file exists, we get a callback.
devlog("commandline","[!] Listening on port %d ..."%self.port)
try:
self.s.set_timeout(None)
except:
devlog("commandline", "[!] likely a socket wrapper object, set_timeout not supported ...")
conn, addr = self.s.accept()
logging.info("Connected to by %s" % str(addr))
f=file(filename, "w")
self.client=addr
self.handleConnection(conn)
return
def handleConnection(self,connection):
try:
connection.set_timeout(None)
except:
logging.warning("likely an ipv6 socket, set_timeout not supported")
server = self.engine.new_node_connection(None, connection, mosdef_type=self.type)
if self.mode=="interactive":
logging.info("Letting user interact with server")
if server:
server.interact()
else:
logging.info("No server - exiting this shell")
if server and hasattr(server, "disconnect"):
server.disconnect()
return
def printTargets(targets):
for a in range(0, len(targets)):
logging.info("%d) %s" % (a, targets[a]))
def usage(targets):
print """
Command Line Interface Version 1.0, Immunity, Inc.
usage: commandlineInterface.py -p port -v <ver number> [-i initstring] [-l localip (for HTTP)]
initstring values:
fromcreatethread (used for MSRPC attacks, for example)
"""
printTargets(targets)
#this stuff happens.
if __name__ == '__main__':
##Do pre run sanity checks
canvasengine.license_check()
logging.info("Running command line interface v 1.0")
app = interactiveServer()
import canvasengine
engine=canvasengine.canvasengine(None)
# sys.stdout = commandline_logger( sys.stdout, engine.logfile )
app.engine=engine #set the app's engine here.
app.loadTargets()
app.setType("WIN32MOSDEF")
port=""
try:
(opts,args)=getopt.getopt(sys.argv[1:],"dp:c:u:v:i:l:df:X")
except getopt.GetoptError:
#print help
usage(app.targets)
sys.exit(2)
i=0
for o,a in opts:
if o in ["-f"]:
app.infile=a
if o in ["-p"]:
i+=1
port=a
app.setPort(a)
if o in ["-c"]:
app.command=a
app.setMode("Run one command")
if o in ["-d"]:
app.localport=int(a)
if o in ["-u"]:
app.uploadfiles.append(a)
if o in ["-i"]:
a=a.replace("formcreatethread","fromcreatethread")
app.argsDict[a]=1
if o in ["-l"]:
app.localhost=a
# XXX: switches commandline mosdef into IPv6 mode ;)
if o in ["-X"]:
app.ipv6 = 1
if o in ["-v"]:
a=int(a)
if a < len(app.targets) :
i+=1
app.setType(app.targets[a])
else:
logging.error("Unknown target")
if i!=2 :
usage(app.targets)
sys.exit(0)
try:
engine.localsniffer.shutdown() #don't need this
except:
pass
try:
app.run()
except timeoutsocket.Timeout:
logging.error("Failed to run commandline (socket timed out - DEP?)")