如何从 Java 中的 BotD 中隐藏 Geckodriver 中的 WebDriver?

Posted

技术标签:

【中文标题】如何从 Java 中的 BotD 中隐藏 Geckodriver 中的 WebDriver?【英文标题】:How to Conceal WebDriver in Geckodriver from BotD in Java? 【发布时间】:2022-01-02 05:00:32 【问题描述】:

我关注this post on *** 禁用Firefox WebDriver 检测。

启动 Geckodriver:

System.setProperty("webdriver.gecko.driver", geckdriverExecutableFilePath);



File firefoxProfileFile = new File(fullPathOfFirefoxInstallationFolder);



FirefoxProfile firefoxProfile = null;

        
 try 
    firefoxProfile = new FirefoxProfile(firefoxProfileFile);
      catch (Exception e) 

    e.printStackTrace();
     

我禁用了WebDriver

WebDriver Disabled

FirefoxOptions firefoxOptions = new FirefoxOptions();

firefoxOptions.setProfile(firefoxProfile);

// Disables WebRTC
firefoxProfile.setPreference("media.peerconnection.enabled", false);

我禁用了自动化扩展:

Automation Extension Disabled

// Disables Automation Extension
firefoxProfile.setPreference("useAutomationExtension", false);

我添加了代理:

    DesiredCapabilities dc = DesiredCapabilities.firefox();
    Proxy proxy = new Proxy();
    proxy.setHttpProxy(ipAddress + ":" + port);
    proxy.setFtpProxy(ipAddress + ":" + port);
    proxy.setSslProxy(ipAddress + ":" + port);

   dc.setCapability(CapabilityType.PROXY, proxy);




   firefoxOptions.merge(dc);

   driver = new FirefoxDriver(firefoxOptions);

然而BotD 仍然检测到我的浏览器被自动化工具控制。

BotD Detection

我该如何解决这个问题?

【问题讨论】:

用你的代码试验更新问题。 @DebanjanB 代码已更新。 您想用 Python 评估答案吗? @DebanjanB 如果代码可以轻松移植到 Java,python 就可以了。意思是,如果代码使用了一些仅在 python 中可用的特殊函数,那么它对我来说就没有用了,因为我的整个代码库都是用 Java 编写的。提前感谢您的回复!你似乎是这个论坛上关于 Selenium 知识最渊博的用户 :) 【参考方案1】:

当使用Selenium驱动GeckoDriver发起firefox时浏览上下文

当用户代理处于远程控制之下时,webdriver-active flag 设置为 true。最初是false

如果设置了 webdriver-active 标志,webdriver 返回 true,否则返回 false

作为:

navigator.webdriver 定义了一种标准方式,用于协作用户代理通知文档它由 WebDriver 控制,例如 示例,以便可以在期间触发备用代码路径 自动化。

在他的comments 中进一步确认@whimboo

此实现必须符合此要求。因此 我们不会提供规避方法。


结论

所以,底线是:

Selenium 自我识别

并且无法隐藏浏览器是WebDriver驱动的事实。


建议

然而,一些权威人士提出了一些不同的方法,这些方法可以隐藏 Mozilla Firefox 浏览器是通过使用 Firefox ProfilesProxies 来控制 WebDriver 的事实,如下所示:

selenium4 兼容python 代码

from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

profile_path = r'C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\s8543x41.default-release'
options=Options()
options.set_preference('profile', profile_path)
options.set_preference('network.proxy.type', 1)
options.set_preference('network.proxy.socks', '127.0.0.1')
options.set_preference('network.proxy.socks_port', 9050)
options.set_preference('network.proxy.socks_remote_dns', False)
service = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = Firefox(service=service, options=options)
driver.get("https://www.google.com")
driver.quit()

潜在解决方案

一个潜在的解决方案是使用tor 浏览器,如下所示:

selenium4 兼容python 代码

from selenium.webdriver import Firefox  
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
import os

torexe = os.popen(r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe')
profile_path = r'C:\Users\username\Desktop\Tor Browser\Browser\TorBrowser\Data\Browser\profile.default'
firefox_options=Options()
firefox_options.set_preference('profile', profile_path)
firefox_options.set_preference('network.proxy.type', 1)
firefox_options.set_preference('network.proxy.socks', '127.0.0.1')
firefox_options.set_preference('network.proxy.socks_port', 9050)
firefox_options.set_preference("network.proxy.socks_remote_dns", False)
firefox_options.binary_location = r'C:\Users\username\Desktop\Tor Browser\Browser\firefox.exe'
service = Service('C:\\BrowserDrivers\\geckodriver.exe')
driver = webdriver.Firefox(service=service, options=firefox_options)
driver.get("https://www.tiktok.com/")

参考

您可以在

中找到一些相关的详细讨论 How to initiate a Tor Browser 9.5 which uses the default Firefox to 68.9.0esr using GeckoDriver and Selenium through Python How to connect to Tor browser using Python How to use Tor with Chrome browser through Selenium

【讨论】:

赞成使用 TOR 添加解决方案并开箱即用。 +1 对您将 TOR 与 Firefox 捆绑的建议印象深刻。 SO suggested 上的用户可以修改 chromedriver 源代码以删除任何机器人识别属性。是否可以通过修改 Geckodriver 或 Firefox WebDriver 源代码来实现与 Firefox 类似的结果?然后我们可以删除机器人识别而不需要将 TOR 与 Firefox 捆绑在一起吗?您提到了 Selenium 标识自己,但我们当然可以修改源代码以删除类似于在 chrome 驱动程序中完成的标识? @BradfordGriggs 如果您能提出问题,我很乐意发布答案。 @DebanjanB 我以 +50 赏金奖励您的回复。我提出了另一个关于removing bot identifying features from Geckodriver / WebDriver source code 的问题。如果你能想出解决这个问题的办法,我会再增加一个 +50 赏金!【参考方案2】:

BotD 检测到您,因为您没有覆盖 navigator.webdriver 属性。

我可以用这段代码覆盖它:

((javascriptExecutor)driver).executeScript("Object.defineProperty(navigator, 'webdriver', get: () => undefined)");

driver.get("BotD url") 之后使用这一行重新运行您的代码,然后单击 “开始检测”在 BotD 页面上。

它将不再显示检测到 webdriver。

我了解到您正在寻找一种方法来使其在初始页面加载之前工作。

但这里有两件事需要考虑:

    Webdriver 开发人员希望他们的工具能够被浏览器检测到。 Gecko 驱动程序开发人员不会实现禁用navigator.webdriver 属性的选项。 (This是壁虎开发者的官方回复。)

【讨论】:

我了解到您正在寻找一种在初始页面加载之前使其工作的方法 正确。页面已加载后的覆盖属性没有任何价值,因为浏览器已被检测为机器人。我知道 Gecko 驱动程序开发人员本身并没有包含此选项,但我相信如果我们修改底层代码,它仍然可以实现这一点 - 类似于使用 Chrome 实现它的方式。 所以您正在寻找一些关于如何破解驱动程序的建议?如果有 Chrome,为什么您不想切换到 Chrome,您需要什么? 因为我的整个代码库(数千行代码)都是为 Firefox 编写的。将来我希望增加对 Chrome 的支持,但无论哪种情况,我都不想因为无法解决一个问题而放弃使用 Firefox。我希望找到解决这个问题的方法..一定有办法..

以上是关于如何从 Java 中的 BotD 中隐藏 Geckodriver 中的 WebDriver?的主要内容,如果未能解决你的问题,请参考以下文章

判断标签页显示隐藏(visibilitychange事件)

如何从视图中隐藏iphone中的标题栏(顶部栏)[重复]

从 Mac/Linux Dock 中隐藏 Java 应用程序

如何仅从屏幕阅读器隐藏页面中的任何元素,而不是普通用户的页面?

如何使用导航从其他片段隐藏主机活动中的视图

如何从 PHP 中的文本字符串中删除隐藏字符?