def test_add_remove_queue(self): # Add the queue and receive a packet: queue = Queue() self.rx_thread.add_packet_queue(lambda packet: True, queue) self.simulate_recv_packet(Packet()) packet = queue.get(block=True, timeout=0.1) self.assertEquals(packet, Packet()) # Remove the queue, receive a packet, it shouldn't get added: self.rx_thread.remove_packet_queue(queue) self.simulate_recv_packet(Packet()) with self.assertRaises(Empty): packet = queue.get(block=False, timeout=0.1)
def test_unload_packet(): """Check that a string packet is transformed into a Packet.""" string_pkt = str( b'E\x00\x00\x14\x00\x01\x00\x00@\x00|\xc3\xa7\x7f\x00\x00\x01\x7f\x00\x00\x01' ) expected_pkt = Packet(string_pkt) transformed_pkt = pyutils.unload_packet(string_pkt) assert transformed_pkt == expected_pkt
def cb(i,payload): data = payload.get_data() # Add padding before packet # src mac + dst mac + 0x0800 (type: IP) pad = "\0" * 12 + "\x08\0" + data pkt = Packet(_pkt=pad) writer.write(pkt) return 1
def run_hook(): """Test hook class.""" test_hook = hook.TestHook("dummy", "description", True) # Check that there is a hook to be ran curr_dir = os.path.dirname(os.path.abspath(__file__)) # Whitebox curr_file = os.path.dirname(curr_dir) # Tests curr_file = os.path.dirname(curr_file) # ntps sample_hook_path = "{}{}".format(curr_file, "/Hooks/DNSsport.py") assert check_file_exists(sample_hook_path) is True orig_pkt = 'E\x00\x00\x14\x00\x01\x00\x00@\x00|\xe7\x7f\x00\x00\x01\x7f\x00\x00\x01' pkt = load_packet(orig_pkt) print("Original packet: {}".format(pkt)) output = test_hook.run(sample_hook_path, pkt) print("Raw output: {}".format(output)) print() # print("decoded output 1: {}".format(output.decode("utf-8").strip())) # print() # Translate no_escapes = output.decode('unicode_escape').strip() # pkt_decoded = pkt_decoded.translate() # no_escapes = no_escapes[3:] # no_escapes = no_escapes[:-1] print("decoded output 1.5: {}".format(no_escapes)) assert orig_pkt == orig_pkt assert orig_pkt == str(no_escapes) # pkt_decoded = output.decode("utf-8").strip() # pkt_decoded = str(pkt_decoded) # pkt_decoded = pkt_decoded.decode("utf-8") # pkt_decoded = pkt_decoded[3:] # pkt_decoded = pkt_decoded[:-1] # print("decoded output 2: {}".format(pkt_decoded)) # print() # pkt_decoded = Packet(pkt_decoded) # print("Retreived packet: {}".format(str(pkt_decoded.payload))) print(type(no_escapes)) no_escapes = Packet(no_escapes) print("Retreived packet: {}".format(str(no_escapes.payload))) assert output is not None assert True is False """Ignore the following as it is to remember to never give up."""
def __init__(self, _pkt: bytes = None, scapy_pkt: Packet = None, **kwargs): self.scapy_pkt = scapy_pkt if scapy_pkt else Packet() self._set_fields(**kwargs)
def unload_packet(string_packet): """Unloads packet to be read after subprocess.""" return Packet(string_packet)
def load_packet(binary_packet): """Loads a packet to be sent via subprocess.""" pkt = Packet(binary_packet) pkt = str(pkt.payload) return pkt
def create_packet(binary_packet): """Loads a packet to be sent via subprocess.""" return Packet(binary_packet)