From ff6bf5c19802e985a6de346bb30959f7334f5086 Mon Sep 17 00:00:00 2001 From: Simone Margaritelli Date: Sun, 20 Oct 2019 17:36:34 +0200 Subject: [PATCH] started working on #343 --- pwnagotchi/defaults.yml | 6 ++ pwnagotchi/plugins/default/auto-update.py | 67 +++++++++++++++++++++++ pwnagotchi/utils.py | 3 + 3 files changed, 76 insertions(+) create mode 100644 pwnagotchi/plugins/default/auto-update.py diff --git a/pwnagotchi/defaults.yml b/pwnagotchi/defaults.yml index 3142761..0aed0f7 100644 --- a/pwnagotchi/defaults.yml +++ b/pwnagotchi/defaults.yml @@ -17,6 +17,12 @@ main: report: false # don't report pwned networks by default! exclude: # do not report the following networks (accepts both ESSIDs and BSSIDs) - YourHomeNetworkHere + + auto-update: + enabled: false + interval: 12 # every 12 hours + install: true # if false, it will only warn that updates are available, if true it will install them + auto-backup: enabled: false interval: 1 # every day diff --git a/pwnagotchi/plugins/default/auto-update.py b/pwnagotchi/plugins/default/auto-update.py new file mode 100644 index 0000000..87969a0 --- /dev/null +++ b/pwnagotchi/plugins/default/auto-update.py @@ -0,0 +1,67 @@ +__author__ = 'evilsocket@gmail.com' +__version__ = '1.1.0' +__name__ = 'auto-update' +__license__ = 'GPL3' +__description__ = 'This plugin checks when updates are available and applies them when internet is available.' + +import logging +import subprocess +from pwnagotchi.utils import StatusFile + +OPTIONS = dict() +READY = False +STATUS = StatusFile('/root/.auto-update') + + +def on_loaded(): + global READY + if 'interval' not in OPTIONS or ('interval' in OPTIONS and OPTIONS['interval'] is None): + logging.error("[update] main.plugins.auto-update.interval is not set") + return + READY = True + logging.info("[update] plugin loaded.") + + +def run(cmd): + return subprocess.Popen(cmd, shell=True, stdin=None, stdout=open("/dev/null", "w"), stderr=None, + executable="/bin/bash") + + +def on_internet_available(agent): + global STATUS + + if READY: + if STATUS.newer_then_hours(OPTIONS['interval']): + logging.debug("[update] last check happened less than %d hours ago" % OPTIONS['interval']) + return + + logging.debug("[update] start") + + display = agent.view() + prev_status = display.get('status') + + try: + display.set('status', 'Checking for updates ...') + display.update() + + """ + logging.info("auto-update: updating pwnagotchi ...") + run('pip3 install --upgrade --upgrade-strategy only-if-needed pwnagotchi').wait() + + if OPTIONS['system']: + logging.info("auto-update: updating packages index ...") + run('apt update -y').wait() + + logging.info("auto-update: updating packages ...") + run('apt upgrade -y').wait() + """ + + logging.info("[update] done") + + STATUS.update() + + except Exception as e: + logging.error("[update] %s" % e) + + display.set('status', prev_status if prev_status is not None else '') + display.update() diff --git a/pwnagotchi/utils.py b/pwnagotchi/utils.py index 419d80d..46f1103 100644 --- a/pwnagotchi/utils.py +++ b/pwnagotchi/utils.py @@ -263,6 +263,9 @@ class StatusFile(object): def newer_then_minutes(self, minutes): return self._updated is not None and ((datetime.now() - self._updated).seconds / 60) < minutes + def newer_then_hours(self, hours): + return self._updated is not None and ((datetime.now() - self._updated).seconds / (60*60)) < hours + def newer_then_days(self, days): return self._updated is not None and (datetime.now() - self._updated).days < days