-
Notifications
You must be signed in to change notification settings - Fork 49
/
bucketeer.sh
executable file
·168 lines (140 loc) · 4.89 KB
/
bucketeer.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
#!/bin/bash
echo ""
echo " _ _ _____ _ _ _ "
echo " | | | | |____ | | | | | | | "
echo " | |_ ___| |__ ___ / / | |__ _ _ ___| | _____| |_ ___ ___ _ __ "
echo " | __/ _ \ '_ \ / __| \ \ | '_ \| | | |/ __| |/ / _ \ __/ _ \/ _ \ '__|"
echo " | || __/ | | | \__ \.___/ / | |_) | |_| | (__| < __/ || __/ __/ | "
echo " \__\___|_| |_| |___/\____/ |_.__/ \__,_|\___|_|\_\___|\__\___|\___|_| "
echo ""
if [[ -z $@ ]]; then
echo "Error: no targets specified."
echo ""
echo "Usage: ./bucketeer.sh <target> <target>"
exit 1
fi
AWS_CREDENTIALS_SETUP_DOCUMENTATION_URL="https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html#setup-credentials-setting"
AWS_CREDENTIALS_FILE=~/.aws/credentials
THREADS=20
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
ensure_aws_credentials() {
if [[ ! -e ${AWS_CREDENTIALS_FILE} ]]; then
echo "Warning: Required AWS credential file ${AWS_CREDENTIALS_FILE} not found."
echo ""
echo "Documentation on how to set up these credentials can be found here:"
echo ${AWS_CREDENTIALS_SETUP_DOCUMENTATION_URL}
exit 1
fi
}
ensure_dependency_installed() {
dependency=${1}
command -v ${dependency} >/dev/null
if [[ "$?" -ne 0 ]]; then
echo "Required dependency \"${dependency}\" not installed. Please install and retry."
exit 1
fi
}
test_bucket() {
bucket_name="${1}"
#echo "testing $bucket_name"
result=$(curl -m 5 -s -X 'GET' -o/dev/null -w '%{http_code}' -I http://${bucket_name}.s3.amazonaws.com/?max-keys=1)
if [[ ${result} == "403" || ${result} == "404" ]]; then
aws s3 ls s3://${bucket_name} &> aws.txt;
if [[ $? == 0 ]]; then
echo "[${GREEN}FOUND${NORMAL}] https://${bucket_name}.s3.amazonaws.com";
echo "https://${bucket_name}.s3.amazonaws.com" >> ${RESULT_FILE}
else
cat aws.txt | grep -E -q "AccessDenied|AllAccessDisabled";
if [[ $? == 0 ]]; then
echo "[${RED}FOUND${NORMAL}] https://${bucket_name}.s3.amazonaws.com";
fi
fi
elif [[ $result == "200" ]]; then
echo "[${GREEN}FOUND${NORMAL}] https://${bucket_name}.s3.amazonaws.com"
echo "https://${bucket_name}.s3.amazonaws.com" >> ${RESULT_FILE}
fi
}
check_prefix() {
local bucket_part="${1}"
# from @nahamsec's lazys3
ENVIRONMENTS=(backup dev development stage s3 staging prod production test)
# simple
test_bucket "${NAME}-${bucket_part}"
test_bucket "${NAME}.${bucket_part}"
test_bucket "${NAME}${bucket_part}"
test_bucket "${bucket_part}-${NAME}"
test_bucket "${bucket_part}.${NAME}"
test_bucket "${bucket_part}${NAME}"
for ENV in ${ENVIRONMENTS[@]}; do
test_bucket "${NAME}-${bucket_part}-${ENV}"
test_bucket "${NAME}-${bucket_part}.${ENV}"
test_bucket "${NAME}-${bucket_part}${ENV}"
test_bucket "${NAME}.${bucket_part}-${ENV}"
test_bucket "${NAME}.${bucket_part}.${ENV}"
test_bucket "${NAME}-${ENV}-${bucket_part}"
test_bucket "${NAME}-${ENV}.${bucket_part}"
test_bucket "${NAME}-${ENV}${bucket_part}"
test_bucket "${NAME}.${ENV}-${bucket_part}"
test_bucket "${NAME}.${ENV}.${bucket_part}"
test_bucket "${bucket_part}-${NAME}-${ENV}"
test_bucket "${bucket_part}-${NAME}.${ENV}"
test_bucket "${bucket_part}-${NAME}${ENV}"
test_bucket "${bucket_part}.${NAME}-${ENV}"
test_bucket "${bucket_part}.${NAME}.${ENV}"
test_bucket "${bucket_part}-${ENV}-${NAME}"
test_bucket "${bucket_part}-${ENV}.${NAME}"
test_bucket "${bucket_part}-${ENV}${NAME}"
test_bucket "${bucket_part}.${ENV}-${NAME}"
test_bucket "${bucket_part}.${ENV}.${NAME}"
test_bucket "${ENV}-${NAME}-${bucket_part}"
test_bucket "${ENV}-${NAME}.${bucket_part}"
test_bucket "${ENV}-${NAME}${bucket_part}"
test_bucket "${ENV}.${NAME}-${bucket_part}"
test_bucket "${ENV}.${NAME}.${bucket_part}"
test_bucket "${ENV}-${bucket_part}-${NAME}"
test_bucket "${ENV}-${bucket_part}.${NAME}"
test_bucket "${ENV}-${bucket_part}${NAME}"
test_bucket "${ENV}.${bucket_part}-${NAME}"
test_bucket "${ENV}.${bucket_part}.${NAME}"
done
}
print_results() {
if [[ -s ${RESULT_FILE} ]]; then
NR_OF_RESULTS=$(wc -l ${RESULT_FILE} | awk '{print $1}')
else
NR_OF_RESULTS=0
fi
echo ""
echo "Scanning done. Found ${NR_OF_RESULTS} results."
}
open_sem() {
mkfifo pipe-$$
exec 3<>pipe-$$
rm -f pipe-$$
local i=$1
for ((; i > 0; i--)); do
printf %s 000 >&3
done
}
run_with_lock() {
local x
read -u 3 -n 3 x && ((0==x)) || exit $x
(
"$@"
printf '%.3d' $? >&3
)&
}
ensure_dependency_installed aws
ensure_aws_credentials
for NAME in ${@}
do
RESULT_FILE="results-${NAME}-$(date +%Y-%m-%d_%H:%M).txt"
test_bucket ${NAME}
open_sem ${THREADS}
while read line; do
run_with_lock check_prefix ${line}
done < ./common_bucket_prefixes.txt
print_results
done