forked from johnmccuk/cloudflare-ip-security-group-update
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf-security-group-update.py
429 lines (345 loc) · 16.1 KB
/
cf-security-group-update.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import os
import boto3
import json
import requests
#from botocore.vendored import requests
from ipaddress import ip_network, ip_address
def get_cloudflare_ip_list():
""" Call the CloudFlare API and return a list of IPs """
response = requests.get('https://api.cloudflare.com/client/v4/ips')
temp = response.json()
if 'result' in temp:
ipv4_list = temp['result']['ipv4_cidrs']
new_ipv4s = []
for ip in ipv4_list:
mask = int(ip.split(u'/')[1])
if (mask == 8 or (mask >= 16 and mask <= 32)):
new_ipv4s.append(ip)
continue
for new_ip in ip_network(ip).subnets(new_prefix=16):
new_ipv4s.append(str(new_ip))
temp['result']['ipv4_cidrs_workaround'] = new_ipv4s
ipv6_list = temp['result']['ipv6_cidrs']
new_ipv6s = []
for ip in ipv6_list:
mask = int(ip.split(u'/')[1])
if (mask in [24, 32, 48, 56, 64, 128]):
new_ipv6s.append(ip)
continue
new_prefix = 32
for supported_mask in [24, 32, 48, 56, 64, 128]:
if mask <= supported_mask:
new_prefix = supported_mask
break
for new_ip in ip_network(ip).subnets( new_prefix = new_prefix ):
new_ipv6s.append(str(new_ip))
temp['result']['ipv6_cidrs_workaround'] = new_ipv6s
return temp['result']
raise Exception("Cloudflare response error")
def get_aws_s3_bucket_policy(s3_id):
""" Return the Policy of an S3 """
s3 = boto3.client('s3')
result = s3.get_bucket_policy(Bucket=s3_id)
if not 'Policy' in result:
raise Exception("Failed to retrieve Policy from S3 %s" % (s3_id))
policy = json.loads(result['Policy'])
return { 'id' : s3_id, s3_id : policy }
def check_waf_v1_ipset_ipvx_rule_exists(ipset_content, address, ip_type):
""" Check if the rule currently exists """
if not "IPSet" in ipset_content:
raise Exception("Structure of IP SET v1 is not well formated. Missing 'IPSet' tag.")
ipset = ipset_content['IPSet']
if not "IPSetDescriptors" in ipset:
raise Exception("Structure of IP SET v1 is not well formated. Missing 'IPSetDescriptors' tag inside 'IPSet'.")
if not isinstance(address, unicode):
address = unicode(address, 'utf-8')
ipset_descriptors = ipset['IPSetDescriptors']
for ipset_descriptor in ipset_descriptors:
if not ip_type == ipset_descriptor['Type']:
continue
net_ipaddr = ip_network(address)
net_value = ip_network(ipset_descriptor['Value'])
if net_ipaddr == net_value or net_ipaddr.overlaps(net_value):
return True
return False
def add_waf_v1_ipset_ipvx_rule(ipset_id, ip_address, ip_type, messages):
""" Add the IP address to an IP Set from AWS WAF v1 """
waf = boto3.client('waf')
change_token_response = waf.get_change_token()
change_token = change_token_response['ChangeToken']
updates = [{
'Action': 'INSERT',
'IPSetDescriptor': {
'Type': ip_type,
'Value': ip_address
}
}]
waf.update_ip_set(
IPSetId = ipset_id,
ChangeToken = change_token,
Updates = updates
)
print("Added %s (%s) to %s " % (ip_address, ip_type, ipset_id))
messages.append("Added %s (%s) to %s " % (ip_address, ip_type, ipset_id))
return
def delete_waf_v1_ipset_ipvx_rule(ipset_id, ip_address, ip_type, messages):
""" Delete the IP address of an IP Set from AWS WAF v1 """
waf = boto3.client('waf')
change_token_response = waf.get_change_token()
change_token = change_token_response['ChangeToken']
updates = [{
'Action': 'DELETE',
'IPSetDescriptor': {
'Type': ip_type,
'Value': ip_address
}
}]
waf.update_ip_set(
IPSetId = ipset_id,
ChangeToken = change_token,
Updates = updates
)
print("Deleted %s (%s) to %s " % (ip_address, ip_type, ipset_id))
messages.append("Deleted %s (%s) to %s " % (ip_address, ip_type, ipset_id))
return
def get_waf_v1_ipset(ipset_id):
""" Return the defined IP Set from AWS WAF v1 """
waf = boto3.client('waf')
policy = waf.get_ip_set( IPSetId = ipset_id )
#policy = json.loads(waf.get_ip_set( IPSetId = ipset_id ))
return { 'id' : ipset_id, 'content' : policy }
def check_waf_v1_ipset_ipv4_rule_exists(ipset_content, address):
""" Check if the rule currently exists """
return check_waf_v1_ipset_ipvx_rule_exists(ipset_content, address, 'IPV4')
def add_waf_v1_ipset_ipv4_rule(ipset_id, ip_address, messages):
""" Add the IPv4 address to the IP Set from AWS WAF v1 """
add_waf_v1_ipset_ipvx_rule(ipset_id, ip_address, 'IPV4', messages)
def delete_waf_v1_ipset_ipv4_rule(ipset_id, ip_address, messages):
""" Delete the IP address of an IP Set from AWS WAF v1 """
delete_waf_v1_ipset_ipvx_rule(ipset_id, ip_address, 'IPV4', messages)
def check_waf_v1_ipset_ipv6_rule_exists(ipset_content, address):
""" Check if the rule currently exists """
return check_waf_v1_ipset_ipvx_rule_exists(ipset_content, address, 'IPV6')
def add_waf_v1_ipset_ipv6_rule(ipset_id, ip_address, messages):
""" Add the IPv6 address to the IP Set from AWS WAF v1 """
add_waf_v1_ipset_ipvx_rule(ipset_id, ip_address, 'IPV6', messages)
def delete_waf_v1_ipset_ipv6_rule(ipset_id, ip_address, messages):
""" Delete the IP address of an IP Set from AWS WAF v1 """
delete_waf_v1_ipset_ipvx_rule(ipset_id, ip_address, 'IPV6', messages)
def get_aws_security_group(group_id):
""" Return the defined Security Group """
ec2 = boto3.resource('ec2')
group = ec2.SecurityGroup(group_id)
if group.group_id == group_id:
return group
raise Exception('Failed to retrieve Security Group')
def check_ipv4_rule_exists(rules, address, port):
""" Check if the rule currently exists """
for rule in rules:
for ip_range in rule['IpRanges']:
if ip_range['CidrIp'] == address and rule['FromPort'] == port:
return True
return False
def add_ipv4_rule(group, address, port, messages):
""" Add the IP address/port to the security group """
group.authorize_ingress(IpProtocol="tcp",
CidrIp=address,
FromPort=port,
ToPort=port)
print("Added %s : %i to %s " % (address, port, group.group_id))
messages.append("Added %s : %i to %s " % (address, port, group.group_id))
def delete_ipv4_rule(group, address, port, messages):
""" Remove the IP address/port from the security group """
group.revoke_ingress(IpProtocol="tcp",
CidrIp=address,
FromPort=port,
ToPort=port)
print("Removed %s : %i from %s " % (address, port, group.group_id))
messages.append("Removed %s : %i from %s " % (address, port, group.group_id))
def check_ipv6_rule_exists(rules, address, port):
""" Check if the rule currently exists """
for rule in rules:
for ip_range in rule['Ipv6Ranges']:
if ip_range['CidrIpv6'] == address and rule['FromPort'] == port:
return True
return False
def add_ipv6_rule(group, address, port, messages):
""" Add the IP address/port to the security group """
group.authorize_ingress(IpPermissions=[{
'IpProtocol': "tcp",
'FromPort': port,
'ToPort': port,
'Ipv6Ranges': [
{
'CidrIpv6': address
},
]
}])
print("Added %s : %i to %s " % (address, port, group.group_id))
messages.append("Added %s : %i to %s " % (address, port, group.group_id))
def delete_ipv6_rule(group, address, port):
""" Remove the IP address/port from the security group """
group.revoke_ingress(IpPermissions=[{
'IpProtocol': "tcp",
'FromPort': port,
'ToPort': port,
'Ipv6Ranges': [
{
'CidrIpv6': address
},
]
}])
print("Removed %s : %i from %s " % (address, port, group.group_id))
messages.append("Removed %s : %i from %s " % (address, port, group.group_id))
def update_ip_set_v1_policies(ip_addresses, messages):
""" Updates IP set from AWS WAF Classic """
if not "IPSET_V1_IDS_LIST" in os.environ and not "IPSET_V1_ID" in os.environ:
print("Missing Web ACL Classic configuration 'IPSET_V1_IDS_LIST' or 'IPSET_V1_ID'. Will not check Security Policy.")
messages.append("Missing Web ACL Classic configuration 'IPSET_V1_IDS_LIST' or 'IPSET_V1_ID'. Will not check Security Policy.")
return
ip_sets = map(get_waf_v1_ipset, os.environ['IPSET_V1_IDS_LIST'].split(","))
if not ip_sets:
ip_sets = [get_waf_v1_ipset(os.environ['IPSET_V1_ID'])]
## Security Groups
for ipset in ip_sets:
ipset_id = ipset['id']
current_rules = ipset['content']
## IPv4
# add new addresses
for ipv4_cidr in ip_addresses['ipv4_cidrs_workaround']:
if not check_waf_v1_ipset_ipv4_rule_exists(current_rules, ipv4_cidr):
add_waf_v1_ipset_ipv4_rule(ipset_id, ipv4_cidr, messages)
## IPv6 -- because of boto3 syntax, this has to be separate
# add new addresses
for ipv6_cidr in ip_addresses['ipv6_cidrs_workaround']:
if not check_waf_v1_ipset_ipv6_rule_exists(current_rules, ipv6_cidr):
add_waf_v1_ipset_ipv6_rule(ipset_id, ipv6_cidr, messages)
# remove old addresses
for rule in current_rules['IPSet']['IPSetDescriptors']:
ip_type = rule['Type']
ip_addr = rule['Value']
if not isinstance(ip_addr, unicode):
ip_addr = unicode(ip_addr, 'utf-8')
in_ipv4 = False
in_ipv6 = False
if 'IPV4' == ip_type:
for addr in ip_addresses['ipv4_cidrs_workaround']:
if not isinstance(addr, unicode):
addr = unicode(addr, 'utf-8')
if not isinstance(ip_addr, unicode):
ip_addr = unicode(ip_addr, 'utf-8')
net_ipaddr = ip_network(addr)
net_value = ip_network(ip_addr)
if net_ipaddr == net_value or net_ipaddr.overlaps(net_value):
in_ipv4 = True
break
if 'IPV6' == ip_type:
for addr in ip_addresses['ipv6_cidrs_workaround']:
if not isinstance(addr, unicode):
addr = unicode(addr, 'utf-8')
if not isinstance(ip_addr, unicode):
ip_addr = unicode(ip_addr, 'utf-8')
net_ipaddr = ip_network(addr)
net_value = ip_network(ip_addr)
if net_ipaddr == net_value or net_ipaddr.overlaps(net_value):
in_ipv4 = True
break
if not in_ipv6 and not in_ipv4:
delete_waf_v1_ipset_ipvx_rule(ipset_id, ip_addr, ip_type, messages)
return
def update_s3_policies(ip_addresses, messages):
""" Update S3 policies """
print("Checking policies of S3")
messages.append("Checking policies of S3")
s3 = boto3.client('s3')
ipv4 = ip_addresses['ipv4_cidrs']
ipv6 = ip_addresses['ipv6_cidrs']
cloudflare_ips = ipv4 + ipv6
if not "S3_CLOUDFLARE_SID" in os.environ:
print("Not configured 'S3_CLOUDFLARE_SID' variable, so will not check S3")
messages.append("Not configured 'S3_CLOUDFLARE_SID' variable, so will not check S3")
return
if not "S3_BUCKET_IDS_LIST" in os.environ and not "S3_BUCKET_ID" in os.environ:
raise Exception("Missing S3 basic configuration 'S3_BUCKET_IDS_LIST' or 'S3_BUCKET_ID'.")
sid = os.environ['S3_CLOUDFLARE_SID']
s3_policy_tuple = map(get_aws_s3_bucket_policy, os.environ['S3_BUCKET_IDS_LIST'].split(","))
if not s3_policy_tuple:
s3_policy_tuple = [get_aws_s3_bucket_policy(os.environ['S3_BUCKET_ID'])]
for s3_tuple in s3_policy_tuple:
updated = False
s3_id = s3_tuple['id']
print("Checking Policy of S3 Bucket '%s'" % (s3_id) )
messages.append("Checking Policy of S3 Bucket '%s'" % (s3_id) )
policy = s3_tuple[s3_id]
if not 'Statement' in policy:
raise Exception("Problem reading policy of S3 Bucket '%s'" % (s3_id) )
for statement in policy['Statement']:
if not "Sid" in statement:
raise Exception("Problem reading Sid inside Statement of S3 Bucket '%s'" % (s3_id) )
if ((not sid == statement['Sid']) or
(not "Condition" in statement) or
(not "IpAddress" in statement["Condition"]) or
(not "aws:SourceIp" in statement["Condition"]["IpAddress"])):
continue
statement["Condition"]["IpAddress"]["aws:SourceIp"] = cloudflare_ips
updated = True
if updated:
policy = json.dumps(policy)
print("Going to update policy %s " % (s3_id) )
messages.append("Going to update policy %s " % (s3_id) )
s3.put_bucket_policy(Bucket=s3_id, Policy=policy)
def update_security_group_policies(ip_addresses, messages):
""" Update Information of Security Groups """
print("Checking policies of Security Groups")
messages.append("Checking policies of Security Groups")
if not "SECURITY_GROUP_IDS_LIST" in os.environ and not "SECURITY_GROUP_ID" in os.environ:
print("Missing S3 basic configuration 'SECURITY_GROUP_IDS_LIST' or 'SECURITY_GROUP_ID'. Will not check Security Policy.")
messages.append("Missing S3 basic configuration 'SECURITY_GROUP_IDS_LIST' or 'SECURITY_GROUP_ID'. Will not check Security Policy.")
return
ports = map(int, os.environ['PORTS_LIST'].split(","))
if not ports:
ports = [80]
security_groups = map(get_aws_security_group, os.environ['SECURITY_GROUP_IDS_LIST'].split(","))
if not security_groups:
security_groups = [get_aws_security_group(os.environ['SECURITY_GROUP_ID'])]
## Security Groups
for security_group in security_groups:
current_rules = security_group.ip_permissions
## IPv4
# add new addresses
for ipv4_cidr in ip_addresses['ipv4_cidrs']:
for port in ports:
if not check_ipv4_rule_exists(current_rules, ipv4_cidr, port):
add_ipv4_rule(security_group, ipv4_cidr, port, messages)
# remove old addresses
for port in ports:
for rule in current_rules:
# is it necessary/correct to check both From and To?
if rule['FromPort'] == port and rule['ToPort'] == port:
for ip_range in rule['IpRanges']:
if ip_range['CidrIp'] not in ip_addresses['ipv4_cidrs']:
delete_ipv4_rule(security_group, ip_range['CidrIp'], port, messages)
## IPv6 -- because of boto3 syntax, this has to be separate
# add new addresses
for ipv6_cidr in ip_addresses['ipv6_cidrs']:
for port in ports:
if not check_ipv6_rule_exists(current_rules, ipv6_cidr, port):
add_ipv6_rule(security_group, ipv6_cidr, port, messages)
# remove old addresses
for port in ports:
for rule in current_rules:
for ip_range in rule['Ipv6Ranges']:
if ip_range['CidrIpv6'] not in ip_addresses['ipv6_cidrs']:
delete_ipv6_rule(security_group, ip_range['CidrIpv6'], port, messages)
def lambda_handler(event, context):
""" AWS Lambda main function """
messages = []
ip_addresses = get_cloudflare_ip_list()
update_ip_set_v1_policies(ip_addresses, messages)
update_security_group_policies(ip_addresses, messages)
update_s3_policies(ip_addresses, messages)
return {
'statusCode': 200,
'messages': json.dumps(messages),
'cloudflare_data': ip_addresses
}