-
Notifications
You must be signed in to change notification settings - Fork 176
/
bucket-disclose.sh
executable file
·215 lines (182 loc) · 8.54 KB
/
bucket-disclose.sh
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
#!/bin/bash
# Using error messages to decloak an S3 bucket. Uses soap, unicode, post, multipart,
# streaming and index listing as ways of figure it out. You do need a valid aws-key
# (never the secret) to properly get the error messages
#### FORKED FROM https://gist.github.com/bl4de/35d5831e6eb4a29fe40626efbea7818e
#### Written by Frans Rosén (twitter.com/fransrosen)
_debug="$2" #turn on debug
_timeout="20"
#you need a valid key, since the errors happens after it validates that the key exist. we do not need the secret key, only access key
_aws_key=$AWS_KEY
H_ACCEPT="accept-language: en-US,en;q=0.9,sv;q=0.8,zh-TW;q=0.7,zh;q=0.6,fi;q=0.5,it;q=0.4,de;q=0.3"
H_AGENT="user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
_log_file="./bucket-disclose.log"
_sed_bin=$(which gsed)
if [ "${_sed_bin}" == "" ]; then
_sed_bin=$(which sed)
fi
_timeout_bin=$(which gtimeout)
if [ "${_timeout_bin}" == "" ]; then
_timeout_bin=$(which timeout)
fi
r=$(echo "$1" | ${_sed_bin} -E 's/^[a-z]+:\/\///') #raw but without protocol
d=$(echo "$r" | cut -d "/" -f 1) #domain
pr=$(echo "$1" | ${_sed_bin} -E 's/(^[a-z]+:\/\/)?.*/\1/') #protocol, could be empty, use http if so
if [ "$pr" == "" ]; then pr="http://"; fi
pd="${pr}${d}" #protocol and domain always and only
u=$(echo "$1") #full url
p="${pr}$(echo "${r}" | rev | cut -d "/" -f 2- | rev)" # path without trailing slash (will be weird when bucket is in dir, but will be found later on)
result() {
echo "$1" >> $_log_file
[ "$1" != "" ] && echo "$1" && exit 0;
}
debug() {
echo "* $1" >> $_log_file
[ "$_debug" != "" ] && echo "* $1"
}
is_bucket_already() {
_no_protocol=$(echo "$1" | ${_sed_bin} -E 's/^[a-z]+:\/\///') # function is reused after, make sure we clean up proto
_comp=$(echo "${_no_protocol}" | grep -vE "^[^:]*s3[^:]+amazonaws.*") #check if domain contains s3.*amazonaws.com
if [ "${_comp}" == "" ]; then
result $(echo "${_no_protocol}" | grep -E '^s3.*amazon[^/]+\/.*' | cut -d "/" -f 2)
result $(echo "${_no_protocol}" | grep -E '^[^:]+.s3.*amazon.*' | ${_sed_bin} -E 's/^([^\:]+).s3[^:]+amazon.*$/\1/');
fi
}
is_bucket() {
debug "checking if bucket: ${1}..."
_response=$(${_timeout_bin} ${_timeout} curl -H "${H_ACCEPT}" -H "${H_AGENT}" "${1}" -sL -D- --insecure --max-time 5 | \
grep -iE "^x-amz-error-code|^server: amazons3|<Code>AccessDenied</Code>|Code: AccessDenied|NoSuchKey")
}
is_bucket_soap() {
debug "checking if bucket using soap... ${1}/soap"
_response=$(${_timeout_bin} ${_timeout} curl -H "${H_ACCEPT}" -H "${H_AGENT}" "${1}/soap" -X POST -sL -D- --insecure --max-time 5 | \
grep ">Missing SOAPAction header<")
}
is_bucket_invalid_char() {
debug "checking if bucket using invalid char: ${1}/%83..."
_response=$(${_timeout_bin} ${_timeout} curl -H "${H_ACCEPT}" -H "${H_AGENT}" "${1}/%83" -sL -D- --insecure --max-time 5 | \
grep -iE "<Code>InvalidURI</Code>|Code: InvalidURI|NoSuchKey")
}
is_bucket_invalid_method() {
debug "checking if bucket using invalid method... ${1}/xyz"
_response=$(${_timeout_bin} ${_timeout} curl -H "${H_ACCEPT}" -H "${H_AGENT}" "${1}/xyz" -X POSTX -sL -D- --insecure --max-time 5 | \
grep "<Code>BadRequest</Code><Message>")
if [ "${_response}" != "" ]; then
_response="${1}/xyz"
else
_response=""
fi
}
is_bucket_listable() {
debug "checking if bucket is listable... ${1}/?abc"
_response=$(${_timeout_bin} ${_timeout} curl -H "${H_ACCEPT}" -H "${H_AGENT}" -sL "${1}/?abc" --insecure --max-time 5 | \
grep "<ListBucketResult" -A 1 | grep -Eo "<Name>[^<]+</Name>" | cut -d ">" -f 2 | cut -d "<" -f 1)
}
is_bucket_domain() {
debug "checking if domain exists as bucket: http://${1}.s3.amazonaws.com..."
_response=$(${_timeout_bin} ${_timeout} curl -H "${H_ACCEPT}" -H "${H_AGENT}" "http://${1}.s3.amazonaws.com" -sL --insecure --max-time 5 | grep -E "ListBucketResult|AccessDenied|PermanentRedirect")
}
is_bucket_redirectable() {
debug "checking if url redirects to bucket... ${1}/xyzadad"
_response=$(${_timeout_bin} ${_timeout} curl -H "${H_ACCEPT}" -H "${H_AGENT}" -sL "$1/xyzadad" -D- | grep -i "^location:" | grep amazonaws | cut -d " " -f 2)
_response=$(is_bucket_already "${_response}") #remove s3-domain stuff
}
is_redirecting() {
debug "check if URL redirects to other domain... ${1}"
_response=$(curl -Ls -o /dev/null -H "${H_ACCEPT}" -H "${H_AGENT}" -w %{url_effective} "${1}")
_od=$(echo "${1}" | ${_sed_bin} -E 's/^[a-z]+:\/\///' | cut -d "/" -f 1)
_rd=$(echo "${_response}" | ${_sed_bin} -E 's/^[a-z]+:\/\///' | cut -d "/" -f 1)
if [ "${_od}" == "${_rd}" ]; then
_response=""
fi
}
bucket_unicode_sign_error() {
debug "checking unicode-error... ${1}/åäö"
_response=$(${_timeout_bin} ${_timeout} curl -H "${H_ACCEPT}" -H "${H_AGENT}" -sL "${1}/åäö" --insecure --max-time 5 | grep "host:" | grep "amazonaws.com" | cut -d ":" -f 2)
_response=$(is_bucket_already "${_response}") #remove s3-domain stuff
}
bucket_post_sign_error() {
debug "checking post-error... ${1}/okok?456"
_response=$(${_timeout_bin} ${_timeout} curl -H "${H_ACCEPT}" -H "${H_AGENT}" "${1}/okok?456" -H "Authorization: AWS ${_aws_key}:x" -H \
"Date: $(date -u +%a,\ %d\ %b\ %Y\ %H:%M:%S\ GMT)" -sL | \
grep "</StringToSign>" | \
cut -d "<" -f 1 | cut -d "/" -f 2)
}
bucket_get_sign_error() {
debug "checking get-error... ${1}/okok?AWSAccessKey..."
_response=$(${_timeout_bin} ${_timeout} curl -X GET -H "${H_ACCEPT}" -H "${H_AGENT}" "${1}/okok?AWSAccessKeyId=${_aws_key}&Expires=$(expr $(date +%s) + 1000)&Signature=x" -H \
"Date: $(date -u +%a,\ %d\ %b\ %Y\ %H:%M:%S\ GMT)" -sL | \
grep "</StringToSign>" | \
cut -d "<" -f 1 | cut -d "/" -f 2)
}
bucket_put_sign_error() {
debug "checking put-error... ${1}/okok?AWSAccessKey..."
_response=$(${_timeout_bin} ${_timeout} curl -X PUT -H "${H_ACCEPT}" -H "${H_AGENT}" "${1}/okok?AWSAccessKeyId=${_aws_key}&Expires=$(expr $(date +%s) + 1000)&Signature=x" -H \
"Date: $(date -u +%a,\ %d\ %b\ %Y\ %H:%M:%S\ GMT)" -sL | \
grep "</StringToSign>" | \
cut -d "<" -f 1 | cut -d "/" -f 2)
}
bucket_multipart_sign_error() {
debug "checking multipart-error... ${1}/?789"
_response=$(${_timeout_bin} ${_timeout} curl -H "${H_ACCEPT}" -H "${H_AGENT}" --form "d=x" -X POST "${1}/?789" \
-H "Authorization: AWS ${_aws_key}:x" -H \
"Date: $(date -u +%a,\ %d\ %b\ %Y\ %H:%M:%S\ GMT)" -sL | \
grep "</StringToSign>" | cut -d "<" -f 1 | cut -d "/" -f 2)
}
bucket_streaming_sign_error() {
debug "checking streaming sign-error... ${1}/ioioio?987"
_response=$(${_timeout_bin} ${_timeout} curl -H "${H_ACCEPT}" -H "${H_AGENT}" "${1}/ioio?987" -sL -H \
"Authorization: AWS4-HMAC-SHA256 Credential=${_aws_key}/20180101/ap-south-1/s3/aws4_request,SignedHeaders=date;host;x-amz-acl;x-amz-content-sha256;x-amz-date,Signature=x" -H \
"Date: $(date -u +%a,\ %d\ %b\ %Y\ %H:%M:%S\ GMT)" -H \
"x-amz-content-sha256: STREAMING-AWS4-HMAC-SHA256-PAYLOAD" | grep "<CanonicalRequest>" -A 1 | tail -n 1 | cut -d "/" -f 2)
}
change_bucket_location() {
debug "changing location of bucket... ${1}"
p="$1"
_response="1"
}
> $_log_file
debug "start checking... ${u}"
is_bucket_already "${r}"
is_bucket "${pd}"
[ "${_response}" == "" ] && is_bucket_soap "${pd}"
_is_root_bucket="${_response}"
_bucket_location=""
if [ "${_is_root_bucket}" != "" ]; then
_bucket_location="${pd}"
else
is_bucket_soap "${p}"
[ "${_response}" == "" ] && is_bucket "${p}"
[ "${_response}" == "" ] && is_bucket "${p}/xyzabc" && change_bucket_location "${p}/xyzabc"
[ "${_response}" == "" ] && is_bucket_invalid_method "${p}"
[ "${_response}" == "" ] && is_bucket_invalid_char "${p}"
if [ "${_response}" != "" ]; then
_bucket_location="${p}"
fi
fi
if [ "${_bucket_location}" != "" ]; then
debug "bucket-location: ${_bucket_location}"
bucket_unicode_sign_error "${_bucket_location}"
result "${_response}"
bucket_streaming_sign_error "${_bucket_location}"
result "${_response}"
bucket_get_sign_error "${_bucket_location}"
result "${_response}"
bucket_post_sign_error "${_bucket_location}"
result "${_response}"
bucket_put_sign_error "${_bucket_location}"
result "${_response}"
bucket_multipart_sign_error "${_bucket_location}" #this one can bug out with fastly, since host is being different when POST
result "${_response}"
is_bucket_listable "${_bucket_location}"
result "${_response}"
is_bucket_redirectable "${_bucket_location}"
result "${_response}"
fi
#if [ "${_is_root_bucket}" ]; then
is_bucket_domain "${d}"
[ "${_response}" != "" ] && result "${d}"
#fi
is_redirecting "${pr}${r}"
[ "${_response}" != "" ] && bash $0 "${_response}" "$2" || debug "location is not a bucket: ${pr}${r} (${_bucket_location})" && exit 1
exit 0