def generate_pyasn_v1_2_ip_to_asn_mapping(pyasn_db, size): mapping = {} asndb = PyASN.new(pyasn_db) filename = os.path.basename(pyasn_db).split('.')[0] size = int(size) while len(mapping) < size: i1 = random.randint(1, 223) i2 = random.randint(0, 255) i3 = random.randint(0, 255) i4 = random.randint(0, 255) sip = "%d.%d.%d.%d" % (i1, i2, i3, i4) ip = unpack('>I', inet_aton(sip))[0] # for efficient, store ip as 32-bit-int mapping[ip] = asndb.Lookup(sip) f = gzip.open("pyasn_v1.2__%s__sample_%d.pickle.gz" % (filename, size), "wb") pickle.dump(mapping, f) f.close()
def geoasn_load(dt, root): """Loads the nearest possible GeoDB & AsnDB files to the given data and return as tuple""" # find nearest existing dtu = dt while dtu > dt-datetime.timedelta(days=10): files = geoasn_exists(dtu, root) if files: break dtu -= datetime.timedelta(days = 1) if not files: raise Exception('Geo & ASN files for %s not found' % dt) # load & return geodb_file, asndb_file = files asndb = PyASN.new( asndb_file ) if platform.system() == "Linux": import GeoIP geodb = GeoIP.open( geodb_file, GeoIP.GEOIP_MEMORY_CACHE) else: import pygeoip geodb = pygeoip.GeoIP( geodb_file, pygeoip.MEMORY_CACHE) return geodb,asndb, (geodb_file,asndb_file)
def geoasn_load(dt, root): """Loads the nearest possible GeoDB & AsnDB files to the given data and return as tuple""" # find nearest existing dtu = dt while dtu > dt - datetime.timedelta(days=10): files = geoasn_exists(dtu, root) if files: break dtu -= datetime.timedelta(days=1) if not files: raise Exception('Geo & ASN files for %s not found' % dt) # load & return geodb_file, asndb_file = files asndb = PyASN.new(asndb_file) if platform.system() == "Linux": import GeoIP geodb = GeoIP.open(geodb_file, GeoIP.GEOIP_MEMORY_CACHE) else: import pygeoip geodb = pygeoip.GeoIP(geodb_file, pygeoip.MEMORY_CACHE) return geodb, asndb, (geodb_file, asndb_file)
def test_all_ipasn_dbs(self): """ Checks compatibility of PyASN 1.2 results with current pyasn for all 2014 ipasn dbs . """ version = sys.version_info[0] try: import PyASN assert version == 2 except: print("SKIPPING - Python 2 or PyASN 1.2 not present ...", file=sys.stderr, end=' ') return dbs = glob(IPASN_DB_PATH + "ipasn_2014*.dat") print("", file=sys.stderr) for db in sorted(dbs): random.seed(db) # for reproducibility print("comparing %s" % db, file=sys.stderr) newdb = pyasn.pyasn(db) olddb = PyASN.new(db) for i in range(1000000): i1 = random.randint(1, 223) i2 = random.randint(0, 255) i3 = random.randint(0, 255) i4 = random.randint(0, 255) sip = "%d.%d.%d.%d" % (i1, i2, i3, i4) newas, prefix = newdb.lookup(sip) if newas: self.assertTrue(newas > 0, msg="Negative AS for IP %s = %s" % (sip, newas)) oldas = olddb.Lookup(sip) if oldas < 0: # this is an overflow bug in the old version, # e.g. 193.181.4.145 on 2014/10/07 returns -33785576 continue self.assertEqual(oldas, newas, msg="Failed for IP %s" % sip)
def loadASN(): p = PyASN.new('ipasndat.gz') # see below for more info print "[+] Total number of records loaded: %s" % p.records print "" return p
def __init__(self, main_config_file_loc): super(AsnScanner, self).__init__(main_config_file_loc) # pyASN actually queries the Databases self.asn_mapfile = self.mainConfig.get("asn", "mapfile") self.pyasn = PyASN.new(self.asn_mapfile)