-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathday15.py
291 lines (251 loc) · 8.69 KB
/
day15.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
import urllib3
import boto3
import socket
import ipaddress
import botocore.exceptions
from datetime import datetime
from time import sleep
wafv2 = boto3.client('wafv2')
CINSSCORE_URL = 'http://cinsscore.com/list/ci-badguys.txt'
CINSSCORE_FILE_NAME = './ci_badguys.txt'
COINBLOCKER_URL = 'https://zerodot1.gitlab.io/CoinBlockerLists/list.txt'
COINBLOCKER_FILE_NAME = './coinblockerlist.txt'
CINSSCORE_IPSET_NAME = '100DaysOfCloud-CINS'
COINBLOCKER_IPSET_NAME = '100DaysOfCloud-COINBLOCKER'
def download_ioc_feeds():
'''
Downloads and saves various IOC feeds to file
'''
# Pool Manager for urllib3
http = urllib3.PoolManager()
# CINSSCORE
try:
# GET request
r = http.request(
'GET',
CINSSCORE_URL
)
with open(CINSSCORE_FILE_NAME, 'wb') as file:
file.write(r.data)
r.release_conn()
del r
print(f'Wrote CINS Score IOC feed to file.')
except Exception as e:
raise e
# COINBLOCKER
try:
# GET request
r = http.request(
'GET',
COINBLOCKER_URL
)
with open(COINBLOCKER_FILE_NAME, 'wb') as file:
file.write(r.data)
r.release_conn()
del r
print(f'Wrote CINS Score IOC feed to file.')
except Exception as e:
raise e
del http
return True
def parse_cinsscore():
'''
Reads out the CINS Score IOC file and parses the first 10K IOCs
'''
# Empty list for new IOCs
cinscoreIocs = []
# Readout the file and close it
cinsscoreIocFile = open(CINSSCORE_FILE_NAME, 'r')
cinsscoreIocList = cinsscoreIocFile.readlines()
cinsscoreIocFile.close()
# Parse the list and strip the newline
for ioc in cinsscoreIocList:
# IP Sets accept up to 10K values, stop there
if len(cinscoreIocs) < 10000:
try:
ioc = ioc.replace('\n','')
# Check to ensure the IP Address is good...
ipaddress.ip_address(ioc)
# Write into the list as a CIDR
if ioc not in cinscoreIocs:
cinscoreIocs.append(f'{ioc}/32')
else:
continue
except ValueError:
print(f'{ioc} is invalid!')
continue
else:
break
print(f'Parsed {len(cinscoreIocs)} IOCs into a new list.')
return cinscoreIocs
def parse_coinblocker():
'''
Reads out the CINS Score IOC file and parses the first 10K IOCs
'''
# Empty list for new IOCs
coinblockerIocs = []
# Readout the file and close it
coinblockerIocFile = open(COINBLOCKER_FILE_NAME, 'r')
coinblockerIocList = coinblockerIocFile.readlines()
coinblockerIocFile.close()
# List for domains that have been seen
seenDomains = []
# Parse the list and strip the newline
for ioc in coinblockerIocList:
# IP Sets accept up to 10K values, stop there
if len(coinblockerIocs) < 10000:
try:
ioc = ioc.replace('\n','')
# Find the top-level domain instead
splitter = ioc.split('.')
domain = f'{splitter[-2]}.{splitter[-1]}'
if domain not in seenDomains:
seenDomains.append(domain)
else:
continue
# Attempt to transform domain IOC into an IP
ip = domain_to_ip(domain)
if ip == None:
continue
# Check to ensure the IP Address is good...
ipaddress.ip_address(ip)
# Write into the list as a CIDR
if ip not in coinblockerIocs:
coinblockerIocs.append(f'{ip}/32')
else:
continue
except ValueError:
print(f'{ip} is invalid!')
continue
else:
break
del seenDomains
print(f'Parsed {len(coinblockerIocs)} IOCs into a new list.')
return coinblockerIocs
def domain_to_ip(domain):
'''
This function receives Domain IOCs and attempts to convert them into IP addresses using `socket`
'''
try:
ip = socket.gethostbyname(domain)
except Exception as e:
print(f'Socket error {e} encountered for {domain}')
ip = None
return ip
def wafv2_ip_set_commander():
'''
Controls or updates multiple IP Sets from various IOC feeds
'''
# Download fresh feeds
download_ioc_feeds()
# Retrieve parsed IOCs
cinsscoreIocs = parse_cinsscore()
coinblockerIocs = parse_coinblocker()
# Determine if the IP Sets exist. Create a list of IP Sets to reference, if any exist
ipsets = []
# Save ListIpSets results to a variable, it will need to be accessed multiple times
listIpSets = wafv2.list_ip_sets(Scope='REGIONAL')['IPSets']
# Write unique WAFv2 IP Set names into the list
for ipset in listIpSets:
ipsetName = ipset['Name']
if ipsetName not in ipsets:
ipsets.append(ipsetName)
# CINS Score Create/Update loop
if CINSSCORE_IPSET_NAME not in ipsets:
cinsscoreIpSetArn = waf_ip_set_creator(
ip_set_name=CINSSCORE_IPSET_NAME,
ioc_payload=cinsscoreIocs
)
else:
for ipset in listIpSets:
if ipset['Name'] == CINSSCORE_IPSET_NAME:
ipSetId = ipset['Id']
ipSetLockToken = ipset['LockToken']
cinsscoreIpSetArn = ipset['ARN']
waf_ip_set_updater(
ip_set_name=CINSSCORE_IPSET_NAME,
ip_set_id=ipSetId,
ip_set_lock_token=ipSetLockToken,
ioc_payload=cinsscoreIocs
)
break
else:
continue
# Coinblocker Create/Update loop
if COINBLOCKER_IPSET_NAME not in ipsets:
coinblockerIpSetArn = waf_ip_set_creator(
ip_set_name=COINBLOCKER_IPSET_NAME,
ioc_payload=coinblockerIocs
)
else:
for ipset in listIpSets:
if ipset['Name'] == COINBLOCKER_IPSET_NAME:
ipSetId = ipset['Id']
ipSetLockToken = ipset['LockToken']
coinblockerIpSetArn = ipset['ARN']
waf_ip_set_updater(
ip_set_name=COINBLOCKER_IPSET_NAME,
ip_set_id=ipSetId,
ip_set_lock_token=ipSetLockToken,
ioc_payload=coinblockerIocs
)
break
else:
continue
return cinsscoreIpSetArn, coinblockerIpSetArn
def waf_ip_set_creator(ip_set_name, ioc_payload):
'''
Receives a name and IOC payload from `wafv2_ip_set_commander` and creates a new IP Set and returns the ARN
'''
# Generate timestamp for IP Set Description
dtgNow = str(datetime.utcnow())
# Create the IP Set
try:
r = wafv2.create_ip_set(
Name=ip_set_name,
Scope='REGIONAL',
Description=f'IP Set corresponding to IOCs of the named IP Set - IOCs loaded at {dtgNow}',
IPAddressVersion='IPV4',
Addresses=ioc_payload,
Tags=[
{
'Key': 'Name',
'Value': ip_set_name
}
]
)
ipSetArn = r['Summary']['ARN']
# Wait 15 seconds for the IP Set to "stabilize"
print(f'Succesfully created a new IP Set {ip_set_name}. Waiting 7 seconds for the IP Set to stabilize')
sleep(7)
except botocore.exceptions.ClientError as error:
raise error
return ipSetArn
def waf_ip_set_updater(ip_set_name, ip_set_id, ip_set_lock_token, ioc_payload):
'''
Receives a name, ID, lock token, and IOC payload from `wafv2_ip_set_commander` and updates an existing IP Set and returns the ARN
'''
# Generate timestamp for IP Set Description
dtgNow = str(datetime.utcnow())
# Update the IP Set
try:
wafv2.update_ip_set(
Name=ip_set_name,
Scope='REGIONAL',
Id=ip_set_id,
Description=f'IP Set corresponding to IOCs of the named IP Set - IOCs loaded at {dtgNow}',
Addresses=ioc_payload,
LockToken=ip_set_lock_token
)
print(f'Sucessfully updated IP Set {ip_set_name}')
except Exception as e:
raise e
def day15_placeholder():
'''
Nothing yet...
'''
# With multiple `return` statements, the values are provided as a tuple which can be accessed like a list
ipSets = wafv2_ip_set_commander()
print(f'CINS Score IP Set ARN is {ipSets[0]}')
print(f'Coinblocker IP Set ARN is {ipSets[1]}')
day15_placeholder()