-
Notifications
You must be signed in to change notification settings - Fork 11
/
check_bucket.py
executable file
·59 lines (52 loc) · 2.19 KB
/
check_bucket.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
#!/usr/bin/env python
import random, requests, time, re
from constants import base_url
no_bucket_responses = [
"NoSuchBucket",
"InvalidBucketName",
]
denied_responses = [
"AccessDenied",
"AllAccessDisabled",
]
def check_s3_bucket(bucket_name, redirect=False):
bucket_result = {
"name":bucket_name,
"url":"{base_url}{bucket_name}".format(base_url=base_url, bucket_name=bucket_name),
"exists":False,
"public":None,
"empty":None,
"error":False,
"redirected":False,
}
if redirect:
bucket_result["redirected"] = True
bucket_result["url"] = "https://{bucket_name}.s3.amazonaws.com".format(bucket_name=bucket_name)
try:
request = requests.get(bucket_result["url"], verify=False)
except Exception as e:
bucket_result["error"] = e
return bucket_result
for no_bucket_response in no_bucket_responses:
if "<Code>{message}</Code>".format(message=no_bucket_response) in request.text:
bucket_result["error"] = no_bucket_response
return bucket_result
for denied_response in denied_responses:
if "<Code>{message}</Code>".format(message=denied_response) in request.text:
bucket_result["exists"] = True
bucket_result["public"] = False
bucket_result["error"] = denied_response
return bucket_result
#If a redirect is seen, go to it
if "<Endpoint>" in request.text:
return check_s3_bucket(bucket_name=re.search("<Endpoint>(.+?)</Endpoint>", request.text).group(1).replace(".s3.amazonaws.com",""), redirect=True)
#At this point the bucket exists, just seeing if it is empty
else:
bucket_result["exists"] = True
bucket_result["public"] = True
if "<Key>" in request.text:
bucket_result["empty"] = False
return bucket_result
else:
bucket_result["empty"] = True
return bucket_result