用户
 找回密码
 入住 CI 中国社区
搜索
查看: 184|回复: 0
收起左侧

恶意爬虫?能让恶意爬虫遁于无形的小Tips

[复制链接]
发表于 2023-4-7 16:01:46 | 显示全部楼层 |阅读模式
本帖最后由 宙哈哈 于 2023-4-7 16:03 编辑

前言
验证码是阻挡机器人攻击的有效实践,网络爬虫,又被称为网络机器人,是按照一定的规则,自动地抓取网络信息和数据的程序或者脚本。如何防控,这里简单提供几个小Tips。

1.jpg

使用nginx的自带功能
通过对httpuseragent阻塞来实现,包括GET/POST方式的请求,以nginx为例。

拒绝以wget方式的httpuseragent,增加如下内容:

  1. Block http user agent - wget

  2. if ($http_user_agent ~* (Wget) ) {

  3.     return 403;

  4. }
复制代码


如何拒绝多种httpuseragent,内容如下:

  1. if ($http_user_agent ~ (agent1|agent2|Foo|Wget|Catall Spider|AcoiRobot) ) {

  2.     return 403;

  3. }
复制代码


限制User-Agent字段
User-Agent字段能识别用户所使用的操作系统、版本、CPU、浏览器等信息,如果请求来自非浏览器,就能识别其为爬虫,阻止爬虫抓取网站信息。

限制IP或账号
根据业务需求,要求用户通过验证码后才能使用某些功能或权限。当同一IP、同一设备在一定时间内访问网站的次数,系统自动限制其访问浏览。只有在输入正确的验证码之后才能继续访问。

验证码拦截
在登录页等页面,添加验证码,以识别是正常流量还是恶意爬虫,也是一种基本的操作。

2.PNG

HTML代码:

HTML复制代码
<script src="captcha.js?appid=xxx"></script>
 
<script>
 
kg.captcha({
 
    // 绑定元素,验证框显示区域
 
    bind: "#captchaBox3",
 
    // 验证成功事务处理
 
    success: function(e) {
 
        console.log(e);
 
        document.getElementById('kgCaptchaToken').value = e['token']
 
    },
 
    // 验证失败事务处理
 
    failure: function(e) {
 
        console.log(e);
 
    },
 
    // 点击刷新按钮时触发
 
    refresh: function(e) {
 
        console.log(e);
 
    }
 
});
 
</script>
 
 
 
<div id="captchaBox3">载入中 ...</div>
 
<input type="hidden" name="kgCaptchaToken" value="" />
复制代码



Python代码:

  1. from wsgiref.simple_server import make_server

  2. from KgCaptchaSDK import KgCaptcha

  3. def start(environ, response):

  4.     # 填写你的 AppId,在应用管理中获取

  5.     AppID = &quot;xxx&quot;

  6.     # 填写你的 AppSecret,在应用管理中获取

  7.     AppSecret = &quot;xxx&quot;

  8.     request = KgCaptcha(AppID, AppSecret)

  9.     # 填写应用服务域名,在应用管理中获取

  10.     request.appCdn = &quot;https://cdn.kgcaptcha.com&quot;

  11.     # 请求超时时间,秒

  12.     request.connectTimeout = 10

  13.     # 用户id/登录名/手机号等信息,当安全策略中的防控等级为3时必须填写

  14.     request.userId = &quot;kgCaptchaDemo&quot;

  15.     # 使用其它 WEB 框架时请删除 request.parse,使用框架提供的方法获取以下相关参数

  16.     parseEnviron = request.parse(environ)

  17.     # 前端验证成功后颁发的 token,有效期为两分钟

  18.     request.token = parseEnviron[&quot;post&quot;].get(&quot;kgCaptchaToken&quot;, &quot;&quot;)  # 前端 _POST[&quot;kgCaptchaToken&quot;]

  19.     # 客户端IP地址

  20.     request.clientIp = parseEnviron[&quot;ip&quot;]

  21.     # 客户端浏览器信息

  22.     request.clientBrowser = parseEnviron[&quot;browser&quot;]

  23.     # 来路域名

  24.     request.domain = parseEnviron[&quot;domain&quot;]

  25.     # 发送请求

  26.     requestResult = request.sendRequest()

  27.     if requestResult.code == 0:

  28.         # 验证通过逻辑处理

  29.         html = &quot;验证通过&quot;

  30.     else:

  31.         # 验证失败逻辑处理

  32.         html = f&quot;{requestResult.msg} - {requestResult.code}&quot;

  33.     response(&quot;200 OK&quot;, [(&quot;Content-type&quot;, &quot;text/html; charset=utf-8&quot;)])

  34.     return [bytes(str(html), encoding=&quot;utf-8&quot;)]

  35. httpd = make_server(&quot;0.0.0.0&quot;, 8088, start)  # 设置调试端口  http://localhost:8088/

  36. httpd.serve_forever()
复制代码



最后
SDK开源地址:https://github.com/KgCaptcha,顺便做了一个演示:https://www.kgcaptcha.com/demo/

本版积分规则