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

1次阅读

共计 2053 个字符,预计需要花费 6 分钟才能阅读完成。

前言

验证码是阻挡机器人攻打的无效实际,网络爬虫,又被称为网络机器人,是依照肯定的规定,主动地抓取网络信息和数据的程序或者脚本。如何防控,这里简略提供几个小 Tips。

应用 nginx 的自带性能

通过对 httpuseragent 阻塞来实现,包含 GET/POST 形式的申请,以 nginx 为例。

回绝以 wget 形式的 httpuseragent,减少如下内容:

Block http user agent - wget
if ($http_user_agent ~* (Wget) ) {return 403;}

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

if ($http_user_agent ~ (agent1|agent2|Foo|Wget|Catall Spider|AcoiRobot) ) {return 403;}

限度 User-Agent 字段

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

限度 IP 或账号

依据业务需要,要求用户通过验证码后能力应用某些性能或权限。当同一 IP、同一设施在肯定工夫内拜访网站的次数,零碎主动限度其拜访浏览。只有在输出正确的验证码之后能力持续拜访。

验证码拦挡

在登录页等页面,增加验证码,以辨认是失常流量还是歹意爬虫,也是一种根本的操作。

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 代码:

from wsgiref.simple_server import make_server
from KgCaptchaSDK import KgCaptcha
def start(environ, response):
    # 填写你的 AppId,在利用治理中获取
    AppID = "xxx"
    # 填写你的 AppSecret,在利用治理中获取
    AppSecret = "xxx"
    request = KgCaptcha(AppID, AppSecret)
    # 填写应用服务域名,在利用治理中获取
    request.appCdn = "https://cdn.kgcaptcha.com"
    # 申请超时工夫,秒
    request.connectTimeout = 10
    # 用户 id/ 登录名 / 手机号等信息,当安全策略中的防控等级为 3 时必须填写
    request.userId = "kgCaptchaDemo"
    # 应用其它 WEB 框架时请删除 request.parse,应用框架提供的办法获取以下相干参数
    parseEnviron = request.parse(environ)
    # 前端验证胜利后颁发的 token,有效期为两分钟
    request.token = parseEnviron["post"].get("kgCaptchaToken", "")  # 前端 _POST["kgCaptchaToken"]
    # 客户端 IP 地址
    request.clientIp = parseEnviron["ip"]
    # 客户端浏览器信息
    request.clientBrowser = parseEnviron["browser"]
    # 去路域名
    request.domain = parseEnviron["domain"]
    # 发送申请
    requestResult = request.sendRequest()
    if requestResult.code == 0:
        # 验证通过逻辑解决
        html = "验证通过"
    else:
        # 验证失败逻辑解决
        html = f"{requestResult.msg} - {requestResult.code}"
    response("200 OK", [("Content-type", "text/html; charset=utf-8")])
    return [bytes(str(html), encoding="utf-8")]
httpd = make_server("0.0.0.0", 8088, start)  # 设置调试端口  http://localhost:8088/
httpd.serve_forever()

最初

SDK 开源地址:KgCaptcha (KgCaptcha) · GitHub,顺便做了一个演示:凯格行为验证码在线体验

正文完
 0