Last active
December 3, 2022 20:01
-
-
Save C0nw0nk/f3fe5541567bf93998d22dca64eeb185 to your computer and use it in GitHub Desktop.
Powershell Script to patch Chromedriver.exe for selenium to make it undetectable msedgedriver.exe and operadriver.exe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$regexA = 'cdc_.{22}'; | |
$ThisFile = 'C:\Users\MEDIA-SERVER\Desktop\chromedriver.exe' | |
#$ThisFile = 'C:\Users\MEDIA-SERVER\Desktop\msedgedriver.exe' #Yes this can also be patched | |
#$ThisFile = 'C:\Users\MEDIA-SERVER\Desktop\operadriver.exe' #Yes this can also be patched | |
$new_date="10/10/2000 13:37:02"; | |
if ( (Get-ChildItem $ThisFile).CreationTime -notlike $new_date ) { | |
Write-Output "Patching now"; | |
} else { | |
Write-Output "is already patched"; | |
Exit; | |
} | |
function Get-RandomCharacters($length, $characters) { | |
$random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length } | |
$private:ofs="" ; | |
return [String]$characters[$random]; | |
} | |
$random += Get-RandomCharacters -length 3 -characters 'abcdefghijklmnopqrstuvwxyz'; | |
$random = 'cdc_' + $random; | |
$randomupper = Get-RandomCharacters -length 1 -characters 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$randomtwo = Get-RandomCharacters -length 12 -characters 'abcdefghijklmnopqrstuvwxyz'; | |
$randomuppertwo = Get-RandomCharacters -length 2 -characters 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$randomthree = Get-RandomCharacters -length 4 -characters 'abcdefghijklmnopqrstuvwxyz'; | |
$output = $random += $randomupper += $randomtwo += $randomuppertwo += $randomthree | |
Write-Output "New cdc string is : $output" | |
Get-ChildItem $ThisFile | ForEach-Object { | |
$c = (Get-Content $_.FullName -Raw) | |
if ($c -match $regexA) { | |
$existing_cdc = $matches[0] | |
Write-Output "Existing cdc to be replaced: $existing_cdc" | |
} | |
} | |
# To compensate for a difference between Windows PowerShell and PowerShell (Core) 7+ | |
# with respect to how byte processing is requested: -Encoding Byte vs. -AsByteStream | |
$byteEncParam = | |
if ($IsCoreCLR) { @{ AsByteStream = $true } } | |
else { @{ Encoding = 'Byte' } } | |
# Read the file *as a byte array*. | |
$data = Get-Content @byteEncParam -ReadCount 0 $ThisFile | |
# Convert the array to a "hex string" in the form "nn-nn-nn-...", | |
# where nn represents a two-digit hex representation of each byte, | |
# e.g. '41-42' for 0x41, 0x42, which, if interpreted as a | |
# single-byte encoding (ASCII), is 'AB'. | |
$dataAsHexString = [BitConverter]::ToString($data) | |
# Define the search and replace strings, and convert them into | |
# "hex strings" too, using their UTF-8 byte representation. | |
$search = $existing_cdc | |
$replacement = $output | |
$searchAsHexString = [BitConverter]::ToString([Text.Encoding]::UTF8.GetBytes($search)) | |
$replaceAsHexString = [BitConverter]::ToString([Text.Encoding]::UTF8.GetBytes($replacement)) | |
# Perform the replacement. | |
$dataAsHexString = $dataAsHexString.Replace($searchAsHexString, $replaceAsHexString) | |
# Convert he modified "hex string" back to a byte[] array. | |
$modifiedData = [byte[]] ($dataAsHexString -split '-' -replace '^', '0x') | |
# Save the byte array back to the file. | |
Set-Content @byteEncParam $ThisFile -Value $modifiedData | |
(Get-ChildItem $ThisFile).CreationTime = $new_date; | |
(Get-ChildItem $ThisFile).LastWriteTime = $new_date; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment