From 4fb7205281004625e52f3d27c8b5c8eba1e9bb2b Mon Sep 17 00:00:00 2001 From: dadav <33197631+dadav@users.noreply.github.com> Date: Sat, 7 Dec 2019 09:30:05 +0100 Subject: [PATCH] Add lock --- pwnagotchi/plugins/default/onlinehashcrack.py | 74 ++++++++++--------- pwnagotchi/plugins/default/wpa-sec.py | 4 +- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/pwnagotchi/plugins/default/onlinehashcrack.py b/pwnagotchi/plugins/default/onlinehashcrack.py index 9e65e89e..11b37f91 100644 --- a/pwnagotchi/plugins/default/onlinehashcrack.py +++ b/pwnagotchi/plugins/default/onlinehashcrack.py @@ -2,20 +2,27 @@ import os import logging import re import requests +from threading import Lock from pwnagotchi.utils import StatusFile import pwnagotchi.plugins as plugins +from json.decoder import JSONDecodeError class OnlineHashCrack(plugins.Plugin): __author__ = '33197631+dadav@users.noreply.github.com' - __version__ = '2.0.0' + __version__ = '2.0.1' __license__ = 'GPL3' __description__ = 'This plugin automatically uploads handshakes to https://onlinehashcrack.com' def __init__(self): self.ready = False - self.report = StatusFile('/root/.ohc_uploads', data_format='json') + try: + self.report = StatusFile('/root/.ohc_uploads', data_format='json') + except JSONDecodeError as json_err: + os.remove('/root/.ohc_uploads') + self.report = StatusFile('/root/.ohc_uploads', data_format='json') self.skip = list() + self.lock = Lock() def on_loaded(self): """ @@ -68,39 +75,40 @@ class OnlineHashCrack(plugins.Plugin): """ Called in manual mode when there's internet connectivity """ - if self.ready: - display = agent.view() - config = agent.config() - reported = self.report.data_field_or('reported', default=list()) + with self.lock: + if self.ready: + display = agent.view() + config = agent.config() + reported = self.report.data_field_or('reported', default=list()) - handshake_dir = config['bettercap']['handshakes'] - handshake_filenames = os.listdir(handshake_dir) - handshake_paths = [os.path.join(handshake_dir, filename) for filename in handshake_filenames if - filename.endswith('.pcap')] + handshake_dir = config['bettercap']['handshakes'] + handshake_filenames = os.listdir(handshake_dir) + handshake_paths = [os.path.join(handshake_dir, filename) for filename in handshake_filenames if + filename.endswith('.pcap')] - # pull out whitelisted APs - handshake_paths = filter(lambda path: self._filter_handshake_file(path), handshake_paths) + # pull out whitelisted APs + handshake_paths = filter(lambda path: self._filter_handshake_file(path), handshake_paths) - handshake_new = set(handshake_paths) - set(reported) - set(self.skip) + handshake_new = set(handshake_paths) - set(reported) - set(self.skip) - if handshake_new: - logging.info("OHC: Internet connectivity detected. Uploading new handshakes to onelinehashcrack.com") + if handshake_new: + logging.info("OHC: Internet connectivity detected. Uploading new handshakes to onelinehashcrack.com") - for idx, handshake in enumerate(handshake_new): - display.set('status', - f"Uploading handshake to onlinehashcrack.com ({idx + 1}/{len(handshake_new)})") - display.update(force=True) - try: - self._upload_to_ohc(handshake) - if handshake not in reported: - reported.append(handshake) - self.report.update(data={'reported': reported}) - logging.info(f"OHC: Successfully uploaded {handshake}") - except requests.exceptions.RequestException as req_e: - self.skip.append(handshake) - logging.error("OHC: %s", req_e) - continue - except OSError as os_e: - self.skip.append(handshake) - logging.error("OHC: %s", os_e) - continue + for idx, handshake in enumerate(handshake_new): + display.set('status', + f"Uploading handshake to onlinehashcrack.com ({idx + 1}/{len(handshake_new)})") + display.update(force=True) + try: + self._upload_to_ohc(handshake) + if handshake not in reported: + reported.append(handshake) + self.report.update(data={'reported': reported}) + logging.info(f"OHC: Successfully uploaded {handshake}") + except requests.exceptions.RequestException as req_e: + self.skip.append(handshake) + logging.error("OHC: %s", req_e) + continue + except OSError as os_e: + self.skip.append(handshake) + logging.error("OHC: %s", os_e) + continue diff --git a/pwnagotchi/plugins/default/wpa-sec.py b/pwnagotchi/plugins/default/wpa-sec.py index 7dd8a844..4c4dac30 100644 --- a/pwnagotchi/plugins/default/wpa-sec.py +++ b/pwnagotchi/plugins/default/wpa-sec.py @@ -1,8 +1,8 @@ import os import logging -import threading import requests from datetime import datetime +from threading import Lock from pwnagotchi.utils import StatusFile from pwnagotchi import plugins from json.decoder import JSONDecodeError @@ -16,7 +16,7 @@ class WpaSec(plugins.Plugin): def __init__(self): self.ready = False - self.lock = threading.Lock() + self.lock = Lock() try: self.report = StatusFile('/root/.wpa_sec_uploads', data_format='json') except JSONDecodeError as json_err: