Merge pull request from alanyee/patch-1

Replace string formatting with logging laziness in agent.py
This commit is contained in:
evilsocket 2019-12-06 15:04:45 +02:00 committed by GitHub
commit e04e053cee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -49,9 +49,9 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
if not os.path.exists(config['bettercap']['handshakes']): if not os.path.exists(config['bettercap']['handshakes']):
os.makedirs(config['bettercap']['handshakes']) os.makedirs(config['bettercap']['handshakes'])
logging.info("%s@%s (v%s)" % (pwnagotchi.name(), self.fingerprint(), pwnagotchi.version)) logging.info("%s@%s (v%s)", pwnagotchi.name(), self.fingerprint(), pwnagotchi.version)
for _, plugin in plugins.loaded.items(): for _, plugin in plugins.loaded.items():
logging.debug("plugin '%s' v%s" % (plugin.__class__.__name__, plugin.__version__)) logging.debug("plugin '%s' v%s", plugin.__class__.__name__, plugin.__version__)
def config(self): def config(self):
return self._config return self._config
@ -63,11 +63,11 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
return self._supported_channels return self._supported_channels
def setup_events(self): def setup_events(self):
logging.info("connecting to %s ..." % self.url) logging.info("connecting to %s ...", self.url)
for tag in self._config['bettercap']['silence']: for tag in self._config['bettercap']['silence']:
try: try:
self.run('events.ignore %s' % tag, verbose_errors=False) self.run('events.ignore %s', tag, verbose_errors=False)
except Exception as e: except Exception as e:
pass pass
@ -90,7 +90,7 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
s = self.session() s = self.session()
for iface in s['interfaces']: for iface in s['interfaces']:
if iface['name'] == mon_iface: if iface['name'] == mon_iface:
logging.info("found monitor interface: %s" % iface['name']) logging.info("found monitor interface: %s", iface['name'])
has_mon = True has_mon = True
break break
@ -99,11 +99,11 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
logging.info("starting monitor interface ...") logging.info("starting monitor interface ...")
self.run('!%s' % mon_start_cmd) self.run('!%s' % mon_start_cmd)
else: else:
logging.info("waiting for monitor interface %s ..." % mon_iface) logging.info("waiting for monitor interface %s ...", mon_iface)
time.sleep(1) time.sleep(1)
logging.info("supported channels: %s" % self._supported_channels) logging.info("supported channels: %s", self._supported_channels)
logging.info("handshakes will be collected inside %s" % self._config['bettercap']['handshakes']) logging.info("handshakes will be collected inside %s", self._config['bettercap']['handshakes'])
self._reset_wifi_settings() self._reset_wifi_settings()
@ -151,12 +151,12 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
if not channels: if not channels:
self._current_channel = 0 self._current_channel = 0
logging.debug("RECON %ds" % recon_time) logging.debug("RECON %ds", recon_time)
self.run('wifi.recon.channel clear') self.run('wifi.recon.channel clear')
else: else:
logging.debug("RECON %ds ON CHANNELS %s" % (recon_time, ','.join(map(str, channels)))) logging.debug("RECON %ds ON CHANNELS %s", recon_time, ','.join(map(str, channels)))
try: try:
self.run('wifi.recon.channel %s' % ','.join(map(str, channels))) self.run('wifi.recon.channel %s', ','.join(map(str, channels)))
except Exception as e: except Exception as e:
logging.exception("error") logging.exception("error")
@ -212,7 +212,7 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
ch = ap['channel'] ch = ap['channel']
# if we're sticking to a channel, skip anything # if we're sticking to a channel, skip anything
# which is not on that channel # which is not on that channel
if channels != [] and ch not in channels: if not channels and ch not in channels:
continue continue
if ch not in grouped: if ch not in grouped:
@ -274,7 +274,7 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
pwnagotchi.reboot() pwnagotchi.reboot()
def _save_recovery_data(self): def _save_recovery_data(self):
logging.warning("writing recovery data to %s ..." % RECOVERY_DATA_FILE) logging.warning("writing recovery data to %s ...", RECOVERY_DATA_FILE)
with open(RECOVERY_DATA_FILE, 'w') as fp: with open(RECOVERY_DATA_FILE, 'w') as fp:
data = { data = {
'started_at': self._started_at, 'started_at': self._started_at,
@ -289,7 +289,7 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
try: try:
with open(RECOVERY_DATA_FILE, 'rt') as fp: with open(RECOVERY_DATA_FILE, 'rt') as fp:
data = json.load(fp) data = json.load(fp)
logging.info("found recovery data: %s" % data) logging.info("found recovery data: %s", data)
self._started_at = data['started_at'] self._started_at = data['started_at']
self._epoch.epoch = data['epoch'] self._epoch.epoch = data['epoch']
self._handshakes = data['handshakes'] self._handshakes = data['handshakes']
@ -297,7 +297,7 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
self._last_pwnd = data['last_pwnd'] self._last_pwnd = data['last_pwnd']
if delete: if delete:
logging.info("deleting %s" % RECOVERY_DATA_FILE) logging.info("deleting %s", RECOVERY_DATA_FILE)
os.unlink(RECOVERY_DATA_FILE) os.unlink(RECOVERY_DATA_FILE)
except: except:
if not no_exceptions: if not no_exceptions:
@ -334,7 +334,7 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
new_shakes += 1 new_shakes += 1
ap_and_station = self._find_ap_sta_in(sta_mac, ap_mac, s) ap_and_station = self._find_ap_sta_in(sta_mac, ap_mac, s)
if ap_and_station is None: if ap_and_station is None:
logging.warning("!!! captured new handshake: %s !!!" % key) logging.warning("!!! captured new handshake: %s !!!", key)
self._last_pwnd = ap_mac self._last_pwnd = ap_mac
plugins.on('handshake', self, filename, ap_mac, sta_mac) plugins.on('handshake', self, filename, ap_mac, sta_mac)
else: else:
@ -342,15 +342,15 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
self._last_pwnd = ap['hostname'] if ap['hostname'] != '' and ap[ self._last_pwnd = ap['hostname'] if ap['hostname'] != '' and ap[
'hostname'] != '<hidden>' else ap_mac 'hostname'] != '<hidden>' else ap_mac
logging.warning( logging.warning(
"!!! captured new handshake on channel %d, %d dBm: %s (%s) -> %s [%s (%s)] !!!" % ( "!!! captured new handshake on channel %d, %d dBm: %s (%s) -> %s [%s (%s)] !!!",
ap['channel'], ap['channel'],
ap['rssi'], ap['rssi'],
sta['mac'], sta['vendor'], sta['mac'], sta['vendor'],
ap['hostname'], ap['mac'], ap['vendor'])) ap['hostname'], ap['mac'], ap['vendor'])
plugins.on('handshake', self, filename, ap, sta) plugins.on('handshake', self, filename, ap, sta)
except Exception as e: except Exception as e:
logging.error("error: %s" % e) logging.error("error: %s", e)
finally: finally:
self._update_handshakes(new_shakes) self._update_handshakes(new_shakes)
@ -392,15 +392,15 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
def associate(self, ap, throttle=0): def associate(self, ap, throttle=0):
if self.is_stale(): if self.is_stale():
logging.debug("recon is stale, skipping assoc(%s)" % ap['mac']) logging.debug("recon is stale, skipping assoc(%s)", ap['mac'])
return return
if self._config['personality']['associate'] and self._should_interact(ap['mac']): if self._config['personality']['associate'] and self._should_interact(ap['mac']):
self._view.on_assoc(ap) self._view.on_assoc(ap)
try: try:
logging.info("sending association frame to %s (%s %s) on channel %d [%d clients], %d dBm..." % ( \ logging.info("sending association frame to %s (%s %s) on channel %d [%d clients], %d dBm...",
ap['hostname'], ap['mac'], ap['vendor'], ap['channel'], len(ap['clients']), ap['rssi'])) ap['hostname'], ap['mac'], ap['vendor'], ap['channel'], len(ap['clients']), ap['rssi'])
self.run('wifi.assoc %s' % ap['mac']) self.run('wifi.assoc %s' % ap['mac'])
self._epoch.track(assoc=True) self._epoch.track(assoc=True)
except Exception as e: except Exception as e:
@ -413,15 +413,15 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
def deauth(self, ap, sta, throttle=0): def deauth(self, ap, sta, throttle=0):
if self.is_stale(): if self.is_stale():
logging.debug("recon is stale, skipping deauth(%s)" % sta['mac']) logging.debug("recon is stale, skipping deauth(%s)", sta['mac'])
return return
if self._config['personality']['deauth'] and self._should_interact(sta['mac']): if self._config['personality']['deauth'] and self._should_interact(sta['mac']):
self._view.on_deauth(sta) self._view.on_deauth(sta)
try: try:
logging.info("deauthing %s (%s) from %s (%s %s) on channel %d, %d dBm ..." % ( logging.info("deauthing %s (%s) from %s (%s %s) on channel %d, %d dBm ...",
sta['mac'], sta['vendor'], ap['hostname'], ap['mac'], ap['vendor'], ap['channel'], ap['rssi'])) sta['mac'], sta['vendor'], ap['hostname'], ap['mac'], ap['vendor'], ap['channel'], ap['rssi'])
self.run('wifi.deauth %s' % sta['mac']) self.run('wifi.deauth %s' % sta['mac'])
self._epoch.track(deauth=True) self._epoch.track(deauth=True)
except Exception as e: except Exception as e:
@ -434,7 +434,7 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
def set_channel(self, channel, verbose=True): def set_channel(self, channel, verbose=True):
if self.is_stale(): if self.is_stale():
logging.debug("recon is stale, skipping set_channel(%d)" % channel) logging.debug("recon is stale, skipping set_channel(%d)", channel)
return return
# if in the previous loop no client stations has been deauthenticated # if in the previous loop no client stations has been deauthenticated
@ -450,12 +450,12 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
if channel != self._current_channel: if channel != self._current_channel:
if self._current_channel != 0 and wait > 0: if self._current_channel != 0 and wait > 0:
if verbose: if verbose:
logging.info("waiting for %ds on channel %d ..." % (wait, self._current_channel)) logging.info("waiting for %ds on channel %d ...", wait, self._current_channel)
else: else:
logging.debug("waiting for %ds on channel %d ..." % (wait, self._current_channel)) logging.debug("waiting for %ds on channel %d ...", wait, self._current_channel)
self.wait_for(wait) self.wait_for(wait)
if verbose and self._epoch.any_activity: if verbose and self._epoch.any_activity:
logging.info("CHANNEL %d" % channel) logging.info("CHANNEL %d", channel)
try: try:
self.run('wifi.recon.channel %d' % channel) self.run('wifi.recon.channel %d' % channel)
self._current_channel = channel self._current_channel = channel
@ -465,4 +465,4 @@ class Agent(Client, Automata, AsyncAdvertiser, AsyncTrainer):
plugins.on('channel_hop', self, channel) plugins.on('channel_hop', self, channel)
except Exception as e: except Exception as e:
logging.error("error: %s" % e) logging.error("error: %s", e)