-
Notifications
You must be signed in to change notification settings - Fork 10
/
BucketScanner.py
executable file
·291 lines (228 loc) · 9.7 KB
/
BucketScanner.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
#!/bin/env python
'''
--------------
BucketScanner
By @Rzepsky
--------------
======================= Notes =======================
This tool is made for legal purpose only!!! It allows you to:
- find collectable files for an anonymous/authenticated user in your buckets
- verify if an anonymous/authenticated user is allowed to upload arbitrary files to your buckets
====================== Options ======================
-l: specify a list with bucket names to check.
-w: specify a file to upload to a bucket.
-r: specify a regular expression to filter the output.
-s: look only for files bigger than 's' bytes
-m: look only for files smaller than 'm' bytes
-t: specify number of threads to use.
-o: specify an output file.
-h: prints a help message.
====================== Example ======================
$ python BucketScanner.py -l bucket_list.txt -w upload_file.txt -r '^.*\.(db|sql)' -t 50 -m 5242880 -o output.txt
The above command will:
- test all buckets from bucket_list.txt file
- test if you can upload upload_file.txt to any of the bucket included in bucket_list.txt
- provide URLs in output.txt only to files bigger than 5 MB and with .db or .sql extension
- work on 50 threads
'''
from argparse import ArgumentParser
from threading import Thread, Lock
import math
import boto3
import requests
import Queue
import re
import sys
queue = Queue.Queue()
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''
class Settings(object):
def __init__(self):
self._WRITE_TEST_ENABLED = False
self._WRITE_TEST_FILE = False
self._OUTPUT_FILE = "output.txt"
self._MIN_SIZE = 1
self._MAX_SIZE = 0
self._REGEX = ".*"
self._ANONYMOUS_MODE = False
self._DISPLAY_SIZE = True
def set_write_test(self, write_file):
self._WRITE_TEST_ENABLED = True
self._WRITE_TEST_FILE = write_file
def set_output_file(self, output_file):
self._OUTPUT_FILE = output_file
def set_minsize(self, min_SIZE):
self._MIN_SIZE = min_SIZE
def set_maxsize(self, max_SIZE):
self._MAX_SIZE = max_SIZE
def set_anonymous_mode(self):
self._ANONYMOUS_MODE = True
print('''All tests will be executed in anonymous mode:
If you want to send all requests using your AWS account please specify
AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY variables in {0} file
'''.format(sys.argv[0]))
def set_regex(self, regex):
self._REGEX = regex
def get_region(bucket_name):
try:
response = requests.get('http://' + bucket_name + '.s3.amazonaws.com/')
region = response.headers.get('x-amz-bucket-region')
return region
except Exception as e:
print "Error: couldn't connect to '{0}' bucket. Details: {1}".format(response, e)
def get_session(bucket_name, region):
try:
if settings._ANONYMOUS_MODE:
sess = boto3.session.Session(region_name=region)
else:
sess = boto3.session.Session(
region_name=region,
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
conn = sess.resource('s3')
bucket = conn.Bucket(bucket_name)
return bucket
except Exception as e:
print "Error: couldn't create a session with '{0}' bucket. Details: {1}".format(bucket_name, e)
def get_bucket(bucket_name):
region = get_region(bucket_name)
bucket = ""
if region == 'None':
print "Bucket '{0}' does not exist.".format(bucket_name.encode('utf-8'))
else:
bucket = get_session(bucket_name, region)
return bucket
def size(size_bytes):
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return "{0}{1}".format(s, size_name[i])
def is_in_limits(minsize, maxsize, content_length):
if maxsize:
return minsize < content_length < maxsize
return minsize < content_length
def bucket_reader(bucket_name):
region = get_region(bucket_name)
if region == 'None':
pass
else:
print "Testing bucket {0}...".format(bucket_name)
bucket = get_bucket(bucket_name)
results = ""
try:
for s3_object in bucket.objects.all():
try:
content_length = s3_object.get()["ContentLength"]
if is_in_limits(settings._MIN_SIZE, settings._MAX_SIZE, content_length) and \
re.match(settings._REGEX, s3_object.key):
item = "http://s3.{0}.amazonaws.com/{1}/{2}".format(
region, bucket_name,
s3_object.key.encode('utf-8'))
results += item + '\n'
print "Collectable: {0} {1}".format(item, size(content_length))
except Exception as e:
print "Error: couldn't get '{0}' object in '{1}' bucket. Details: {2}\n".format(
s3_object.key.encode('utf-8'),
bucket_name, e)
append_output(results)
except Exception as e:
print "Error: couldn't access the '{0}' bucket. Details: {1}\n".format(bucket_name, e)
def write_test(bucket_name, filename):
region = get_region(bucket_name)
if region != 'None':
try:
data = open(filename, 'rb')
bucket = get_bucket(bucket_name)
bucket.put_object(Bucket=bucket_name, Key=filename, Body=data)
print "Success: bucket '{0}' allows for uploading arbitrary files!!!".format(bucket_name.encode('utf-8'))
results = "http://s3.{0}.amazonaws.com/{1}/{2}\n".format(region,
bucket_name,
filename)
append_output(results)
except Exception as e:
print "Error: couldn't upload a {0} file to {1}. Details: {2}\n".format(filename,
bucket_name,
e)
def append_output(results):
with open(settings._OUTPUT_FILE, "a") as output:
output.write(results)
def bucket_worker():
while True:
try:
bucket = queue.get()
bucket_reader(bucket)
if settings._WRITE_TEST_ENABLED:
write_test(bucket, settings._WRITE_TEST_FILE)
except Exception as e:
print "Error: {0}\n".format(e)
queue.task_done()
def print_help():
print('''
--------------
BucketScanner
By @Rzepsky
--------------
======================= Notes =======================
This tool is made for legal purpose only!!! It allows you to:
- find collectable files for an anonymous/authenticated user in your buckets
- verify if an anonymous/authenticated user is allowed to upload arbitrary files to your buckets
====================== Options ======================
-l: specify a list with bucket names to check.
-w: specify a file to upload to a bucket.
-r: specify a regular expression to filter the output.
-s: look only for files bigger than 's' bytes
-m: look only for files smaller than 'm' bytes
-t: specify number of threads to use.
-o: specify an output file.
-h: prints a help message.
====================== Example ======================
$ python BucketScanner.py -l bucket_list.txt -w upload_file.txt -r '^.*\.(db|sql)' -t 50 -m 5242880 -o output.txt
The above command will:
- test all buckets from bucket_list.txt file
- test if you can upload upload_file.txt to any of the bucket included in bucket_list.txt
- provide URLs in output.txt only to files bigger than 5 MB and with .db or .sql extension
- work on 50 threads
''')
def closing_words():
print "That's all folks! All collectable files can be found in {0}.".format(settings._OUTPUT_FILE)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-l", dest="bucket_list", required=True, help="a list with bucket names.")
parser.add_argument("-w", dest="write", type=str, required=False,
default="", help="file to execute upload test.")
parser.add_argument("-r", dest="regex", required=False,
default='', help="regular expression filter")
parser.add_argument("-s", dest="min", type=int, required=False, default=1, help="minimun size.")
parser.add_argument("-m", dest="max", type=int, required=False, default=0, help="maximum size.")
parser.add_argument("-t", dest="threads", type=int, required=False,
default=10, help="thread count.")
parser.add_argument("-o", dest="output", type=str, required=False,
default="output.txt", help="output file.")
if len(sys.argv) == 1:
print_help()
sys.exit()
settings = Settings()
arguments = parser.parse_args()
if arguments.output is not "output.txt":
settings.set_output_file(arguments.output)
if arguments.write:
settings.set_write_test(arguments.write)
if arguments.regex:
settings.set_regex(arguments.regex)
if arguments.min > 1:
settings.set_minsize(arguments.min)
if arguments.max > 1:
settings.set_maxsize(arguments.max)
if not (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY):
settings.set_anonymous_mode()
arguments = parser.parse_args()
for i in range(0, arguments.threads):
t = Thread(target=bucket_worker)
t.daemon = True
t.start()
with open(arguments.bucket_list, 'r') as f:
for bucket in f:
queue.put(bucket.rstrip())
queue.join()
closing_words()