Initial commit

This commit is contained in:
Willy 2023-09-08 16:50:28 -04:00
parent 6361a31d55
commit f5dd04c71d
3 changed files with 153 additions and 0 deletions

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
Pillow==10.0.0
pystray==0.19.4
pywin32==306
WMI==1.5.1

101
windows_usbkill.py Normal file
View File

@ -0,0 +1,101 @@
# windows_usbkill
# Author: Willy
import wmi
import subprocess
import os
import pystray
import threading
import sys
import pystray
import PIL.Image
import ctypes
import logging
import pythoncom
from pystray import MenuItem as item
if sys.platform != 'win32':
print("This script only works on Windows!")
logging.info("[ERROR] This script only works on Windows!")
os._exit(0)
# Logging
logging.basicConfig(filename="log.txt",
format='%(asctime)s | %(message)s',
level=logging.INFO)
# Load icon
current_dir_image = os.path.dirname(os.path.abspath(__file__))
image_file = os.path.join(current_dir_image, 'assets', 'icon.png')
try:
image = PIL.Image.open(image_file)
except FileNotFoundError:
asset_error = (None, 'Asset folder missing!', 'Fatal Error!', 0x10)
logging.info("[ERROR] Asset folder missing!")
ctypes.windll.user32.MessageBoxW(*asset_error)
os._exit(0)
logging.info("[INFO] Script Started!")
# Variables
is_paused = False
event = threading.Event()
# Scan
def start_scan():
pythoncom.CoInitialize()
global is_paused
while True:
if is_paused == False:
print("[Info] Scanning...")
raw_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_USBHub\'"
a = wmi.WMI ()
watcher = a.watch_for(raw_wql=raw_wql)
if is_paused == True:
break
while 1:
usb = watcher ()
if usb:
if is_paused == True:
break
logging.info("[TRIGGERED] USB INSERTED! Shutting down!")
print("[TRIGGERED] USB INSERTED! Shutting down!")
os.system("shutdown /p /f")
else:
break
# Pause
def toggle_pause():
pythoncom.CoInitialize()
global is_paused
is_paused = not is_paused
if is_paused:
print("[Info] Paused!")
else:
print("[Info] Resumed!")
scan_thread = threading.Thread(target=start_scan)
scan_thread.start()
# Exit
def terminate():
pythoncom.CoInitialize()
print("[Info] Exited!")
icon = pystray.Icon
icon.visible = False
os._exit(0)
# Tray
def create_tray_icon():
pythoncom.CoInitialize()
icon = pystray.Icon("Test", image, 'Windows usbkill', menu=pystray.Menu(
pystray.MenuItem("Pause", toggle_pause, checked=lambda item: is_paused),
pystray.MenuItem("Exit", terminate)
))
icon.run()
# Start
scan_thread = threading.Thread(target=start_scan)
scan_thread.start()
trigger_thread = threading.Thread(target=create_tray_icon)
trigger_thread.start()

48
windows_usbkill.spec Normal file
View File

@ -0,0 +1,48 @@
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
added_files = [
( 'assets/icon.png', '/assets/' )
]
a = Analysis(
['windows_usbkill.py'],
pathex=[],
binaries=[],
datas=added_files,
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='windows_usbkill-0.1-beta',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon=['assets\\icon.png'],
)