mirror of
https://github.com/jayofelony/pwnagotchi.git
synced 2025-07-01 18:37:27 -04:00
bluetoothsniffer.py: more pythonic
Signed-off-by: Jeroen Oudshoorn <oudshoorn.jeroen@gmail.com>
This commit is contained in:
@ -12,7 +12,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
|
|
||||||
class BluetoothSniffer(plugins.Plugin):
|
class BluetoothSniffer(plugins.Plugin):
|
||||||
__author__ = 'diytechtinker'
|
__author__ = 'diytechtinker, fixed by Jayofelony'
|
||||||
__version__ = '0.1.3'
|
__version__ = '0.1.3'
|
||||||
__license__ = 'GPL3'
|
__license__ = 'GPL3'
|
||||||
__description__ = 'A plugin that sniffs Bluetooth devices and saves their MAC addresses, name and counts to a JSON file'
|
__description__ = 'A plugin that sniffs Bluetooth devices and saves their MAC addresses, name and counts to a JSON file'
|
||||||
@ -70,17 +70,15 @@ class BluetoothSniffer(plugins.Plugin):
|
|||||||
|
|
||||||
# Method for scanning the nearby bluetooth devices
|
# Method for scanning the nearby bluetooth devices
|
||||||
def scan(self, display):
|
def scan(self, display):
|
||||||
logging.info("[BtS] Scanning for bluetooths...")
|
logging.info("[BtS] Scanning for bluetooth devices...")
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
changed = False
|
changed = False
|
||||||
|
device_class = None
|
||||||
|
|
||||||
# Running the system command hcitool to scan nearby bluetooth devices
|
# Running the system command hcitool to scan nearby bluetooth devices
|
||||||
cmd_inq = "hcitool inq --flush"
|
cmd_inq = "hcitool inq --flush"
|
||||||
try:
|
try:
|
||||||
inq_output = subprocess.check_output(cmd_inq.split())
|
inq_output = subprocess.check_output(cmd_inq.split())
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
logging.error("[BtS] Error running command: %s", e)
|
|
||||||
|
|
||||||
for line in inq_output.splitlines()[1:]:
|
for line in inq_output.splitlines()[1:]:
|
||||||
fields = line.split()
|
fields = line.split()
|
||||||
mac_address = fields[0].decode()
|
mac_address = fields[0].decode()
|
||||||
@ -111,27 +109,37 @@ class BluetoothSniffer(plugins.Plugin):
|
|||||||
logging.info("[BtS] Updated bluetooth class: %s", device_class)
|
logging.info("[BtS] Updated bluetooth class: %s", device_class)
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
last_seen_time = int(datetime.strptime(self.data[mac_address]['last_seen'], '%H:%M:%S %d-%m-%Y').timestamp())
|
last_seen_time = int(
|
||||||
|
datetime.strptime(self.data[mac_address]['last_seen'], '%H:%M:%S %d-%m-%Y').timestamp())
|
||||||
if current_time - last_seen_time >= self.options['count_interval']:
|
if current_time - last_seen_time >= self.options['count_interval']:
|
||||||
self.data[mac_address]['count'] += 1
|
self.data[mac_address]['count'] += 1
|
||||||
self.data[mac_address]['last_seen'] = time.strftime('%H:%M:%S %d-%m-%Y', time.localtime(current_time))
|
self.data[mac_address]['last_seen'] = time.strftime('%H:%M:%S %d-%m-%Y',
|
||||||
|
time.localtime(current_time))
|
||||||
self.data[mac_address]['new_info'] = 2
|
self.data[mac_address]['new_info'] = 2
|
||||||
logging.info("[BtS] Updated bluetooth count.")
|
logging.info("[BtS] Updated bluetooth count.")
|
||||||
changed = True
|
changed = True
|
||||||
else:
|
else:
|
||||||
name = self.get_device_name(mac_address)
|
name = self.get_device_name(mac_address)
|
||||||
manufacturer = self.get_device_manufacturer(mac_address)
|
manufacturer = self.get_device_manufacturer(mac_address)
|
||||||
self.data[mac_address] = {'name': name, 'count': 1, 'class': device_class, 'manufacturer': manufacturer, 'first_seen': time.strftime('%H:%M:%S %d-%m-%Y', time.localtime(current_time)), 'last_seen': time.strftime('%H:%M:%S %d-%m-%Y', time.localtime(current_time)), 'new_info': True}
|
self.data[mac_address] = {'name': name, 'count': 1, 'class': device_class,
|
||||||
|
'manufacturer': manufacturer,
|
||||||
|
'first_seen': time.strftime('%H:%M:%S %d-%m-%Y',
|
||||||
|
time.localtime(current_time)),
|
||||||
|
'last_seen': time.strftime('%H:%M:%S %d-%m-%Y',
|
||||||
|
time.localtime(current_time)),
|
||||||
|
'new_info': True}
|
||||||
logging.info("[BtS] Added new bluetooth device %s with MAC: %s", name, mac_address)
|
logging.info("[BtS] Added new bluetooth device %s with MAC: %s", name, mac_address)
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
logging.error("[BtS] Error running command: %s", e)
|
||||||
|
|
||||||
# Save the updated devices to the JSON file
|
# Save the updated devices to the JSON file
|
||||||
if changed:
|
if changed:
|
||||||
with open(self.options['devices_file'], 'w') as f:
|
with open(self.options['devices_file'], 'w') as f:
|
||||||
logging.info("[BtS] Saving bluetooths %s into json.", name)
|
logging.info("[BtS] Saving bluetooths %s into json.", name)
|
||||||
json.dump(self.data, f)
|
json.dump(self.data, f)
|
||||||
display.set('status', 'Bluetooth sniffed and stored!')
|
display.set('status', 'Bluetooth sniffed and stored!').update(force=True)
|
||||||
display.update(force=True)
|
|
||||||
|
|
||||||
# Method to get the device name
|
# Method to get the device name
|
||||||
def get_device_name(self, mac_address):
|
def get_device_name(self, mac_address):
|
||||||
|
@ -1,36 +0,0 @@
|
|||||||
import os
|
|
||||||
import logging
|
|
||||||
import re
|
|
||||||
import subprocess
|
|
||||||
from io import TextIOWrapper
|
|
||||||
from pwnagotchi import plugins
|
|
||||||
|
|
||||||
|
|
||||||
class Watchdog(plugins.Plugin):
|
|
||||||
__author__ = '33197631+dadav@users.noreply.github.com'
|
|
||||||
__version__ = '0.1.0'
|
|
||||||
__license__ = 'GPL3'
|
|
||||||
__description__ = 'Restart pwnagotchi when blindbug is detected.'
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.options = dict()
|
|
||||||
self.pattern = re.compile(r'brcmf_cfg80211_nexmon_set_channel.*?Set Channel failed')
|
|
||||||
|
|
||||||
def on_loaded(self):
|
|
||||||
"""
|
|
||||||
Gets called when the plugin gets loaded
|
|
||||||
"""
|
|
||||||
logging.info("Watchdog plugin loaded.")
|
|
||||||
|
|
||||||
def on_epoch(self, agent, epoch, epoch_data):
|
|
||||||
# get last 10 lines
|
|
||||||
last_lines = ''.join(list(TextIOWrapper(subprocess.Popen(['journalctl','-n10','-k', '--since', '-5m'],
|
|
||||||
stdout=subprocess.PIPE).stdout))[-10:])
|
|
||||||
if len(self.pattern.findall(last_lines)) >= 5:
|
|
||||||
display = agent.view()
|
|
||||||
display.set('status', 'Blind-Bug detected. Restarting.')
|
|
||||||
display.update(force=True)
|
|
||||||
logging.info('[WATCHDOG] Blind-Bug detected. Restarting.')
|
|
||||||
mode = 'MANU' if agent.mode == 'manual' else 'AUTO'
|
|
||||||
import pwnagotchi
|
|
||||||
pwnagotchi.reboot(mode=mode)
|
|
Reference in New Issue
Block a user