def test_check_ip(self): """ Test check_ip. """ check1 = utils.check_ip('127.0.0.1') check2 = utils.check_ip('882.983.099.232') check3 = utils.check_ip('-93.-33.-34.-32') check4 = utils.check_ip('90.32') check5 = utils.check_ip('-300') self.assertEqual(check1, True) self.assertEqual(check2, False) self.assertEqual(check3, False) self.assertEqual(check4, False) self.assertEqual(check5, False)
def _extracted_from_parse_outbound_IPRule_4(self, arg0, arg1): action = int(self.cred[arg0]['action']) temp_ip_inbound = [] if len(self.cred[arg0][arg1]): list_of_IPs = str(self.cred[arg0][arg1]) list_of_IPs = list_of_IPs.split(',') for IP in list_of_IPs: if '-' in IP: for new_ip in utils.generate_IPs(IP): if new_ip not in temp_ip_inbound and utils.check_ip( new_ip): temp_ip_inbound.append(str(new_ip).strip()) elif utils.check_ip(IP): if IP not in temp_ip_inbound: temp_ip_inbound.append(str(IP).strip()) return temp_ip_inbound, action
def parse_outbound_IPRule(self): """ Parse the outbound IP rules and generate ip_outbound list. Args: None Raises: None Returns: temp_ip_outbound (list): Parsed IP outbound list action (int): 0 or 1 """ try: action = int(self.cred['outbound_IPRule']['action']) temp_ip_outbound = [] if len(self.cred['outbound_IPRule']['ip_outbound']): list_of_IPs = str(self.cred['outbound_IPRule']['ip_outbound']) list_of_IPs = list_of_IPs.split(',') for IP in list_of_IPs: if '-' in IP: for new_ip in utils.generate_IPs(IP): if (new_ip not in temp_ip_outbound and utils.check_ip(new_ip)): temp_ip_outbound.append(str(new_ip).strip()) elif (utils.check_ip(IP)): if IP not in temp_ip_outbound: temp_ip_outbound.append(str(IP).strip()) return temp_ip_outbound, action except Exception as e: self.logger.log("Error: " + str(e), logtype="error") # Return empty list and block action return [], 0
def check_invalid_ip(pkt): """ Check invalid IP. Drop a packet if IP is invalid or is 0.0.0.0. Args: scapy_pkt (scapy_object): Packet to filter Raises: None Returns: bool (int): Allow or drop """ if (pkt.haslayer(scapy.IP)): source_ip = pkt[scapy.IP].src if (utils.check_ip(source_ip) and str(source_ip) != "0.0.0.0"): return 1 else: return 0 else: return 1