How to use the pypacker.psocket.SocketHndl 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 / tools / tcp_syn.py View on Github external
from pypacker import psocket

IFACE	= "wlan0"
MAC_SRC	= "00:13:e8:63:f3:8f"
MAC_DST	= "24:65:11:85:E9:AC"
IP_SRC	= "192.168.178.26"
FILE_IP_DST = sys.argv[2]

print("destination IP addresses file: %s" % FILE_IP_DST)
IP_DST = open(FILE_IP_DST, "r").read().split("\n")[:-1]
print("amount addresses: %d" % len(IP_DST))

REPITITIONS = int(sys.argv[1])
print("repititiona: %d" % REPITITIONS)

psock_req	= psocket.SocketHndl(iface_name=IFACE, mode=psocket.SocketHndl.MODE_LAYER_2)
tcp_syn		= ethernet.Ethernet(dst_s=MAC_DST, src_s=MAC_SRC) +\
			ip.IP(src_s=IP_SRC, dst_s="127.0.0.1", p=ip.IP_PROTO_TCP) +\
			tcp.TCP(sport=12345, dport=1337)

print("%r" % tcp_syn)
ip = tcp_syn.ip
tcp = tcp_syn.ip.tcp
randrange = random.randrange

for x in range(REPITITIONS):
	if x % 10000 == 0:
		print("sent %d" % x)
	ip_dst_str = IP_DST[randrange(0, len(IP_DST))]
	try:
		ip.dst_s = ip_dst_str
	except:
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
			return False

		while is_running:
			try:
				pkt_rsp = psock_rcv.recvp(filter_match_recv=filter_cb)[0]
				#logger.debug("got SYN,ACK: %r", pkt_rsp)
github mike01 / pypacker / pypacker / psocket.py View on Github external
self.iface_name = iface_name
		self._socket_send = None
		self._socket_recv = None
		self.__mode = mode

		logger.info("creating socket on interface: %s", iface_name)
		# use raw socket for receiving in all modes
		self._socket_recv = socket.socket(socket.AF_PACKET,
			socket.SOCK_RAW,
			socket.htons(SocketHndl.ETH_P_ALL))

		self._socket_recv.settimeout(timeout)

		if iface_name is not None:
			self._socket_recv.bind((iface_name, SocketHndl.ETH_P_ALL))

		# same socket for sending
		if mode == SocketHndl.MODE_LAYER_2:
			self._socket_send = self._socket_recv
		# different socket for sending
		elif mode == SocketHndl.MODE_LAYER_3:
			# IPPROTO_RAW = handle ip header stuff in user space
			self._socket_send = socket.socket(socket.AF_INET,
				socket.SOCK_RAW,
				socket.IPPROTO_RAW)
			# IP_HDRINCL = IP header is given by user space (not generated by kernel)
			self._socket_send.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1)

		if buffersize_recv is not None:
			self._socket_recv.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, buffersize_recv)
		if buffersize_send is not None:
github mike01 / pypacker / tools / ntp_spoofer.py View on Github external
ntp.NTP(li=ntp.NO_WARNING, v=3, mode=ntp.CLIENT)
print("sending NTP request and waiting for answer..")
answer	= psock_req.sr(ntp_req)[0][ntp.NTP]
"""

# print("answer is: %s" % answer)
#unpack_I = struct.Struct(">I").unpack
# print("seconds since 1.1.1900: %d" % unpack_I(answer.transmit_time[0:4])[0])
# 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
github mike01 / pypacker / examples / examples_general.py View on Github external
bc_cnt += 1
		except Exception as e:
			print(e)

	if bc_cnt == 0:
		print("got no beacons, try to change channel or get closer to the AP")
	wlan_reader.close()
except socket.error as e:
	print(e)

# write packets to network interface (default lo) using raw sockets
try:
	#
	# send packets on layer 2
	#
	psock = psocket.SocketHndl(iface_name="lo", mode=psocket.SocketHndl.MODE_LAYER_2, timeout=10)

	# 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) +\