Files
USB_installer/tests/who-holds-flash2.ps1

107 lines
4.5 KiB
PowerShell

# Кто держит файлы на томах флешки. Только чтение.
$ErrorActionPreference = 'Stop'
$outFile = 'C:\PF\USBInstaller\test\who-holds-flash2.txt'
$log = New-Object System.Collections.Generic.List[string]
function W([string]$s) { $script:log.Add($s) }
$cs = @'
using System;
using System.Runtime.InteropServices;
public static class H2 {
[DllImport("ntdll.dll")] public static extern int NtQuerySystemInformation(int c, IntPtr b, int l, out int r);
[DllImport("ntdll.dll")] public static extern int NtQueryObject(IntPtr h, int c, IntPtr b, int l, out int r);
[DllImport("kernel32.dll", SetLastError=true)] public static extern IntPtr OpenProcess(int a, bool i, int p);
[DllImport("kernel32.dll", SetLastError=true)] public static extern bool DuplicateHandle(IntPtr sp, IntPtr sh, IntPtr tp, out IntPtr th, int a, bool i, int o);
[DllImport("kernel32.dll")] public static extern IntPtr GetCurrentProcess();
[DllImport("kernel32.dll", SetLastError=true)] public static extern bool CloseHandle(IntPtr h);
[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Unicode)] public static extern IntPtr CreateFileW(string n, uint a, uint s, IntPtr sec, uint d, uint f, IntPtr t);
public static string Name(IntPtr h) {
int r = 0;
IntPtr b = Marshal.AllocHGlobal(8192);
try {
int st = NtQueryObject(h, 1, b, 8192, out r);
if (st != 0) return null;
int len = (int)Marshal.ReadInt16(b, 0);
IntPtr p = Marshal.ReadIntPtr(b, 8);
if (len <= 0 || p == IntPtr.Zero) return null;
return Marshal.PtrToStringUni(p, len / 2);
} finally { Marshal.FreeHGlobal(b); }
}
}
'@
Add-Type -TypeDefinition $cs -ErrorAction Stop
W ("start " + (Get-Date -Format 'HH:mm:ss'))
$mineFs = [System.IO.File]::Open('C:\PF\USBInstaller\test\.h', [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::ReadWrite)
$myH = $mineFs.SafeFileHandle.DangerousGetHandle().ToInt64()
$myPid = $PID
$len = 4MB
$buf = [Runtime.InteropServices.Marshal]::AllocHGlobal($len)
$ret = 0
while ($true) {
$st = [H2]::NtQuerySystemInformation(64, $buf, $len, [ref]$ret)
if ($st -eq 0) { break }
if ($st -ne -1073741820) { throw ("NtQuerySystemInformation 0x{0:X8}" -f $st) }
[Runtime.InteropServices.Marshal]::FreeHGlobal($buf)
$len *= 2
$buf = [Runtime.InteropServices.Marshal]::AllocHGlobal($len)
}
$count = [Runtime.InteropServices.Marshal]::ReadInt64($buf, 0)
$fileType = -1
for ($i = 0; $i -lt $count; $i++) {
$e = 16 + 40 * $i
if ([Runtime.InteropServices.Marshal]::ReadInt64($buf, $e + 8) -eq $myPid -and [Runtime.InteropServices.Marshal]::ReadInt64($buf, $e + 16) -eq $myH) {
$fileType = [int][Runtime.InteropServices.Marshal]::ReadInt16($buf, $e + 30); break
}
}
if ($mine -ne [IntPtr]::Zero -and $mine -ne [IntPtr](-1)) { [void][H2]::CloseHandle($mine) }
W ("handles=" + $count + " fileType=" + $fileType)
$cur = [H2]::GetCurrentProcess()
$hits = @{}
$scanned = 0
for ($i = 0; $i -lt $count; $i++) {
$e = 16 + 40 * $i
$tidx = [int][Runtime.InteropServices.Marshal]::ReadInt16($buf, $e + 30)
if ($tidx -ne $fileType) { continue }
$scanned++
$opid = [Runtime.InteropServices.Marshal]::ReadInt64($buf, $e + 8)
$hval = [Runtime.InteropServices.Marshal]::ReadInt64($buf, $e + 16)
$hp = [H2]::OpenProcess(0x0040, $false, [int]$opid)
if ($hp -eq [IntPtr]::Zero) { continue }
$dup = [IntPtr]::Zero
if ([H2]::DuplicateHandle($hp, [IntPtr]$hval, $cur, [ref]$dup, 0, $false, 2)) {
$nm = $null
try { $nm = [H2]::Name($dup) } catch { $nm = $null }
if ($nm -and $nm -match '^\\Device\\HarddiskVolume\d+\\?(.*)$') {
$rest = $Matches[1]
foreach ($dl in @('F','G','H')) {
$p = $dl + ':\' + $rest
if ($rest -ne '' -and (Test-Path -LiteralPath $p -ErrorAction SilentlyContinue)) {
if (-not $hits.ContainsKey($opid)) { $hits[$opid] = New-Object System.Collections.Generic.List[string] }
if ($hits[$opid].Count -lt 5) { $hits[$opid].Add($p) }
break
}
}
}
[void][H2]::CloseHandle($dup)
}
[void][H2]::CloseHandle($hp)
}
[Runtime.InteropServices.Marshal]::FreeHGlobal($buf)
W ("file handles scanned: " + $scanned)
W "--- holders ---"
if ($hits.Count -eq 0) { W " none" }
foreach ($k in $hits.Keys) {
$p = Get-Process -Id $k -ErrorAction SilentlyContinue
$nm = if ($p) { $p.ProcessName } else { "?" }
W (" PID " + $k + " " + $nm)
foreach ($s in $hits[$k]) { W (" " + $s) }
}
[System.IO.File]::WriteAllLines($outFile, $log, (New-Object System.Text.UTF8Encoding($true)))
Write-Host "done"