修改$cdc_特征字符串后ChromeDriver启动崩溃,求原因
问题描述
我通过Java代码修改ChromeDriver中的$cdc_asdjflasutopfhvcZLmcfl_字符串为$abc_asdjflasutopfhvcZLmcfl_,试图规避Selenium自动化检测。diff验证显示只有该字符串变更,但补丁后运行Selenium时ChromeDriver崩溃,报错如下:
org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. Host info: host: 'Nikolas-MacBook-Pro-2.local', ip: 'fe80:0:0:0:10fc:9205:ef69:8927%en0' at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:561) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:229) at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:157) at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:101) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:82) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:71) at org.example.PatchedChromeDriver.main(PatchedChromeDriver.java:60) Caused by: org.openqa.selenium.WebDriverException: Driver server process died prematurely. Build info: version: '4.8.1', revision: '8ebccac989' System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '12.5', java.version: '17.0.5' Driver info: driver.version: PatchedChromeDriver at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:244) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:98) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543) ... 6 more
使用的补丁方法如下:
private static void patchDriver(String chromeDriverPath) throws IOException { Path executablePath = Paths.get(chromeDriverPath); byte[] fileContent = Files.readAllBytes(executablePath); // Convert the byte array to a character array char[] charBuffer = new char[fileContent.length]; for (int i = 0; i < fileContent.length; i++) { charBuffer[i] = (char) fileContent[i]; } // Replace the tracker pattern with the replacement string String content = new String(charBuffer); System.out.println(content); content = content.replace("$cdc_asdjflasutopfhvcZLmcfl_", "$abc_asdjflasutopfhvcZLmcfl_"); System.out.println(content); charBuffer = content.toCharArray(); fileContent = new byte[charBuffer.length]; for (int i = 0; i < charBuffer.length; i++) { fileContent[i] = (byte) charBuffer[i]; } // Write the patched content to the executable path Files.write(executablePath, fileContent); }
未打补丁时代码运行正常,打补丁后崩溃,请问原因是什么?
问题原因与解决方案
核心原因
你的补丁方法错误地将二进制可执行文件当作文本字符串处理,直接破坏了ChromeDriver的可执行结构,具体问题包括:
- 字节-字符转换的信息丢失:ChromeDriver是二进制文件,包含大量非ASCII字节(如机器指令、二进制数据)。将
byte强制转成char再转回byte时,大于127的字节会被截断或编码错误,导致可执行文件的核心结构损坏。 - 编码不一致问题:用
String处理二进制内容时,依赖JVM默认编码(如UTF-8),会把二进制字节错误解码为字符,再编码回字节时完全改变原有内容——哪怕只替换了目标字符串,整个文件的二进制完整性已经被破坏。
正确的补丁方法
直接在字节数组层面匹配并替换目标字符串,避免任何字符编码转换。由于你要替换的字符串长度完全一致($cdc_和$abc_长度相同,整段目标字符串长度不变),不会改变文件大小,也不会破坏其他二进制内容:
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.charset.StandardCharsets; private static void patchDriver(String chromeDriverPath) throws IOException { Path executablePath = Paths.get(chromeDriverPath); byte[] fileContent = Files.readAllBytes(executablePath); // 将目标字符串和替换字符串转为ASCII字节数组(目标字符串为纯ASCII) byte[] targetBytes = "$cdc_asdjflasutopfhvcZLmcfl_".getBytes(StandardCharsets.US_ASCII); byte[] replaceBytes = "$abc_asdjflasutopfhvcZLmcfl_".getBytes(StandardCharsets.US_ASCII); // 在字节数组中查找目标序列并替换 for (int i = 0; i <= fileContent.length - targetBytes.length; i++) { boolean match = true; for (int j = 0; j < targetBytes.length; j++) { if (fileContent[i + j] != targetBytes[j]) { match = false; break; } } if (match) { System.arraycopy(replaceBytes, 0, fileContent, i, replaceBytes.length); // 若只需替换第一个匹配项,可在此处添加break // break; } } // 写入修补后的字节数组 Files.write(executablePath, fileContent); }
补充说明
- 必须使用US_ASCII编码转换字符串,确保生成的字节序列和ChromeDriver二进制中的目标字符串完全一致。
- 替换后文件大小不变,可执行文件结构未被破坏,ChromeDriver才能正常启动。
内容的提问来源于stack exchange,提问作者user3362334
相关产品推荐
相关产品推荐

