From 9ee9afb76e294924606b800a18669f4a553559bb Mon Sep 17 00:00:00 2001 From: Klesh Wong Date: Mon, 11 May 2020 09:14:05 +0800 Subject: [PATCH] [feature] ms-wubi-no-shift --- win/setup.ps1 | 13 ++++++++-- win/wubi-no-shift.ps1 | 60 +++++++++++++++---------------------------- 2 files changed, 31 insertions(+), 42 deletions(-) diff --git a/win/setup.ps1 b/win/setup.ps1 index dc1ac4b..14140c8 100644 --- a/win/setup.ps1 +++ b/win/setup.ps1 @@ -1,3 +1,8 @@ +if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { + Start-Process PowerShell -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`""; + exit; +} + Set-ExecutionPolicy RemoteSigned # link config files @@ -11,5 +16,9 @@ if (Test-Path $sshconf -PathType Leaf) { New-Item -ItemType SymbolicLink -Target $sshconf -Path $home\.ssh\config -Force } -# disable Shift key toggling CN/EN -Install-Module -Name PSReflect-Functions \ No newline at end of file +# fix Shift key toggling Cn/En fro MS wubi +$wubiAction = New-ScheduledTaskAction -Execute 'Powershell.exe' ` + -Argument "-NoProfile -WindowStyle Hidden -File $PSScriptRoot\wubi-no-shift.ps1" +$wubiTrigger = New-ScheduledTaskTrigger -AtLogOn +$wubiTrigger.Delay = 'PT3S' +Register-ScheduledTask -Action $wubiAction -Trigger $wubiTrigger -TaskName "Wubi No Shift" -Description "Disable Shift key toggling CN/EN" -RunLevel Highest \ No newline at end of file diff --git a/win/wubi-no-shift.ps1 b/win/wubi-no-shift.ps1 index c693099..b27b925 100644 --- a/win/wubi-no-shift.ps1 +++ b/win/wubi-no-shift.ps1 @@ -1,6 +1,8 @@ -#Requires -RunAsAdministrator +if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { + Start-Process PowerShell -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`""; + exit; +} -Import-Module PSReflect-Functions $Supported = @{ "B3448BF077665F2E1CA67094BCF2A7C5" = 0x14DE1; @@ -18,53 +20,31 @@ $offsetAddr = $Supported[$ChsIMEHash] if (-not $offsetAddr) { throw [System.Exception] "Unsupported ChsIme.exe" } -echo "Offset Address: $offsetAddr" -# reference -<# - public enum ProcessAccessFlags : uint - { - All = 0x001F0FFF, - Terminate = 0x00000001, - CreateThread = 0x00000002, - VirtualMemoryOperation = 0x00000008, - VirtualMemoryRead = 0x00000010, - VirtualMemoryWrite = 0x00000020, - DuplicateHandle = 0x00000040, - CreateProcess = 0x000000080, - SetQuota = 0x00000100, - SetInformation = 0x00000200, - QueryInformation = 0x00000400, - QueryLimitedInformation = 0x00001000, - Synchronize = 0x00100000 - } - public enum SnapshotFlags : uint - { - HeapList = 0x00000001, - Process = 0x00000002, - Thread = 0x00000004, - Module = 0x00000008, - Module32 = 0x00000010, - All = (HeapList | Process | Thread | Module), - Inherit = 0x80000000, - NoHeaps = 0x40000000 - - } -#> +Add-Type -MemberDefinition @' +[DllImport("kernel32.dll", SetLastError = true)] + public static extern bool WriteProcessMemory( + IntPtr hProcess, + IntPtr lpBaseAddress, + byte[] lpBuffer, + Int32 nSize, + out IntPtr lpNumberOfBytesWritten); +'@ -Name Kernel32 -Namespace Pinvoke $ps = Get-Process -Name $ChsIME foreach ($p in $ps) { - $hProcess = Get-Process -Name $ChsIme - if (!$hProcess) { - throw [System.Exception] "Unable to open process $pid"; - } - $hModule = $hProcess.Modules | Where-Object {$_.ModuleName -eq $ChsIMEExe} + $hModule = $p.Modules | Where-Object {$_.ModuleName -eq $ChsIMEExe} if (!$hModule) { continue } $hModule = $hModule[0] $addr = [IntPtr]::Add($hModule.BaseAddress, $offsetAddr) [Int32]$n = 0 - [PSReflectFunctions.kernel32]::WriteProcessMemory($hProcess.Handle[0], $addr, @(0x31, 0xc0), 2, [ref]$n) + $pidd = $p.id + if ([Pinvoke.Kernel32]::WriteProcessMemory($p.Handle[0], $addr, @(0x31, 0xc0), 2, [ref]$n)) { + echo "$pidd is patched" + } else { + throw [System.Exception] "Failed to patch $pidd" + } }