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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]'Administrator')) { switch ((Get-Culture).Name) { 'pt-BR' { Write-Host 'Você não executou este script como Administrador. Este script será executado automaticamente como Administrador.' -ForegroundColor Green } Default { Write-Host 'You didn''t run this script as an Administrator. This script will self elevate to run as an Administrator and continue.' -ForegroundColor Green } } Start-Sleep -Milliseconds 2500 Start-Process PowerShell.exe -ArgumentList ("-NoProfile -ExecutionPolicy Bypass -File `"{0}`"" -f $PSCommandPath) -Verb RunAs Exit } $OSArchitecture = (Get-CimInstance -ClassName Win32_OperatingSystem).OSArchitecture $termsrvDllFile = "$env:SystemRoot\System32\termsrv.dll" $termsrvDllCopy = "$env:SystemRoot\System32\termsrv.dll.copy" $termsrvPatched = "$env:SystemRoot\System32\termsrv.dll.patched" $patterns = @{ Pattern = [regex]'39 81 3C 06 00 00 0F (?:[0-9A-F]{2} ){4}00' Win24H2 = [regex]'8B 81 38 06 00 00 39 81 3C 06 00 00 75' } function Get-OSInfo { $OSInfo = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' [PSCustomObject]@{ CurrentBuild = $OSInfo.CurrentBuild BuildRevision = $OSInfo.UBR FullOSBuild = "$($OSInfo.CurrentBuild).$($OSInfo.UBR)" DisplayVersion = $OSInfo.DisplayVersion InstallationType = $OSInfo.InstallationType } } function Get-OSVersion { [version]$OSVersion = [System.Environment]::OSVersion.Version $installationType = (Get-OSInfo).InstallationType if ($OSVersion.Major -eq 6 -and $OSVersion.Minor -eq 1) { return 'Windows 7' } elseif ($OSVersion.Major -eq 10 -and $OSVersion.Build -lt 22000 -and $installationType -eq 'Client') { return 'Windows 10' } elseif ($OSVersion.Major -eq 10 -and $OSVersion.Build -gt 22000) { return 'Windows 11' } elseif ($OSVersion.Major -eq 10 -and $OSVersion.Build -lt 22000 -and $installationType -eq 'Server') { return 'Windows Server 2016' } elseif ($OSVersion.Major -eq 10 -and $OSVersion.Build -eq 20348) { return 'Windows Server 2022' } else { return 'Unsupported OS' } } function Update-Dll { [CmdletBinding()] param ( [Parameter(Mandatory)] [regex]$InputPattern, [Parameter(Mandatory)] [string]$Replacement, [Parameter(Mandatory)] [string]$TermsrvDllAsText, [Parameter(Mandatory)] [string]$TermsrvDllAsFile, [Parameter(Mandatory)] [string]$TermsrvDllAsPatch, [Parameter(Mandatory)] [System.Security.AccessControl.FileSecurity]$TermsrvAclObject ) begin { $match = $TermsrvDllAsText -match $InputPattern $patch = $TermsrvDllAsText -match $Replacement } process { if ($match) { Write-Host "`nPattern matching!`n" -ForegroundColor Green $dllAsTextReplaced = $TermsrvDllAsText -replace $InputPattern, $Replacement [byte[]] $dllAsBytesReplaced = -split $dllAsTextReplaced -replace '^', '0x' [System.IO.File]::WriteAllBytes($TermsrvDllAsPatch, $dllAsBytesReplaced) fc.exe /b $TermsrvDllAsPatch $TermsrvDllAsFile
Start-Sleep -Milliseconds 1500 Copy-Item -Path $TermsrvDllAsPatch -Destination $TermsrvDllAsFile -Force } elseif ($patch) { Write-Host "The file is already patched. No changes are needed.`n" -ForegroundColor Green } else { Write-Host "The pattern was not found. Nothing will be changed.`n" -ForegroundColor Yellow } Set-Acl -Path $TermsrvDllAsFile -AclObject $TermsrvAclObject Start-Service TermService -PassThru } } function Stop-TermService { try { Stop-Service -Name TermService -Force -ErrorAction Stop } catch { Write-Warning -Message $_.Exception.Message return } while ((Get-Service -Name TermService).Status -ne 'Stopped') { Start-Sleep -Milliseconds 500 } Write-Host "`nThe Remote Desktop Services (TermService) has been stopped sucsessfully`n" -ForegroundColor Green } Stop-TermService
$termsrvDllAcl = Get-Acl -Path $termsrvDllFile Write-Host "Owner of termsrv.dll: $($termsrvDllAcl.Owner)"
Copy-Item -Path $termsrvDllFile -Destination $termsrvDllCopy -Force
takeown.exe /F $termsrvDllFile
$currentUserName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
icacls.exe $termsrvDllFile /grant "$($currentUserName):F"
$dllAsByte = [System.IO.File]::ReadAllBytes($termsrvDllFile)
$dllAsText = ($dllAsByte | ForEach-Object { $_.ToString('X2') }) -join ' ' $commonParams = @{ TermsrvDllAsText = $dllAsText TermsrvDllAsFile = $termsrvDllFile TermsrvDllAsPatch = $termsrvPatched TermsrvAclObject = $termsrvDllAcl } switch (Get-OSVersion) { 'Windows 7' { if ($OSArchitecture -eq '64-bit') { switch ((Get-OSInfo).FullOSBuild) { '7601.23964' { $dllAsTextReplaced = $dllAsText -replace '8B 87 38 06 00 00 39 87 3C 06 00 00 0F 84 2F C3 00 00', 'B8 00 01 00 00 90 89 87 38 06 00 00 90 90 90 90 90 90' ` -replace '4C 24 60 BB 01 00 00 00', '4C 24 60 BB 00 00 00 00' ` -replace '83 7C 24 50 00 74 18 48 8D', '83 7C 24 50 00 EB 18 48 8D' } '7601.24546' { $dllAsTextReplaced = $dllAsText -replace '8B 87 38 06 00 00 39 87 3C 06 00 00 0F 84 3E C4 00 00', 'B8 00 01 00 00 90 89 87 38 06 00 00 90 90 90 90 90 90' ` -replace '4C 24 60 BB 01 00 00 00', '4C 24 60 BB 00 00 00 00' ` -replace '83 7C 24 50 00 74 43 48 8D', '83 7C 24 50 00 EB 18 48 8D' } Default { $dllAsTextReplaced = $dllAsText -replace '8B 87 38 06 00 00 39 87 3C 06 00 00 0F 84 3E C4 00 00', 'B8 00 01 00 00 90 89 87 38 06 00 00 90 90 90 90 90 90' ` -replace '4C 24 60 BB 01 00 00 00', '4C 24 60 BB 00 00 00 00' ` -replace '83 7C 24 50 00 74 43 48 8D', '83 7C 24 50 00 EB 18 48 8D' } } } [byte[]] $dllAsBytesReplaced = -split $dllAsTextReplaced -replace '^', '0x' [System.IO.File]::WriteAllBytes($termsrvPatched, $dllAsBytesReplaced) fc.exe /B $termsrvPatched $termsrvDllFile
Start-Sleep -Milliseconds 1500 Copy-Item -Path $termsrvPatched -Destination $termsrvDllFile -Force Set-Acl -Path $termsrvDllFile -AclObject $termsrvDllAcl Start-Sleep -Milliseconds 2500 Start-Service TermService -PassThru } 'Windows 10' { Update-Dll @commonParams -InputPattern $patterns.Pattern -Replacement 'B8 00 01 00 00 89 81 38 06 00 00 90' } 'Windows 11' { if ((Get-OSInfo).DisplayVersion -eq '23H2') { Update-Dll @commonParams -InputPattern $patterns.Pattern -Replacement 'B8 00 01 00 00 89 81 38 06 00 00 90' } elseif ((Get-OSInfo).DisplayVersion -eq '24H2') { Update-Dll @commonParams -InputPattern $patterns.Win24H2 -Replacement 'B8 00 01 00 00 89 81 38 06 00 00 90 EB' } } 'Windows Server 2016' { Update-Dll @commonParams -InputPattern $patterns.Pattern -Replacement 'B8 00 01 00 00 89 81 38 06 00 00 90' } 'Windows Server 2022' { Update-Dll @commonParams -InputPattern $patterns.Pattern -Replacement 'B8 00 01 00 00 89 81 38 06 00 00 90' } 'Unsupported OS' { Write-Host 'Unable to get OS Version' } }
|