-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUSBBlocker.ps1
More file actions
134 lines (112 loc) · 4.37 KB
/
Copy pathUSBBlocker.ps1
File metadata and controls
134 lines (112 loc) · 4.37 KB
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
<#
.SYNOPSIS
Block/unblock USB flash drives and external storage while keeping mouse, keyboard, audio and other USB peripherals working.
.DESCRIPTION
Operates on the USB storage class drivers:
- USBSTOR : USB sticks, standard external HDD/SSD
- UASPStor : high-performance USB 3.x drives (USB Attached SCSI)
- WpdBusEnum : Windows Portable Devices (smartphones in MTP/PTP mode)
Peripherals left untouched (different drivers): mouse, keyboard, webcam, USB audio,
printers, wireless dongles, smart card readers, joysticks, etc.
.PARAMETER Block
Disables the USB storage drivers. Drives already mounted stay available until
they are disconnected; new ones are rejected.
.PARAMETER Unblock
Re-enables the USB storage drivers.
.PARAMETER Status
Shows the current driver state.
.EXAMPLE
.\USBBlocker.ps1 -Block
.\USBBlocker.ps1 -Unblock
.\USBBlocker.ps1 -Status
#>
[CmdletBinding(DefaultParameterSetName='Status')]
param(
[Parameter(ParameterSetName='Block', Mandatory)] [switch]$Block,
[Parameter(ParameterSetName='Unblock', Mandatory)] [switch]$Unblock,
[Parameter(ParameterSetName='Status')] [switch]$Status
)
$ErrorActionPreference = 'Stop'
$Drivers = @(
@{ Name = 'USBSTOR'; Description = 'USB Mass Storage (sticks, external HDDs)' },
@{ Name = 'UASPStor'; Description = 'USB Attached SCSI (fast USB 3.x drives)' },
@{ Name = 'WpdBusEnum'; Description = 'Windows Portable Devices (MTP/PTP phones)' }
)
$LogFile = Join-Path $PSScriptRoot 'usbblocker.log'
function Write-Log {
param([string]$Message, [string]$Level = 'INFO')
$ts = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
$line = "[$ts] [$Level] $Message"
Add-Content -Path $LogFile -Value $line -Encoding utf8
switch ($Level) {
'ERROR' { Write-Host $line -ForegroundColor Red }
'WARN' { Write-Host $line -ForegroundColor Yellow }
'OK' { Write-Host $line -ForegroundColor Green }
default { Write-Host $line }
}
}
function Test-Admin {
$id = [Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object Security.Principal.WindowsPrincipal($id)
return $p.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Get-DriverStartValue {
param([string]$Name)
$path = "HKLM:\SYSTEM\CurrentControlSet\Services\$Name"
if (-not (Test-Path $path)) { return $null }
return (Get-ItemProperty -Path $path -Name 'Start' -ErrorAction SilentlyContinue).Start
}
function Set-DriverStartValue {
param([string]$Name, [int]$Value)
$path = "HKLM:\SYSTEM\CurrentControlSet\Services\$Name"
if (-not (Test-Path $path)) {
Write-Log "Driver $Name not present on this system, skipping" 'WARN'
return $false
}
Set-ItemProperty -Path $path -Name 'Start' -Value $Value
return $true
}
function Show-Status {
Write-Host ''
Write-Host '=== USB storage driver status ===' -ForegroundColor Cyan
foreach ($d in $Drivers) {
$val = Get-DriverStartValue -Name $d.Name
$state = switch ($val) {
0 { 'BOOT (always on)' }
1 { 'SYSTEM (on)' }
2 { 'AUTO (on)' }
3 { 'DEMAND (on-demand) - UNBLOCKED' }
4 { 'DISABLED - BLOCKED' }
$null { 'not present' }
default { "unknown value: $val" }
}
$color = if ($val -eq 4) { 'Red' } elseif ($null -eq $val) { 'DarkGray' } else { 'Green' }
Write-Host (" {0,-12} {1,-50} [{2}]" -f $d.Name, $d.Description, $state) -ForegroundColor $color
}
Write-Host ''
}
# === Main ===
if ($PSCmdlet.ParameterSetName -eq 'Status' -or $Status) {
Show-Status
return
}
if (-not (Test-Admin)) {
Write-Log 'This script requires administrator privileges. Re-run it from an elevated PowerShell.' 'ERROR'
exit 1
}
$newValue = if ($Block) { 4 } else { 3 }
$action = if ($Block) { 'BLOCK' } else { 'UNBLOCK' }
Write-Log "Starting USB storage driver $action" 'INFO'
foreach ($d in $Drivers) {
try {
$ok = Set-DriverStartValue -Name $d.Name -Value $newValue
if ($ok) {
Write-Log "$($d.Name) -> Start=$newValue ($($d.Description))" 'OK'
}
} catch {
Write-Log "Error on $($d.Name): $_" 'ERROR'
}
}
Write-Log "$action complete. Devices already mounted stay accessible until disconnected." 'OK'
Write-Host ''
Show-Status