How to use the pypacker.layer4.tcp function in pypacker

To help you get started, we’ve selected a few pypacker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github taf3 / taf / taf / testlib / Ixia / IxiaHAL.py View on Github external
offset = 32
                        else:
                            offset = 28
                        self.udf_dict["arp_ip"] = {"udf_id": udf_id, "initval": sip_initval, "offset": offset, "counter_type": 'c32',
                                                   "step": sip_increment_step, "count": sip_increment_count, "continuous": continuous}
                        tcl_commands.extend(self._set_ixia_udf_field(**self.udf_dict["arp_ip"]))

                elif class_name == "TCP":
                    payl, commands = self._configure_tcp(payl, chassis, card, port)
                    tcl_commands.extend(commands)

                    # Set tcp source port increment
                    if stcp_increment is not None:
                        stcp_increment_step, stcp_increment_count = self._check_increment(stcp_increment, "stcp_increment")
                        udf_id = len(self.udf_dict) + 1
                        stcp = packet[pypacker.layer4.tcp.TCP].sport
                        stcp_initval = str(hex(stcp))[2:].zfill(4)
                        if packet.vlan:
                            offset = 38
                        else:
                            offset = 34
                        self.udf_dict["stcp"] = {"udf_id": udf_id, "initval": stcp_initval, "offset": offset, "counter_type": 'c16',
                                                 "step": stcp_increment_step, "count": stcp_increment_count, "continuous": continuous}
                        tcl_commands.extend(self._set_ixia_udf_field(**self.udf_dict["stcp"]))

                    # Set tcp destination port increment
                    if dtcp_increment is not None:
                        dtcp_increment_step, dtcp_increment_count = self._check_increment(dtcp_increment, "dtcp_increment")
                        udf_id = len(self.udf_dict) + 1
                        dtcp = packet[pypacker.layer4.tcp.TCP].dport
                        dtcp_initval = str(hex(dtcp))[2:].zfill(4)
                        if packet.vlan:
github mike01 / pypacker / tests / test_pypacker.py View on Github external
def test_dynamicfield(self):
		print_header("dynamic fields")
		eth1 = ethernet.Ethernet() + ip.IP() + tcp.TCP()
		tcp1 = eth1[tcp.TCP]
		# find packets
		del tcp1.opts[:]
		tcp1.opts.extend([tcp.TCPOptMulti(type=0, len=3, body_bytes=b"\x00\x11\x22"), tcp.TCPOptSingle(type=1),
			tcp.TCPOptSingle(type=2)])
		self.assertEqual(tcp1.opts.find_pos(lambda v: v.type == 2), 2)

		tcp1.opts.extend([(b"key1", b"value1"), (b"key2", b"value2")])
		idx, res = tcp1.opts.get_by_key(b"key1")
		self.assertEqual(res, b"value1")
		idx, res = tcp1.opts.get_by_key(b"key2")
		self.assertEqual(res, b"value2")
		tcp1.opts.set_by_key(b"key1", b"value1b")
		tcp1.opts.set_by_key(b"key2", b"value2b")
		idx, res = tcp1.opts.get_by_key(b"key1")
		self.assertEqual(res, b"value1b")
		idx, res = tcp1.opts.get_by_key(b"key2")
		self.assertEqual(res, b"value2b")
github mike01 / pypacker / examples / extension / new_proto.py View on Github external
return pypacker.Packet.DIR_UNKNOWN

# these types have to be extracted in _dissect()
NEW_PROTO_UPPERTYPE_1	= 1
NEW_PROTO_UPPERTYPE_2	= 2
NEW_PROTO_UPPERTYPE_3	= 3

# load handler
from pypacker.layer3 import ip
from pypacker.layer4 import tcp
from pypacker.layer567 import http

pypacker.Packet.load_handler(NewProtocol,
	{
		NEW_PROTO_UPPERTYPE_1: ip.IP,
		NEW_PROTO_UPPERTYPE_2: tcp.TCP,
		NEW_PROTO_UPPERTYPE_3: http.HTTP
	}
)


newproto = NewProtocol(dynamic_field0=[b"xx", b"yy", b"zz"])
print(newproto)
github mike01 / pypacker / examples / examples_general.py View on Github external
print("unknown direction for packet 1/2, type: %d" % dir)

#
# read packets from pcap-file using pypacker-reader
#

pcap = ppcap.Reader(filename="packets_ether.pcap")
cnt = 0

for ts, buf in pcap:
	cnt += 1
	eth = ethernet.Ethernet(buf)

	if eth[tcp.TCP] is not None:
		print("%d: %s:%s -> %s:%s" % (ts, eth[ip.IP].src_s, eth[tcp.TCP].sport,
			eth[ip.IP].dst_s, eth[tcp.TCP].dport))
pcap.close()
#
# send/receive packets to/from network using raw sockets
#
try:
	psock = psocket.SocketHndl(timeout=10)
	print("please do a ping to localhost to receive bytes!")
	raw_bytes = psock.recv()
	print(ethernet.Ethernet(raw_bytes))
	psock.close()
except socket.error as e:
	print("you need to be root to execute the raw socket-examples!")

# read 802.11 packets from wlan monitor interface
# command to create/remove interface (replace wlanX with your managed wlan-interface):
# iw dev [wlanX] interface add mon0 type monitor
github mike01 / pypacker / examples / examples_new_protocol.py View on Github external
print("Same as above: %s" % newproto_pkt[tcp.TCP])


# Create new Packet by defining every single header and adding higher layers
newproto_pkt = NewProtocol(
	type=0x66, src=b"AAAA", dst=b"BBBB", hlen=0x11, yolo=b"1234", options=[b"\x00A\x01B\x02C"]) +\
	ip.IP() +\
	tcp.TCP()

print()
print(">>> Layers of packet:")
print("Output all layers: %s" % newproto_pkt)
print("Access some fields: 0x%X %s %s" % (newproto_pkt.type, newproto_pkt.src, newproto_pkt.dst))
print("Access next upper layer (IP): %s" % newproto_pkt.ip)
print("A layer above IP: %s" % newproto_pkt.ip.tcp)
print("Same as above: %s" % newproto_pkt[tcp.TCP])
github mike01 / pypacker / examples / examples_portscanner.py View on Github external
def filter_pkt(pkt):
	tcp_pkt = pkt[tcp.TCP]

	if tcp_pkt is None or\
		tcp_pkt.dport != PORT_SRC or\
		tcp_pkt.flags != (tcp.TH_SYN | tcp.TH_ACK) or\
		tcp_pkt.ack != TCP_SEQ + 1:
		return False

	open_ports.put(tcp_pkt.sport)
	return True
github mike01 / pypacker / examples / examples_portscanner.py View on Github external
def filter_pkt(pkt):
	tcp_pkt = pkt[tcp.TCP]

	if tcp_pkt is None or\
		tcp_pkt.dport != PORT_SRC or\
		tcp_pkt.flags != (tcp.TH_SYN | tcp.TH_ACK) or\
		tcp_pkt.ack != TCP_SEQ + 1:
		return False

	open_ports.put(tcp_pkt.sport)
	return True
github mike01 / pypacker / examples / examples_new_protocol.py View on Github external
def reverse_address(self):
		"""
		reverse_a# ddress(...) should be overwritten to be able to reverse
		source/destination addresses (see ethernet.Ethernet)
		"""
		self.src, self.dst = self.dst, self.src


# Parse from raw bytes
# First layer (Layer 2)
newproto_bytes = b"\x66" + b"AAAA" + b"BBBB" + b"\x00\x16" + b"\x00" + b"1234" + b"\x00A\x01B\x02C"
# Next upper layer (Layer 3)
ip_bytes = ip.IP().bin()
# Layer above upper layer (Layer 4)
tcp_bytes = tcp.TCP().bin()
newproto_pkt = NewProtocol(newproto_bytes + ip_bytes + tcp_bytes)

print()
print(">>> Layers of packet:")
print("Output all layers: %s" % newproto_pkt)
print("Access some fields: 0x%X %s %s" % (newproto_pkt.type, newproto_pkt.src, newproto_pkt.dst))
print("Access next upper layer (IP): %s" % newproto_pkt.ip)
print("A layer above IP: %s" % newproto_pkt.ip.tcp)
print("Same as above: %s" % newproto_pkt[tcp.TCP])


# Create new Packet by defining every single header and adding higher layers
newproto_pkt = NewProtocol(
	type=0x66, src=b"AAAA", dst=b"BBBB", hlen=0x11, yolo=b"1234", options=[b"\x00A\x01B\x02C"]) +\
	ip.IP() +\
	tcp.TCP()
github mike01 / pypacker / pypacker / layer3 / ip6.py View on Github external
def __get_flow(self):
		return self.v_fc_flow & 0xFFFFF

	def __set_flow(self, v):
		self.v_fc_flow = (self.v_fc_flow & ~0xFFFFF) | (v & 0xFFFFF)
	flow = property(__get_flow, __set_flow)

	# Convenient access for: src[_s], dst[_s]
	src_s = pypacker.get_property_ip6("src")
	dst_s = pypacker.get_property_ip6("dst")

	__handler__ = {
		IP_PROTO_ICMP6: icmp6.ICMP6,
		IP_PROTO_IGMP: igmp.IGMP,
		IP_PROTO_TCP: tcp.TCP,
		IP_PROTO_UDP: udp.UDP,
		IP_PROTO_ESP: esp.ESP,
		IP_PROTO_PIM: pim.PIM,
		IP_PROTO_IPXIP: ipx.IPX,
		IP_PROTO_SCTP: sctp.SCTP,
		IP_PROTO_OSPF: ospf.OSPF
	}

	def _dissect(self, buf):
		type_nxt = buf[6]
		off = 40
		opts = []

		# logger.debug("type: %d buflen: %d" % (type_nxt, len(buf)))
		# logger.debug("parsing opts from bytes (dst: %s): (len: %d) %s" %
		#	 (buf[24:40], self.hdr_len, buf[off:]))
github mike01 / pypacker / examples / examples_portscanner.py View on Github external
thread_rcv = threading.Thread(target=rcv_cycler, args=[sock_rcv])
thread_rcv.start()

print("starting to scan")

for port in range(0, 0xFFFF + 1):
	print("\rPinging TCP port %d" % port, end="")

	while not open_ports.empty():
		port_open = open_ports.get()
		print("\nTCP Port %d on %s seems to be open" % (port_open, IP_DST))

	pkt_send = ethernet.Ethernet(dst_s=MAC_DST, src_s=MAC_SRC) +\
		ip.IP(src_s=IP_SRC, dst_s=IP_DST) +\
		tcp.TCP(sport=PORT_SRC, dport=port, seq=TCP_SEQ)
	sock_send.send(pkt_send.bin())

print()
print("waiting some seconds to receive delayed responses")
time.sleep(2)

sock_rcv.close()
sock_send.close()