How to use the pypacker.layer3.ip.IP 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 mike01 / pypacker / tests / test_pypacker.py View on Github external
print(udp1)
		udp_bin = udp1.bin()
		print(udp1)
		print(udp1.ulen)
		print(udp_bin)
		print(udp1.sum)
		print("sum 1: %X" % udp1.sum)
		self.assertEqual(udp1.sum, 0xf6eb)

		# print("setting new port")
		udp1.dport = 1234
		udp1.bin()
		print("sum 2: %X" % udp1.sum)
		self.assertEqual(udp1.sum, 0xf24e)

		udp2 = ethernet.Ethernet() + ip.IP() + udp.UDP()
		udp2[udp.UDP].body_bytes = b"A" * 10
		udp2.bin()
		self.assertEqual(udp2[udp.UDP].sum, 0xDAD6)
		udp2[udp.UDP].body_bytes = b"A" * 11
		udp2.bin()
		self.assertEqual(udp2[udp.UDP].sum, 0x99D4)
github mike01 / pypacker / tests / test_pypacker.py View on Github external
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")
github mike01 / pypacker / tools / visualize_ether.py View on Github external
def src_dst_cb(pkt):
	try:
		return pkt[ip.IP].src_s, pkt[ip.IP].dst_s
	except:
		return None, None
github mike01 / pypacker / tools / dns_spoofer.py View on Github external
filter	= lambda p: p[dns.DNS] is not None and p[ip.IP].src_s == IP_SRC
answer	= psock.recvp(filter_match_recv=filter)[0]
github mike01 / sledgehammer / sledgehammer.py View on Github external
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
github mike01 / pypacker / examples / examples_general.py View on Github external
# send ARP request
	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)
github mike01 / pypacker / tools / ntp_spoofer.py View on Github external
# psock_req.close()


#
# spoof NTP response
#
print("waiting for NTP request")
psock	= psocket.SocketHndl(iface_name=IFACE, timeout=600)
filter	= lambda p: p[ntp.NTP] is not None and p[ip.IP].src_s == IP_SRC
answer	= psock.recvp(filter_match_recv=filter)[0]
answer_ntp	= answer[ntp.NTP]

print("got NTP packet: %s" % answer_ntp)

ntp_answer_send	= Ethernet(dst=answer[Ethernet].src, src=answer[Ethernet].dst) +\
			ip.IP(src=answer[ip.IP].dst, dst_s=IP_SRC, p=ip.IP_PROTO_UDP) +\
			UDP(sport=answer[UDP].dport, dport=answer[UDP].sport) +\
			ntp.NTP(li=ntp.NO_WARNING, v=3, mode=ntp.SERVER, stratum=2, interval=4,
				update_time=answer_ntp.transmit_time,
				originate_time=answer_ntp.transmit_time,
				receive_time=b"\x00" * 4 + answer_ntp.transmit_time[4:],
				transmit_time=b"\x00" * 4 + answer_ntp.transmit_time[4:])

# alternative packet creation
"""
ntp_answer_send	= answer.create_reverse()
layer_ntp		= ntp_answer_send[ntp.NTP]
layer_ntp.mode		= ntp.SERVER
layer_ntp.originate_time = answer_ntp.transmit_time
layer_ntp.receive_time	= layer_ntp.transmit_time = b"\x00"*4 + answer_ntp.transmit_time[4:]
"""