【干货分享】使用selenium实现异步流量获取和反爬处理

admin 2023年1月11日13:29:07评论96 views字数 2298阅读7分39秒阅读模式


【干货分享】使用selenium实现异步流量获取和反爬处理

本文约2512字,阅读约需7分钟。

【干货分享】使用selenium实现异步流量获取和反爬处理


【干货分享】使用selenium实现异步流量获取和反爬处理

三个模拟浏览器操作库

【干货分享】使用selenium实现异步流量获取和反爬处理
【干货分享】使用selenium实现异步流量获取和反爬处理

selenium

【干货分享】使用selenium实现异步流量获取和反爬处理


是最广泛使用的开源Web UI(用户界面)自动化测试套件之一。它最初由杰森·哈金斯(Jason Huggins)于2004年开发,作为Thought Works的内部工具。Selenium支持跨不同浏览器,平台和编程语言的自动化。

Selenium通过使用特定于每种语言的驱动程序支持各种编程语言。Selenium支持的语言包括C#,Java,Perl,PHP,Python和Ruby


【干货分享】使用selenium实现异步流量获取和反爬处理

pyppeteer

【干货分享】使用selenium实现异步流量获取和反爬处理

Puppeteer 的 Python 版本的实现,在puppeteer之上稍微包装了下,Puppeteer 是 Google 基于 Node.js 开发的一个工具,主要是用来操纵 Chrome 浏览器的 API,通过 Javascript 代码来操纵 Chrome 浏览器的一些操作,用作网络爬虫完成数据爬取、Web 程序自动测试等任务,其 API 极其完善,功能非常强大

pyppeteer的web自动化是基于chromium来实现的,仅支持chromium 浏览器。同时它不是官方库,很久未维护了

【干货分享】使用selenium实现异步流量获取和反爬处理

对比

【干货分享】使用selenium实现异步流量获取和反爬处理

Pyppeteer在安装配置的便利性和运行效率方面都要远胜selenium,一些简单操作比selenium来的快

【干货分享】使用selenium实现异步流量获取和反爬处理

另一个Playwright

【干货分享】使用selenium实现异步流量获取和反爬处理

微软开发,有维护,新出,模拟人工操作,可直接根据操作生成代码

playwright-python,是一款基于python的自动化测试工具,可以通过录制功能自动生成测试脚本;Playwright是一个强大的Python库,仅用一个API即可自动执行Chromium、Firefox、WebKit(Safari )等主流浏览器自动化操作,并同时支持以无头模式、有头模式运行。


【干货分享】使用selenium实现异步流量获取和反爬处理

Selenium+Browsermobproxy实现异步流量获取

【干货分享】使用selenium实现异步流量获取和反爬处理

browsermobproxy是一个java开发的抓包工具,因此运行环境需要要java


【干货分享】使用selenium实现异步流量获取和反爬处理


开启流量代理

# 开启Proxy服务self._bmpserver = browsermobproxy.Server(self._BMP_path, options=self._BMP_options)self._bmpserver.start()# 开启对服务器信任选项 params={"trustAllServers": "true"}self._bmpclient = self._bmpserver.create_proxy(params={"trustAllServers": "true"})# 流量走代理监听option.add_argument("--proxy-server={}".format(self._bmpclient.proxy))

导出流量,都是以字典结构保存的

# 设置捕获请求头和内容、二进制内容 options={'captureHeaders': True, 'captureContent': True, 'captureBinaryContent':True}self._bmpclient.new_har(options={'captureHeaders': captureHeader, 'captureContent': captureContent})self._chrome_driver.get(target)self._chromeloads()
# 导出流量result = self._bmpclient.harfor entry in result['log']['entries']: _url = entry['request']['url']    print("请求地址:", _url)

使用属性来配置是否保留http的头和内容,都保留和burp一样,都不保留则只记录访问url信息

new_har(options={'captureHeaders': True, 'captureContent': True})


【干货分享】使用selenium实现异步流量获取和反爬处理


browsermobproxy会保存访问目标过程中产生的全部请求

【干货分享】使用selenium实现异步流量获取和反爬处理


selenium默认的网页加载模式是“网页全部资源加载”,但是面对某宇盾时,会误认为防护脚本(某个js页面)是目标网页,需要设置延时大于1秒来绕过,可以用某vnd站来测试


【干货分享】使用selenium实现异步流量获取和反爬处理



【干货分享】使用selenium实现异步流量获取和反爬处理

selenium的防反爬检测

【干货分享】使用selenium实现异步流量获取和反爬处理
【干货分享】使用selenium实现异步流量获取和反爬处理

stealth.min.js

【干货分享】使用selenium实现异步流量获取和反爬处理

主要以过https://bot.sannysoft.com/这个检测为目标

js文件获取

https://cdn.jsdelivr.net/gh/requireCool/stealth.min.js/stealth.min.js

如果失效在github或Google搜索找一下

用法很简便,使用execute_cdp_cmd执行js脚本即可


【干货分享】使用selenium实现异步流量获取和反爬处理


伪造效果


【干货分享】使用selenium实现异步流量获取和反爬处理

【干货分享】使用selenium实现异步流量获取和反爬处理

selenium_stealth

【干货分享】使用selenium实现异步流量获取和反爬处理

py的一个反反爬库

pip3 install selenium-stealth

同样使用简便,还可以直接自定义一些信息


【干货分享】使用selenium实现异步流量获取和反爬处理


效果


【干货分享】使用selenium实现异步流量获取和反爬处理


【干货分享】使用selenium实现异步流量获取和反爬处理

手动配置(不推荐)

【干货分享】使用selenium实现异步流量获取和反爬处理

使用execute_cdp_cmd去挨个配置选项值,如下配置webdriver属性

# 设置webdriverdriver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {    "source": """() => {const newProto = navigator.__proto__delete newProto.webdrivernavigator.__proto__ = newProto"""})

这种方式适用于一些专业的防反爬处理,性价比低,不推荐用

END

【干货分享】使用selenium实现异步流量获取和反爬处理


原文始发于微信公众号(哈拉少安全小队):【干货分享】使用selenium实现异步流量获取和反爬处理

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年1月11日13:29:07
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   【干货分享】使用selenium实现异步流量获取和反爬处理https://cn-sec.com/archives/1511602.html

发表评论

匿名网友 填写信息