From 91f43d2331699fcf9628d1cadc038b351c96bc8a Mon Sep 17 00:00:00 2001 From: Rath Pascal Date: Sun, 17 Nov 2024 13:56:26 +0100 Subject: [PATCH] fix edge-cases --- src/builder/build.py | 25 +++++++++++++++---------- src/builder/enrich_data.py | 12 +++++++++--- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/builder/build.py b/src/builder/build.py index ffb1a6d..b8551fe 100644 --- a/src/builder/build.py +++ b/src/builder/build.py @@ -50,18 +50,23 @@ def build_dbs_ip_asn(reports: dict, ptrs: dict, lookup_lists: dict, networks: di IPv4Address(ip) mmdb4.insert_network(ipset, {**asn_info['full'], **net}) - if asn not in json4: - json4[asn] = {} + if asn != 0: + if asn not in json4: + json4[asn] = {} - json4[asn][ip] = {**asn_info['small'], **net_sm} + json4[asn][ip] = {**asn_info['small'], **net_sm} except AddressValueError: mmdb6.insert_network(ipset, {**asn_info['full'], **net}) - if asn not in json6: - json6[asn] = {} + if asn != 0: + if asn not in json6: + json6[asn] = {} - json6[asn][ip] = {**asn_info['small'], **net_sm} + json6[asn][ip] = {**asn_info['small'], **net_sm} + + if asn == 0: + continue if asn not in asn_reports: try: @@ -94,13 +99,13 @@ def build_dbs_ip_asn(reports: dict, ptrs: dict, lookup_lists: dict, networks: di } if not asn_reports[asn]['kind']['hosting']: - asn_info = str(asn_reports[asn]['info']['org']).lower() - if asn_info.find('cloud') != -1 or asn_info.find('hosting') != -1: + asn_org = str(asn_reports[asn]['info']['org']).lower() + if asn_org.find('cloud') != -1 or asn_org.find('host') != -1: asn_reports[asn]['kind']['hosting'] = True except KeyError as e: - print(f'ERROR: Failed to lookup metadata of ASN {asn} ({e})') - asn_reports[asn] = {'reports': ip_reports} + print(f'ERROR: Failed to lookup metadata of ASN {asn} (KeyError: {e})') + continue else: for report_type, report_count in ip_reports.items(): diff --git a/src/builder/enrich_data.py b/src/builder/enrich_data.py index bbb658e..9abffa8 100644 --- a/src/builder/enrich_data.py +++ b/src/builder/enrich_data.py @@ -62,10 +62,15 @@ def ip_asn_info(ip: str, reports: dict, lookup_lists: dict, ptrs: dict) -> dict: ip_md = m.get(ip) try: - asn = int(ip_md['asn'][2:]) + asn = int(ip_md['asn']) + # asn = int(ip_md['asn'][2:]) # ipinfo DB except ValueError: - return {} + return { + 'nr': 0, + 'full': {}, + 'small': {}, + } d = { 'asn': asn, @@ -110,7 +115,8 @@ def net_asn_info(ip: str) -> dict: ip_md = m.get(ip) try: - asn = int(ip_md['asn'][2:]) + asn = int(ip_md['asn']) + # asn = int(ip_md['asn'][2:]) # ipinfo DB except ValueError: return {}