Skip to content

Instantly share code, notes, and snippets.

@kisst
Last active October 2, 2022 00:54
Show Gist options
  • Save kisst/3932f2cc30281915f8ec5be7deed98b3 to your computer and use it in GitHub Desktop.
Save kisst/3932f2cc30281915f8ec5be7deed98b3 to your computer and use it in GitHub Desktop.
usbguard "gui"
#!/usr/bin/env python
import os
import re
import sys
DEVICE_LIST_RE = r"(\d{1,3}):\s(allow|block)\sid\s([0-9abcdef]{4}:[0-9abcdef]{4})\sserial\s\"(.*)\"\sname\s\"(.*)\"\shash\s\"([a-zA-Z0-9=+\/]{44})\"\sparent-hash\s\"([a-zA-Z0-9=+\/]{44})\"\svia-port\s\"(.{1,8})\"\swith-interface\s([^\"]*)\s\"(.*)\""
stream = os.popen('usbguard list-devices')
list = stream.read().strip().splitlines()
ids = []
devices = []
for line in list:
match = re.search(DEVICE_LIST_RE, line)
device = {}
device["usb_id"] = int(match.group(1))
ids.append(int(match.group(1)))
device["usb_status"] = match.group(2)
if match.group(5) == "":
device["usb_name"] = "? Unnamed [{}]".format(match.group(3))
else:
device["usb_name"] = "{} [{}]".format(match.group(5), match.group(3))
device["usb_usage"] = match.group(10)
devices.append(device)
print_line = "[ {} ][ {} ] {}"
print(print_line.format("id", "status", "name"))
print("-"*50)
for device in devices:
if device["usb_status"] == "allow":
sign = "+"
elif device["usb_status"] == "block":
sign = "-"
else:
sign = "?"
print(print_line.format(device["usb_id"], sign, device["usb_name"]))
answer = ""
while answer not in ids:
try:
answer = int(input("Type the id which you want to toggle:"))
if answer not in ids:
print("Invalid USB ID")
except Exception as exc:
print("That didn't work try again")
print(exc)
sys.exit()
for item in devices:
if item["usb_id"] == answer:
if item["usb_status"] == "allow":
os.popen('usbguard block-device {}'.format(answer))
else:
os.popen('usbguard allow-device {}'.format(answer))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment