I was wondering if it was possible to send CTS frames in python3 with modules such as scapy. If not, how would I do it with the sockets module?
Thanks in advance.
I can't say for scapy, but CTS frames and 802.11 in general seems to be too deep for the python socket module.
This is OSI Level 2, while socket have limited capabilities below OSI Level 3.
Some possible starting points are:
People already tried to work with 802.11 via sockets.
You may try to modify this code for Ethernet communication. Note the socket creation: socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) - AF_PACKET instead of AF_INET allows Level 2 operations.
Try to use socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))). ETH_P_ALL means the socket will be receiving all Level 2 packets. All because actually I haven't find any 802.11-specific sources in the Linux kernel.
Get an open source driver for your Wireless NIC and see how they do it. It may appear that communicating directly with the hardware will be more fruitful than trying to find a general mechanism embedded in sockets.
A related email thread: Correct way to obtain the 802.11 headers with a raw socket?
Related
I am working with an ESP8266 (NodeMCU) with MicroPython and want to be able to do packet injection or send raw packets / freedom packets. I cannot find anyway to open a raw socket (usocket/socket module) or do this via the 'network' module. Is there anyway I can do this?
The normal python equivalent would be:
import socket
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))
s.bind(("wlan0", 0x0003)) #wlan0 being in monitor mode
In micropython, you can enter monitor mode (station mode) like this
import network
sta_if = network.WLAN(network.STA_IF)
But from there, you cannot send/receive any packets. Is there any way to be able to do this?
Any help is much appreciated.
Sorry for the lack of detail but I have no idea what to do from here.
I don't think you can achieve raw socket by using micropython#esp8266
One reason is the memory are very limited on esp8266. Another reason is their socket doesn't implement raw socket.
You could try to use CC3200 ports and modify it's micropython's firmware.
I want to make a client-server model where server will send some UDP packet and client will receive them. I am thinking of using Scapy to send packets. Does Scapy gives any facility to receive packets(listen for packets)?
Scapy is able to craft packets, i.e. to build specific packets according to your needs. And yes, sending and receiving functions are the core functions of scapy. However, This is more for debugging purposes than for production systems. You should consider using Python's socket module directly.
I have a twisted proxy from here: Python Twisted proxy - how to intercept packets .
It prints the HTTP data, and I would like also to intercept and examine the raw IP datgrams. How to hook the callback for the IP packets?
http://twistedmatrix.com/documents/11.0.0/api/twisted.pair.ip.IPProtocol.html
Twisted doesn't have a built-in friendly way to hook in a listener on a raw IP socket (SOCK_RAW). This is for several reasons:
using SOCK_RAW can be tricky and it can work in non-obvious ways;
in most environments, using such a socket requires elevated privileges;
and the packets you actually get through a raw socket differ a lot between operating systems (e.g., you won't get any raw TCP-protocol IP packets on *BSD/Darwin through a raw socket, even if you're root).
The best way to capture raw datagrams in general, in a remotely portable manner, is with libpcap. Here is a link to someone who appears to have combined pcap and Twisted in a reasonably intelligent way; that may help.
Twisted doesn't include comprehensive support for operating at the IP level. There is some support for parsing IP datagrams, as you found, but no built-in support for hooking into platform support for sending or receiving these.
You might want to take a look at scapy.
I want to figure out whether my computer is somehow causing a UDP flood that is originating from my network. So that's my underlying problem, and what follows is simply my non-network-person attempt to hypothesize a solution using python. I'm extrapolating from recipe 13.1 ("Passing Messages with Socket Datagrams") from the python cookbook (also here).
Would it possible/sensible/not insane to try somehow writing an outgoing UDP proxy in python, so that outgoing packets could be logged before being sent on their merry way? If so, how would one go about it? Based on my quick research, perhaps I could start a server process listening on suspect UDP ports and log anything that gets sent, then forward it on, such as:
import socket
s =socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", MYPORT))
while True:
packet = dict(zip('data', 'addr'), s.recvfrom(1,024))
log.info("Recieved {data} from {addr}.".format(**packet))
But what about doing this for a large number of ports simultaneously? Impractical? Are there drawbacks or other reasons not to bother with this? Is there a better way to solve this problem (please be gentle).
It might be easier just to install Wireshark, instead of rolling your own in Python.
I would like to be able to perform a ping and traceroute from within Python without having to execute the corresponding shell commands so I'd prefer a native python solution.
If you don't mind using an external module and not using UDP or TCP, scapy is an easy solution:
from scapy.all import *
target = ["192.168.1.254"]
result, unans = traceroute(target,l4=UDP(sport=RandShort())/DNS(qd=DNSQR(qname="www.google.com")))
Or you can use the tcp version
from scapy.all import *
target = ["192.168.1.254"]
result, unans = traceroute(target,maxttl=32)
Please note you will have to run scapy as root in order to be able to perform these tasks or you will get:
socket.error: [Errno 1] Operation not permitted
Running interpreters as root is often frowned upon on security grounds (and of course you DO need to have root permission to access the "raw" socked as needed by the ICMP specs of ping and traceroute!), but if you have no problems with that it's not hard -- e.g., this post(dead?) or this post give a workable ping, and Jeremy Hylton's old page has still-usable underlying code for ICMP (both ping and traceroute) though it's written for very old Python versions and needs a litte facelift to shine with modern ones -- but, the concepts ARE all there, in both the URLs I gave you!
The Webb Library is very handy in performing all kinds of web related extracts...and ping and traceroute can be done easily through it. Just include the URL you want to traceroute to:
import webb
webb.traceroute("your-web-page-url")
If you wish to store the traceroute log to a text file automatically, use the following command:
webb.traceroute("your-web-page-url",'file-name.txt')
Similarly a IP address of a URl (server) can be obtained with the following lines of code:
print(webb.get_ip("your-web-page-url"))
Hope it helps!
The mtrpacket package can be used to send network probes, which can perform either a ping or a traceroute. Since it uses the back-end to the mtr commandline tool, it doesn't require that your script run as root.
It also uses asyncio's event loop, so you can have multiple ongoing traceroutes or pings simultaneously, and deal with their results as they complete.
Here is a Python script to traceroute to 'example.com':
import asyncio
import mtrpacket
async def trace():
async with mtrpacket.MtrPacket() as mtr:
for ttl in range(1, 256):
result = await mtr.probe('example.com', ttl=ttl)
print(result)
if result.success:
break
asyncio.get_event_loop().run_until_complete(trace())
The loop with 'ttl' is used because the 'time-to-live' of an outgoing packet determines the number of network hops the packet will travel before expiring and sending an error back to the original source.
you might want to check out the scapy package. it's the swiss army knife of network tools for python.
ICMP Ping is standard as part of the ICMP protocol.
Traceroute uses features of ICMP and IP to determine a path via Time To Live values. Using TTL values, you can do traceroutes in a variety of protocols as long as IP/ICMP work because it is the ICMP TTL EXceeded messages that tell you about the hop in the path.
If you attempt to access a port where no listener is available, by ICMP protocol rules, the host is supposed to send an ICMP Port Unreachable message.
I wrote a simple tcptraceroute in python which does not need root privileges http://www.thomas-guettler.de/scripts/tcptraceroute.py.txt
But it can't display the IP addresses of the intermediate hops. But sometimes it is useful, since you can guess where the blocking firewall is: Either at the beginning or at the end of the route.