patternpythonMinor
A rapidly-growing script that visualizes WiFi sniffing
Viewed 0 times
scriptsniffingrapidlyvisualizesgrowingthatwifi
Problem
Below is one of the versions of my
```
#!/usr/bin/python
"""
Sniffs on the WiFi network and generates a graph showing the communication
between the devices, including information about the announced SSIDs. Will
loop infinitely - kill the program with SIGINT in order to stop it
gracefully. You can also use SIGUSR1 to force the generation of the graph
without killing the program - useful for the periodic generation of the
visuals.
Example usage (the "timeout" kills wifimap after 5s):
timeout --foreground -s INT 5s wifimap | dot -Tsvg > out.svg
TODO:
* split wifimap into sniffing and graphing programs, maybe also ch-hopping
* verify if there's a bug related to 'Announces' being printed with wrong
source
Author: Jacek Wielemborek, licensed under WTFPL
"""
import subprocess
import sys
import time
import re
import threading
from StringIO import StringIO
from lxml import etree
from collections import defaultdict
import signal
def can_do_sudo():
"""Tells whether we can run sudo without being asked for password."""
p = subprocess.Popen("SUDO_ASKPASS=/usr/bin/false sudo -A true",
shell=True)
p.communicate()
return p.returncode == 0
class ChannelHopper(threading.Thread):
"""A channel hopper thread. Switches the Wireless channel periodically."""
def __init__(self, interval, iface):
"""
Constructs a ChannelHopper instance.
Args:
interval - the time between channel switches
iface - the interface to perform switching on
"""
threading.Thread.__init__(self)
self.running = True
self.interval = interval
self.iface = iface
def get_frequency(self):
"""Returns the current frequency of the interface."""
s = subprocess.check_output(["iwconfig", self.iface])
return re.findall('Frequency:([^ ]+)', s)[0]
def run(self):
if not can_do_sudo():
sys.stderr.write("Cou
wifimap script:```
#!/usr/bin/python
"""
Sniffs on the WiFi network and generates a graph showing the communication
between the devices, including information about the announced SSIDs. Will
loop infinitely - kill the program with SIGINT in order to stop it
gracefully. You can also use SIGUSR1 to force the generation of the graph
without killing the program - useful for the periodic generation of the
visuals.
Example usage (the "timeout" kills wifimap after 5s):
timeout --foreground -s INT 5s wifimap | dot -Tsvg > out.svg
TODO:
* split wifimap into sniffing and graphing programs, maybe also ch-hopping
* verify if there's a bug related to 'Announces' being printed with wrong
source
Author: Jacek Wielemborek, licensed under WTFPL
"""
import subprocess
import sys
import time
import re
import threading
from StringIO import StringIO
from lxml import etree
from collections import defaultdict
import signal
def can_do_sudo():
"""Tells whether we can run sudo without being asked for password."""
p = subprocess.Popen("SUDO_ASKPASS=/usr/bin/false sudo -A true",
shell=True)
p.communicate()
return p.returncode == 0
class ChannelHopper(threading.Thread):
"""A channel hopper thread. Switches the Wireless channel periodically."""
def __init__(self, interval, iface):
"""
Constructs a ChannelHopper instance.
Args:
interval - the time between channel switches
iface - the interface to perform switching on
"""
threading.Thread.__init__(self)
self.running = True
self.interval = interval
self.iface = iface
def get_frequency(self):
"""Returns the current frequency of the interface."""
s = subprocess.check_output(["iwconfig", self.iface])
return re.findall('Frequency:([^ ]+)', s)[0]
def run(self):
if not can_do_sudo():
sys.stderr.write("Cou
Solution
Style
You seem to be adhering to PEP8, which is usually a good thing.
Many of your functions have descriptive names, but your variables do not.
The following snippet stood out:
Are you sure this is working the way you intend it to work? And why is the
Usage
Unfortunately I have not managed to get your script running. This may be my fault, but in my opinion programs and scripts should not be a pain in the behind to get running. We are past those days, even in Linux land.
The example usage:
You seem to be adhering to PEP8, which is usually a good thing.
Many of your functions have descriptive names, but your variables do not.
p, s, x, d, w and c are terrible. k1 and k2 are also lousy index names, but since they're used only in a relatively small loop it's less problematic. I have no idea why they're called k1 and k2 while code should be self descriptive. Nick (known on SE as 'nhgrif') wrote a post about this recently.The following snippet stood out:
def get_dump_wifimap(w, skip_broadcast):
"""
Returns a closure that is supposed to work as a signal handler. It can be
used when SIGUSR1 is received to force the generation of the report at the
given time.
"""
def dump_wifimap(*args, **kwargs):
try:
sys.stdout.seek(0)
except IOError:
pass
w.print_report(skip_broadcast)
sys.stdout.flush()
return dump_wifimapAre you sure this is working the way you intend it to work? And why is the
IOError passed?Usage
Unfortunately I have not managed to get your script running. This may be my fault, but in my opinion programs and scripts should not be a pain in the behind to get running. We are past those days, even in Linux land.
The example usage:
timeout --foreground -s INT 5s wifimap | dot -Tsvg > out.svg seems incomplete. I suppose this should have been timeout --foreground -s INT 5s python wifimap.py | dot -Tsvg > out.svg, assuming wifimap.py as name of the file. However, it will fail on line 227-229:p = subprocess.Popen(["tshark", "-i", args.iface, "-I",
"-y", "IEEE802_11_RADIO", "-T", "pdml"],
stdout=subprocess.PIPE)OSError: [Errno 2] No such file or directoryCode Snippets
def get_dump_wifimap(w, skip_broadcast):
"""
Returns a closure that is supposed to work as a signal handler. It can be
used when SIGUSR1 is received to force the generation of the report at the
given time.
"""
def dump_wifimap(*args, **kwargs):
try:
sys.stdout.seek(0)
except IOError:
pass
w.print_report(skip_broadcast)
sys.stdout.flush()
return dump_wifimapp = subprocess.Popen(["tshark", "-i", args.iface, "-I",
"-y", "IEEE802_11_RADIO", "-T", "pdml"],
stdout=subprocess.PIPE)Context
StackExchange Code Review Q#88407, answer score: 7
Revisions (0)
No revisions yet.