-
Notifications
You must be signed in to change notification settings - Fork 582
/
waf-import-ip-set-facebook.sh
executable file
·296 lines (262 loc) · 8.87 KB
/
waf-import-ip-set-facebook.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
#!/usr/bin/env bash
### NOTE: This process currently will not work because Facebook uses CIDR blocks outside of the range acceptable to AWS WAF.
### AWS WAF supports /8, /16, /24, and /32 IP address ranges for IPv4, and /24, /32, /48, /56, /64 and /128 for IPv6.
### See: https://developers.facebook.com/docs/sharing/webmasters/crawler#access
### And: https://docs.aws.amazon.com/waf/latest/APIReference/API_IPSet.html
# This script will save a list of current Facebook crawler server IPs in the file iplist
# Then create an AWS WAF IP Set with rules to allow access to each IP
# Requires the AWS CLI and jq
# Set Variables
CONDITIONNAME="Facebook"
DEBUGMODE="0"
# Functions
# Check for command
function check_command {
type -P $1 &>/dev/null || fail "Unable to find $1, please install it and run this script again."
}
# Completed
function completed(){
echo
HorizontalRule
tput setaf 2; echo "Completed!" && tput sgr0
HorizontalRule
echo
}
# Fail
function fail(){
tput setaf 1; echo "Failure: $*" && tput sgr0
exit 1
}
function HorizontalRule(){
echo "====================================================="
}
# Verify AWS CLI Credentials are setup
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
if ! grep -q aws_access_key_id ~/.aws/config; then
if ! grep -q aws_access_key_id ~/.aws/credentials; then
fail "AWS config not found or CLI not installed. Please run \"aws configure\"."
fi
fi
# Get Facebook IPv4 IPs
function GetIPs(){
whois -h whois.radb.net -- '-i origin AS32934' | grep ^route: | rev | cut -d ' ' -f1 | rev | \
sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 > iplist
TOTALIPS=$(cat iplist | wc -l | tr -d ' ')
# TOTALIPS=$(cat iplist | wc -l | cut -d " " -f7)
echo "Total Facebook IPs: "$TOTALIPS
}
# Gets a Change Token
function ChangeToken(){
CHANGETOKEN=$(aws waf get-change-token | jq '.ChangeToken' | cut -d '"' -f2)
if [[ $DEBUGMODE = "1" ]]; then
echo "CHANGETOKEN: "$CHANGETOKEN
fi
}
# Inserts a single IP into the IP Set from the var iplist and reports status using the changetoken
function InsertIPSet(){
ChangeToken
if [[ $DEBUGMODE = "1" ]]; then
echo "IPSETID: "$IPSETID
echo "IP: "$iplist
fi
UPDATESET=$(aws waf update-ip-set --ip-set-id $IPSETID --change-token $CHANGETOKEN --updates 'Action=INSERT,IPSetDescriptor={Type=IPV4,Value="'"$iplist"'"}' 2>&1) # | jq .)
if echo $UPDATESET | grep -q error; then
fail "$UPDATESET"
else
if [[ $DEBUGMODE = "1" ]]; then
echo "UPDATESET: "$UPDATESET
fi
echo $CHANGETOKEN >> changetokenlist
CHANGETOKENSTATUS=$(aws waf get-change-token-status --change-token $CHANGETOKEN | jq '.ChangeTokenStatus' | cut -d '"' -f2)
echo $CHANGETOKENSTATUS: $iplist
fi
# echo "$UPDATESET"
}
# Deletes a single IP from the IP Set using the var iplist and reports status using the changetoken
function DeleteIPSet(){
ChangeToken
if [[ $DEBUGMODE = "1" ]]; then
echo "IPSETID: "$IPSETID
echo "IP: "$iplist
fi
UPDATESET=$(aws waf update-ip-set --ip-set-id $IPSETID --change-token $CHANGETOKEN --updates 'Action=DELETE,IPSetDescriptor={Type=IPV4,Value="'"$iplist"'"}' 2>&1) # | jq .)
if echo $UPDATESET | grep -q error; then
fail "$UPDATESET"
else
if [[ $DEBUGMODE = "1" ]]; then
echo "UPDATESET: "$UPDATESET
fi
echo $CHANGETOKEN >> changetokenlist
CHANGETOKENSTATUS=$(aws waf get-change-token-status --change-token $CHANGETOKEN | jq '.ChangeTokenStatus' | cut -d '"' -f2)
echo $CHANGETOKENSTATUS: $iplist
fi
# echo "$UPDATESET"
}
# Checks the status of a single changetoken in the changetokenlist
function CheckStatus(){
echo
HorizontalRule
echo "Checking Status:"
while read CHANGETOKEN
do
CHANGETOKENSTATUS=$(aws waf get-change-token-status --change-token $CHANGETOKEN | jq '.ChangeTokenStatus' | cut -d '"' -f2)
echo $CHANGETOKENSTATUS
done < changetokenlist
HorizontalRule
echo
}
# Exports a list of IPs in existing IP Set to the file iplist
function ExportExistingIPSet(){
IPSETID=$(aws waf list-ip-sets --limit 99 --output=json 2>&1 | jq '.IPSets | .[] | select(.Name=="'"$CONDITIONNAME"'") | .IPSetId' | cut -d '"' -f2)
GetIPSet=$(aws waf get-ip-set --ip-set-id "$IPSETID" 2>&1 | jq '.IPSet | .IPSetDescriptors | .[] | .Value' | cut -d '"' -f2)
if [[ $DEBUGMODE = "1" ]]; then
echo "IPSETID: "$IPSETID
echo "GetIPSet: "$GetIPSet
fi
if [ -z "$GetIPSet" ]; then
echo "No IPs in Set!"
fi
# Delete any existing file iplist-existing
if [ -f iplist-existing ]; then
rm iplist-existing
fi
echo "$GetIPSet" >> iplist-existing
CountIPSetIPs=$(echo "$GetIPSet" | wc -l)
# echo IPs in set $CONDITIONNAME: $CountIPSetIPs
}
function WAF(){
# Delete any existing changetokenlist
if [ -f changetokenlist ]; then
rm changetokenlist
fi
# Check for existing IP Set with the same name and create the set if none exists
if ! aws waf list-ip-sets --limit 99 --output=json | jq '.IPSets | .[] | .Name' | grep -q "$CONDITIONNAME"; then
echo
HorizontalRule
echo "Creating IP Set: "$CONDITIONNAME
ChangeToken
IPSETID=$(aws waf create-ip-set --name "$CONDITIONNAME" --change-token $CHANGETOKEN 2>&1 | jq '.IPSet | .IPSetId' | cut -d '"' -f2)
echo "IP Set ID:" "$IPSETID"
HorizontalRule
echo
echo
HorizontalRule
echo "Adding IPs to IP Set: "$CONDITIONNAME
echo "IPs to be added: "$TOTALIPS
HorizontalRule
echo
while read iplist
do
InsertIPSet
done < iplist
completed
else
tput setaf 1
echo
HorizontalRule
echo "IP Set: $CONDITIONNAME Already Exists"
IPSETID=$(aws waf list-ip-sets --limit 99 --output=json 2>&1 | jq '.IPSets | .[] | select(.Name=="'"$CONDITIONNAME"'") | .IPSetId' | cut -d '"' -f2)
echo IPSetID: "$IPSETID"
HorizontalRule
tput sgr0
echo
read -r -p "Do you want to delete the set and recreate it? (y/n) " DELETESET
if [[ $DELETESET =~ ^([yY][eE][sS]|[yY])$ ]]; then
ExportExistingIPSet
echo
if ! [ -z "$GetIPSet" ]; then
HorizontalRule
echo "Deleting IPs from IP Set: "$CONDITIONNAME
echo "IPs to be deleted: "$CountIPSetIPs
HorizontalRule
echo
while read iplist
do
DeleteIPSet
done < iplist-existing
# Verifying list is empty
ExportExistingIPSet
if [ "$CountIPSetIPs" -eq "1" ]; then
completed
else
fail "Error deleting IPs from IP Set."
fi
fi
echo
HorizontalRule
echo "Adding IPs to IP Set: "$CONDITIONNAME
echo "IPs to be added: "$TOTALIPS
HorizontalRule
echo
while read iplist
do
InsertIPSet
done < iplist
# Verifying all IPs added
ExportExistingIPSet
if [ "$CountIPSetIPs" -eq "$TOTALIPS" ]; then
completed
else
fail "Error adding IPs to IP Set."
fi
else
read -r -p "Do you want to update the set with new IPs? (y/n) " UPDATESET
if [[ $UPDATESET =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo
HorizontalRule
echo "Adding IPs to IP Set: "$CONDITIONNAME
echo "IPs to be added: "$TOTALIPS
HorizontalRule
echo
while read iplist
do
InsertIPSet
done < iplist
completed
fi
fi
fi
}
# You can't delete an IPSet if it's still used in any Rules or if it still includes any IP addresses.
# You can't delete a Rule if it's still used in any WebACL objects.
# RULENAME=$(aws waf list-rules --limit 99 --output=json 2>&1 | jq '.Rules | .[] | .Name' | grep "$CONDITIONNAME" | cut -d '"' -f2)
# RULEID=$(aws waf list-rules --limit 99 --output=json 2>&1 | jq '.Rules | .[] | select(.Name=="'"$RULENAME"'") | .RuleId' | cut -d '"' -f2)
# echo
# echo "====================================================="
# echo "Deleting Rule Name $RULENAME, Rule ID $RULEID"
# echo "====================================================="
# DELETERULE=$(aws waf delete-rule --rule-id "$RULEID" 2>&1)
# if echo $DELETERULE | grep -q error; then
# fail "$DELETERULE"
# else
# echo "$DELETERULE"
# fi
# echo
# echo "====================================================="
# echo "Deleting Set $CONDITIONNAME, Set ID $IPSETID"
# echo "====================================================="
# DELETESET=$(aws waf delete-ip-set --ip-set-id "$IPSETID" 2>&1)
# if echo $DELETESET | grep -q error; then
# fail "$DELETESET"
# else
# echo "$DELETESET"
# # echo
# # echo "====================================================="
# # echo "Creating Security Group "$GROUPNAME
# # GROUPID=$(aws ec2 create-security-group --group-name "$GROUPNAME" --description "$DESCRIPTION" --vpc-id $VPCID 2>&1)
# # echo $GROUPID
# # aws ec2 create-tags --resources $(aws ec2 describe-security-groups --output=json | jq '.SecurityGroups | .[] | select(.GroupName=="$GROUPNAME") | .GroupId' | cut -d '"' -f2) --tags Key=Name,Value="$GROUPNAME"
# # echo "====================================================="
# fi
# Check required commands
check_command "aws"
check_command "jq"
# Ensure Variables are set
if [ "$CONDITIONNAME" = "YOUR-CONDITION-NAME-HERE" ]; then
fail "Must set variables!"
exit 1
fi
GetIPs
# TOTALIPS=$(wc -l iplist | cut -d " " -f7)
WAF
# CheckStatus