javascript - 当您将 Selenium 与 chromedriver 一起使用时,网站可以检测到吗?

标签 javascript python google-chrome selenium selenium-chromedriver

我一直在用 Chromedriver 测试 Selenium,我注意到有些页面可以检测到您正在使用 Selenium,即使根本没有自动化。即使我只是通过 Selenium 和 Xephyr 使用 Chrome 手动浏览,我也经常得到一个页面,说检测到可疑事件。我检查了我的用户代理和浏览器指纹,它们都与普通的 Chrome 浏览器完全相同。

当我在普通 Chrome 中浏览这些网站时,一切正常,但在我使用 Selenium 的那一刻,我就被检测到了。

理论上,chromedriver 和 Chrome 在任何网络服务器上看起来应该完全一样,但它们可以通过某种方式检测到它。

如果你想要一些测试代码试试这个:

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=1, size=(1600, 902))
display.start()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--profile-directory=Default')
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-plugins-discovery");
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.delete_all_cookies()
driver.set_window_size(800,800)
driver.set_window_position(0,0)
print 'arguments done'
driver.get('http://stubhub.com')

如果您浏览 stubhub,您将在一两个请求中被重定向和“阻止”。我一直在对此进行调查,但无法弄清楚他们如何判断用户正在使用 Selenium。

他们是怎么做到的?

我在 Firefox 中安装了 Selenium IDE 插件,当我在普通的 Firefox 浏览器中仅使用附加插件访问 stubhub.com 时被禁止。

当我使用 Fiddler 时要查看来回发送的 HTTP 请求,我注意到“假浏览器”请求通常在响应 header 中包含“无缓存”。

这样的结果 Is there a way to detect that I'm in a Selenium Webdriver page from JavaScript 建议应该没有办法检测您何时使用 webdriver。但这一证据表明并非如此。

该网站将指纹上传到他们的服务器,但我检查了 Selenium 的指纹与使用 Chrome 时的指纹相同。

这是他们发送到服务器的指纹有效负载之一:

{"appName":"Netscape","platform":"Linuxx86_64","cookies":1,"syslang":"en-US","userlang":"en-
US","cpu":"","productSub":"20030107","setTimeout":1,"setInterval":1,"plugins":
{"0":"ChromePDFViewer","1":"ShockwaveFlash","2":"WidevineContentDecryptionMo
dule","3":"NativeClient","4":"ChromePDFViewer"},"mimeTypes":
{"0":"application/pdf","1":"ShockwaveFlashapplication/x-shockwave-
flash","2":"FutureSplashPlayerapplication/futuresplash","3":"WidevineContent
DecryptionModuleapplication/x-ppapi-widevine-
cdm","4":"NativeClientExecutableapplication/x-
nacl","5":"PortableNativeClientExecutableapplication/x-
pnacl","6":"PortableDocumentFormatapplication/x-google-chrome-
pdf"},"screen":{"width":1600,"height":900,"colorDepth":24},"fonts":
{"0":"monospace","1":"DejaVuSerif","2":"Georgia","3":"DejaVuSans","4":"Trebu
chetMS","5":"Verdana","6":"AndaleMono","7":"DejaVuSansMono","8":"LiberationM
ono","9":"NimbusMonoL","10":"CourierNew","11":"Courier"}}

在 Selenium 和 Chrome 中是相同的。

VPN 只能一次性使用,但在我加载第一页后就会被检测到。显然,正在运行一些 JavaScript 来检测 Selenium。

最佳答案

基本上,Selenium 检测的工作方式是,它们测试使用 Selenium 运行时出现的预定义 JavaScript 变量。 bot 检测脚本通常会在任何变量(在窗口对象上)中查找包含单词“selenium”/“webdriver”的任何内容,并且还会记录名为 $cdc_$wdc_ 的变量>。当然,所有这些都取决于您使用的浏览器。所有不同的浏览器都暴露了不同的东西。

对我来说,我使用的是 Chrome,所以,我所要做的就是确保 $cdc_ 不再作为文档变量存在,然后瞧(下载chromedriver源码,修改chromedriver重新编译$cdc_改名)

这是我在chromedriver中修改的函数:

文件call_function.js:

function getPageCache(opt_doc) {
  var doc = opt_doc || document;
  //var key = '$cdc_asdjflasutopfhvcZLmcfl_';
  var key = 'randomblabla_';
  if (!(key in doc))
    doc[key] = new Cache();
  return doc[key];
}

(注意注释。我所做的只是将 $cdc_ 转换为 randomblabla_。)

这里是伪代码,演示了僵尸网络可能使用的一些技术:

runBotDetection = function () {
    var documentDetectionKeys = [
        "__webdriver_evaluate",
        "__selenium_evaluate",
        "__webdriver_script_function",
        "__webdriver_script_func",
        "__webdriver_script_fn",
        "__fxdriver_evaluate",
        "__driver_unwrapped",
        "__webdriver_unwrapped",
        "__driver_evaluate",
        "__selenium_unwrapped",
        "__fxdriver_unwrapped",
    ];

    var windowDetectionKeys = [
        "_phantom",
        "__nightmare",
        "_selenium",
        "callPhantom",
        "callSelenium",
        "_Selenium_IDE_Recorder",
    ];

    for (const windowDetectionKey in windowDetectionKeys) {
        const windowDetectionKeyValue = windowDetectionKeys[windowDetectionKey];
        if (window[windowDetectionKeyValue]) {
            return true;
        }
    };
    for (const documentDetectionKey in documentDetectionKeys) {
        const documentDetectionKeyValue = documentDetectionKeys[documentDetectionKey];
        if (window['document'][documentDetectionKeyValue]) {
            return true;
        }
    };

    for (const documentKey in window['document']) {
        if (documentKey.match(/\$[a-z]dc_/) && window['document'][documentKey]['cache_']) {
            return true;
        }
    }

    if (window['external'] && window['external'].toString() && (window['external'].toString()['indexOf']('Sequentum') != -1)) return true;

    if (window['document']['documentElement']['getAttribute']('selenium')) return true;
    if (window['document']['documentElement']['getAttribute']('webdriver')) return true;
    if (window['document']['documentElement']['getAttribute']('driver')) return true;

    return false;
};

According to user szx ,也可以直接在十六进制编辑器中打开chromedriver.exe,手动替换,无需实际编译。

关于javascript - 当您将 Selenium 与 chromedriver 一起使用时,网站可以检测到吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225947/

相关文章:

python - 从两个可变长度字符串数组返回相似度矩阵(scipy 选项?)

python - 通过 imp.load_source 加载具有相同名称的模块导致模块合并

python - 用手工计算还是程序计算来测试?

javascript - 过渡开始得太早,而 css 样式尚未完全应用

css - 谷歌浏览器错误 - 溢出 :auto | scroll doesn't work

javascript - 在 JavaScript 中解析查询字符串

javascript - 指令范围内的函数不需要被触发

javascript - 仅使用 JavaScript 选择 NAV 元素时如何更改其样式?

javascript - 我需要一些关于 javascript 对象的指导

c# - 调整 Web 浏览器大小时调试 session 在 visual studio 中意外结束