-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiam-enumerate.sh
310 lines (247 loc) · 9.71 KB
/
iam-enumerate.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
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
#!/bin/bash
RED='\e[41;97m'
WHITE='\033[0;37m'
RESET='\033[0m'
YELLOW='\033[0;93m'
TEST='\e[45m'
YEL='\e[38;5;226m'
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]
then
echo "Usage:"
echo "-h for help"
echo -e "Eg: \e[44;97m./iam-enumerate.sh${RESET}"
exit 1
fi
if [[ $(ls ~/.aws/config 2>&1) == *"No such file"* ]] && [[ $(ls ~/.aws/credentials 2>&1) == *"No such file"* ]];
then
echo -e "\nPlease configure your AWS CLI by typing in the following in your terminal: \e[40;38;5;82maws configure${RESET}\n"
exit 1
fi
echo -e "\n"
echo -e "${YEL} _____ __ __ ______ _ "
echo -e "${YEL} |_ _| /\ | \/ | | ____| | "
echo -e "${YEL} | | / \ | \ / |______| |__ | | ______ _____ "
echo -e "${YEL} | | / /\ \ | |\/| |______| __| | |/ _ \ \ /\ / / __| "
echo -e "${YEL} _| |_ / ____ \| | | | | | | | (_| |\ V V /\__ \ "
echo -e "${YEL} |_____/_/ \_\_| |_| |_| |_|\__,_| \_/\_/ |___/ "
echo -e "\n"
echo -e " \e[44;97mBY NIKHIL SAHOO and SHIVRAM AMIRTHA${RESET} "
echo -e "\n"
echo -e " \e[38;5;159mAWS IAM Security Toolkit : Enumeration | Privilege Escalation | CIS Benchmarks${RESET} "
echo -e "\n\n${YEL}==================================================================================${RESET}\n"
echo -e " ${RED}ENUMERATION${RESET}"
echo -e "\n\n"
echo -e "\n\n\e[40;38;5;82m>>Enumeration<<${RESET}\n\n"
temp=0
abc=""
abcd=""
b=$(aws iam get-user 2>&1)
echo -e "\n\e[30;48;5;82mGet User:${RESET}\n"
if [[ $b == *"An error"* ]]; then
b=$(aws sts get-caller-identity 2>&1 | jq ".Arn" | cut -d "\"" -f2 | cut -d ":" -f6 | cut -d "/" -f2 | sed -r '/^\s*$/d')
if [[ $b == *"An error"* ]]; then
echo -e "Failed\n"
(( temp += 1 ))
else
CurrentUser=$(echo -e "$b\n")
echo $CurrentUser
fi
else
CurrentUser=$(echo -e "$b\n" | jq ".User.UserName" 2>&1 | cut -d "\"" -f2 | sed -r '/^\s*$/d')
echo $CurrentUser
fi
a=$(aws iam list-users 2>&1)
echo -e "\n\e[30;48;5;82mList Users:${RESET}\n"
if [[ $a == *"An error"* ]]; then
echo -e "Failed\n"
(( temp += 1 ))
if [[ $temp == 2 ]]; then
exit 1
fi
else
echo -e "$a" | jq ".Users[].UserName" 2>&1 | cut -d "\"" -f2
fi
echo -e "\nEnter the User from above to proceed with:\n"
read awsuser
c=$(aws iam list-groups-for-user --user-name $awsuser 2>&1 | jq ".Groups[].GroupName" 2>&1 | cut -d "\"" -f2)
echo -e "\n\n\e[30;48;5;82mList Groups:${RESET}\n"
if [[ $c == *"error"* ]] || [[ -z $c ]]; then
echo -e "Failed\n"
else
echo "$c"
while IFS= read -r bkg ;
do
i=$(aws iam list-group-policies --group-name $bkg 2>&1 | jq ".PolicyNames[]" 2>&1 | cut -d "\"" -f2)
echo -e "\n\e[1;4mGroup Name: $bkg${RESET}"
echo -e "\n\e[30;48;5;82mList Group Policies:${RESET}\n"
if [[ $i == *"error"* ]] || [[ -z $i ]]; then
echo -e "Failed\n"
else
echo "$i"
while IFS= read -r bk ;
do
j2=$(aws iam get-group-policy --group-name $bkg --policy-name $bk 2>&1)
j=$(echo "$j2" | jq ".PolicyDocument.Statement[].Effect" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
echo -e "\n\n\e[30;48;5;82mGet Group Policies: $bk${RESET}\n"
if [[ $j == *"error"* ]] || [[ -z $j ]]; then
echo -e "Failed\n"
else
cooo=0
while IFS= read -r gxyz ;
do
if [[ $gxyz == *"Allow"* ]]; then
eii=$(echo "$j2" | jq ".PolicyDocument.Statement[$cooo].Action" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
eii2=$(echo "$j2" | jq ".PolicyDocument.Statement[$cooo].Resource" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
echo -e "\nAction:\n"
echo "$eii"
echo -e "\nResource:\n"
echo "$eii2"
abc=$(echo -e "$abc\n$eii")
elif [[ $gxyz == *"Deny"* ]]; then
exx=$(echo "$j2" | jq ".PolicyDocument.Statement[$cooo].Action" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
abcd=$(echo -e "$abcd\n$exx")
fi
(( cooo += 1 ))
done <<< "$j"
fi
done <<< "$i"
fi
ij=$(aws iam list-attached-group-policies --group-name $bkg 2>&1 | jq ".AttachedPolicies[].PolicyArn" 2>&1 | cut -d "\"" -f2)
echo -e "\n\n\e[30;48;5;82mList Group Attached Policies:${RESET}\n"
if [[ $ij == *"error"* ]] || [[ -z $ij ]]; then
echo -e "Failed\n"
else
echo "$ij" | cut -d "/" -f 2
priv=$(echo -e "$priv\n$ij")
while IFS= read -r ijk ;
do
gi=$(aws iam get-policy --policy-arn $ijk 2>&1 | jq ".Policy.DefaultVersionId" 2>&1 | cut -d "\"" -f2)
if [[ $gi == *"error"* ]] || [[ -z $gi ]]; then
:
else
hi2=$(aws iam get-policy-version --policy-arn $ijk --version-id $gi 2>&1)
hi=$(echo "$hi2" | jq ".PolicyVersion.Document.Statement[].Effect" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
con=0
if [[ $hi == *"error"* ]] || [[ -z $hi ]]; then
echo -e "Failed\n"
else
while IFS= read -r xyzu ;
do
if [[ $xyzu == *"Allow"* ]]; then
klop=$(echo -e "$ijk" | cut -d "/" -f 2)
echo -e "\n\e[30;48;5;82mGet Group Attached Policies: $klop${RESET}\n"
ho=$(echo "$hi2" | jq ".PolicyVersion.Document.Statement[$con].Action" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
ho2=$(echo "$hi2" | jq ".PolicyVersion.Document.Statement[$con].Resource" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
echo -e "\nAction:\n"
echo "$ho"
echo -e "\nResource:\n"
echo "$ho2"
abc=$(echo -e "$abc\n$ho")
elif [[ $xyzu == *"Deny"* ]]; then
ho=$(echo "$hi2" | jq ".PolicyVersion.Document.Statement[$con].Action" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
abcd=$(echo -e "$abcd\n$ho")
fi
(( con += 1 ))
done <<< "$hi"
fi
fi
done <<< "$ij"
fi
done <<< "$c"
fi
d=$(aws iam list-user-policies --user-name $awsuser 2>&1 | jq ".PolicyNames[]" 2>&1 | cut -d "\"" -f2)
echo -e "\n\n\e[30;48;5;82mList User Policies:${RESET}\n"
if [[ $d == *"error"* ]] || [[ -z $d ]]; then
echo -e "Failed\n"
else
echo "$d"
while IFS= read -r bu ;
do
e2=$(aws iam get-user-policy --user-name $awsuser --policy-name $bu 2>&1)
if [[ $e2 == *"error"* ]] || [[ -z $e2 ]]; then
echo -e "Failed\n"
else
e=$(echo "$e2" | jq ".PolicyDocument.Statement[].Effect" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
co=0
echo -e "\n\n\e[30;48;5;82mGet User Policies: $bu${RESET}\n"
if [[ $e == *"error"* ]] || [[ -z $e ]]; then
echo -e "Failed\n"
else
while IFS= read -r xyz ;
do
if [[ $xyz == *"Allow"* ]]; then
ei=$(echo "$e2" | jq ".PolicyDocument.Statement[$co].Action" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
ei2=$(echo "$e2" | jq ".PolicyDocument.Statement[$co].Resource" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
echo -e "\nAction:\n"
echo "$ei"
echo -e "\nResource:\n"
echo "$ei2"
abc=$(echo -e "$abc\n$ei")
elif [[ $xyz == *"Deny"* ]]; then
ex=$(echo "$e2" | jq ".PolicyDocument.Statement[$co].Action" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d' )
abcd=$(echo -e "$abcd\n$ex")
fi
(( co += 1 ))
done <<< "$e"
fi
fi
done <<< "$d"
fi
f=$(aws iam list-attached-user-policies --user-name $awsuser 2>&1 | jq ".AttachedPolicies[].PolicyArn" 2>&1 | cut -d "\"" -f2)
echo -e "\n\n\e[30;48;5;82mList User Attached Policies:${RESET}\n"
if [[ $f == *"error"* ]] || [[ -z $f ]]; then
echo -e "Failed\n"
else
echo "$f" | cut -d "/" -f 2
priv=$(echo -e "$priv\n$f")
while IFS= read -r buc ;
do
g=$(aws iam get-policy --policy-arn $buc 2>&1 | jq ".Policy.DefaultVersionId" 2>&1 | cut -d "\"" -f2)
if [[ $g == *"error"* ]] || [[ -z $g ]]; then
:
else
h2=$(aws iam get-policy-version --policy-arn $buc --version-id $g 2>&1)
h=$(echo "$h2" | jq ".PolicyVersion.Document.Statement[].Effect" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d')
if [[ $h == *"error"* ]] || [[ -z $h ]]; then
:
else
coo=0
while IFS= read -r xyy ;
do
if [[ $xyy == *"Allow"* ]]; then
ho=$(echo "$h2" | jq ".PolicyVersion.Document.Statement[$coo].Action" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d')
ho2=$(echo "$h2" | jq ".PolicyVersion.Document.Statement[$coo].Resource" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d')
klop=$(echo -e "$buc" | cut -d "/" -f 2)
echo -e "\n\n\e[30;48;5;82mAttached User Policies Permissions: $klop${RESET}\n"
echo -e "\nAction:\n"
echo "$ho"
echo -e "\nResource:\n"
echo "$ho2"
abc=$(echo -e "$abc\n$ho")
elif [[ $xyy == *"Deny"* ]]; then
hp=$(echo "$h2" | jq ".PolicyVersion.Document.Statement[$coo].Action" 2>&1 | cut -d "\"" -f2 | tr -d "[]" | sed -r '/^\s*$/d')
abcd=$(echo -e "$abcd\n$hp")
fi
(( coo += 1 ))
done <<< "$h"
fi
fi
done <<< "$f"
fi
abc=$(echo "$abc" | sort | uniq | sed '/^[[:space:]]*$/d')
echo -e "\n"
echo -e "\n\e[30;48;5;82mAllowed Permissions${RESET}\n"
if [[ -z $abc ]]; then
echo -e "None"
else
echo -e "$abc"
fi
abcd=$(echo "$abcd" | sort | uniq | sed '/^[[:space:]]*$/d')
echo -e "\n\e[30;48;5;82mDenied Permissions${RESET}\n"
if [[ -z $abcd ]]; then
echo -e "None"
else
echo -e "$abcd\n"
fi
echo -e "\n ${YEL}Enumeration Complete. Thanks !!!${RESET}"
echo -e "\n\n${YEL}==================================================================================${RESET}\n"