forked from hephaest0s/usbkill
/
usbkill.py
379 lines (318 loc) · 12.6 KB
/
usbkill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# _ _ _ _ _
# | | | | (_) | |
# _ _ ___| |__ | | _ _| | |
# | | | |/___) _ \| |_/ ) | | |
# | |_| |___ | |_) ) _ (| | | |
# |____/(___/|____/|_| \_)_|\_)_)
#
#
# Hephaestos <hephaestos@riseup.net> - 8764 EF6F D5C1 7838 8D10 E061 CF84 9CE5 42D0 B12B
# <https://github.com/hephaest0s/usbkill>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__version__ = "1.0-rc.2"
import re
import subprocess
import platform
import os, sys, signal
from time import sleep
from datetime import datetime
# Get the current platform
CURRENT_PLATFORM = platform.system().upper()
# Darwin specific library
if CURRENT_PLATFORM.startswith("DARWIN"):
import plistlib
# We compile this function beforehand for efficiency.
DEVICE_RE = [ re.compile(".+ID\s(?P<id>\w+:\w+)"), re.compile("0x([0-9a-z]{4})") ]
# Set the settings filename here
SETTINGS_FILE = '/etc/usbkill/settings.ini'
help_message = """
usbkill is a simple program with one goal: quickly shutdown the computer when a USB is inserted or removed.
Events are logged in /var/log/usbkill/kills.log
You can configure a whitelist of USB ids that are acceptable to insert and the remove.
The USB id can be found by running the command 'lsusb'.
Settings can be changed in /etc/usbkill/settings
In order to be able to shutdown the computer, this program needs to run as root.
"""
def log(settings, msg):
log_file = settings['log_file']
contents = '\n{0} {1}\nCurrent state:\n'.format(str(datetime.now()), msg)
with open(log_file, 'a+') as log:
log.write(contents)
# Log current USB state
if CURRENT_PLATFORM.startswith("DARWIN"):
os.system("system_profiler SPUSBDataType >> " + log_file)
else:
os.system("lsusb >> " + log_file)
def shred(settings):
shredder = settings['remove_file_command']
# List logs and settings to be removed
if settings['melt_usbkill']:
settings['folders_to_remove'].append(os.path.dirname(settings['log_file']))
settings['folders_to_remove'].append(os.path.dirname(SETTINGS_FILE))
usbkill_folder = os.path.dirname(os.path.realpath(__file__))
if usbkill_folder.upper().startswith('USB'):
settings['folders_to_remove'].append(usbkill_folder)
else:
settings['files_to_remove'].append(os.path.realpath(__file__))
settings['files_to_remove'].append(usbkill_folder + "/settings.ini")
# Remove files and folders
for _file in settings['files_to_remove'] + settings['folders_to_remove']:
os.system(shredder + _file)
def kill_computer(settings):
# Log what is happening:
if not settings['melt_usbkill']: # No need to spend time on logging if logs will be removed
log(settings, "Detected a USB change. Dumping the list of connected devices and killing the computer...")
# Shred as specified in settings
shred(settings)
if settings['do_sync']:
# Sync the filesystem to save recent changes
os.system("sync")
# Execute kill commands in order.
for command in settings['kill_commands']:
os.system(command)
else:
# If syncing is risky because it might take too long, then sleep for 5ms.
# This will still allow for syncing in most cases.
sleep(0.05)
if settings['shut_down']: # Use argument --no-shut-down to prevent a shutdown.
# Finally poweroff computer immediately
if CURRENT_PLATFORM.startswith("DARWIN"):
# OS X (Darwin) - Will halt ungracefully, without signaling apps
os.system("killall Finder && killall loginwindow && halt -q")
elif CURRENT_PLATFORM.endswith("BSD"):
# BSD-based systems - Will shutdown
os.system("shutdown -h now")
else:
# Linux-based systems - Will shutdown
os.system("poweroff -f")
# Exit the process to prevent executing twice (or more) all commands
sys.exit(0)
def lsusb_darwin():
# Use OS X system_profiler (native and 60% faster than lsusb port)
df = subprocess.check_output("system_profiler SPUSBDataType -xml -detailLevel mini", shell=True)
if sys.version_info[0] == 2:
df = plistlib.readPlistFromString(df)
elif sys.version_info[0] == 3:
df = plistlib.loads(df)
def check_inside(result, devices):
"""
I suspect this function can become more readable.
"""
# Do not take devices with Built-in_Device=Yes
try:
result["Built-in_Device"]
except KeyError:
# Check if vendor_id/product_id is available for this one
try:
assert "vendor_id" in result and "product_id" in result
# Append to the list of devices
devices.append(DEVICE_RE[1].findall(result["vendor_id"])[0] + ':' + DEVICE_RE[1].findall(result["product_id"])[0])
except AssertionError: {}
# Check if there is items inside
try:
# Looks like, do the while again
for result_deep in result["_items"]:
# Check what's inside the _items array
check_inside(result_deep, devices)
except KeyError: {}
# Run the loop
devices = []
for result in df[0]["_items"]:
check_inside(result, devices)
return devices
def lsusb():
# A Python version of the command 'lsusb' that returns a list of connected usbids
if CURRENT_PLATFORM.startswith("DARWIN"):
# Use OS X system_profiler (native and 60% faster than lsusb port)
return lsusb_darwin()
else:
# Use lsusb on linux and bsd
return DEVICE_RE[0].findall(subprocess.check_output("lsusb", shell=True).decode('utf-8').strip())
def program_present(program):
if sys.version_info[0] == 3:
# Python3
from shutil import which
return which(program) != None
else:
"""
Test if an executable exist in Python2
-> http://stackoverflow.com/a/377028
"""
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath and is_exe(program):
return True
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return True
return False
def load_settings(filename):
# Libraries that are only needed in this function:
from json import loads as jsonloads
if sys.version_info[0] == 3:
# Python3
import configparser
def get_setting(name, gtype=''):
"""
configparser: Compatibility layer for Python 2/3
Function currently depends on a side effect, which is not necessary.
"""
section = config['config']
if gtype == 'FLOAT':
return section.getfloat(name)
elif gtype == 'INT':
return section.getint(name)
elif gtype == 'BOOL':
return section.getboolean(name)
return section[name]
else:
#Python2
import ConfigParser as configparser
def get_setting(name, gtype=''):
if gtype == 'FLOAT':
return config.getfloat('config', name)
elif gtype == 'INT':
return config.getint('config', name)
elif gtype == 'BOOL':
return config.getboolean('config', name)
return config.get('config', name)
config = configparser.ConfigParser()
# Read all lines of settings file
config.read(filename)
# Build settings
settings = dict({
'sleep_time' : get_setting('sleep', 'FLOAT'),
'whitelist': jsonloads(get_setting('whitelist').strip()),
'log_file': get_setting('log_file'),
'melt_usbkill' : get_setting('melt_usbkill', 'BOOL'),
'remove_file_command' : get_setting('remove_file_command') + " ",
'files_to_remove' : jsonloads(get_setting('files_to_remove').strip()),
'folders_to_remove' : jsonloads(get_setting('folders_to_remove').strip()),
'do_sync' : get_setting('do_sync', 'BOOL'),
'kill_commands': jsonloads(get_setting('kill_commands').strip()),
'double_usbid_detection' : get_setting('double_usbid_detection', 'BOOL')
})
return settings
def loop(settings):
# Main loop that checks every 'sleep_time' seconds if computer should be killed.
# Allows only whitelisted usb devices to connect!
# Does not allow usb device that was present during program start to disconnect!
start_devices = lsusb()
acceptable_devices = set(start_devices + settings['whitelist'])
# Write to logs that loop is starting:
msg = "[INFO] Started patrolling the USB ports every " + str(settings['sleep_time']) + " seconds..."
log(settings, msg)
print(msg)
# Main loop
while True:
# List the current usb devices
current_devices = lsusb()
# Check that no usbids are connected twice.
# Two devices with same usbid implies a usbid copy attack
if settings['double_usbid_detection'] and not len(current_devices) == len(set(current_devices)):
kill_computer(settings)
# Check that all current devices are in the set of acceptable devices
for device in current_devices:
if device not in acceptable_devices:
kill_computer(settings)
# Check that all start devices are still present in current devices
for device in start_devices:
if device not in current_devices:
kill_computer(settings)
sleep(settings['sleep_time'])
def startup_checks():
# Splash
print(" _ _ _ _ _ \n" +
" | | | | (_) | | \n" +
" _ _ ___| |__ | | _ _| | | \n" +
" | | | |/___) _ \| |_/ ) | | | \n" +
" | |_| |___ | |_) ) _ (| | | | \n" +
" |____/(___/|____/|_| \_)_|\_)_)\n")
# Check arguments
args = sys.argv[1:]
# Check for help
if '-h' in args or '--help' in args:
sys.exit(help_message)
copy_settings = False
if '--cs' in args:
args.remove('--cs')
copy_settings = True
shut_down = True
if '--no-shut-down' in args:
print("[NOTICE] Ready to execute all the (potentially destructive) commands, but NOT shut down the computer.")
args.remove('--no-shut-down')
shut_down = False
# Check all other args
if len(args) > 0:
sys.exit("\n[ERROR] Argument not understood. Can only understand -h\n")
# Check if program is run as root, else exit.
# Root is needed to power off the computer.
if not os.geteuid() == 0:
sys.exit("\n[ERROR] This program needs to run as root.\n")
# Warn the user if he does not have FileVault
if CURRENT_PLATFORM.startswith("DARWIN"):
try:
# fdesetup return exit code 0 when true and 1 when false
subprocess.check_output(["/usr/bin/fdesetup", "isactive"])
except subprocess.CalledProcessError:
print("[NOTICE] FileVault is disabled. Sensitive data SHOULD be encrypted.")
if not os.path.isdir("/etc/usbkill/"):
os.mkdir("/etc/usbkill/")
# On first time use copy settings.ini to /etc/usebkill/settings.ini
# If dev-mode, always copy and don't remove old settings
if not os.path.isfile(SETTINGS_FILE) or copy_settings:
sources_path = os.path.dirname(os.path.realpath(__file__)) + '/'
if not os.path.isfile(sources_path + "settings.ini"):
sys.exit("\n[ERROR] You have lost your settings file. Get a new copy of the settings.ini and place it in /etc/usbkill/ or in " + sources_path + "/\n")
print("[NOTICE] Copying setting.ini to " + SETTINGS_FILE )
os.system("cp " + sources_path + "settings.ini " + SETTINGS_FILE)
if not copy_settings:
os.remove(sources_path + "settings.ini")
# Load settings
settings = load_settings(SETTINGS_FILE)
settings['shut_down'] = shut_down
# Make sure no spaces a present in paths to be wiped.
for name in settings['folders_to_remove'] + settings['files_to_remove']:
if ' ' in name:
msg += "[ERROR][WARNING] '" + name + "'as specified in your settings.ini contains a space.\n"
sys.exit(msg)
# Make sure srm is present if it will be used.
if settings['melt_usbkill'] or len(settings['folders_to_remove'] + settings['files_to_remove']) > 0:
if not program_present('srm'):
sys.exit("[ERROR] usbkill configured to destroy data, but srm not installed.\n")
if not settings['remove_file_command'].startswith('srm'):
sys.exit("[ERROR] remove_file_command should start with `srm'. srm should be used for automated data overwrite.\n")
# Make sure there is a logging folder
log_folder = os.path.dirname(settings['log_file'])
if not os.path.isdir(log_folder):
os.mkdir(log_folder)
return settings
if __name__=="__main__":
# Run startup checks and load settings
settings = startup_checks()
# Define exit handler now that settings are loaded...
def exit_handler(signum, frame):
print("\n[INFO] Exiting because exit signal was received\n")
log(settings, "[INFO] Exiting because exit signal was received")
sys.exit(0)
# Register handlers for clean exit of program
for sig in [signal.SIGINT, signal.SIGTERM, signal.SIGQUIT, ]:
signal.signal(sig, exit_handler)
# Start main loop
loop(settings)