|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# data to visualize with svgmap: https://stephanwagner.me/create-world-map-charts-with-svgmap#svgMapDemoGDP |
| 4 | + |
| 5 | +from argparse import ArgumentParser |
| 6 | +from json import loads as json_loads |
| 7 | +from json import dumps as json_dumps |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from maxminddb import open_database as mmdb_database |
| 11 | + |
| 12 | +SRC_PATH = Path(__file__).resolve().parent |
| 13 | +HIGHLIGHT_TOP_N = 20 |
| 14 | +CATEGORIES = ['all', 'bot', 'probe', 'rate', 'attack', 'crawler'] |
| 15 | + |
| 16 | +# todo: add change to last month |
| 17 | +DATA = { |
| 18 | + 'targetElementID': 'svgMap', |
| 19 | + 'data': { |
| 20 | + 'applyData': 'all', |
| 21 | + 'data': { |
| 22 | + 'all': { |
| 23 | + 'name': 'Reported abuse originating from this country', |
| 24 | + 'format': '{0}', |
| 25 | + 'thousandSeparator': '.', |
| 26 | + 'thresholdMax': 2_000, |
| 27 | + 'thresholdMin': 500 |
| 28 | + }, |
| 29 | + 'bot': { |
| 30 | + 'name': 'Reported bots', |
| 31 | + 'format': '{0}', |
| 32 | + }, |
| 33 | + 'probe': { |
| 34 | + 'name': 'Reported probes/scanners', |
| 35 | + 'format': '{0}', |
| 36 | + }, |
| 37 | + 'rate': { |
| 38 | + 'name': 'Reported rate-limit hits', |
| 39 | + 'format': '{0}', |
| 40 | + }, |
| 41 | + 'attack': { |
| 42 | + 'name': 'Reported attacks', |
| 43 | + 'format': '{0}', |
| 44 | + }, |
| 45 | + 'crawler': { |
| 46 | + 'name': 'Reported crawlers', |
| 47 | + 'format': '{0}', |
| 48 | + }, |
| 49 | + }, |
| 50 | + 'values': {}, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +def main(): |
| 56 | + with open(args.file, 'r', encoding='utf-8') as f: |
| 57 | + raw = json_loads(f.read()) |
| 58 | + |
| 59 | + with mmdb_database(args.country_db) as m: |
| 60 | + for asn, ips in raw.items(): |
| 61 | + for ip, reports in ips.items(): |
| 62 | + ip_md = m.get(ip) |
| 63 | + if ip_md['country'] not in DATA['data']['values']: |
| 64 | + DATA['data']['values'][ip_md['country']] = {c: 0 for c in CATEGORIES} |
| 65 | + |
| 66 | + for c in CATEGORIES: |
| 67 | + if c in reports: |
| 68 | + DATA['data']['values'][ip_md['country']][c] += reports[c] |
| 69 | + |
| 70 | + DATA['data']['values'] = dict(sorted(DATA['data']['values'].items(), key=lambda item: item[1]['all'], reverse=True)) |
| 71 | + with open(SRC_PATH / 'world_map.json', 'w', encoding='utf-8') as f: |
| 72 | + top_country_values = list(DATA['data']['values'].values()) |
| 73 | + DATA['data']['data']['all']['thresholdMax'] = top_country_values[0]['all'] |
| 74 | + DATA['data']['data']['all']['thresholdMin'] = top_country_values[HIGHLIGHT_TOP_N]['all'] |
| 75 | + f.write(json_dumps(DATA, indent=4)) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + parser = ArgumentParser() |
| 80 | + parser.add_argument('-f', '--file', help='JSON file to parse', default='risk_ip4_med.json') |
| 81 | + parser.add_argument('-c', '--country-db', help='MMDB country data to use (IPInfo)', default='country_asn.mmdb') |
| 82 | + args = parser.parse_args() |
| 83 | + main() |
0 commit comments