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_add(self):
print_header("pkt1 + pkt2 + pkt3")
eth1 = ethernet.Ethernet()
ip1 = ip.IP()
tcp1 = tcp.TCP()
pkt = eth1 + ip1 + tcp1
self.assertEqual(pkt.highest_layer.__class__.__name__, "TCP")
packet_bytes = get_pcap("tests/packets_ip6.pcap")
s = packet_bytes[0]
print(s)
eth = ethernet.Ethernet(s)
print("> searching ip6 in ether")
ip_6 = eth[ip6.IP6]
print("> calling bin on eth")
self.assertEqual(eth.bin(), s)
print("> counting options")
self.assertEqual(len(ip_6.opts), 1)
self.assertEqual(len(ip_6.opts[0].opts), 2)
self.assertEqual(ip_6.opts[0].opts[0].type, 5)
self.assertEqual(ip_6.opts[0].opts[1].type, 1)
pkt_eth_ip_tcp = ethernet.Ethernet() + ip6.IP6() + tcp.TCP()
pkt_eth_ip_tcp.bin()
ip6len_real = len(pkt_eth_ip_tcp.ip6.opts.bin()) + len(pkt_eth_ip_tcp.ip6.tcp.bin())
# length should be updated
self.assertEqual(pkt_eth_ip_tcp.ip6.dlen, ip6len_real)
# header type should be updated
self.assertEqual(pkt_eth_ip_tcp.ip6.nxt,
pypacker.Packet.get_id_for_handlerclass(pkt_eth_ip_tcp.ip6.__class__,
pkt_eth_ip_tcp.ip6.tcp.__class__))
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)
def test_iadd(self):
print_header("pkt1 += pkt2")
eth1 = ethernet.Ethernet()
ip1 = ip.IP()
tcp1 = tcp.TCP()
eth1 += ip1
eth1 += tcp1
self.assertEqual(eth1.highest_layer.__class__.__name__, "TCP")
print(p_all_concat[tcp.TCP])
print("--------------")
print(p_all[telnet.Telnet])
print(p_all_concat[telnet.Telnet])
print("--------------")
self.assertEqual(p_all.bin(), bytes_eth_ip_tcp_tn)
self.assertEqual(p_all.bin(), p_all_concat.bin())
print()
print(">>> Testing keyword construction")
# create layers using keyword-constructor
eth2 = ethernet.Ethernet(dst=eth1.dst, src=eth1.src, type=eth1.type)
ip2 = ip.IP(v_hl=ip1.v_hl, tos=ip1.tos, len=ip1.len, id=ip1.id, off=ip1.off, ttl=ip1.ttl, p=ip1.p,
sum=ip1.sum, src=ip1.src, dst=ip1.dst)
tcp2 = tcp.TCP(sport=tcp1.sport, dport=tcp1.dport, seq=tcp1.seq, ack=tcp1.ack, off_x2=tcp1.off_x2,
flags=tcp1.flags, win=tcp1.win, sum=tcp1.sum, urp=tcp1.urp)
self.assertEqual(tcp1.off_x2, tcp2.off_x2)
for opt in ip1.opts:
print("adding ip option: %s" % opt)
totallen = 0
for opt in tcp1.opts:
print("adding tcp option: %s" % opt)
tcp2.opts.append(opt.bin()) # use raw bytes instead packets, must work
totallen += len(opt)
print("total length: 20+%d" % totallen)
self.assertEqual(tcp1.off_x2, tcp2.off_x2)
print(tcp1.body_bytes)
tn2 = telnet.Telnet(tcp1.body_bytes)
arpreq = ethernet.Ethernet(src_s="12:34:56:78:90:12", type=ethernet.ETH_TYPE_ARP) +\
arp.ARP(sha_s="12:34:56:78:90:12", spa_s="192.168.0.2",
tha_s="12:34:56:78:90:13", tpa_s="192.168.0.1")
psock.send(arpreq.bin())
# send ICMP request
icmpreq = ethernet.Ethernet(src_s="12:34:56:78:90:12", dst_s="12:34:56:78:90:13", type=ethernet.ETH_TYPE_IP) +\
ip.IP(p=ip.IP_PROTO_ICMP, src_s="192.168.0.2", dst_s="192.168.0.1") +\
icmp.ICMP(type=8) +\
icmp.ICMP.Echo(id=1, ts=123456789, body_bytes=b"12345678901234567890")
psock.send(icmpreq.bin())
# send TCP SYN
tcpsyn = ethernet.Ethernet(src_s="12:34:56:78:90:12", dst_s="12:34:56:78:90:13", type=ethernet.ETH_TYPE_IP) +\
ip.IP(p=ip.IP_PROTO_TCP, src_s="192.168.0.2", dst_s="192.168.0.1") +\
tcp.TCP(sport=12345, dport=80)
psock.send(tcpsyn.bin())
# send UDP data
udpcon = ethernet.Ethernet(src_s="12:34:56:78:90:12", dst_s="12:34:56:78:90:13", type=ethernet.ETH_TYPE_IP) +\
ip.IP(p=ip.IP_PROTO_UDP, src_s="192.168.0.2", dst_s="192.168.0.1") +\
udp.UDP(sport=12345, dport=80)
udpcon[udp.UDP].body_bytes = b"udpdata"
psock.send(udpcon.bin())
psock.close()
#
# send and receive packets on layer 3 (assumes running HTTP-server on port 80)
#
packet_ip = ip.IP(src_s="127.0.0.1", dst_s="127.0.0.1") + tcp.TCP(dport=80)
psock = psocket.SocketHndl(mode=psocket.SocketHndl.MODE_LAYER_3, timeout=10)
packets = psock.sr(packet_ip, max_packets_recv=1)
# update dynamic header parts. buf: 1010???? -clear reserved-> 1010 -> *4
ol = ((buf[12] >> 4) << 2) - 20 # dataoffset - TCP-standard length
if ol > 0:
# parse options, add offset-length to standard-length
opts_bytes = buf[20: 20 + ol]
self._init_triggerlist("opts", opts_bytes, self._parse_opts)
elif ol < 0:
raise Exception("invalid header length")
ports = [unpack_H(buf[0:2])[0], unpack_H(buf[2:4])[0]]
try:
# source or destination port should match
# logger.debug("TCP handler: %r" % self._id_handlerclass_dct[TCP])
htype = [x for x in ports if x in self._id_handlerclass_dct[TCP]][0]
#logger.debug("TCP: trying to set handler, type: %d = %s" %
#(type, self._id_handlerclass_dct[TCP][type]))
self._init_handler(htype, buf[20 + ol:])
except:
# no type found
pass
return 20 + ol
tcpsyn = ethernet.Ethernet(src_s="12:34:56:78:90:12", dst_s="12:34:56:78:90:13", type=ethernet.ETH_TYPE_IP) +\
ip.IP(p=ip.IP_PROTO_TCP, src_s="192.168.0.2", dst_s="192.168.0.1") +\
tcp.TCP(sport=12345, dport=80)
psock.send(tcpsyn.bin())
# send UDP data
udpcon = ethernet.Ethernet(src_s="12:34:56:78:90:12", dst_s="12:34:56:78:90:13", type=ethernet.ETH_TYPE_IP) +\
ip.IP(p=ip.IP_PROTO_UDP, src_s="192.168.0.2", dst_s="192.168.0.1") +\
udp.UDP(sport=12345, dport=80)
udpcon[udp.UDP].body_bytes = b"udpdata"
psock.send(udpcon.bin())
psock.close()
#
# send and receive packets on layer 3 (assumes running HTTP-server on port 80)
#
packet_ip = ip.IP(src_s="127.0.0.1", dst_s="127.0.0.1") + tcp.TCP(dport=80)
psock = psocket.SocketHndl(mode=psocket.SocketHndl.MODE_LAYER_3, timeout=10)
packets = psock.sr(packet_ip, max_packets_recv=1)
for p in packets:
print("got layer 3 packet: %s" % p)
psock.close()
except socket.timeout as e:
print("timeout!")
except socket.error as e:
print("you need to be root to execute the raw socket-examples!")
def tcp_cb(pargs):
"""TCP DoS"""
iptables_rules_info = """
iptables -I OUTPUT -p tcp --tcp-flags ALL RST,ACK -j DROP
iptables -I OUTPUT -p tcp --tcp-flags ALL RST -j DROP
iptables -I INPUT -p tcp --tcp-flags ALL RST -j DROP
"""
logger.info("For best performance set set these rules: %s", iptables_rules_info)
pkt_tcp_syn = ethernet.Ethernet(dst_s=pargs.mac_dst, src_s=pargs.mac_src) +\
ip.IP(src_s=pargs.ip_src, dst_s=pargs.ip_dst, p=ip.IP_PROTO_TCP) +\
tcp.TCP(sport=12345, dport=pargs.port_dst)
# Use raw sockets to circumvent network stack
psock_send = psocket.SocketHndl(iface_name=pargs.iface_name,
mode=psocket.SocketHndl.MODE_LAYER_2)
psock_rcv = psocket.SocketHndl(iface_name=pargs.iface_name,
mode=psocket.SocketHndl.MODE_LAYER_2)
is_running = True
def answer_cycler():
def filter_cb(pkt):
try:
return pkt.ip.tcp.flags == tcp.TH_SYN | tcp.TH_ACK
except Exception as ex:
#logger.warning(ex)
pass
return False