-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathip2geo.py
109 lines (105 loc) · 3.91 KB
/
ip2geo.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
import os
import sys
import geoip2.database
from json import dumps
mmdb_city_path = os.environ.get('MMMDB_CITY_PATH')
mmdb_asn_path = os.environ.get('MMMDB_ASN_PATH')
def sanitize_ip(ip):
if '.' in ip:
ip = ip.split('.')
ip[-1] = 'xxx'
ip = '.'.join(ip)
else:
components = ip.split(':')
if len(components) < 8:
expanded_address = ip.replace('::', ':' + ':'.join(['0'] * (8 - len(components) + 1 )) + ':')
components = expanded_address.split(':')
components[-1] = 'xxxx'
covered_address = ':'.join([comp.lstrip('0') or '0' for comp in components])
ip = covered_address
return ip
scanners = dict()
with geoip2.database.Reader(mmdb_city_path) as mmdb_city, geoip2.database.Reader(mmdb_asn_path) as mmdb_asn:
for line in sys.stdin:
line = line.strip()
if not line:
continue
line = line.split(',')
timestamp = line[0]
target_ip = line[1]
source_ip = line[2]
protocol = line[3]
mmdb_source_city_response = None
try:
mmdb_source_city_response = mmdb_city.city(source_ip)
except Exception as e:
mmdb_source_city_response = 'Unknown'
continue
mmdb_source_asn_response = None
try:
mmdb_source_asn_response = mmdb_asn.asn(source_ip)
except Exception as e:
mmdb_source_asn_response = 'Unknown'
mmdb_target_city_response = None
try:
mmdb_target_city_response = mmdb_city.city(target_ip)
except Exception as e:
mmdb_target_city_response = 'Unknown'
mmdb_target_asn_response = None
try:
mmdb_target_asn_response = mmdb_asn.asn(target_ip)
except Exception as e:
mmdb_target_asn_response = 'Unknown'
source_ip = sanitize_ip(source_ip)
target_ip = sanitize_ip(target_ip)
if (source_ip, target_ip, protocol) not in scanners:
scanners[(source_ip, protocol)] = {
"count": 1,
"source_ip": source_ip,
"target_ip": target_ip,
"protocol": protocol.replace('"', '\\"'),
"continent": mmdb_source_city_response.continent.name.replace('"', '\\"') if mmdb_source_city_response != 'Unknown' else "Unknown",
"country": mmdb_source_city_response.country.name.replace('"', '\\"') if mmdb_source_city_response != 'Unknown' else "Unknown",
"city": mmdb_source_city_response.city.name.replace('"', '\\"') if mmdb_source_city_response != 'Unknown' else "Unknown",
"latitude": mmdb_source_city_response.location.latitude if mmdb_source_city_response != 'Unknown' else "Unknown",
"longitude": mmdb_source_city_response.location.longitude if mmdb_source_city_response != 'Unknown' else "Unknown",
"asn": mmdb_source_asn_response.autonomous_system_organization.replace('"', '\\"') if mmdb_source_asn_response != 'Unknown' else "Unknown",
"target_latitude": mmdb_target_city_response.location.latitude if mmdb_target_city_response != 'Unknown' else "Unknown",
"target_longitude": mmdb_target_city_response.location.longitude if mmdb_target_city_response != 'Unknown' else "Unknown"
}
else:
scanners[(source_ip, protocol)]["count"] += 1
json = ''
json += '{"type": "FeatureCollection","features": ['
for scanner in scanners.values():
json += dumps({
"type": "Feature",
"properties": {
"count": scanner["count"],
"source_ip": scanner["source_ip"],
"target_ip": scanner["target_ip"],
"protocol": scanner["protocol"],
"continent": scanner["continent"],
"country": scanner["country"],
"city": scanner["city"],
"asn": scanner["asn"]
},
"source_geometry": {
"type": "Point",
"coordinates": [
scanner["longitude"],
scanner["latitude"]
]
},
"target_geometry": {
"type": "Point",
"coordinates": [
scanner["target_longitude"],
scanner["target_latitude"]
]
}
}) + ','
json = json[:-1]
json += ']}'
print(json)
# cat *.csv | docker compose exec -T where-are-the-scanners python main.py