From b8e6f9f979d8f1d34c2132400014e04b13285149 Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Fri, 4 Oct 2019 09:11:32 +0200 Subject: [PATCH 1/3] Upload via python library --- .../pwnagotchi/plugins/default/wpa-sec.py | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py index 64df84e..30e136e 100644 --- a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py +++ b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py @@ -1,25 +1,22 @@ __author__ = '33197631+dadav@users.noreply.github.com' __version__ = '1.0.0' -__name__ = 'wpa_sec' +__name__ = 'wpa-sec' __license__ = 'GPL3' __description__ = 'This plugin automatically uploades handshakes to https://wpa-sec.stanev.org' import os import logging -import subprocess +import requests READY = False API_KEY = None ALREADY_UPLOADED = None -# INSTALLATION: -## apt-get install libcurl4-openssl-dev -## https://github.com/ZerBea/hcxtools.git -## cd hcxtools; gcc wlancap2wpasec.c -o wlancap2wpasec -lcurl -## mv wlancap2wpasec /usr/bin/ - def on_loaded(): + """ + Gets called when the plugin gets loaded + """ global READY global API_KEY global ALREADY_UPLOADED @@ -28,50 +25,57 @@ def on_loaded(): logging.error("WPA_SEC: API-KEY isn't set. Can't upload to wpa-sec.stanev.org") return - try: - subprocess.call("wlancap2wpasec -h >/dev/null".split(), stdout=open(os.devnull, 'wb')) - except OSError: - logging.error("WPA_SEC: Can't find wlancap2wpasec. Install hcxtools to use this plugin!") - return - try: with open('/root/.wpa_sec_uploads', 'r') as f: ALREADY_UPLOADED = f.read().splitlines() except OSError: - logging.error('WPA_SEC: No upload-file found.') + logging.warning('WPA_SEC: No upload-file found.') ALREADY_UPLOADED = [] READY = True -def _upload_to_wpasec(path): - try: - subprocess.call(f"wlancap2wpasec -k {API_KEY} {path}".split(), stdout=open(os.devnull, 'wb')) - except OSError as os_e: - logging.error(f"WPA_SEC: Error while uploading {path}") - raise os_e +def _upload_to_wpasec(path, timeout=30): + """ + Uploads the file to wpa-sec.stanev.org + """ + with open(path, 'rb') as file_to_upload: + headers = {'key': API_KEY} + payload = {'file': file_to_upload} + + try: + requests.post('https://wpa-sec.stanev.org/?submit', + headers=headers, + files=payload, + timeout=timeout) + except requests.exceptions.RequestException as e: + logging.error(f"WPA_SEC: Got an exception while uploading {path} -> {e}") + raise e -# called in manual mode when there's internet connectivity def on_internet_available(display, config, log): + """ + Called in manual mode when there's internet connectivity + """ if READY: - handshake_dir = config['bettercap']['handshakes'] handshake_filenames = os.listdir(handshake_dir) handshake_paths = [os.path.join(handshake_dir, filename) for filename in handshake_filenames] handshake_new = set(handshake_paths) - set(ALREADY_UPLOADED) if handshake_new: - logging.info("Internet connectivity detected.\ + logging.info("WPA_SEC: Internet connectivity detected.\ Uploading new handshakes to wpa-sec.stanev.org") for idx, handshake in enumerate(handshake_new): - display.set('status', "Uploading handshake to wpa-sec.stanev.org ({idx + 1}/{len(handshake_new})") + display.set('status', f"Uploading handshake to wpa-sec.stanev.org ({idx + 1}/{len(handshake_new)})") display.update(force=True) try: _upload_to_wpasec(handshake) ALREADY_UPLOADED.append(handshake) with open('/root/.wpa_sec_uploads', 'a') as f: f.write(handshake + "\n") - except OSError: + except requests.exceptions.RequestException: pass + except OSError as os_e: + logging.error(f"WPA_SEC: Got the following error: {os_e}") From bc10be69a0c0ceefa925497ef7fae5c863bd4dae Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Fri, 4 Oct 2019 09:26:08 +0200 Subject: [PATCH 2/3] Add already submitted check --- .../pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py index 30e136e..e3267e0 100644 --- a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py +++ b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py @@ -44,10 +44,12 @@ def _upload_to_wpasec(path, timeout=30): payload = {'file': file_to_upload} try: - requests.post('https://wpa-sec.stanev.org/?submit', + result = requests.post('https://wpa-sec.stanev.org/?submit', headers=headers, files=payload, timeout=timeout) + if ' already submitted' in result.text: + logging.warning(f"{path} was already submitted.") except requests.exceptions.RequestException as e: logging.error(f"WPA_SEC: Got an exception while uploading {path} -> {e}") raise e From d52ea8718f306418ab85e9c354be4eee412f365f Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Fri, 4 Oct 2019 09:33:11 +0200 Subject: [PATCH 3/3] Be a lil bit more verbose --- .../pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py index e3267e0..b4ee86c 100644 --- a/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py +++ b/sdcard/rootfs/root/pwnagotchi/scripts/pwnagotchi/plugins/default/wpa-sec.py @@ -77,6 +77,7 @@ def on_internet_available(display, config, log): ALREADY_UPLOADED.append(handshake) with open('/root/.wpa_sec_uploads', 'a') as f: f.write(handshake + "\n") + logging.info(f"WPA_SEC: Successfuly uploaded {handshake}") except requests.exceptions.RequestException: pass except OSError as os_e: