Skip to content

порядок операций в скрипте для терминального сервера. #3

Description

@VasOleMil
#=====
#запуск скрипта из командного файла cmd, необходимы права администратора.
#PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& 'D:\Install\Programs\RDP\Patch\RDP_patch.ps1'"
#=====

# поиск шаблона в файле termsrv.dll
Write-Output 'Checking for version'
$dll_as_bytes = Get-Content c:\windows\system32\termsrv.dll -Raw -Encoding byte
$dll_as_text = $dll_as_bytes.forEach('ToString', 'X2') -join ' '
$patternregex = ([regex]'39 81 3C 06 00 00(\s\S\S){6}')
$patch = 'B8 00 01 00 00 89 81 38 06 00 00 90'
$checkPattern=Select-String -Pattern $patternregex -InputObject $dll_as_text

If ($checkPattern -ne $null) 
{
    Write-Output 'Patching...'
    # Остановить службы
    Stop-Service UmRdpService -Force
    Stop-Service TermService -Force
    # сделать копию файла и разрешений
    Copy-Item c:\windows\system32\termsrv.dll c:\windows\system32\termsrv.dll.copy
    $termsrv_dll_acl = Get-Acl c:\windows\system32\termsrv.dll
    # изменить разрешения
    takeown /f c:\windows\system32\termsrv.dll
    $new_termsrv_dll_owner = (Get-Acl c:\windows\system32\termsrv.dll).owner
    cmd /c "icacls c:\windows\system32\termsrv.dll /Grant $($new_termsrv_dll_owner):F /C"

    # модификация файла termsrv.dll
    $dll_as_text_replaced = $dll_as_text -replace $patternregex, $patch
    [byte[]] $dll_as_bytes_replaced = -split $dll_as_text_replaced -replace '^', '0x'
    Set-Content c:\windows\system32\termsrv.dll.patched -Encoding Byte -Value $dll_as_bytes_replaced
    # Сравним два файла 
    fc.exe /b c:\windows\system32\termsrv.dll.patched c:\windows\system32\termsrv.dll
    # замена оригинального файла
    Copy-Item c:\windows\system32\termsrv.dll.patched c:\windows\system32\termsrv.dll -Force
    # установка оригинальных разрешений
    Set-Acl c:\windows\system32\termsrv.dll $termsrv_dll_acl
    # Запустить службы
    Start-Service UmRdpService
    Start-Service TermService

    Write-Output 'The termsrv.dll file succefully patched'
}
Elseif (Select-String -Pattern $patch -InputObject $dll_as_text) 
{
    Write-Output 'The termsrv.dll file is already patched'
}
else 
{ 
    Write-Output "Version not supported "
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions