eris/ingestors/ingest_rir_transfers.py

190 lines
6.3 KiB
Python
Raw Normal View History

2024-03-14 00:51:53 +00:00
#!/usr/bin/env python
# Elasticsearch Recon Ingestion Scripts (ERIS) - Developed by Acidvegas (https://git.acid.vegas/eris)
# ingest_rir_transfers.py
import json
import ipaddress
import time
try:
import aiohttp
except ImportError:
raise ImportError('Missing required \'aiohttp\' library. (pip install aiohttp)')
# Set a default elasticsearch index if one is not provided
2024-11-22 06:18:28 +00:00
default_index = 'eris-rir-transfers'
2024-03-14 00:51:53 +00:00
# Transfers data sources
transfers_db = {
'afrinic' : 'https://ftp.afrinic.net/stats/afrinic/transfers/transfers_latest.json',
'apnic' : 'https://ftp.apnic.net/stats/apnic/transfers/transfers_latest.json',
'arin' : 'https://ftp.arin.net/pub/stats/arin/transfers/transfers_latest.json',
'lacnic' : 'https://ftp.lacnic.net/pub/stats/lacnic/transfers/transfers_latest.json',
'ripencc' : 'https://ftp.ripe.net/pub/stats/ripencc/transfers/transfers_latest.json'
}
2024-03-14 00:51:53 +00:00
def construct_map() -> dict:
'''Construct the Elasticsearch index mapping for records'''
# Match on exact value or full text search
keyword_mapping = { 'type': 'text', 'fields': { 'keyword': { 'type': 'keyword', 'ignore_above': 256 } } }
# Construct the index mapping
mapping = {
'mappings': {
'properties': {
'date' : { 'type': 'date' },
'ip4nets' : {
'properties': {
2024-11-22 06:18:28 +00:00
'original_set': { 'properties': { 'start_address': { 'type': 'ip' }, 'end_address': { 'type': 'ip' } } },
'transfer_set': { 'properties': { 'start_address': { 'type': 'ip' }, 'end_address': { 'type': 'ip' } } }
2024-03-14 00:51:53 +00:00
}
},
'ip6nets' : {
'properties': {
2024-11-22 06:18:28 +00:00
'original_set': { 'properties': { 'start_address': { 'type': 'ip' }, 'end_address': { 'type': 'ip' } } },
'transfer_set': { 'properties': { 'start_address': { 'type': 'ip' }, 'end_address': { 'type': 'ip' } } }
2024-03-14 00:51:53 +00:00
}
},
'asns' : {
'properties': {
2024-11-22 06:18:28 +00:00
'original_set': { 'properties': { 'start': { 'type': 'integer' }, 'end': { 'type': 'integer' } } },
'transfer_set': { 'properties': { 'start': { 'type': 'integer' }, 'end': { 'type': 'integer' } } }
2024-03-14 00:51:53 +00:00
}
},
'type' : { 'type': 'keyword' },
2024-11-22 06:18:28 +00:00
'source_organization' : { 'properties': { 'name': keyword_mapping, 'country_code': { 'type': 'keyword' } } },
'recipient_organization' : { 'properties': { 'name': keyword_mapping, 'country_code': { 'type': 'keyword' } } },
2024-03-14 00:51:53 +00:00
'source_rir' : { 'type': 'keyword' },
'recipient_rir' : { 'type': 'keyword' },
2024-11-22 06:18:28 +00:00
'seen' : { 'type': 'date' }
2024-03-14 00:51:53 +00:00
}
}
}
return mapping
async def process_data():
'''Read and process the transfers data.'''
for registry, url in transfers_db.items():
try:
headers = {'Connection': 'keep-alive'} # This is required for AIOHTTP connections to LACNIC
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url) as response:
if response.status != 200:
2024-03-14 00:54:36 +00:00
raise Exception(f'Failed to fetch {registry} delegation data: {response.status}')
2024-03-14 00:51:53 +00:00
data = await response.text()
try:
json_data = json.loads(data)
except json.JSONDecodeError as e:
2024-03-14 00:54:36 +00:00
raise Exception(f'Failed to parse {registry} delegation data: {e}')
2024-03-14 00:51:53 +00:00
if 'transfers' not in json_data:
2024-03-14 00:54:36 +00:00
raise Exception(f'Invalid {registry} delegation data: {json_data}')
2024-03-14 00:51:53 +00:00
for record in json_data['transfers']:
2024-11-22 06:18:28 +00:00
record['seen'] = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
2024-03-14 00:51:53 +00:00
if 'asns' in record:
for set_type in ('original_set', 'transfer_set'):
if set_type in record['asns']:
count = 0
for set_block in record['asns'][set_type]:
for option in ('start', 'end'):
asn = set_block[option]
if type(asn) != int:
if not asn.isdigit():
2024-03-14 00:54:36 +00:00
raise Exception(f'Invalid {set_type} {option} ASN in {registry} data: {asn}')
2024-03-14 00:51:53 +00:00
else:
record['asns'][set_type][count][option] = int(asn)
count += 1
2024-03-14 00:51:53 +00:00
if 'ip4nets' in record or 'ip6nets' in record:
for ip_type in ('ip4nets', 'ip6nets'):
if ip_type in record:
for set_type in ('original_set', 'transfer_set'):
if set_type in record[ip_type]:
count = 0
for set_block in record[ip_type][set_type]:
for option in ('start_address', 'end_address'):
try:
ipaddress.ip_address(set_block[option])
except ValueError as e:
octets = set_block[option].split('.')
normalized_ip = '.'.join(str(int(octet)) for octet in octets)
try:
ipaddress.ip_address(normalized_ip)
record[ip_type][set_type][count][option] = normalized_ip
except ValueError as e:
2024-03-14 00:54:36 +00:00
raise Exception(f'Invalid {set_type} {option} IP in {registry} data: {e}')
2024-03-14 00:51:53 +00:00
count += 1
2024-03-14 00:51:53 +00:00
if record['type'] not in ('MERGER_ACQUISITION', 'RESOURCE_TRANSFER'):
2024-03-14 00:54:36 +00:00
raise Exception(f'Invalid transfer type in {registry} data: {record["type"]}')
2024-03-14 00:51:53 +00:00
yield {'_index': default_index, '_source': record}
except Exception as e:
2024-03-14 00:54:36 +00:00
raise Exception(f'Error processing {registry} delegation data: {e}')
2024-03-14 00:51:53 +00:00
async def test():
'''Test the ingestion process'''
async for document in process_data():
print(document)
if __name__ == '__main__':
import asyncio
asyncio.run(test())
'''
Output:
{
"transfer_date" : "2017-09-15T19:00:00Z",
"ip4nets" : {
"original_set" : [ { "start_address": "94.31.64.0", "end_address": "94.31.127.255" } ],
"transfer_set" : [ { "start_address": "94.31.64.0", "end_address": "94.31.127.255" } ]
},
"type" : "MERGER_ACQUISITION",
"source_organization" : { "name": "Unser Ortsnetz GmbH" },
"recipient_organization" : {
"name" : "Deutsche Glasfaser Wholesale GmbH",
"country_code" : "DE"
},
"source_rir" : "RIPE NCC",
"recipient_rir" : "RIPE NCC"
},
2024-03-14 00:51:53 +00:00
{
"transfer_date" : "2017-09-18T19:00:00Z",
"asns" : {
"original_set" : [ { "start": 198257, "end": 198257 } ],
"transfer_set" : [ { "start": 198257, "end": 198257 } ]
},
"type" : "MERGER_ACQUISITION",
"source_organization" : { "name": "CERT PLIX Sp. z o.o." },
"recipient_organization" : {
"name" : "Equinix (Poland) Sp. z o.o.",
2024-03-14 00:51:53 +00:00
"country_code" : "PL"
},
"source_rir" : "RIPE NCC",
"recipient_rir" : "RIPE NCC"
}
2024-03-14 00:51:53 +00:00
Input:
Nothing changed from the output for now...
2024-03-14 00:51:53 +00:00
'''