Created
April 19, 2018 19:33
-
-
Save atastycookie/6c1de19389a8ae4024c36e3aeefc80ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import socket | |
import binascii | |
import sys | |
SUBNET_LIST = { | |
"52.56.0.0/16": "Amazon AWS", | |
"206.189.0.0/16": "DigitalOcean", | |
"167.99.0.0/16": "DigitalOcean", | |
"165.227.0.0/16": "DigitalOcean", | |
"159.89.0.0/16": "DigitalOcean", | |
"206.189.0.0/16": "DigitalOcean", | |
"167.99.0.0/16": "DigitalOcean", | |
"35.208.0.0/12": "GOOGLE-CLOUD", | |
"35.224.0.0/12": "GOOGLE-CLOUD", | |
"35.192.0.0/12": "GOOGLE-CLOUD", | |
"35.184.0.0/13": "GOOGLE-CLOUD", | |
"34.192.0.0/10": "Amazon AWS", | |
"52.192.0.0/11": "Amazon AWS", | |
"52.64.0.0/12": "Amazon AWS", | |
"54.144.0.0/12": "Amazon AWS", | |
"54.160.0.0/12": "Amazon AWS", | |
"34.240.0.0/13": "Amazon AWS", | |
"34.248.0.0/13": "Amazon AWS", | |
"18.130.0.0/16": "Amazon AWS", | |
"13.125.0.0/16": "Amazon AWS", | |
"52.57.0.0/16": "Amazon AWS", | |
"35.180.0.0/16": "Amazon AWS", | |
"18.144.0.0/16": "Amazon AWS", | |
"52.58.0.0/15": "Amazon AWS", | |
"159.89.0.0/16": "DigitalOcean", | |
"18.196.0.0/15": "Amazon AWS", | |
"18.184.0.0/15": "Amazon AWS", | |
"54.228.0.0/15": "Amazon AWS", | |
"35.178.0.0/15": "Amazon AWS", | |
"35.156.0.0/14": "Amazon AWS", | |
"13.56.0.0/14": "Amazon AWS", | |
"35.160.0.0/13": "Amazon AWS", | |
"18.194.0.0/15": "Amazon AWS", | |
"165.227.0.0/16": "DigitalOcean", | |
"139.59.0.0/16": "DigitalOcean, LLC", | |
"18.236.0.0/15": "Amazon.com, Inc.", | |
"54.64.0.0/13": "Amazon Technologies Inc.", | |
"23.251.128.0/19": "Google LLC", | |
"174.138.0.0/17": "DigitalOcean, LLC", | |
"188.166.0.0/17": "Digital Ocean, Inc.", | |
"159.203.0.0/16": "DigitalOcean, LLC", | |
"128.199.0.0/16": "DigitalOcean Cloud", | |
"159.65.0.0/16": "DigitalOcean, LLC", | |
"52.32.0.0/16": "Amazon Technologies Inc.", | |
"54.212.0.0/15": "Amazon.com, Inc.", | |
"18.218.0.0/16": "Amazon Technologies Inc.", | |
"13.230.0.0/15": "Amazon Data Services Japan", | |
"35.176.0.0/15": "Amazon Data Services UK", | |
"195.154.0.0/17": "Iliad Entreprises Customers", | |
"51.136.0.0/15": "Microsoft Limited UK", | |
"46.101.128.0/17": "Digital Ocean, Inc.", | |
"185.166.212.0/23": "Clouding.io Virtual Machine Hosting", | |
"178.63.0.0/16": "Hetzner Online GmbH", | |
"51.15.0.0/16": "ONLINENETDEDICATEDSERVERSNL", | |
"18.204.0.0/14": "Amazon Technologies Inc.", | |
"91.121.0.0/16": "OVH SAS" | |
} | |
def ip_in_subnetwork(ip_address, subnetwork): | |
(ip_integer, version1) = ip_to_integer(ip_address) | |
(ip_lower, ip_upper, version2) = subnetwork_to_ip_range(subnetwork) | |
if version1 != version2: | |
raise ValueError("incompatible IP versions") | |
return (ip_lower <= ip_integer <= ip_upper) | |
def ip_to_integer(ip_address): | |
for version in (socket.AF_INET, socket.AF_INET6): | |
try: | |
ip_hex = socket.inet_pton(version, ip_address) | |
ip_integer = int(binascii.hexlify(ip_hex), 16) | |
return (ip_integer, 4 if version == socket.AF_INET else 6) | |
except: | |
pass | |
raise ValueError("invalid IP address") | |
def subnetwork_to_ip_range(subnetwork): | |
try: | |
fragments = subnetwork.split('/') | |
network_prefix = fragments[0] | |
netmask_len = int(fragments[1]) | |
for version in (socket.AF_INET, socket.AF_INET6): | |
ip_len = 32 if version == socket.AF_INET else 128 | |
try: | |
suffix_mask = (1 << (ip_len - netmask_len)) - 1 | |
netmask = ((1 << ip_len) - 1) - suffix_mask | |
ip_hex = socket.inet_pton(version, network_prefix) | |
ip_lower = int(binascii.hexlify(ip_hex), 16) & netmask | |
ip_upper = ip_lower + suffix_mask | |
return (ip_lower, | |
ip_upper, | |
4 if version == socket.AF_INET else 6) | |
except: | |
pass | |
except: | |
pass | |
raise ValueError("invalid subnetwork") | |
if __name__ == '__main__': | |
try: | |
ip2check = sys.argv[1] | |
for subnet in SUBNET_LIST: | |
if ip_in_subnetwork(ip2check, subnet): | |
print "RKN block IP {}({}) with subnet {}. Check it here: https://blocklist.rkn.gov.ru".format(ip2check, dict(SUBNET_LIST)[subnet], subnet) | |
else: | |
print "Looks should be fine. But who knows" | |
except: | |
print "python rknblock.py 127.0.0.1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment