-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathshell_command_execution.py
More file actions
361 lines (249 loc) · 9.7 KB
/
Copy pathshell_command_execution.py
File metadata and controls
361 lines (249 loc) · 9.7 KB
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
import os
import shlex
import subprocess
import asyncio
from .execution_base import Exploit, AsyncEventLoop
from typing import List
class ShellExploit(Exploit):
category_name = "Shell Command Execution"
def generate_payload(shell_command: str) -> str:
return shell_command
class SubprocessCallExploit(ShellExploit):
vulnerable_function = subprocess.call
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
subprocess.call(args)
class SubprocessRunExploit(ShellExploit):
vulnerable_function = subprocess.run
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
subprocess.run(args)
class SubprocessCheckCallExploit(ShellExploit):
vulnerable_function = subprocess.check_call
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
subprocess.check_call(args)
class SubprocessCheckOutputExploit(ShellExploit):
vulnerable_function = subprocess.check_output
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
subprocess.check_output(args)
class SubprocessPopenExploit(ShellExploit):
vulnerable_function = subprocess.Popen
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
subprocess.Popen(args)
class WaitingProtocol(asyncio.SubprocessProtocol):
def __init__(self, exit_future):
self.exit_future = exit_future
def process_exited(self):
self.exit_future.set_result(True)
class AsyncioSubprocessExecExploit(ShellExploit):
vulnerable_function = asyncio.events.AbstractEventLoop.subprocess_exec
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
with AsyncEventLoop() as loop:
exit_future = asyncio.Future(loop=loop)
transport, _ = loop.run_until_complete(loop.subprocess_exec(lambda: WaitingProtocol(exit_future), *args))
loop.run_until_complete(exit_future)
transport.close()
class SubprocessGetStatusOutputExploit(ShellExploit):
vulnerable_function = subprocess.getstatusoutput
def run_payload(shell_command: str) -> None:
subprocess.getstatusoutput(shell_command)
class SubprocessGetOutputExploit(ShellExploit):
vulnerable_function = subprocess.getoutput
def run_payload(shell_command: str) -> None:
subprocess.getoutput(shell_command)
class AsyncioCreateSubprocessShellExploit(ShellExploit):
vulnerable_function = asyncio.subprocess.create_subprocess_shell
def run_payload(shell_command: str) -> None:
with AsyncEventLoop() as loop:
proc = loop.run_until_complete(asyncio.subprocess.create_subprocess_shell(shell_command))
loop.run_until_complete(proc.wait())
class OSPopenExploit(ShellExploit):
vulnerable_function = os.popen
def run_payload(shell_command: str) -> None:
proc = os.popen(shell_command)
proc.close()
class OSSystemExploit(ShellExploit):
vulnerable_function = os.system
def run_payload(shell_command: str) -> None:
os.system(shell_command)
class AsyncioSubprocessShellExploit(ShellExploit):
vulnerable_function = asyncio.events.AbstractEventLoop.subprocess_shell
def run_payload(shell_command: str) -> None:
with AsyncEventLoop() as loop:
exit_future = asyncio.Future(loop=loop)
transport, _ = loop.run_until_complete(loop.subprocess_shell(lambda: WaitingProtocol(exit_future), shell_command))
loop.run_until_complete(exit_future)
transport.close()
class OSExeclpExploit(ShellExploit):
vulnerable_function = os.execlp
def run_payload(shell_command: str) -> None:
pid = os.fork()
if pid == 0:
# We're the child
args = shlex.split(shell_command)
program = args[0]
os.execlp(program, *args)
else:
# We're the parent
os.waitpid(pid, 0)
class OSExeclpeExploit(ShellExploit):
vulnerable_function = os.execlpe
def run_payload(shell_command: str) -> None:
pid = os.fork()
if pid == 0:
# We're the child
args = shlex.split(shell_command)
program = args[0]
os.execlpe(program, *args, os.environ)
else:
# We're the parent
os.waitpid(pid, 0)
class OSExecvpExploit(ShellExploit):
vulnerable_function = os.execvp
def run_payload(shell_command: str) -> None:
pid = os.fork()
if pid == 0:
# We're the child
args = shlex.split(shell_command)
program = args[0]
os.execvp(program, args)
else:
# We're the parent
os.waitpid(pid, 0)
class OSExecvpeExploit(ShellExploit):
vulnerable_function = os.execvpe
def run_payload(shell_command: str) -> None:
pid = os.fork()
if pid == 0:
# We're the child
args = shlex.split(shell_command)
program = args[0]
os.execvpe(program, args, os.environ)
else:
# We're the parent
os.waitpid(pid, 0)
class OSSpawnlpExploit(ShellExploit):
vulnerable_function = os.spawnlp
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
program = args[0]
os.spawnlp(os.P_WAIT, program, *args)
class OSSpawnlpeExploit(ShellExploit):
vulnerable_function = os.spawnlpe
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
program = args[0]
os.spawnlpe(os.P_WAIT, program, *args, os.environ)
class OSSpawnvpExploit(ShellExploit):
vulnerable_function = os.spawnvp
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
program = args[0]
os.spawnvp(os.P_WAIT, program, args)
class OSSpawnvpeExploit(ShellExploit):
vulnerable_function = os.spawnvpe
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
program = args[0]
os.spawnvpe(os.P_WAIT, program, args, os.environ)
class AsyncioCreateSubprocessExecExploit(ShellExploit):
vulnerable_function = asyncio.subprocess.create_subprocess_exec
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
program = args[0]
with AsyncEventLoop() as loop:
proc = loop.run_until_complete(asyncio.subprocess.create_subprocess_exec(program, *args))
loop.run_until_complete(proc.communicate())
class ShellPathExploit(ShellExploit):
def generate_payload(shell_command: str) -> str:
return shlex.join(["/bin/bash", "-c", shell_command])
class OSPosixSpawnpExploit(ShellPathExploit):
vulnerable_function = os.posix_spawnp
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
path = args[0]
pid = os.posix_spawnp(path, args, os.environ)
os.waitpid(pid, 0)
class OSPosixSpawnExploit(ShellPathExploit):
vulnerable_function = os.posix_spawn
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
path = args[0]
pid = os.posix_spawn(path, args, os.environ)
os.waitpid(pid, 0)
class OSExeclExploit(ShellPathExploit):
vulnerable_function = os.execl
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
path = args[0]
pid = os.fork()
if pid == 0:
# We're the child
os.execl(path, *args)
else:
# We're the parent
os.waitpid(pid, 0)
class OSExecleExploit(ShellPathExploit):
vulnerable_function = os.execle
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
path = args[0]
pid = os.fork()
if pid == 0:
# We're the child
os.execle(path, *args, os.environ)
else:
# We're the parent
os.waitpid(pid, 0)
class OSExecvExploit(ShellPathExploit):
vulnerable_function = os.execv
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
path = args[0]
pid = os.fork()
if pid == 0:
# We're the child
os.execv(path, args)
else:
# We're the parent
os.waitpid(pid, 0)
class OSExecveExploit(ShellPathExploit):
vulnerable_function = os.execve
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
path = args[0]
pid = os.fork()
if pid == 0:
# We're the child
os.execve(path, args, os.environ)
else:
# We're the parent
os.waitpid(pid, 0)
class OSSpawnlExploit(ShellPathExploit):
vulnerable_function = os.spawnl
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
path = args[0]
os.spawnl(os.P_WAIT, path, *args)
class OSSpawnleExploit(ShellPathExploit):
vulnerable_function = os.spawnle
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
path = args[0]
os.spawnle(os.P_WAIT, path, *args, os.environ)
class OSSpawnvExploit(ShellPathExploit):
vulnerable_function = os.spawnv
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
path = args[0]
os.spawnv(os.P_WAIT, path, args)
class OSSpawnveExploit(ShellPathExploit):
vulnerable_function = os.spawnve
def run_payload(shell_command: str) -> None:
args = shlex.split(shell_command)
path = args[0]
os.spawnve(os.P_WAIT, path, args, os.environ)