2023-08-23 14:06:28 +02:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
import random
|
|
|
|
from io import TextIOWrapper
|
2023-09-16 22:05:42 +02:00
|
|
|
|
|
|
|
import pwnagotchi
|
2023-08-23 14:06:28 +02:00
|
|
|
from pwnagotchi import plugins
|
|
|
|
|
|
|
|
import pwnagotchi.ui.faces as faces
|
|
|
|
from pwnagotchi.bettercap import Client
|
|
|
|
|
|
|
|
|
2023-09-07 14:19:04 +02:00
|
|
|
class FixServices(plugins.Plugin):
|
2023-09-17 09:55:18 +02:00
|
|
|
__author__ = 'jayofelony'
|
|
|
|
__version__ = '1.0'
|
2023-08-23 14:06:28 +02:00
|
|
|
__license__ = 'GPL3'
|
2023-09-05 03:17:59 +02:00
|
|
|
__description__ = 'Fix blindness, firmware crashes and brain not being loaded'
|
2023-09-07 07:49:53 +02:00
|
|
|
__name__ = 'Fix_Services'
|
2023-08-23 14:06:28 +02:00
|
|
|
__help__ = """
|
|
|
|
Reload brcmfmac module when blindbug is detected, instead of rebooting. Adapted from WATCHDOG.
|
|
|
|
"""
|
|
|
|
__defaults__ = {
|
2023-09-07 13:00:40 +02:00
|
|
|
'enabled': True,
|
2023-08-23 14:06:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.options = dict()
|
|
|
|
self.pattern = re.compile(r'brcmf_cfg80211_nexmon_set_channel.*?Set Channel failed')
|
|
|
|
self.pattern2 = re.compile(r'wifi error while hopping to channel')
|
2023-09-06 10:20:43 +02:00
|
|
|
self.pattern3 = re.compile(r'Firmware has halted or crashed')
|
2023-09-06 17:01:26 +02:00
|
|
|
self.pattern4 = re.compile(r'error 400: could not find interface wlan0mon')
|
2023-08-23 14:06:28 +02:00
|
|
|
self.isReloadingMon = False
|
|
|
|
self.connection = None
|
|
|
|
self.LASTTRY = 0
|
|
|
|
self._count = 0
|
2023-09-05 13:22:06 +02:00
|
|
|
|
2023-08-23 14:06:28 +02:00
|
|
|
def on_loaded(self):
|
|
|
|
"""
|
|
|
|
Gets called when the plugin gets loaded
|
|
|
|
"""
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services] plugin loaded.")
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
def on_ready(self, agent):
|
2024-01-20 23:54:32 +01:00
|
|
|
last_lines = self.get_last_lines('journalctl', ['-n10', '-k'], 10)
|
2023-08-23 14:06:28 +02:00
|
|
|
try:
|
|
|
|
cmd_output = subprocess.check_output("ip link show wlan0mon", shell=True)
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services ip link show wlan0mon]: %s" % repr(cmd_output))
|
2023-08-23 14:06:28 +02:00
|
|
|
if ",UP," in str(cmd_output):
|
|
|
|
logging.info("wlan0mon is up.")
|
2023-09-05 03:17:59 +02:00
|
|
|
|
2023-09-05 13:22:06 +02:00
|
|
|
if len(self.pattern.findall(last_lines)) >= 3:
|
2023-08-23 14:06:28 +02:00
|
|
|
if hasattr(agent, 'view'):
|
|
|
|
display = agent.view()
|
|
|
|
display.set('status', 'Blind-Bug detected. Restarting.')
|
|
|
|
display.update(force=True)
|
2023-09-17 21:23:36 +02:00
|
|
|
logging.info('[Fix_Services] Blind-Bug detected. Restarting.')
|
2023-08-23 14:06:28 +02:00
|
|
|
try:
|
|
|
|
self._tryTurningItOffAndOnAgain(agent)
|
|
|
|
except Exception as err:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.warning("[Fix_Services turnOffAndOn] %s" % repr(err))
|
2023-09-05 03:17:59 +02:00
|
|
|
|
2023-08-23 14:06:28 +02:00
|
|
|
else:
|
2023-09-17 21:22:10 +02:00
|
|
|
logging.info("[Fix_Services] Logs look good!")
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
except Exception as err:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.error("[Fix_Services ip link show wlan0mon]: %s" % repr(err))
|
2023-08-23 14:06:28 +02:00
|
|
|
try:
|
|
|
|
self._tryTurningItOffAndOnAgain(agent)
|
|
|
|
except Exception as err:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.error("[Fix_Services OffNOn]: %s" % repr(err))
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
# bettercap sys_log event
|
|
|
|
# search syslog events for the brcmf channel fail, and reset when it shows up
|
|
|
|
# apparently this only gets messages from bettercap going to syslog, not from syslog
|
|
|
|
def on_bcap_sys_log(self, agent, event):
|
|
|
|
if re.search('wifi error while hopping to channel', event['data']['Message']):
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services]SYSLOG MATCH: %s" % event['data']['Message'])
|
|
|
|
logging.info("[Fix_Services]**** restarting wifi.recon")
|
2023-08-23 14:06:28 +02:00
|
|
|
try:
|
|
|
|
result = agent.run("wifi.recon off; wifi.recon on")
|
|
|
|
if result["success"]:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services] wifi.recon flip: success!")
|
2023-08-23 14:06:28 +02:00
|
|
|
if hasattr(agent, 'view'):
|
|
|
|
display = agent.view()
|
2023-09-17 09:55:18 +02:00
|
|
|
if display:
|
2023-09-20 11:38:32 +02:00
|
|
|
display.update(force=True, new_data={"status": "Wifi recon flipped!", "face": faces.COOL})
|
2023-08-23 14:06:28 +02:00
|
|
|
else:
|
|
|
|
print("Wifi recon flipped")
|
|
|
|
else:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.warning("[Fix_Services] wifi.recon flip: FAILED: %s" % repr(result))
|
2023-09-17 21:44:42 +02:00
|
|
|
self._tryTurningItOffAndOnAgain(agent)
|
2023-08-23 14:06:28 +02:00
|
|
|
except Exception as err:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.error("[Fix_Services]SYSLOG wifi.recon flip fail: %s" % err)
|
2023-09-17 21:44:42 +02:00
|
|
|
self._tryTurningItOffAndOnAgain(agent)
|
2023-08-23 14:06:28 +02:00
|
|
|
|
2024-01-20 23:54:32 +01:00
|
|
|
def get_last_lines(self, command, args, n):
|
|
|
|
try:
|
|
|
|
process = subprocess.Popen([command] + args, stdout=subprocess.PIPE)
|
|
|
|
output = TextIOWrapper(process.stdout)
|
|
|
|
lines = output.readlines()
|
|
|
|
last_n_lines = ''.join(lines[-n:])
|
|
|
|
return last_n_lines
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error occurred: {e}")
|
|
|
|
return None
|
|
|
|
|
2023-08-23 14:06:28 +02:00
|
|
|
def on_epoch(self, agent, epoch, epoch_data):
|
2024-01-20 23:54:32 +01:00
|
|
|
|
|
|
|
last_lines = self.get_last_lines('journalctl', ['-n10', '-k'], 10)
|
|
|
|
other_last_lines = self.get_last_lines('journalctl', ['-n10'], 10)
|
|
|
|
other_other_last_lines = self.get_last_lines('tail', ['-n10', '/home/pi/logs/pwnagotchi.log'], 10)
|
|
|
|
|
2023-08-23 14:06:28 +02:00
|
|
|
# don't check if we ran a reset recently
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.debug("[Fix_Services]**** epoch")
|
2023-08-23 14:06:28 +02:00
|
|
|
if time.time() - self.LASTTRY > 180:
|
|
|
|
# get last 10 lines
|
|
|
|
display = None
|
2023-09-05 03:17:59 +02:00
|
|
|
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.debug("[Fix_Services]**** checking")
|
2023-09-05 03:17:59 +02:00
|
|
|
|
|
|
|
# Look for pattern 1
|
2023-09-05 13:22:06 +02:00
|
|
|
if len(self.pattern.findall(last_lines)) >= 3:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services]**** Should trigger a reload of the wlan0mon device:\n%s" % last_lines)
|
2023-08-23 14:06:28 +02:00
|
|
|
if hasattr(agent, 'view'):
|
|
|
|
display = agent.view()
|
|
|
|
display.set('status', 'Blind-Bug detected. Restarting.')
|
|
|
|
display.update(force=True)
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info('[Fix_Services] Blind-Bug detected. Restarting.')
|
2023-08-23 14:06:28 +02:00
|
|
|
try:
|
|
|
|
self._tryTurningItOffAndOnAgain(agent)
|
|
|
|
except Exception as err:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.warning("[Fix_Services] TTOAOA: %s" % repr(err))
|
2023-09-05 03:17:59 +02:00
|
|
|
|
|
|
|
# Look for pattern 2
|
2023-09-05 13:22:06 +02:00
|
|
|
elif len(self.pattern2.findall(other_last_lines)) >= 5:
|
2023-09-07 13:00:40 +02:00
|
|
|
logging.info("[Fix_Services]**** Should trigger a reload of the wlan0mon device:\n%s" % last_lines)
|
2023-08-23 14:06:28 +02:00
|
|
|
if hasattr(agent, 'view'):
|
|
|
|
display = agent.view()
|
|
|
|
display.set('status', 'Wifi channel stuck. Restarting recon.')
|
|
|
|
display.update(force=True)
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info('[Fix_Services] Wifi channel stuck. Restarting recon.')
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
result = agent.run("wifi.recon off; wifi.recon on")
|
|
|
|
if result["success"]:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services] wifi.recon flip: success!")
|
2023-08-23 14:06:28 +02:00
|
|
|
if display:
|
|
|
|
display.update(force=True, new_data={"status": "Wifi recon flipped!",
|
|
|
|
"face": faces.COOL})
|
|
|
|
else:
|
|
|
|
print("Wifi recon flipped\nthat was easy!")
|
|
|
|
else:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.warning("[Fix_Services] wifi.recon flip: FAILED: %s" % repr(result))
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
except Exception as err:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.error("[Fix_Services wifi.recon flip] %s" % repr(err))
|
2023-09-04 07:54:57 +02:00
|
|
|
|
2023-09-05 03:17:59 +02:00
|
|
|
# Look for pattern 3
|
2023-09-07 14:12:33 +02:00
|
|
|
elif len(self.pattern3.findall(other_last_lines)) >= 1:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services] Firmware has halted or crashed. Restarting wlan0mon.")
|
2023-09-05 03:17:59 +02:00
|
|
|
if hasattr(agent, 'view'):
|
|
|
|
display = agent.view()
|
|
|
|
display.set('status', 'Firmware has halted or crashed. Restarting wlan0mon.')
|
|
|
|
display.update(force=True)
|
|
|
|
try:
|
|
|
|
# Run the monstart command to restart wlan0mon
|
2023-09-05 20:29:09 +02:00
|
|
|
cmd_output = subprocess.check_output("monstart", shell=True)
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services monstart]: %s" % repr(cmd_output))
|
2023-09-05 03:17:59 +02:00
|
|
|
except Exception as err:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.error("[Fix_Services monstart]: %s" % repr(err))
|
2023-09-06 17:01:26 +02:00
|
|
|
|
2023-09-16 09:49:55 +02:00
|
|
|
# Look for pattern 4
|
2023-09-07 13:04:00 +02:00
|
|
|
elif len(self.pattern4.findall(other_other_last_lines)) >= 3:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services] wlan0 is down!")
|
2023-09-06 17:01:26 +02:00
|
|
|
if hasattr(agent, 'view'):
|
|
|
|
display = agent.view()
|
|
|
|
display.set('status', 'Restarting wlan0 now!')
|
|
|
|
display.update(force=True)
|
|
|
|
try:
|
|
|
|
# Run the monstart command to restart wlan0mon
|
2023-09-07 13:00:40 +02:00
|
|
|
cmd_output = subprocess.check_output("monstart", shell=True)
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services monstart]: %s" % repr(cmd_output))
|
2023-09-06 17:01:26 +02:00
|
|
|
except Exception as err:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.error("[Fix_Services monstart]: %s" % repr(err))
|
2023-09-05 03:17:59 +02:00
|
|
|
|
2023-09-03 12:29:46 +02:00
|
|
|
else:
|
2023-09-04 07:54:57 +02:00
|
|
|
print("logs look good")
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
def logPrintView(self, level, message, ui=None, displayData=None, force=True):
|
|
|
|
try:
|
2023-09-07 13:00:40 +02:00
|
|
|
if level == "error":
|
2023-08-23 14:06:28 +02:00
|
|
|
logging.error(message)
|
2023-09-07 13:00:40 +02:00
|
|
|
elif level == "warning":
|
2023-08-23 14:06:28 +02:00
|
|
|
logging.warning(message)
|
2023-09-07 13:00:40 +02:00
|
|
|
elif level == "debug":
|
2023-08-23 14:06:28 +02:00
|
|
|
logging.debug(message)
|
|
|
|
else:
|
|
|
|
logging.info(message)
|
|
|
|
|
|
|
|
if ui:
|
|
|
|
ui.update(force=force, new_data=displayData)
|
|
|
|
elif displayData and "status" in displayData:
|
|
|
|
print(displayData["status"])
|
|
|
|
else:
|
|
|
|
print("[%s] %s" % (level, message))
|
|
|
|
except Exception as err:
|
|
|
|
logging.error("[logPrintView] ERROR %s" % repr(err))
|
|
|
|
|
|
|
|
def _tryTurningItOffAndOnAgain(self, connection):
|
|
|
|
# avoid overlapping restarts, but allow it if it's been a while
|
|
|
|
# (in case the last attempt failed before resetting "isReloadingMon")
|
|
|
|
if self.isReloadingMon and (time.time() - self.LASTTRY) < 180:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services] Duplicate attempt ignored")
|
2023-08-23 14:06:28 +02:00
|
|
|
else:
|
|
|
|
self.isReloadingMon = True
|
|
|
|
self.LASTTRY = time.time()
|
|
|
|
|
|
|
|
if hasattr(connection, 'view'):
|
|
|
|
display = connection.view()
|
2023-09-17 09:55:18 +02:00
|
|
|
if display:
|
|
|
|
display.update(force=True, new_data={"status": "I'm blind! Try turning it off and on again",
|
2024-01-13 19:03:25 +01:00
|
|
|
"face": faces.BORED})
|
2023-08-23 14:06:28 +02:00
|
|
|
else:
|
|
|
|
display = None
|
|
|
|
|
|
|
|
# main divergence from WATCHDOG starts here
|
|
|
|
#
|
|
|
|
# instead of rebooting, and losing all that energy loading up the AI
|
|
|
|
# pause wifi.recon, close wlan0mon, reload the brcmfmac kernel module
|
|
|
|
# then recreate wlan0mon, ..., and restart wifi.recon
|
|
|
|
|
|
|
|
# Turn it off
|
|
|
|
|
|
|
|
# attempt a sanity check. does wlan0mon exist?
|
|
|
|
# is it up?
|
|
|
|
try:
|
|
|
|
cmd_output = subprocess.check_output("ip link show wlan0mon", shell=True)
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services ip link show wlan0mon]: %s" % repr(cmd_output))
|
2023-08-23 14:06:28 +02:00
|
|
|
if ",UP," in str(cmd_output):
|
|
|
|
logging.info("wlan0mon is up. Skip reset?")
|
|
|
|
# not reliable, so don't skip just yet
|
|
|
|
# print("wlan0mon is up. Skipping reset.")
|
|
|
|
# self.isReloadingMon = False
|
|
|
|
# return
|
|
|
|
except Exception as err:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.error("[Fix_Services ip link show wlan0mon]: %s" % repr(err))
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
result = connection.run("wifi.recon off")
|
|
|
|
if "success" in result:
|
2023-09-07 07:49:53 +02:00
|
|
|
self.logPrintView("info", "[Fix_Services] wifi.recon off: %s!" % repr(result),
|
2023-08-23 14:06:28 +02:00
|
|
|
display, {"status": "Wifi recon paused!", "face": faces.COOL})
|
|
|
|
time.sleep(2)
|
|
|
|
else:
|
2023-09-07 07:49:53 +02:00
|
|
|
self.logPrintView("warning", "[Fix_Services] wifi.recon off: FAILED: %s" % repr(result),
|
2023-08-23 14:06:28 +02:00
|
|
|
display, {"status": "Recon was busted (probably)",
|
|
|
|
"face": random.choice((faces.BROKEN, faces.DEBUG))})
|
|
|
|
except Exception as err:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.error("[Fix_Services wifi.recon off] error %s" % (repr(err)))
|
2023-08-23 14:06:28 +02:00
|
|
|
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services] recon paused. Now trying wlan0mon reload")
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
try:
|
2023-09-01 10:05:27 +02:00
|
|
|
cmd_output = subprocess.check_output("monstop", shell=True)
|
2023-09-07 07:49:53 +02:00
|
|
|
self.logPrintView("info", "[Fix_Services] wlan0mon down and deleted: %s" % cmd_output,
|
2023-08-23 14:06:28 +02:00
|
|
|
display, {"status": "wlan0mon d-d-d-down!", "face": faces.BORED})
|
|
|
|
except Exception as nope:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.error("[Fix_Services delete wlan0mon] %s" % nope)
|
2023-08-23 14:06:28 +02:00
|
|
|
pass
|
|
|
|
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.debug("[Fix_Services] Now trying modprobe -r")
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
# Try this sequence 3 times until it is reloaded
|
|
|
|
#
|
|
|
|
# Future: while "not fixed yet": blah blah blah. if "max_attemts", then reboot like the old days
|
|
|
|
#
|
|
|
|
tries = 0
|
|
|
|
while tries < 3:
|
|
|
|
try:
|
|
|
|
# unload the module
|
|
|
|
cmd_output = subprocess.check_output("sudo modprobe -r brcmfmac", shell=True)
|
2023-09-07 07:49:53 +02:00
|
|
|
self.logPrintView("info", "[Fix_Services] unloaded brcmfmac", display,
|
2023-09-09 23:05:08 +02:00
|
|
|
{"status": "Turning it off #%s" % tries, "face": faces.SMART})
|
2023-08-23 14:06:28 +02:00
|
|
|
time.sleep(1 + tries)
|
|
|
|
|
|
|
|
# reload the module
|
|
|
|
try:
|
|
|
|
# reload the brcmfmac kernel module
|
|
|
|
cmd_output = subprocess.check_output("sudo modprobe brcmfmac", shell=True)
|
|
|
|
|
2023-09-17 22:22:12 +02:00
|
|
|
self.logPrintView("info", "[Fix_Services] reloaded brcmfmac")
|
2023-08-23 14:06:28 +02:00
|
|
|
time.sleep(10 + 4 * tries) # give it some time for wlan device to stabilize, or whatever
|
|
|
|
|
2023-09-04 07:54:57 +02:00
|
|
|
# success! now make the mon0
|
2023-08-23 14:06:28 +02:00
|
|
|
try:
|
2023-09-09 23:05:08 +02:00
|
|
|
cmd_output = subprocess.check_output("monstart", shell=True)
|
|
|
|
self.logPrintView("info", "[Fix_Services interface add wlan0mon] worked #%s: %s"
|
|
|
|
% (tries, cmd_output))
|
2023-08-23 14:06:28 +02:00
|
|
|
time.sleep(tries + 5)
|
|
|
|
try:
|
|
|
|
# try accessing mon0 in bettercap
|
2023-08-26 00:54:31 +02:00
|
|
|
result = connection.run("set wifi.interface wlan0mon")
|
2023-08-23 14:06:28 +02:00
|
|
|
if "success" in result:
|
2023-09-17 09:55:18 +02:00
|
|
|
logging.info("[Fix_Services set wifi.interface wlan0mon] worked!")
|
2023-08-23 14:06:28 +02:00
|
|
|
self._count = self._count + 1
|
|
|
|
time.sleep(1)
|
|
|
|
# stop looping and get back to recon
|
|
|
|
break
|
|
|
|
else:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info(
|
|
|
|
"[Fix_Services set wifi.interfaceface wlan0mon] failed? %s" % repr(result))
|
2023-08-23 14:06:28 +02:00
|
|
|
except Exception as err:
|
|
|
|
logging.info(
|
2023-09-07 14:19:04 +02:00
|
|
|
"[Fix_Services set wifi.interface wlan0mon] except: %s" % repr(err))
|
2023-08-23 14:06:28 +02:00
|
|
|
except Exception as cerr: #
|
2023-09-17 09:55:18 +02:00
|
|
|
if not display:
|
|
|
|
print("failed loading wlan0mon attempt #%s: %s" % (tries, repr(cerr)))
|
2023-08-23 14:06:28 +02:00
|
|
|
except Exception as err: # from modprobe
|
2023-09-17 09:55:18 +02:00
|
|
|
if not display:
|
|
|
|
print("Failed reloading brcmfmac")
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.error("[Fix_Services] Failed reloading brcmfmac %s" % repr(err))
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
except Exception as nope: # from modprobe -r
|
|
|
|
# fails if already unloaded, so probably fine
|
2023-09-09 23:05:08 +02:00
|
|
|
logging.error("[Fix_Services #%s modprobe -r] %s" % (tries, repr(nope)))
|
2023-09-17 09:55:18 +02:00
|
|
|
if not display:
|
|
|
|
print("[Fix_Services #%s modprobe -r] %s" % (tries, repr(nope)))
|
2023-08-23 14:06:28 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
tries = tries + 1
|
|
|
|
if tries < 3:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services] wlan0mon didn't make it. trying again")
|
2023-09-17 09:55:18 +02:00
|
|
|
if not display:
|
|
|
|
print(" wlan0mon didn't make it. trying again")
|
2023-09-17 22:14:09 +02:00
|
|
|
else:
|
2023-09-17 22:18:02 +02:00
|
|
|
logging.info("[Fix_Services] wlan0mon loading failed, no choice but to reboot ..")
|
2023-09-17 22:14:09 +02:00
|
|
|
pwnagotchi.reboot()
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
# exited the loop, so hopefully it loaded
|
|
|
|
if tries < 3:
|
|
|
|
if display:
|
|
|
|
display.update(force=True, new_data={"status": "And back on again...",
|
|
|
|
"face": faces.INTENSE})
|
|
|
|
else:
|
|
|
|
print("And back on again...")
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services] wlan0mon back up")
|
2023-08-23 14:06:28 +02:00
|
|
|
else:
|
|
|
|
self.LASTTRY = time.time()
|
|
|
|
|
|
|
|
time.sleep(8 + tries * 2) # give it a bit before restarting recon in bettercap
|
|
|
|
self.isReloadingMon = False
|
|
|
|
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.info("[Fix_Services] re-enable recon")
|
2023-08-23 14:06:28 +02:00
|
|
|
try:
|
|
|
|
result = connection.run("wifi.clear; wifi.recon on")
|
|
|
|
|
|
|
|
if "success" in result: # and result["success"] is True:
|
|
|
|
if display:
|
2023-09-17 09:55:18 +02:00
|
|
|
display.update(force=True, new_data={"status": "I can see again! (probably)",
|
2023-08-23 14:06:28 +02:00
|
|
|
"face": faces.HAPPY})
|
|
|
|
else:
|
|
|
|
print("I can see again")
|
2023-09-17 09:55:18 +02:00
|
|
|
logging.info("[Fix_Services] wifi.recon on")
|
2023-08-23 14:06:28 +02:00
|
|
|
self.LASTTRY = time.time() + 120 # 2-minute pause until next time.
|
|
|
|
else:
|
2023-09-17 09:55:18 +02:00
|
|
|
logging.error("[Fix_Services] wifi.recon did not start up")
|
2023-08-23 14:06:28 +02:00
|
|
|
self.LASTTRY = time.time() - 300 # failed, so try again ASAP
|
|
|
|
self.isReloadingMon = False
|
|
|
|
|
|
|
|
except Exception as err:
|
2023-09-07 07:49:53 +02:00
|
|
|
logging.error("[Fix_Services wifi.recon on] %s" % repr(err))
|
2023-09-17 22:08:27 +02:00
|
|
|
pwnagotchi.reboot()
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
def on_unload(self, ui):
|
2023-09-15 13:35:29 +02:00
|
|
|
with ui._lock:
|
|
|
|
try:
|
|
|
|
logging.info("[Fix_Services] unloaded")
|
|
|
|
except Exception as err:
|
|
|
|
logging.info("[Fix_Services] unload err %s " % repr(err))
|
|
|
|
pass
|
2023-08-23 14:06:28 +02:00
|
|
|
|
2023-09-07 07:49:53 +02:00
|
|
|
|
2023-08-23 14:06:28 +02:00
|
|
|
# run from command line to brute force a reload
|
|
|
|
if __name__ == "__main__":
|
2023-08-26 00:54:31 +02:00
|
|
|
print("Performing brcmfmac reload and restart wlan0mon in 5 seconds...")
|
2023-09-07 14:19:04 +02:00
|
|
|
fb = FixServices()
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
data = {'Message': "kernel: brcmfmac: brcmf_cfg80211_nexmon_set_channel: Set Channel failed: chspec=1234"}
|
|
|
|
event = {'data': data}
|
|
|
|
|
2023-08-26 00:54:31 +02:00
|
|
|
agent = Client('localhost', port=8081, username="pwnagotchi", password="pwnagotchi")
|
2023-08-23 14:06:28 +02:00
|
|
|
|
|
|
|
time.sleep(2)
|
|
|
|
print("3 seconds")
|
|
|
|
time.sleep(3)
|
2023-09-07 07:49:53 +02:00
|
|
|
fb.on_epoch(agent, event, None)
|
|
|
|
# fb._tryTurningItOffAndOnAgain(agent)
|