-
Notifications
You must be signed in to change notification settings - Fork 18
/
fuzzer_container.py
482 lines (405 loc) · 11.5 KB
/
fuzzer_container.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
import json
import os
import subprocess
import tempfile
import time
import typing
from os.path import basename
from zipfile import ZipFile
from nonces_storage import get_valid_nonces_for_plugin
def run_in_container(cmd: typing.List[str]) -> None:
subprocess.call(
[
"docker",
"compose",
"exec",
"-T",
"wordpress1",
]
+ cmd
)
def _run_in_container_wp_cli(cmd: typing.List[str]) -> None:
run_in_container(
[
"/fuzzer/nodebug.sh",
"php.orig",
"/wp-cli.phar",
"--allow-root",
]
+ cmd
)
def _copy_plugin_into_container(file_path: str) -> str:
file_name = basename(file_path)
new_file_path = f"/fuzzer/plugin_{file_name}"
subprocess.call(
["docker", "cp", file_path, f"{os.path.basename(os.path.dirname(__file__))}-wordpress1-1:{new_file_path}"]
)
return new_file_path
def copy_nonces_into_container(plugin_name: str) -> None:
nonces = get_valid_nonces_for_plugin(plugin_name)
with tempfile.NamedTemporaryFile() as f:
f.write("\n".join(nonces).encode("utf-8"))
f.flush()
new_file_path = "/fuzzer/valid_nonces.txt"
subprocess.call(
["docker", "cp", f.name, f"{os.path.basename(os.path.dirname(__file__))}-wordpress1-1:{new_file_path}"]
)
def run_in_container_and_get_output(cmd: typing.List[str]) -> bytes:
return subprocess.check_output(
[
"docker",
"compose",
"exec",
"-T",
"wordpress1",
]
+ cmd
)
def get_object_name_from_file(file_path: str) -> str:
with ZipFile(file_path) as zip_file:
for listed in zip_file.namelist():
if listed[-1] != "/":
continue
return listed[:-1]
return ""
def install_dependency(dependency: str) -> None:
install_plugin_from_slug(dependency)
def install_theme_from_slug(slug: str, version: str = None):
if version:
additional_install_options = ["--version=" + version]
else:
additional_install_options = []
_run_in_container_wp_cli(
[
"theme",
"install",
slug,
]
+ additional_install_options
)
def install_plugin_from_slug(slug: str, version: str = None):
if version:
additional_install_options = ["--version=" + version]
else:
additional_install_options = []
_run_in_container_wp_cli(
[
"plugin",
"install",
slug,
]
+ additional_install_options
)
def install_plugin_from_file(file_path: str) -> None:
new_file_path = _copy_plugin_into_container(file_path)
_run_in_container_wp_cli(["plugin", "install", new_file_path])
def activate_plugin(slug: str) -> None:
_run_in_container_wp_cli(
[
"plugin",
"activate",
slug,
]
)
def activate_theme(slug: str) -> None:
_run_in_container_wp_cli(
[
"theme",
"activate",
slug,
]
)
def set_webroot_ownership() -> None:
run_in_container(
[
"chown",
"-R",
"www-data:www-data",
"/var/www/html/",
]
)
def patch_wordpress(reverse: bool = False) -> None:
additional_parameters = []
if reverse:
additional_parameters = ["--reverse"]
run_in_container(["/fuzzer/patch_wordpress.sh"] + additional_parameters)
def patch_plugins_themes(reverse: bool = False) -> None:
additional_parameters = []
if reverse:
additional_parameters = ["--reverse"]
run_in_container(["/fuzzer/patch_plugins_themes.sh"] + additional_parameters)
def get_container_id() -> bytes:
return subprocess.check_output(
[
"docker",
"compose",
"ps",
"-q",
"wordpress1",
]
).strip()
def disconnect_network(container_id: bytes) -> int:
networks = subprocess.check_output(["docker", "network", "ls", "--format", "{{.Name}}"]).decode("utf-8")
network_name = f"{os.path.basename(os.path.dirname(__file__))}_network2"
if network_name not in networks.split():
raise Exception(f"Network {network_name} not found.")
return subprocess.call(["docker", "network", "disconnect", network_name, container_id])
def disconnect_dns() -> None:
run_in_container(["/fuzzer/disconnect_dns.sh"])
def visit_admin_homepage() -> None:
# This is to execute plugin hooks in case it needs to do something
# on the first admin visit
run_in_container(
[
"/fuzzer/just_visit_admin_homepage.sh",
]
)
def reinitialize_containers():
subprocess.call(
[
"docker",
"compose",
"stop",
],
stderr=subprocess.DEVNULL,
)
subprocess.call(
[
"docker",
"compose",
"rm",
"-f",
"-v",
"db1",
"wordpress1",
],
stderr=subprocess.DEVNULL,
)
subprocess.call(
[
"docker",
"compose",
"build",
]
)
subprocess.call(
[
"docker",
"compose",
"up",
"-d",
]
)
run_in_container(["/wait-for-it/wait-for-it.sh", "-h", "db1", "-p", "3306", "-t", "0"])
time.sleep(2)
run_in_container(["chown", "-R", "www-data:www-data", "/var/www/html"])
run_in_container(["/fuzzer/create_findable_files.sh"])
run_in_container(
[
"bash",
"-c",
"mysql --host=db1 -u wordpress --password=wordpress wordpress < /fuzzer/dump.sql",
]
)
run_in_container(["php.orig", "/wp-cli.phar", "--allow-root", "core", "update"])
run_in_container(["php.orig", "/wp-cli.phar", "--allow-root", "core", "update-db"])
def install_plugin_from_svn(slug: str, revision: str):
run_in_container(
[
"svn",
"co",
"https://plugins.svn.wordpress.org/" + slug + "/",
"-r",
revision,
]
)
run_in_container(["mv", slug, slug + ".tmp"])
run_in_container(["mv", slug + ".tmp/trunk", slug])
run_in_container(
[
"zip",
"-r",
slug + ".zip",
slug,
]
)
run_in_container(
[
"/fuzzer/nodebug.sh",
"php.orig",
"/wp-cli.phar",
"--allow-root",
"plugin",
"install",
slug + ".zip",
]
)
def fuzz_file_or_folder(payload_id: str, path: str):
return json.loads(
run_in_container_and_get_output(
[
"python3",
"/fuzzer/fuzz/fuzz_file_or_folder.py",
payload_id,
path,
]
)
)
def fuzz_shortcodes(payload_id: str, shortcodes_to_fuzz: str, plugin_slug: str):
return json.loads(
run_in_container_and_get_output(
[
"python3",
"/fuzzer/fuzz/fuzz_shortcodes.py",
payload_id,
shortcodes_to_fuzz,
plugin_slug,
]
)
)
def fuzz_pages(payload_id: str, user_id: int):
return json.loads(
run_in_container_and_get_output(
[
"python3",
"/fuzzer/fuzz/fuzz_pages.py",
payload_id,
str(user_id),
]
)
)
def fuzz_actions_admin(payload_id: str, actions_to_fuzz: str, plugin_slug: str):
return json.loads(
run_in_container_and_get_output(
[
"python3",
"/fuzzer/fuzz/fuzz_actions.py",
payload_id,
actions_to_fuzz,
plugin_slug,
"BECOME_ADMIN",
]
)
)
def fuzz_actions(payload_id: str, actions_to_fuzz: str, plugin_slug: str):
return json.loads(
run_in_container_and_get_output(
[
"python3",
"/fuzzer/fuzz/fuzz_actions.py",
payload_id,
actions_to_fuzz,
plugin_slug,
]
)
)
def fuzz_menu(payload_id: str, actions_to_fuzz: str, plugin_slug: str, user_id: int):
return json.loads(
run_in_container_and_get_output(
[
"python3",
"/fuzzer/fuzz/fuzz_menu.py",
payload_id,
actions_to_fuzz,
plugin_slug,
str(user_id),
]
)
)
def fuzz_rest_routes(payload_id: str, routes_to_fuzz: str, plugin_slug: str):
return json.loads(
run_in_container_and_get_output(
[
"python3",
"/fuzzer/fuzz/fuzz_rest_routes.py",
payload_id,
routes_to_fuzz,
plugin_slug,
]
)
)
def fuzz_rest_routes_admin(payload_id: str, routes_to_fuzz: str, plugin_slug: str):
return json.loads(
run_in_container_and_get_output(
[
"python3",
"/fuzzer/fuzz/fuzz_rest_routes.py",
payload_id,
routes_to_fuzz,
plugin_slug,
"BECOME_ADMIN",
]
)
)
def _grep_garlic_in_path(path: str) -> str:
# Here we assume the path comes from a trusted source. We aren't
# immune to command injection here.
return run_in_container_and_get_output(
[
"bash",
"-c",
"grep --text --context=3 -R GARLIC " + path + " || true",
]
).decode("utf-8", "ignore")
def find_payloads_in_files():
output = _grep_garlic_in_path("/var/www/html")
command_results = []
for line in output.split("\n"):
line = line.strip()
if not line:
continue
try:
path, line = tuple(line.split(":", 1))
except ValueError:
continue
if (
"functions.php" in path
or "pluggable.php" in path
or "user.php" in path
or "post.php" in path
or "option.php" in path
):
continue
command_results.append(
{
"cmd": "",
"object_name": path,
"return_code": 0,
"output": line,
}
)
return command_results
def find_payloads_in_admin():
run_in_container(["/fuzzer/download_admin.sh"])
output = _grep_garlic_in_path("/var/www/html/127.0.0.1:8001")
command_results = []
for line in output.split("\n"):
line = line.strip()
if not line:
continue
command_results.append(
{
"cmd": "",
"object_name": "ADMIN OUTPUT",
"return_code": 0,
"output": line,
}
)
return command_results
def find_payloads_in_pages():
run_in_container(["/fuzzer/download_pages.sh"])
output = _grep_garlic_in_path("/var/www/html/pages")
command_results = []
for line in output.split("\n"):
line = line.strip()
if not line:
continue
command_results.append(
{
"cmd": "",
"object_name": "PAGES OUTPUT",
"return_code": 0,
"output": line,
}
)
return command_results