def get_illegal_ranges(cidr_list): all_cidrs = [] first_network_range = IPRange( "0.0.0.0", get_first_address_in_network_minus_one(cidr_list[0])) for network in first_network_range.cidrs(): all_cidrs.append(network.cidr.__str__()) # print(f"netmask -c 0.0.0.0:{get_first_address_in_network_minus_one(CIDR_LIST[0])}") i = 0 how_long = len(cidr_list) + 1 while i < how_long: # print(i) if i is len(cidr_list) - 1: last_network_range = IPRange( get_last_address_in_network_plus_one(cidr_list[i]), '255.255.255.255') for network in last_network_range.cidrs(): all_cidrs.append(network.cidr.__str__()) i += 2 else: second_network_range = IPRange( get_last_address_in_network_plus_one(cidr_list[i]), get_first_address_in_network_minus_one(cidr_list[i + 1])) for network in second_network_range.cidrs(): all_cidrs.append(network.cidr.__str__()) i += 1 return all_cidrs
def test_iprange(): range1 = IPRange('192.0.2.1', '192.0.2.15') assert range1 == IPRange('192.0.2.1', '192.0.2.15') assert range1.cidrs() == [ IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29'), ] assert IPRange('192.0.2.0', '192.0.2.255') == IPNetwork('192.0.2.0/24') range2 = IPRange('192.0.2.1', '192.0.2.15') addrs = list(range2) assert addrs == [ IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.4'), IPAddress('192.0.2.5'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9'), IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPAddress('192.0.2.12'), IPAddress('192.0.2.13'), IPAddress('192.0.2.14'), IPAddress('192.0.2.15'), ] assert range2 != addrs assert list(range2) == addrs subnets = range2.cidrs() assert subnets == [ IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29') ] assert range2 != subnets assert range2.cidrs() == subnets
def parse_list(raw_list): ''' INPUT: Takes raw data that was downloaded from the download_list function. ACTION: Verifies each line is a valid IPv4 or IPv6 address or address range. OUTPUT: Returns a list of valid IP addresses. ''' ip_list = [] for line in raw_list: try: #Ignore commented lines if line.startswith('#') or line.startswith(';') or len(line) == 0: pass else: #drops extraneous data that is included after IP addresses in some lists (41.138.172.0/23 ; SBL208940) line = line.split()[0] #parse generic IP ranges. i.e. - 192.168.1.5-192.168.1.65 if '-' in line: start_ip = line.split('-')[0] end_ip = line.split('-')[1] ip_range = IPRange(start_ip, end_ip) subnets = ip_range.cidrs() for subnet in subnets: ip_list.append(subnet) else: #parse anything else. i.e. - 10.0.0.0/8, 1.2.3.4 netblock = IPNetwork(line) ip_list.append(netblock) except AddrFormatError as e: print e print 'Failed to parse ' + line return ip_list
def queryIpWithUpdate(self, ip, masklen=24): if not ip in self.previousSinaIpSet: self.rnode = self.rtree.search_best(ip) if not self.rnode:#this ip is not in the prefix table jsonData = query_local(ip) if jsonData: start = jsonData.get("start","") if start:#we have the sina data: end = jsonData["end"] iprange = IPRange(start, end) self.previousSinaIpSet = IPSet(iprange.cidrs()) for net_tuple in split_network_from_start_to_end(start, end): self.addPrefix(net_tuple[0], net_tuple[1]) for k in ipRadixDB.recordKeys: if k == "ip": self.rnode.data[k] = jsonData.get(k,ip) else: self.rnode.data[k] = jsonData[k] else: self.addPrefix(ip, masklen) for k in ipRadixDB.recordKeys: self.rnode.data[k] = jsonData[k] else: if self.rnode.prefixlen < 24 or not self.rnode.data.get("country",""):#only the prefix is bigger than x/24 network jsonData = query_local(ip)#maybe the prefix is too large, we need to substrac the prefix if jsonData: jsonData["ip"] = ip self.substractPrefix(self.rnode, jsonData)
def test_ipset_membership(): iprange = IPRange('192.0.1.255', '192.0.2.16') assert iprange.cidrs() == [ IPNetwork('192.0.1.255/32'), IPNetwork('192.0.2.0/28'), IPNetwork('192.0.2.16/32'), ] ipset = IPSet(['192.0.2.0/28']) assert [(str(ip), ip in ipset) for ip in iprange] == [ ('192.0.1.255', False), ('192.0.2.0', True), ('192.0.2.1', True), ('192.0.2.2', True), ('192.0.2.3', True), ('192.0.2.4', True), ('192.0.2.5', True), ('192.0.2.6', True), ('192.0.2.7', True), ('192.0.2.8', True), ('192.0.2.9', True), ('192.0.2.10', True), ('192.0.2.11', True), ('192.0.2.12', True), ('192.0.2.13', True), ('192.0.2.14', True), ('192.0.2.15', True), ('192.0.2.16', False), ]
def substractPrefix(self, rnode, jsonData): data = rnode.data prefixlen = rnode.prefixlen network = rnode.network if (jsonData["city"] != data.get("city","") or jsonData["province"] != data.get("province","") or jsonData["isp"] != data.get("isp", "") or jsonData["country"] != data.get("isp", "")):#we got the sina json data start = jsonData.get("start", "") if start: end = jsonData["end"] iprange = IPRange(start, end) self.previousSinaIpSet = IPSet(iprange.cidrs()) for net_tuple in split_network_from_start_to_end(start, end): self.rnode = self.rtree.search_exact(net_tuple[0], net_tuple[1]) if not self.rnode:#//while we donn't have this node self.addPrefix(net_tuple[0], net_tuple[1])#add this node if (network == net_tuple[0]):#the node we added start from our previous node start point data["ip_amount"] -= net_tuple[2] #we substract a subnetwork form the big network, we need to decrease the ip amount else: ip_remove_amount = min(ip_integer_from_string(network) + (1 << (32 - prefixlen)) - ip_integer_from_string(net_tuple[0]), 1<<(32-net_tuple[1])) if ip_remove_amount > 0: data["ip_amount"] -= ip_remove_amount for k in ipRadixDB.recordKeys: self.rnode.data[k] = jsonData[k] if data["ip_amount"] <= 0:#we have divided the big prefix into small piece self.delPrefix(network, prefixlen) #delete big prefix else:#we just have taobao's data, just update the node for k in ipRadixDB.recordKeys: self.rnode.data[k] = jsonData[k]
def ip_range_within_network(ip_range, network): """Check that the whole of a given IP range is within a given network.""" # Make sure that ip_range is an IPRange and not an IPNetwork, # otherwise this won't work. if isinstance(ip_range, IPNetwork): ip_range = IPRange(IPAddress(network.first), IPAddress(network.last)) return all([intersect_iprange(cidr, network) for cidr in ip_range.cidrs()])
def test_ipset_basic_api(): range1 = IPRange('192.0.2.1', '192.0.2.15') ip_list = [ IPAddress('192.0.2.1'), '192.0.2.2/31', IPNetwork('192.0.2.4/31'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), '192.0.2.8', '192.0.2.9', IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPNetwork('192.0.2.12/30'), ] set1 = IPSet(range1.cidrs()) set2 = IPSet(ip_list) assert set2 == IPSet([ '192.0.2.1/32', '192.0.2.2/31', '192.0.2.4/30', '192.0.2.8/29', ]) assert set1 == set2 assert set2.pop() in set1 assert set1 != set2
def check_param(getopt_obj): """ @type getopt_obj:Getopt_scanner """ try: int(getopt_obj.thread_number) except: raise InputError("并发参数错误") if getopt_obj.scan_mode != "ping" and getopt_obj.scan_mode != "tcp": raise InputError("检测参数错误") ip_list = [] ip_str = getopt_obj.ipaddr_str if ip_str.find("-") != -1: startip = ip_str.split("-")[0] endip = ip_str.split("-")[1] if not (check_ip(startip) and check_ip(endip)): raise InputError("ip地址参数错误1") iprange_cidrs = IPRange(startip, endip) for net_cidr in iprange_cidrs.cidrs(): for ip in net_cidr.iter_hosts(): ip_list.append(str(ip)) elif ip_str.find("/") != -1: try: for ip in IPNetwork(ip_str): ip_list.append(str(ip)) except: raise InputError("ip地址参数错误2") else: if not check_ip(ip_str): raise InputError("ip地址参数错误3") ip_list.append(ip_str) getopt_obj.ipaddr_list = ip_list
def queryIpWithUpdate(self, ip, masklen=24): if not ip in self.previousSinaIpSet: self.rnode = self.rtree.search_best(ip) if not self.rnode: #this ip is not in the prefix table jsonData = query_local(ip) if jsonData: start = jsonData.get("start", "") if start: #we have the sina data: end = jsonData["end"] iprange = IPRange(start, end) self.previousSinaIpSet = IPSet(iprange.cidrs()) for net_tuple in split_network_from_start_to_end( start, end): self.addPrefix(net_tuple[0], net_tuple[1]) for k in ipRadixDB.recordKeys: if k == "ip": self.rnode.data[k] = jsonData.get(k, ip) else: self.rnode.data[k] = jsonData[k] else: self.addPrefix(ip, masklen) for k in ipRadixDB.recordKeys: self.rnode.data[k] = jsonData[k] else: if self.rnode.prefixlen < 24 or not self.rnode.data.get( "country", ""): #only the prefix is bigger than x/24 network jsonData = query_local( ip ) #maybe the prefix is too large, we need to substrac the prefix if jsonData: jsonData["ip"] = ip self.substractPrefix(self.rnode, jsonData)
def test_iprange(): range1 = IPRange('192.0.2.1', '192.0.2.15') assert range1 == IPRange('192.0.2.1', '192.0.2.15') assert range1.cidrs() == [ IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29'), ] assert IPRange('192.0.2.0', '192.0.2.255') == IPNetwork('192.0.2.0/24') range2 = IPRange('192.0.2.1', '192.0.2.15') addrs = list(range2) assert addrs == [ IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.4'), IPAddress('192.0.2.5'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9'), IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPAddress('192.0.2.12'), IPAddress('192.0.2.13'), IPAddress('192.0.2.14'), IPAddress('192.0.2.15'), ] assert range2 != addrs assert list(range2) == addrs subnets = range2.cidrs() assert subnets == [ IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29')] assert range2 != subnets assert range2.cidrs() == subnets
def _translate_ip_ranges(indicator, value=None): if value is not None and value['type'] != 'IPv4': return [indicator] try: ip_range = IPRange(*indicator.split('-', 1)) except (AddrFormatError, ValueError, TypeError): return [indicator] return [str(x) if x.size != 1 else str(x.network) for x in ip_range.cidrs()]
def make_iplist(l): """ Expect the input to be well-formatted. :param l: list. ip ranges(or single ip) e.g. [('192.0.2.1', '192.0.2.15'), '192.0.3.1'] :return: list. CIRD notation of ips in the range """ re = [] for ip in l: if type(ip) == types.TupleType: r = IPRange(ip[0], ip[1]) re.extend(r.cidrs()) else: # ip is a str. e.g. '192.0.3.1' re.append(IPAddress(ip)) return cidr_merge(re)
def substractPrefix(self, rnode, jsonData): data = rnode.data prefixlen = rnode.prefixlen network = rnode.network if (jsonData["city"] != data.get("city", "") or jsonData["province"] != data.get("province", "") or jsonData["isp"] != data.get("isp", "") or jsonData["country"] != data.get( "isp", "")): #we got the sina json data start = jsonData.get("start", "") if start: end = jsonData["end"] iprange = IPRange(start, end) self.previousSinaIpSet = IPSet(iprange.cidrs()) for net_tuple in split_network_from_start_to_end(start, end): self.rnode = self.rtree.search_exact( net_tuple[0], net_tuple[1]) if not self.rnode: #//while we donn't have this node self.addPrefix(net_tuple[0], net_tuple[1]) #add this node if ( network == net_tuple[0] ): #the node we added start from our previous node start point data["ip_amount"] -= net_tuple[ 2] #we substract a subnetwork form the big network, we need to decrease the ip amount else: ip_remove_amount = min( ip_integer_from_string(network) + (1 << (32 - prefixlen)) - ip_integer_from_string(net_tuple[0]), 1 << (32 - net_tuple[1])) if ip_remove_amount > 0: data["ip_amount"] -= ip_remove_amount for k in ipRadixDB.recordKeys: self.rnode.data[k] = jsonData[k] if data["ip_amount"] <= 0: #we have divided the big prefix into small piece self.delPrefix(network, prefixlen) #delete big prefix else: #we just have taobao's data, just update the node for k in ipRadixDB.recordKeys: self.rnode.data[k] = jsonData[k]
def match_ipv4_name(view_id, ipv4_file_name): try: with codecs.open(ipv4_file_name, 'r', 'UTF-8') as fr: for line in fr: line = line.strip() # 跳过注释行和空行 if not len(line) or line.startswith('#'): continue tmp = line.split() # ip-start ip-end 视图ID # tmp[0] tmp[1] tmp[2] if view_id == tmp[2]: ip = IPRange(tmp[0], tmp[1]) for cidr in ip.cidrs(): logging.info("ip-cidr [%s]\n", str(cidr)) ip_start = str(IPAddress(tmp[0], flags=ZEROFILL)) ip_end = str(IPAddress(tmp[1], flags=ZEROFILL)) logging.info("ip-start [%s], ip-end [%s]\n", ip_start, ip_end) except Exception: logging.error("traceback.format_exc():%s", traceback.format_exc()) sys.exit(1)
#!/usr/bin/env python from netaddr import IPRange from sys import argv lst=open(argv[1],'r').readlines() t=[] for n in lst: t.append(n.strip()) lst=t del t for ipr in lst: ipr=IPRange(ipr[:ipr.find('-')],ipr[ipr.find('-')+1:]) for cidr in ipr.cidrs(): print cidr
def scan_fn_ip(): country_code = {} for line in open('input/country_code', 'r'): code, name = line.split(" ") country_code[code] = name.strip().decode("utf-8") logger.info(code + ' ' + country_code[code]) rtree = ipRadixDB() ip_area_list = [ "input/delegated-arin-latest", "input/delegated-ripencc-latest", "input/delegated-lacnic-latest", "input/delegated-afrinic-latest", "input/delegated-apnic-latest" ] dft = defaultdict(list) availableIPs = [] for f in ip_area_list: seed_file = open(f, 'r') for l in seed_file.readlines(): params = l.split('|') if len(params) >= 4 and params[ 2] == "ipv4" and params[3] != "*" and params[1] != "CN": startIP = params[3] endIP = ip_integer_to_string( ip_integer_from_string(startIP) + int(params[4]) - 1) logger.info(startIP + ' ' + endIP + ' ' + params[4]) iprange = IPRange(startIP, endIP) if params[1] == '': availableIPs += map(str, iprange.cidrs()) else: dft[params[1]] += map(str, iprange.cidrs()) for key in dft: prefix = dft[key][-1] network, masklen = prefix.split('/') masklen = int(masklen) ip = generate_random_ip(network, masklen) ipset = IPSet(dft[key]) for prefix in ipset.iter_cidrs(): network, masklen = str(prefix).split('/') masklen = int(masklen) rtree.addPrefix(network, masklen) data = rtree.rnode.data country = country_code[key] logger.info(str(prefix) + ' ' + country) data['country'] = country #jsonData.get('country','') data['ip'] = ip data['ip_amount'] = prefix.size data['province'] = '' data['city'] = '' data['isp'] = '' for prefix in availableIPs: network, masklen = prefix.split("/") masklen = int(masklen) ip = generate_random_ip(network, masklen) jsonData = None while jsonData == None: try: jsonData = query_ip(ip) except Exception, e: logger.error(e) time.sleep(0.5) rtree.addPrefix(network, masklen) data = rtree.rnode.data data['country'] = jsonData.get('country', '') data['ip'] = ip data['ip_amount'] = IPNetwork(prefix).size data['province'] = '' data['city'] = '' data['isp'] = '' logger.info(prefix + ' ' + data['country'])
def scan_fn_ip(): country_code = {} for line in open('input/country_code', 'r'): code, name = line.split(" ") country_code[code] = name.strip().decode("utf-8") logger.info(code + ' ' + country_code[code]) rtree = ipRadixDB() ip_area_list = ["input/delegated-arin-latest", "input/delegated-ripencc-latest", "input/delegated-lacnic-latest", "input/delegated-afrinic-latest", "input/delegated-apnic-latest"] dft = defaultdict(list) availableIPs = [] for f in ip_area_list: seed_file = open(f,'r') for l in seed_file.readlines(): params = l.split('|') if len(params) >= 4 and params[2] == "ipv4" and params[3] != "*" and params[1] != "CN": startIP = params[3] endIP = ip_integer_to_string(ip_integer_from_string(startIP) + int(params[4]) - 1) logger.info(startIP + ' ' + endIP + ' ' + params[4]) iprange = IPRange(startIP, endIP) if params[1] == '': availableIPs += map(str, iprange.cidrs()) else: dft[params[1]] += map(str, iprange.cidrs()) for key in dft: prefix = dft[key][-1] network,masklen = prefix.split('/') masklen = int(masklen) ip = generate_random_ip(network,masklen) ipset = IPSet(dft[key]) for prefix in ipset.iter_cidrs(): network,masklen = str(prefix).split('/') masklen = int(masklen) rtree.addPrefix(network,masklen) data = rtree.rnode.data country = country_code[key] logger.info(str(prefix) + ' ' + country) data['country'] = country #jsonData.get('country','') data['ip'] = ip data['ip_amount'] = prefix.size data['province'] = '' data['city'] = '' data['isp'] = '' for prefix in availableIPs: network,masklen = prefix.split("/") masklen = int(masklen) ip = generate_random_ip(network,masklen) jsonData = None; while jsonData == None: try: jsonData = query_ip(ip) except Exception, e: logger.error(e) time.sleep(0.5) rtree.addPrefix(network,masklen) data = rtree.rnode.data data['country'] = jsonData.get('country','') data['ip'] = ip data['ip_amount'] = IPNetwork(prefix).size data['province'] = '' data['city'] = '' data['isp'] = '' logger.info(prefix + ' ' + data['country'])
def split_network_from_start_to_end(start, end): ip = IPRange(start, end) return map(lambda net:(str(net.network), net.prefixlen, net.size), ip.cidrs())
# # # Andrew MacPherson [ andrew <<at>> Paterva.com ] # # # ####################################################### import os, sys, time, random import EasyDialogs from netaddr import IPRange from MaltegoTransform import * me = MaltegoTransform() me.parseArguments(sys.argv) target = me.getValue() targetRange = target.split("-") ipr = IPRange(targetRange[0], targetRange[1]) rangeTarget = "" for i in ipr.cidrs(): rangeTarget = rangeTarget + str(i) + " " fn = target + "-ports-" + str(random.randint(1000, 9999)) + ".dat" defaultScanPorts = "22,21,25,80,443,3306" scanPorts = EasyDialogs.AskString( "Which ports do you want to scan on " + target + "?", defaultScanPorts) if (scanPorts is None): me.returnOutput() exit() nmapCMD = "nmap -n -oG " + fn + " -p" + scanPorts + " -sS -PN " + rangeTarget + ">" + fn + ".stdout" me.debug("running " + nmapCMD + "\n") os.system(nmapCMD) try: if (os.path.exists(fn) == False): me.debug( "File not found, please make sure another scan is not currently running and/or a resource is not using the file"