Skip to content

Instantly share code, notes, and snippets.

@darkk
Last active April 22, 2018 11:29
Show Gist options
  • Save darkk/926c3ef9673d5ed794f2ce1632ab996c to your computer and use it in GitHub Desktop.
Save darkk/926c3ef9673d5ed794f2ce1632ab996c to your computer and use it in GitHub Desktop.
Токсичный реестр... лишние префиксы... бр-р-р!
#!/usr/bin/env python3
import ipaddress
import sys
nets = [ipaddress.ip_network(line.strip())
for line in open('subnets.txt')]
nets.sort()
def do_pass():
for i in range(len(nets)):
for j in range(i+1, len(nets)):
if nets[i].overlaps(nets[j]):
todel = max((i, j), key=lambda x: nets[x].prefixlen)
print(nets[i], '&', nets[j], '=> del', nets[todel], file=sys.stderr)
del nets[todel]
return True
return False
while do_pass():
pass
addr_count = 0
for _ in nets:
print(_)
addr_count += _.num_addresses
for line in open('ips.txt'):
addr = ipaddress.ip_address(line.strip())
for n in nets:
if addr in n:
print(addr, '&', n, '=> del', addr, file=sys.stderr)
break
else:
print(addr)
addr_count += 1
print('Total:', addr_count, 'IP blacklisted', file=sys.stderr)
@darkk
Copy link
Author

darkk commented Apr 21, 2018

$ git log -1 
commit 3e71be81c0d9740536c2334211d49a78a97895ef
Author: Zapret Info <[email protected]>
Date:   Sat Apr 21 12:51:50 2018 +0200

    Updated: 2018-04-21 10:30:00 +0000

$ ./detox.py 2>&1 >/dev/null | head -5
34.192.0.0/10 & 34.240.0.0/13 => del 34.240.0.0/13
34.192.0.0/10 & 34.248.0.0/13 => del 34.248.0.0/13
13.125.217.123 & 13.125.0.0/16 => del 13.125.217.123
13.230.197.97 & 13.230.0.0/15 => del 13.230.197.97
13.231.129.93 & 13.230.0.0/15 => del 13.231.129.93

$ ./detox.py 2>&1 >/dev/null | tail -5
185.220.34.41 & 185.220.34.0/24 => del 185.220.34.41
188.166.125.97 & 188.166.0.0/17 => del 188.166.125.97
203.104.142.52 & 203.104.128.0/20 => del 203.104.142.52
206.189.127.223 & 206.189.0.0/16 => del 206.189.127.223
Total: 17998598 IP blacklisted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment