Skip to content

Instantly share code, notes, and snippets.

@lhecker
Last active November 18, 2023 18:18
Show Gist options
  • Save lhecker/393252fb1e95b3bc118d40ace802a505 to your computer and use it in GitHub Desktop.
Save lhecker/393252fb1e95b3bc118d40ace802a505 to your computer and use it in GitHub Desktop.
Find environment strings with embedded nulls
function Check($key) {
foreach ($name in $key.GetValueNames()) {
if ($key.GetValue($name).ToString().Contains("`0")) {
Write-Output "$($key.Name): $($name)"
}
}
}
$hklm = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, 0)
$hkcu = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::CurrentUser, 0)
Check($hklm.OpenSubKey('Software\Microsoft\Windows\CurrentVersion'))
Check($hklm.OpenSubKey('SYSTEM\CurrentControlSet\Control\Session Manager\Environment'))
Check($hkcu.OpenSubKey('Environment'))
$volenv = $hkcu.OpenSubKey('Volatile Environment')
Check($volenv)
foreach ($name in $volenv.GetSubKeyNames()) {
Check($volenv.OpenSubKey($name))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment