-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathday13.py
199 lines (183 loc) · 5.41 KB
/
day13.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
import os
import socket
import urllib3
import json
from decimal import Decimal
from time import sleep
import boto3
import botocore.exceptions
from datetime import datetime
# Env vars
IP_GEOINT_CACHE_TABLE_NAME = os.environ['IP_GEOINT_CACHE_TABLE_NAME'] # export IP_GEOINT_CACHE_TABLE_NAME='dyanmodb_table_here'
EPOCH_DAY = 86400
# Pool Manager for urllib3 to (re)use
http = urllib3.PoolManager()
# Scary people to find
SOME_HACKERS_I_GUESS = [
'thehackernews.com',
'krebsonsecurity.com',
'csoonline.com',
'darkreading.com',
'threatpost.com',
'welivesecurity.com',
'www.infosecurity-magazine.com',
'www.bleepingcomputer.com'
]
# Boto3 Clients
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(IP_GEOINT_CACHE_TABLE_NAME)
def determine_ip_address(hostnames):
'''
Uses Socket to find the IP address for a given hostname
'''
enrichedHostnames = []
for hostname in hostnames:
try:
ip = socket.gethostbyname(hostname)
except Exception as e:
print(e)
ip = None
if ip != None:
# Get cached results
geoint = get_cached_geo_intelligence(ip)
# If there are not cached results, write fresh
if geoint == None:
print(f'No cached results found for {ip}!')
geoint = geo_intelligence(ip)
countryCode = geoint['CountryCode']
latitude = float(geoint['Latitude'])
longitude = float(geoint['Longitude'])
isp = geoint['Isp']
org = geoint['Org']
asn = geoint['Asn']
asnName = geoint['AsnName']
else:
countryCode = None
latitude = int(0)
longitude = int(0)
isp = None
org = None
asn = None
asnName = None
enrichedHostname = {
'Hostname': hostname,
'IpAddress': ip,
'CountryCode': countryCode,
'Latitude': latitude,
'Longitude': longitude,
'Isp': isp,
'Org': org,
'Asn': asn,
'AsnName': asnName
}
if enrichedHostname not in enrichedHostnames:
enrichedHostnames.append(enrichedHostname)
save_data_to_file(enrichedHostnames)
def get_cached_geo_intelligence(ip):
'''
Attempts to find cached geoint results for a given hostname
'''
try:
r = table.get_item(
Key={
'IpAddress': ip
}
)
if 'Item' in r:
geoint = {
'CountryCode': r['Item']['CountryCode'],
'Latitude': float(r['Item']['Latitude']),
'Longitude': float(r['Item']['Longitude']),
'Isp': r['Item']['Isp'],
'Org': r['Item']['Org'],
'Asn': r['Item']['Asn'],
'AsnName': r['Item']['AsnName']
}
else:
geoint = None
except botocore.exceptions.ClientError as error:
raise error
return geoint
def geo_intelligence(ip):
# Generate request url for use
url = f'http://ip-api.com/json/{ip}?fields=status,message,countryCode,lat,lon,isp,org,as,asname'
# GET request
r = http.request(
'GET',
url
)
ttlHeader = int(r.headers['X-Ttl'])
requestsLeftHeader = int(r.headers['X-Rl'])
# handle throttling
if requestsLeftHeader == 0:
ttlHeader = int(r.headers['X-Ttl'])
waitTime = ttlHeader + 1
sleep(waitTime)
print('Request limit breached - retrying')
del r
# new request
r = http.request(
'GET',
url
)
ipJson = json.loads(r.data.decode('utf-8'))
countryCode = str(ipJson['countryCode'])
latitude = float(ipJson['lat'])
longitude = float(ipJson['lon'])
isp = str(ipJson['isp'])
org = str(ipJson['org'])
asn = str(ipJson['as'])
asnName = str(ipJson['asname'])
# If not fail
else:
ipJson = json.loads(r.data.decode('utf-8'))
countryCode = str(ipJson['countryCode'])
latitude = float(ipJson['lat'])
longitude = float(ipJson['lon'])
isp = str(ipJson['isp'])
org = str(ipJson['org'])
asn = str(ipJson['as'])
asnName = str(ipJson['asname'])
# create ttl and insert into the Dict
tstamp = int(datetime.utcnow().timestamp())
oneWeek = 7 * EPOCH_DAY
ttl = tstamp + oneWeek
geoint = {
'IpAddress': ip,
'CountryCode': countryCode,
'Latitude': latitude,
'Longitude': longitude,
'Isp': isp,
'Org': org,
'Asn': asn,
'AsnName': asnName,
'Ttl': ttl
}
try:
# Write Floats as Decimals...
table.put_item(
Item=json.loads(
json.dumps(
geoint
),
parse_float=Decimal
)
)
except botocore.exceptions.ClientError as error:
print(error)
print('Failed to write results to DynamoDB')
return geoint
def save_data_to_file(enriched_hostnames):
'''
Writes enriched hostnames to JSON file
'''
with open('./enriched_hostnames_list.json', 'w') as jsonfile:
json.dump(
enriched_hostnames,
jsonfile,
default=str,
indent=2
)
determine_ip_address(
hostnames=SOME_HACKERS_I_GUESS
)