Skip to content

Instantly share code, notes, and snippets.

@barreljan
Last active January 16, 2025 11:05
Show Gist options
  • Save barreljan/a3f175ecb2278a473d5e3553c1471ace to your computer and use it in GitHub Desktop.
Save barreljan/a3f175ecb2278a473d5e3553c1471ace to your computer and use it in GitHub Desktop.
Check affected ip-addresses versus your aggregates
#!/usr/bin/env python3
"""This checks over large text files if your ip address if on a list
particular handy with CVEs and leaked data.
syntax:
./affected.py iplistofleak.txt
Needs 2 files to start:
1: a text file named 'aggregates' of your AS-system with CIDR notation,
per line, no other data or empty lines. This could be /32 if needed
2: a text file containing addresses, per line, no other data or empty lines
Example for 1:
10.0.0.0/8
192.168.1.0/24
172.19.31.10/32
Example for 2:
10.0.1.1
10.2.3.4/32
192.168.1.9
"""
import sys
from ipaddress import ip_network
def get_lines(_input_file):
try:
with open(_input_file, 'r') as _input:
_lines = _input.read().splitlines()
except FileNotFoundError:
raise SystemExit(f"Could not read file {f}!")
else:
return _lines
def subnet_of(pfx):
for aggregate in aggregates:
if pfx.subnet_of(aggregate) or pfx == aggregate:
return True
return False
def matching(_input_file):
_hits = []
for line in get_lines(_input_file):
route = ip_network(line)
try:
if subnet_of(route):
_hits.append(route)
except TypeError:
# can not check ipv4 on ipv6 or vice versa
continue
return _hits
if __name__ == '__main__':
try:
input_affected = sys.argv[1]
except IndexError:
raise SystemExit(f"Wrong syntax")
aggregates = []
for line in get_lines('./aggregates'):
aggregates.append(ip_network(line))
affected_count = len(get_lines(input_affected))
print(f'Scanning over {affected_count} affected ip addresses:')
hits = matching(input_affected)
print(f'Found {len(hits)}', end='')
if len(hits) > 0:
print(':')
for item in hits:
print(item.compressed)
else:
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment