def ip_overlap(networks, ip_to_check): ipv4_tc = ipv4(ip_to_check) ipv4_nets = [ipv4(i) for i in networks if i != ip_to_check] overlap = sum([ipv4_tc.overlaps(i) for i in ipv4_nets]) if overlap > 0: return True return False
def create_with_str(cls, ip_port: str) -> 'IpPort': ip, port = unicodedata.normalize('NFKC', ip_port).split(':', 1) ip = ipv4(ip) port = int(port) if port not in range(0x0000, 0xffff): raise ValueError return IpPort(ip, port, key=cls.__construct_key)
def ip_to_dec(ipstring): """convert an octet ip into decimal returns decimal or None""" try: dec = int(ipv4(unicode(ipstring))) return dec except AddressValueError: return None
def ip_to_oct(ipstring): """convert a decimal ip to oct""" try: oct = str(ipv4(ipstring)) return oct except any as e: print e sys.exit()
def get_sniffed_stream(ftr, DATASET, m = None, save=False): out=[] if ftr == 'src': query = lambda x: out.append(int(ipv4(x.src))) elif ftr == 'dst': query = lambda x: out.append(int(ipv4(x.dst))) elif ftr =='sport': query = lambda x: out.append(x.sport) elif ftr =='dport': query = lambda x: out.append(x.dport) elif ftr =='len': query = lambda x: out.append(x.len) sniff(offline=DATASET, prn=query,count= m, store = 0) print(m, out[:5]) if save: if (m <= 1000) and (m >10 ): np.savetxt(f'/home/swei20/SymNormSlidingWindows/test/data/stream/traffic_{ftr}_m{m}.txt', out) elif (m >1000) or (m ==-1): np.savetxt(f'/home/swei20/SymNormSlidingWindows/data/stream/traffic_{ftr}.txt', out) return out
def find_host(): # Start the loading "animation" thread Thread(target=load).start() # Wether or not we're still searching for ip, this is for the # loading "animation" global loading # 192.168.{i}.0 for i in range(1, 256): for ip in ipv4("192.168.%d.0/24" % i): s = is_up(str(ip)) if s: loading = False # Return the socket to use for the connection return s print("[i] There are no hosts online! Exiting.") sys.exit()
def create_with_bin(cls, port_ip: bytes) -> 'IpPort': port, ip = (int.from_bytes(port_ip[:2], 'big'), ipv4(port_ip[2:])) return IpPort(ip, port, key=cls.__construct_key)
def create(cls, ip: Any, port: Any) -> 'IpPort': return IpPort(ipv4(ip), int(port), key=cls.__construct_key)
def printIPS(list): for i in list: print(str(ipv4(i)))
#Utility to pull Ips from a text file and order them by network, removing duplicates. #Usage ./ippull.py [textfile] import sys from ipaddress import IPv4Address as ipv4 args = sys.argv fileName = args[1] ips = [] #Add regex to check each line for IPs with open(fileName, 'r') as file: for line in file: if line[:-1] in ips: #We use [:-1] to get rid of newlines.. pass else: ips.append(int(ipv4(line[:-1]))) #Creates number representation ips.sort() #Sort our completed list. def printIPS(list): for i in list: print(str(ipv4(i))) #Add additional utilities later. printIPS(ips) #IDEA: Use MindMax database to look up the IP OR use WHOIS to find its location, then sort by location if prompted.
def translate_rangedict(rangedict): return {(str(ipv4(name[0])),str(ipv4(name[1]))): val for name, val in rangedict.items()}