-
Notifications
You must be signed in to change notification settings - Fork 0
/
malicious_ip_enricher.py
65 lines (50 loc) · 1.33 KB
/
malicious_ip_enricher.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
import pandas as pd
import numpy as np
import socket
import geoip2.database
import geoip2.errors
import json
import pprint as pp
import netaddr
reader = geoip2.database.Reader('GeoLite2-City.mmdb')
ip_csv = pd.read_csv('malicious_ips.csv', encoding="utf-8")
ips_to_parse = [line for line in ip_csv['ip']]
i = 0
j = 0
ips = []
country_codes = []
latitudes = []
longitudes = []
print(len(ips_to_parse))
for ip in ips_to_parse:
try:
ip = str(netaddr.IPAddress(ip, flags=netaddr.ZEROFILL).ipv4())
country_code = reader.city(ip).country.iso_code
latitude = reader.city(ip).location.latitude
longitude = reader.city(ip).location.longitude
latitudes.append(latitude)
longitudes.append(longitude)
ips.append(ip)
country_codes.append(country_code)
except:
print('error')
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(latitudes))
print(len(longitudes))
ip_csv['ip'] = ips
ip_csv['country'] = country_codes
ip_csv['latitude'] = latitudes
ip_csv['longitude'] = longitudes
ip_csv.dropna(inplace=True)
ip_csv.to_csv('malicious_ips_locations.csv', index=False)