Skip to content

Instantly share code, notes, and snippets.

@tdebatty
Created September 5, 2014 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdebatty/64030c6f3b6151a2f2dc to your computer and use it in GitHub Desktop.
Save tdebatty/64030c6f3b6151a2f2dc to your computer and use it in GitHub Desktop.
Allows you to filter which USB devices may be connected to your system
#! /usr/bin/php
<?php
// Allows you to filter which USB devices may be connected to your system
// To find the ID of devices:
// sudo pkill udevd
// sudo udevd --debug
// Setup:
// Save this file to /opt/udev-filter-usb
// chmod +x /opt/udev-filter-usb
// Add following line in /etc/udev/rules.d/45-filter-usb.rules
// ACTION=="add", SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", RUN+="/bin/sh -c '/opt/udev-filter-usb /sys$DEVPATH'"
$allowed = array(
// "Add_Your_Device_Id",
"Logitech_USB_Optical_Mouse",
"Kingston_DataTraveler_3.0_08123456D90434128BA",
"Generic_Mass_Storage_7C0SKF7897"
);
$devpath=$argv[1];
$serial = getenv("ID_SERIAL");
$rule = "DENY";
if (in_array($serial, $allowed)) {
$rule = "ALLOW";
}
file_put_contents(
"/var/log/udev-filter-usb",
date("Y-m-d H.i.s") . " : $rule : $devpath : $serial\n",
FILE_APPEND);
if ($rule == "DENY") {
$forbidden = "$devpath/authorized";
exec("echo 0 > $forbidden");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment