From 64acac865aa72cab1439b4164f37444474f67189 Mon Sep 17 00:00:00 2001 From: Asutosh Palai Date: Sun, 27 Jan 2019 15:07:49 -0500 Subject: [PATCH] Added CLI option to specify the pcap flie (#46) * Added CLI option to specify the pcap flie * Fixed urlopen import for python2 --- README.md | 5 ++ howmanypeoplearearound/__main__.py | 76 +++++++++++++++++------------- howmanypeoplearearound/oui.py | 8 ++-- 3 files changed, 52 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 3abd302..056e9d5 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,11 @@ Namely you want to find a USB adapter with one of the following chipsets: Athero brew cask install wireshark-chmodbpf ``` +You need to dissociate from any AP before initiating the scanning: +``` +sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -z +``` + ### Linux [tshark](https://www.wireshark.org/docs/man-pages/tshark.html) ``` sudo apt-get install tshark diff --git a/howmanypeoplearearound/__main__.py b/howmanypeoplearearound/__main__.py index eae873e..051a8b4 100644 --- a/howmanypeoplearearound/__main__.py +++ b/howmanypeoplearearound/__main__.py @@ -73,20 +73,21 @@ def fileToMacSet(path): @click.option('--port', default=8001, help='port to use when serving analysis') @click.option('--sort', help='sort cellphone data by distance (rssi)', is_flag=True) @click.option('--targetmacs', help='read a file that contains target MAC addresses', default='') -def main(adapter, scantime, verbose, dictionary, number, nearby, jsonprint, out, allmacaddresses, nocorrection, loop, analyze, port, sort, targetmacs): +@click.option('-f', '--pcap', help='read a pcap file instead of capturing') +def main(adapter, scantime, verbose, dictionary, number, nearby, jsonprint, out, allmacaddresses, nocorrection, loop, analyze, port, sort, targetmacs, pcap): if analyze != '': analyze_file(analyze, port) return if loop: while True: adapter = scan(adapter, scantime, verbose, dictionary, number, - nearby, jsonprint, out, allmacaddresses, nocorrection, loop, sort, targetmacs) + nearby, jsonprint, out, allmacaddresses, nocorrection, loop, sort, targetmacs, pcap) else: scan(adapter, scantime, verbose, dictionary, number, - nearby, jsonprint, out, allmacaddresses, nocorrection, loop, sort, targetmacs) + nearby, jsonprint, out, allmacaddresses, nocorrection, loop, sort, targetmacs, pcap) -def scan(adapter, scantime, verbose, dictionary, number, nearby, jsonprint, out, allmacaddresses, nocorrection, loop, sort, targetmacs): +def scan(adapter, scantime, verbose, dictionary, number, nearby, jsonprint, out, allmacaddresses, nocorrection, loop, sort, targetmacs, pcap): """Monitor wifi signals to count the number of people around you""" # print("OS: " + os.name) @@ -117,39 +118,45 @@ def scan(adapter, scantime, verbose, dictionary, number, nearby, jsonprint, out, if number: verbose = False - if len(adapter) == 0: - if os.name == 'nt': - print('You must specify the adapter with -a ADAPTER') - print('Choose from the following: ' + - ', '.join(netifaces.interfaces())) - sys.exit(1) - title = 'Please choose the adapter you want to use: ' - adapter, index = pick(netifaces.interfaces(), title) - - print("Using %s adapter and scanning for %s seconds..." % - (adapter, scantime)) - - if not number: - # Start timer - t1 = threading.Thread(target=showTimer, args=(scantime,)) - t1.daemon = True - t1.start() - - # Scan with tshark - command = [tshark, '-I', '-i', adapter, '-a', - 'duration:' + scantime, '-w', '/tmp/tshark-temp'] - if verbose: - print(' '.join(command)) - run_tshark = subprocess.Popen( - command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - stdout, nothing = run_tshark.communicate() - if not number: - t1.join() + if not pcap: + if len(adapter) == 0: + if os.name == 'nt': + print('You must specify the adapter with -a ADAPTER') + print('Choose from the following: ' + + ', '.join(netifaces.interfaces())) + sys.exit(1) + title = 'Please choose the adapter you want to use: ' + adapter, index = pick(netifaces.interfaces(), title) + + print("Using %s adapter and scanning for %s seconds..." % + (adapter, scantime)) + + if not number: + # Start timer + t1 = threading.Thread(target=showTimer, args=(scantime,)) + t1.daemon = True + t1.start() + + dump_file = '/tmp/tshark-temp' + # Scan with tshark + command = [tshark, '-I', '-i', adapter, '-a', + 'duration:' + scantime, '-w', dump_file] + if verbose: + print(' '.join(command)) + run_tshark = subprocess.Popen( + command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + stdout, nothing = run_tshark.communicate() + + + if not number: + t1.join() + else: + dump_file = pcap # Read tshark output command = [ tshark, '-r', - '/tmp/tshark-temp', '-T', + dump_file, '-T', 'fields', '-e', 'wlan.sa', '-e', 'wlan.bssid', '-e', @@ -259,7 +266,8 @@ def scan(adapter, scantime, verbose, dictionary, number, nearby, jsonprint, out, f.write(json.dumps(data_dump) + "\n") if verbose: print("Wrote %d records to %s" % (len(cellphone_people), out)) - os.remove('/tmp/tshark-temp') + if not pcap: + os.remove(dump_file) return adapter diff --git a/howmanypeoplearearound/oui.py b/howmanypeoplearearound/oui.py index ef0c44f..6a24389 100644 --- a/howmanypeoplearearound/oui.py +++ b/howmanypeoplearearound/oui.py @@ -1,5 +1,7 @@ -from urllib.request import urlopen -from urllib.request import Request +try: #python3 + from urllib.request import urlopen +except: #python2 + from urllib2 import urlopen def load_dictionary(file): @@ -17,6 +19,6 @@ def load_dictionary(file): def download_oui(to_file): uri = 'http://standards-oui.ieee.org/oui/oui.txt' print("Trying to download current version of oui.txt from [%s] to file [%s]" % (uri, to_file)) - oui_data = urlopen(Request(uri), timeout=10).read() + oui_data = urlopen(uri, timeout=10).read() with open(to_file, 'wb') as oui_file: oui_file.write(oui_data)