-
Notifications
You must be signed in to change notification settings - Fork 0
/
benign_ip_parser.py
77 lines (61 loc) · 1.76 KB
/
benign_ip_parser.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
import pandas as pd
import numpy as np
import socket
import geoip2.database
import geoip2.errors
import json
import pprint as pp
reader = geoip2.database.Reader('GeoLite2-City.mmdb')
file = open('benign_ips_enriched.csv', 'w+')
file.close()
url_csv = pd.DataFrame()
with open('browsing_history.json') as file:
data = json.load(file)
urls_to_parse = []
for line in data:
try:
urls_to_parse.append(line['url'])
except:
print('key "url" not defined in line {0}'.format(line))
i = 0
j = 0
ips = []
urls = []
country_codes = []
latitudes = []
longitudes = []
for url in urls_to_parse:
try:
ip = socket.gethostbyname(str(url).split('/')[2])
country_code = reader.city(ip).country.iso_code
latitude = reader.city(ip).location.latitude
longitude = reader.city(ip).location.longitude
ips.append(ip)
country_codes.append(country_code)
latitudes.append(latitude)
longitudes.append(longitude)
urls.append(url)
except:
urls.append(np.nan)
latitudes.append(np.nan)
longitudes.append(np.nan)
ips.append(np.nan)
country_codes.append(np.nan)
if not i % 100:
print(j)
i = 0
i += 1
j += 1
print("-" * 25)
print(len(ips))
print(len(country_codes))
print(len(urls))
print(len(latitudes))
print(len(longitudes))
url_csv['ip'] = ips
url_csv['country'] = country_codes
url_csv['url'] = urls
url_csv['latitude'] = latitudes
url_csv['longitude'] = longitudes
url_csv.dropna(inplace=True)
url_csv.to_csv('benign_ips_enriched.csv', index=False)