Skip to content

Commit 938f836

Browse files
committed
WOW64FS Redirection Bypass
1 parent 468abea commit 938f836

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

LabTech.psm1

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Tested Versions: v10.5, v11, v12
1212
1313
.NOTES
14-
Version: 1.5
14+
Version: 1.6
1515
Author: Chris Taylor
1616
Website: labtechconsulting.com
1717
Creation Date: 3/14/2016
@@ -32,6 +32,8 @@
3232
Update Date: 8/28/2018
3333
Purpose/Change: Added Update-LTService function
3434
35+
Update Date: 2/26/2019
36+
Purpose/Change: Update to support 32-bit execution in 64-bit without SYSNATIVE redirection
3537
#>
3638

3739
if (-not ($PSVersionTable)) {Write-Warning 'PS1 Detected. PowerShell Version 2.0 or higher is required.';return}
@@ -43,35 +45,31 @@ $ModuleVersion = "1.5"
4345
If ($env:PROCESSOR_ARCHITEW6432 -match '64' -and [IntPtr]::Size -ne 8) {
4446
Write-Warning '32-bit PowerShell session detected on 64-bit OS. Attempting to launch 64-Bit session to process commands.'
4547
$pshell="${env:WINDIR}\sysnative\windowspowershell\v1.0\powershell.exe"
46-
Write-Debug "Launch Context: $($myInvocation | out-String)"
4748
If (!(Test-Path -Path $pshell)) {
48-
Write-Warning 'SYSNATIVE REDIRECTION IS NOT AVAILABLE. Attempting to access 64bit binaries directly.'
49+
Write-Warning 'SYSNATIVE PATH REDIRECTION IS NOT AVAILABLE. Attempting to access 64-bit PowerShell directly.'
4950
$pshell="${env:WINDIR}\System32\WindowsPowershell\v1.0\powershell.exe"
5051
$FSRedirection=$True
51-
$MethodSignature = @"
52+
Add-Type -Debug:$False -Name Wow64 -Namespace "Kernel32" -MemberDefinition @"
5253
[DllImport("kernel32.dll", SetLastError=true)]
5354
public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
55+
56+
[DllImport("kernel32.dll", SetLastError=true)]
57+
public static extern bool Wow64RevertWow64FsRedirection(ref IntPtr ptr);
5458
"@
55-
$Kernel32 = Add-Type -MemberDefinition $MethodSignature -Namespace "Kernel32" -Passthru -Name DisableWow64Redirection
5659
[ref]$ptr = New-Object System.IntPtr
57-
# $Result = $Kernel32::Wow64DisableWow64FsRedirection($ptr) # Now you can call 64-bit Powershell from system32
60+
$Result = [Kernel32.Wow64]::Wow64DisableWow64FsRedirection($ptr) # Now you can call 64-bit Powershell from system32
5861
}
5962
If ($myInvocation.Line) {
60-
Write-Output '&"$pshell" -NonInteractive -NoProfile $myInvocation.Line'
63+
&"$pshell" -NonInteractive -NoProfile $myInvocation.Line
6164
} Elseif ($myInvocation.InvocationName) {
62-
Write-Output '&"$pshell" -NonInteractive -NoProfile -File "$($myInvocation.InvocationName)" $args'
65+
&"$pshell" -NonInteractive -NoProfile -File "$($myInvocation.InvocationName)" $args
6366
} Else {
64-
Write-Output '&"$pshell" -NonInteractive -NoProfile $myInvocation.MyCommand'
67+
&"$pshell" -NonInteractive -NoProfile $myInvocation.MyCommand
6568
}
6669
$ExitResult=$LASTEXITCODE
6770
If ($FSRedirection -eq $True) {
68-
$MethodSignature = @"
69-
[DllImport("kernel32.dll", SetLastError=true)]
70-
public static extern bool Wow64RevertWow64FsRedirection(ref IntPtr ptr);
71-
"@
72-
$Kernel32Default = Add-Type -MemberDefinition $MethodSignature -Namespace "Kernel32" -Passthru -Name RevertWow64Redirection
7371
[ref]$defaultptr = New-Object System.IntPtr
74-
# $Result = $Kernel32Default::Wow64RevertWow64FsRedirection($defaultptr)
72+
$Result = [Kernel32.Wow64]::Wow64RevertWow64FsRedirection($defaultptr)
7573
}
7674
Write-Warning 'Exiting 64-bit session. Module will only remain loaded in native 64-bit PowerShell environment.'
7775
Exit $ExitResult

0 commit comments

Comments
 (0)