Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _sendp(self, packet, iface=None, count=None, inter=0, stop_lock=None, is_valid=False, stream_increments=None):
"""Send packets.
"""
p = PacketGenerator(packet, stream_increments)
count_iter = itertools.count(0, 1)
if count is not None:
count_iter = itertools.takewhile(lambda x: x < count, count_iter)
with closing(psocket.SocketHndl(iface_name=iface)) as s, suppress(KeyboardInterrupt):
for _ in count_iter:
with stop_lock:
s.send(next(p).bin()) # pylint: disable=no-member
self.sent_statistics.increase(iface)
time.sleep(inter)
def wifi_deauth_cb(pargs):
"""
Deauth everyone and everything
"""
if pargs.channels is not None:
channels = [int(channel) for channel in pargs.channels.split(",")]
else:
channels = utils.get_available_wlan_channels(pargs.iface_name)
logger.debug("using channels: %r", channels)
psock_rcv = psocket.SocketHndl(iface_name=pargs.iface_name,
mode=psocket.SocketHndl.MODE_LAYER_2,
timeout=1)
psock_send = psocket.SocketHndl(iface_name=pargs.iface_name,
mode=psocket.SocketHndl.MODE_LAYER_2)
# {channel : {b"AP" : set(b"clients", ...)}}
wdata = collections.defaultdict(lambda: collections.defaultdict(set))
# thread: socket1: listen for traffic, extract ap/client macs
def listen_cycler(pargs_ref):
while pargs_ref.is_running:
try:
rtap = psock_rcv.recvp(lowest_layer=radiotap.Radiotap)[0]
except (IndexError, socket.timeout, OSError):
logger.debug("no packets received..")
continue
"""802.11 beacon fetcher"""
from pypacker import psocket
from pypacker.layer12 import ieee80211, radiotap
import time
wlan_monitor_if = "wlan1"
wlan_reader = psocket.SocketHndl(iface_name=wlan_monitor_if, timeout=999)
print("please wait for wlan traffic to show up")
aps_found = {}
time_start = time.time()
for i in range(100000):
raw_bytes = wlan_reader.recv()
drvinfo = radiotap.Radiotap(raw_bytes)
if i % 1000 == 0:
print("packets/s: %d" % (i / (time.time() - time_start)))
try:
beacon = drvinfo[ieee80211.IEEE80211.Beacon]
import sys
import time
import socket
from pypacker import psocket, ppcap, utils
from pypacker.layer12 import ethernet, linuxcc, radiotap
from pypacker.layer3 import ip
from pypacker.layer4 import tcp
from pypacker.layer567 import http
iface = sys.argv[1]
#print("opening (wlan?) interface: %s" % iface)
#utils.set_wlan_monmode(iface)
sockhndl = psocket.SocketHndl(iface_name=iface, timeout=99999)
#pcapwriter = ppcap.Writer(filename="parsefail.pcap", linktype=ppcap.DLT_IEEE802_11_RADIO)
pcapwriter = ppcap.Writer(filename="parsefail.pcap")
raw_bytes = b""
cnt = 0
time_start = time.time()
for bts in sockhndl:
if cnt % 1000 == 0:
print("%d pps" % (cnt / (time.time() - time_start)))
time_start = time.time()
cnt = 0
cnt += 1
try:
#pkt = radiotap.Radiotap(bts)
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
# iw dev [wlanX] interface del
try:
wlan_reader = psocket.SocketHndl(wlan_monitor_if)
print("please wait for wlan traffic to show up")
raw_bytes = wlan_reader.recv()
_beacon.src = mac
_beacon.bssid = mac
_beacon.params[0].body_bytes = bytes(essid, "ascii")
_beacon.params[0].len = len(essid)
_beacon.params[2].body_bytes = pack_B(channels[0])
_beacon.seq = 0
_beacon.interval = 0xFFFF
# adaptive sleeptime due to full buffer on fast sending
sleeptime = 0.000001
rand_mac = True
rand_essid = True
pargs.is_running = True
logger.info("faking APs on the following channels %r", channels)
psock_send = psocket.SocketHndl(iface_name=pargs.iface_name,
mode=psocket.SocketHndl.MODE_LAYER_2)
cnt = 0
rounds = 0
PACKETS_PER_CHANNEL = 3
starttime = time.time()
_beacon.params[2].body_bytes = pack_B(channels[0])
utils.switch_wlan_channel(pargs.iface_name, channels[0])
for _ in range(pargs.count):
if not pargs.is_running:
break
if cnt & 0xF == 0:
print("%d packets sent\r" % (cnt * PACKETS_PER_CHANNEL), end="")
sys.stdout.flush()
if time.time() - starttime > 60:
from pypacker.layer12 import ethernet
from pypacker.layer3 import ip
from pypacker.layer4 import tcp
from pypacker import psocket
IFACE_NAME = "wlan0"
MAC_SRC = "00:11:22:33:44:55" # MAC address of IFACE_NAME
MAC_DST = "00:11:22:33:44:56" # MAC address of target or router
IP_SRC = "192.168.178.53" # IP address of IFACE_NAME
IP_DST = "192.168.178.1" # IP address of target
PORT_SRC = 12345
TCP_SEQ = 1337
sock_rcv = psocket.SocketHndl(iface_name=IFACE_NAME)
sock_send = psocket.SocketHndl(iface_name=IFACE_NAME)
open_ports = queue.Queue()
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
from pypacker.layer12 import ethernet
from pypacker.layer3 import ip
from pypacker.layer4 import tcp
from pypacker import psocket
IFACE_NAME = "wlan0"
MAC_SRC = "00:11:22:33:44:55" # MAC address of IFACE_NAME
MAC_DST = "00:11:22:33:44:56" # MAC address of target or router
IP_SRC = "192.168.178.53" # IP address of IFACE_NAME
IP_DST = "192.168.178.1" # IP address of target
PORT_SRC = 12345
TCP_SEQ = 1337
sock_rcv = psocket.SocketHndl(iface_name=IFACE_NAME)
sock_send = psocket.SocketHndl(iface_name=IFACE_NAME)
open_ports = queue.Queue()
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
from pypacker import psocket
from pypacker.layer12 import ethernet
from pypacker.layer3 import ip, icmp
# send ICMP request
psock = psocket.SocketHndl(iface_name="wlan0")
icmpreq = ethernet.Ethernet(src_s="20:16:d8:ef:1f:49", dst_s="24:65:11:85:e9:00", type=ethernet.ETH_TYPE_IP) +\
ip.IP(p=ip.IP_PROTO_ICMP, src_s="192.168.178.27", dst_s="192.168.178.24") +\
icmp.ICMP(type=8) +\
icmp.ICMP.Echo(id=1, ts=123456789, body_bytes=b"12345678901234567890")
psock.send(icmpreq.bin())
psock_req = psocket.SocketHndl(iface_name=IFACE, mode=psocket.SocketHndl.MODE_LAYER_3)
dns_req = ip.IP(src_s=IP_SRC, dst_s="192.168.178.1", p=ip.IP_PROTO_UDP) +\
UDP(sport=12345, dport=53) +\
dns.DNS(id=12, questions_amount=1, addrr_amount=1,
queries=dns.DNS.Query(name=b"www.pr0gramm.com"))
answer = psock_req.sr(dns_req)[0][dns.DNS]
print("answer is: %s" % answer)
psock_req.close()
"""
#
# spoof DNS response
#
print("waiting for DNS request")
psock = psocket.SocketHndl(iface_name=IFACE, timeout=600)
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]
answer_dns = answer[dns.DNS]
print("got DNS packet: %s" % answer_dns)
dns_answer_send = answer.create_reverse()
layer_dns = dns_answer_send[dns.DNS]
layer_dns.id = answer_dns.id
layer_dns.flags = 0x8180
layer_dns.queries = answer_dns.queries[0]
layer_dns.answers = dns.DNS.Answer(address=pypacker.ip4_str_to_bytes("173.194.70.1"))
# layer_dns.addrecords = answer_dns.addrecords[0]
layer_dns.addrecords = dns.DNS.AddRecord()
layer_dns.addrecords[0].type = 0x0029
layer_dns.addrecords[0].plen = 0x05b4