From 60f805285b9fef97fd6b45c77532dd74efe2a618 Mon Sep 17 00:00:00 2001 From: Emmanuel Prochasson Date: Sat, 17 Aug 2024 09:57:41 -0400 Subject: [PATCH 1/2] FIX: tshark errors are now captured and displayed as a warning Moved fixed tshark dump to securely generated temporary file to avoid collision errors --- howmanypeoplearearound/__main__.py | 31 ++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/howmanypeoplearearound/__main__.py b/howmanypeoplearearound/__main__.py index c4168bf..2a413f6 100644 --- a/howmanypeoplearearound/__main__.py +++ b/howmanypeoplearearound/__main__.py @@ -6,6 +6,7 @@ import subprocess import json import time +import tempfile import netifaces import click @@ -18,6 +19,7 @@ from pick import pick import curses + def which(program): """Determines whether program exists """ @@ -143,20 +145,28 @@ def scan(adapter, scantime, verbose, dictionary, number, nearby, jsonprint, out, t1.daemon = True t1.start() - dump_file = '/tmp/tshark-temp' + tmpdir = tempfile.TemporaryDirectory() + dump_file = os.path.join(tmpdir.name, 'tshark-temp') + # Scan with tshark - command = [tshark, '-I', '-i', adapter, '-a', + # EP, 20240817: -Q flags silences non-essential output that we don't use anyway, + # so anything on stderr is actually an error + command = [tshark, '-Q', '-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() + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = run_tshark.communicate() + if stderr != b"": + print("Warning: error when running tshark:\n") + print(stderr.decode('utf8').strip()) if not number: t1.join() else: + tmpdir = None dump_file = pcap # Read tshark output @@ -171,8 +181,12 @@ def scan(adapter, scantime, verbose, dictionary, number, nearby, jsonprint, out, if verbose: print(' '.join(command)) run_tshark = subprocess.Popen( - command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - output, nothing = run_tshark.communicate() + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + output, stderr = run_tshark.communicate() + + if stderr != b"": + print("Warning: error parsing tshark output") + print(stderr.decode('utf8').strip()) # read target MAC address targetmacset = set() @@ -180,6 +194,7 @@ def scan(adapter, scantime, verbose, dictionary, number, nearby, jsonprint, out, targetmacset = fileToMacSet(targetmacs) foundMacs = {} + for line in output.decode('utf-8').split('\n'): if verbose: print(line) @@ -277,8 +292,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)) - if not pcap: - os.remove(dump_file) + if tmpdir is not None: + tmpdir.cleanup() return adapter From 06e4f3bf6d2c9d34a93149dce0602301b3e936b5 Mon Sep 17 00:00:00 2001 From: Emmanuel Prochasson Date: Sat, 17 Aug 2024 10:10:28 -0400 Subject: [PATCH 2/2] FIX: remove usage of tempfile.TemporaryDirectory, not available in Python 2 --- howmanypeoplearearound/__main__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/howmanypeoplearearound/__main__.py b/howmanypeoplearearound/__main__.py index 2a413f6..e472526 100644 --- a/howmanypeoplearearound/__main__.py +++ b/howmanypeoplearearound/__main__.py @@ -145,8 +145,8 @@ def scan(adapter, scantime, verbose, dictionary, number, nearby, jsonprint, out, t1.daemon = True t1.start() - tmpdir = tempfile.TemporaryDirectory() - dump_file = os.path.join(tmpdir.name, 'tshark-temp') + tmpdir = tempfile.mkdtemp() + dump_file = os.path.join(tmpdir, 'tshark-temp') # Scan with tshark # EP, 20240817: -Q flags silences non-essential output that we don't use anyway, @@ -293,7 +293,8 @@ def scan(adapter, scantime, verbose, dictionary, number, nearby, jsonprint, out, if verbose: print("Wrote %d records to %s" % (len(cellphone_people), out)) if tmpdir is not None: - tmpdir.cleanup() + os.remove(dump_file) + os.rmdir(tmpdir) return adapter