def GGCIPS(self, File=root + "ggc.txt"): try: F = open(File) except: raise IOError, "%s is not exist" % File IPS = re.split("[\r\n]+", F.read()) F.close() V4s = [] V6s = [] for IPD in IPS: IPD = re.sub("\s+", "", IPD) if len(IPD) == 0: continue if IPD[0] == "#": continue try: V4s.append(self.v4_Tran(IPD)) except: try: V6s.append(self.v6_Tran(IPD)) except: print(IPD) raise gc.collect() return [IPy.IPSet(V4s), IPy.IPSet(V6s)]
def setUp(self): #array self.a = [IPy.IP("192.168." + str(i) + ".0/24") for i in range(256)] #range self.r = IPy.IP('192.168.0.0/16') #testing set self.t = IPy.IPSet(self.a) #control set self.c = IPy.IPSet(self.a) #Could otherwise look like 192.168.128.0/17 self.sixRange = IPy.IP('::c0a8:8000/113')
def main(): china_list_set = IPy.IPSet() for line in sys.stdin: line_params = line.split("|") if len(line_params) < 5 or line_params[2] != "ipv4" or line_params[1] != "CN": continue ip_addr = line_params[3] ip_length = float(line_params[4]) ip_mask = 32 - int(math.ceil(math.log(ip_length, 2))) china_list_set.add(IPy.IP("%s/%d" % (ip_addr, ip_mask))) # 添加内网地址 internal_list = IPy.IPSet(map(IPy.IP, [ "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "112.124.47.0/24", "114.114.114.0/24", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/29", "192.0.2.0/24", "192.88.99.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "224.0.0.0/4", "240.0.0.0/4", ])) china_list_set += internal_list all = china_list_set # 取反 # all = IPy.IPSet([IPy.IP("0.0.0.0/0")]) # 剔除所有孤立的C段 # for ip in china_list_set: # all.discard(ip) # filter = itertools.ifilter(lambda x: len(x) <= 65536, all) # for ip in filter: # all.discard(ip) # all.add(IPy.IP(ip.strNormal(0)).make_net('255.255.0.0')) # 输出结果 for ip in all: print '<item>' + str(ip) + '</item>'
def main(): china_list_set = IPy.IPSet() for line in sys.stdin: china_list_set.add(IPy.IP(line)) # 输出结果 for ip in china_list_set: print '<item>' + str(ip) + '</item>'
def testRandomContains(self): prefixes = [random_ipv4_prefix() for i in xrange(random.randrange(50))] question = random_ipv4_prefix() answer = any(question in pfx for pfx in prefixes) ipset = IPy.IPSet(prefixes) self.assertEqual( question in ipset, answer, "%s in %s != %s (made from %s)" % (question, ipset, answer, prefixes))
def testContains(self): self.assertTrue(IPy.IP('192.168.15.32/28') in self.t) self.assertFalse(IPy.IP('192.169.15.32/28') in self.t) # test for a regression where __contains__ prematurely returns False # after testing a prefix length where all IP instances are greater than # the query IP. ipset = IPy.IPSet([IPy.IP('10.0.0.0/8'), IPy.IP('128.0.0.0/1')]) self.assertTrue(IPy.IP('10.0.0.0') in ipset)
def main(): china_list_set = IPy.IPSet() for line in sys.stdin: china_list_set.add(IPy.IP(line)) # 添加内网地址 internal_list = IPy.IPSet( map(IPy.IP, [ "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "112.124.47.0/24", "114.114.114.0/24", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/29", "192.0.2.0/24", "192.88.99.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "224.0.0.0/4", "240.0.0.0/4", ])) china_list_set += internal_list all = china_list_set # 取反 # all = IPy.IPSet([IPy.IP("0.0.0.0/0")]) # 剔除所有孤立的C段 # for ip in china_list_set: # all.discard(ip) # filter = itertools.ifilter(lambda x: len(x) <= 65536, all) # for ip in filter: # all.discard(ip) # all.add(IPy.IP(ip.strNormal(0)).make_net('255.255.0.0')) # 输出结果 for ip in all: print '<item>' + str(ip) + '</item>'
def testAnd(self): ten24s = IPy.IPSet([ IPy.IP('10.0.1.0/24'), IPy.IP('10.0.3.0/24'), IPy.IP('10.0.5.0/24'), IPy.IP('10.0.7.0/24'), ]) self.assertEqual(ten24s & IPy.IPSet([IPy.IP('10.0.1.10')]), IPy.IPSet([IPy.IP('10.0.1.10')])) self.assertEqual(ten24s & IPy.IPSet([ IPy.IP('10.0.0.99'), IPy.IP('10.0.1.10'), IPy.IP('10.0.3.40'), IPy.IP('11.1.1.99'), ]), IPy.IPSet([ IPy.IP('10.0.1.10'), IPy.IP('10.0.3.40'), ]))
def testRandomDisjoint(self): prefixes1 = [ random_ipv4_prefix() for i in xrange(random.randrange(50)) ] prefixes2 = [ random_ipv4_prefix() for i in xrange(random.randrange(50)) ] # test disjointnes the stupid way disjoint = True for p1, p2 in itertools.product(prefixes1, prefixes2): if p1 in p2 or p2 in p1: disjoint = False break ipset1 = IPy.IPSet(prefixes1) ipset2 = IPy.IPSet(prefixes2) self.assertEqual( ipset1.isdisjoint(ipset2), disjoint, "%s.isdisjoint(%s) != %s" % (ipset1, ipset2, disjoint)) self.assertEqual( ipset2.isdisjoint(ipset1), disjoint, "%s.isdisjoint(%s) != %s" % (ipset2, ipset1, disjoint))
def main(): args = parser.parse_args() args = vars(args) ports = [] for port in args["port"]: for port in port.split(','): ports.append(port) args["port"] = ports tlsv1 = None if args["host"] is not None: for host in args["host"]: for p in args["port"]: sslv3 = check_sslv3(host, p) if args["tls"] == True: tlsv1 = check_tls(host, p) print_results(host, p, sslv3, tlsv1) return net = IPy.IPSet() for network in args["network"]: net.add(IPy.IP(network)) if args["parallel"]: p = multiprocessing.Pool() q = multiprocessing.Queue() for ip in net: q.put((check_net, ip, args["port"], args["tls"])) while True: items = q.get() func = items[0] args = items[1:] p.apply_async(func, args) if q.empty(): p.close() p.join() break else: for ip in net: check_net(ip, args["port"], args["tls"])
def main(hostname): arr = { 'host': [hostname], 'port': [443], 'network': None, 'tls': False, 'parallel': False } tlsv1 = None if arr["host"] is not None: for host in arr["host"]: for p in arr["port"]: sslv3 = check_sslv3(host, p) if arr["tls"] == True: tlsv1 = 'null' print_results(host, p, sslv3, tlsv1) return net = IPy.IPSet() for network in arr["network"]: net.add(IPy.IP(network)) if arr["parallel"]: p = multiprocessing.Pool() q = multiprocessing.Queue() for ip in net: q.put((check_net, ip, arr["port"], arr["tls"])) while True: items = q.get() func = items[0] args = items[1:] p.apply_async(func, args) if q.empty(): p.close() p.join() break else: for ip in net: check_net(ip, arr["port"], arr["tls"])
def testIsdisjoint(self): self.assertTrue( IPy.IPSet([IPy.IP('0.0.0.0/1') ]).isdisjoint(IPy.IPSet([IPy.IP('128.0.0.0/1')]))) self.assertFalse( IPy.IPSet([IPy.IP('0.0.0.0/1') ]).isdisjoint(IPy.IPSet([IPy.IP('0.0.0.0/2')]))) self.assertFalse( IPy.IPSet([IPy.IP('0.0.0.0/2') ]).isdisjoint(IPy.IPSet([IPy.IP('0.0.0.0/1')]))) self.assertFalse( IPy.IPSet([IPy.IP('0.0.0.0/2') ]).isdisjoint(IPy.IPSet([IPy.IP('0.1.2.3')]))) self.assertFalse( IPy.IPSet([IPy.IP('0.1.2.3') ]).isdisjoint(IPy.IPSet([IPy.IP('0.0.0.0/2')]))) self.assertTrue( IPy.IPSet([IPy.IP('1.1.1.1'), IPy.IP('1.1.1.3')]).isdisjoint( IPy.IPSet([IPy.IP('1.1.1.2'), IPy.IP('1.1.1.4')]))) self.assertFalse( IPy.IPSet([ IPy.IP('1.1.1.1'), IPy.IP('1.1.1.3'), IPy.IP('1.1.2.0/24') ]).isdisjoint(IPy.IPSet([IPy.IP('1.1.2.2'), IPy.IP('1.1.1.4')])))
# 将数据库中的数据转行成画图说要用到的数据 # 将数据保存到文件 # 这样不用每次画图都重新从数据库获取数据并计算 import IPy import json from pymongo import MongoClient from conf import DB_URL, dbname, setname my_client = MongoClient(DB_URL) my_db = my_client.get_database(dbname) my_set = my_db.get_collection(setname) result = my_set.find() result_list = list(result[:]) ip_list = IPy.IPSet() data = {} Trace = set() Nodes_Dst = set() Nodes_Route = set() def load_ip_list(): try: with open("./data/ip.list", "r") as fp: tmp = fp.readlines() for ip in tmp: ip_list.add(IPy.IP(ip.strip())) return True except Exception as err: print(err)
# import IPy # ip = IPy(ips.get_remote_ip(self.request)) # gce_list = '130.211.0.0/16' # gce_list.overlaps(ip) import IPy from dancedeets.util import ips banned_list = IPy.IPSet([ IPy.IP('188.165.25.197'), # A scraper that hammered the site too quickly. IPy.IP( '46.101.245.227' ), # Digital Ocean Scraper. Hammered my server for city pages, which started 502-ing on user traffic. ]) def is_abuse(request): user_agent = request.headers.get('user-agent', 'NO_USER_AGENT') if 'python-requests' in user_agent: return True ip = IPy.IP(ips.get_remote_ip(request)) if ip in banned_list: return True return False
# import IPy # ip = IPy(ips.get_remote_ip(self.request)) # gce_list = '130.211.0.0/16' # gce_list.overlaps(ip) import IPy from util import ips banned_list = IPy.IPSet([IPy.IP('188.165.25.197')]) def is_abuse(request): user_agent = request.headers.get('user-agent', 'NO_USER_AGENT') if 'python-requests' in user_agent: return True ip = IPy.IP(ips.get_remote_ip(request)) if ip in banned_list: return True return False