Powershell 修补 Chromedriver.exe 文件变得无法使用

问题描述 投票:0回答:2

因此,如果我用 Notepad++ 等替换 cdc 字符串,总共会出现大约 9 次,效果很好。

但由于某种原因,我的 powershell 代码使该文件无法使用。它替换了字符串,但不再能够执行。

$PSDefaultParameterValues['*:Encoding'] = 'utf8';
$regexA = 'cdc_.{22}';
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 'C:\Users\C0n\Desktop\chromedriver.exe' | ForEach-Object {
    $c = (Get-Content $_.FullName) -replace $regexA, $output -join "`r"
    $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $true
    [IO.File]::WriteAllText($_.FullName, $c, $Utf8NoBomEncoding)
}

这是文件中的 cdc 字符串

cdc_adoQpoasnfa76pfcZLmcfl
它被随机生成的字符串替换。

powershell cdc undetected-chromedriver
2个回答
1
投票

使用 Selenium (Python) 删除

cdp_props

js = '''let objectToInspect = window,
    result = [];
    while(objectToInspect !== null)
        { result = result.concat(Object.getOwnPropertyNames(objectToInspect));
        objectToInspect = Object.getPrototypeOf(objectToInspect); }
    result.forEach(p => p.match(/.+_.+_(Array|Promise|Symbol)/ig)
        &&delete window[p]&&console.log('removed',p))'''

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": js})

这将执行 js 脚本,该脚本在调用时会删除

cdp_props

参考:


1
投票

我的解决方案读取二进制文件并转换为可读的 UTF8 文本,然后再次以二进制形式写回。

$regexA = 'cdc_.{22}';
$ThisFile = 'C:\Users\C0n\Desktop\chromedriver.exe'

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
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.