Those ideas are basically based on my experience. I also referred to some existing posts in Chinese language in that the commercial web security in China is quite usual and high-level. However, it’s my bad and it’s being a while, so I couldn’t find all original website URLs as references.
Encrypted ID
Easy to implement.
Usually the URL patter looks like:
https://xxxx/id=123
To crawl this site, I usually define a for loop to go through the whole ID range.
In this way, I don’t even need to crawl a list page because the ID has its pattern for me to use.
To encrypt the ID, it’s easy to implement by performing a server-side query decryption. The encryption and decryption algorithms can vary, but basically it can be any secure symmetric or asymmetric encryption algorithm. Therefore the URL will look like:
https://xxxx/id=3d030632fb3c9653
The benefit here is that this won’t slow down the database query because finally in SQL query,
the id is recovered from 3d030632fb3c9653 to 123.
It is safe because the encryption key used on server can be very long and not easy to guess. Also, do not leave database related info on the web page. This can secure the algorithm to some extent.
BTW, I saw an interesting one:
http://xxx/pm/ZGphbmdvIOWPjeeIrOiZqw==.html
The URL is too obvious to use Base64, and I can easily decode it. It’s not recommended to encrypt ID.
However, using it to encrypt key is fine, since 3d030632fb3c9653 is coded to PQMGMvs8llM= which is shorter:
https://xxxx/id=PQMGMvs8llM=
Dynamic Key Request Chain
Hard to implement.
Define a rule that how pages can jump from one to another, each jump requires a key provided by server. For example:
https://xxxx/id=3d030632fb3c9653&key=a817f6s9
This key can be a chain encryption, which can be transformed back to Http Header: Referrer page URL or an identifier.
The server will verify this key and try to recover the coming URL.
If the coming URL meets the rule we defined at the beginning, this request is allows to proceed;
otherwise HTTP 400 or fake data, etc.
It has a benefit as well, the URL can be copied and still work because the request is actually static.
Fake Data
Difficult to implement.
Use some similar data, when in bad request mentioned in dynamic key request chain section.
The page can be a robot-generate page with information from a machine learning model or even some simple algorithms to generate interesting correct-look page.
This method can frustrate crawler writers, AHAHAHAHAHAHA.
Also, each bad request should return a certain data. Otherwise, if the crawler requests twice, it can easily find the data is dynamically generated fake data.
Page Rendering after Loaded
Easy to implement.
Using AJAX or simply js codes to write data into web page.
This is like some email encoders used for anti-email-spam.
The web page will contain such codes:
<script type="text/javascript">
//<![CDATA[
<!--
var x="function f(x){var i,o=\"\",ol=x.length,l=ol;while(x.charCodeAt(l/13)!" +
"=49){try{x+=x;l+=l;}catch(e){}}for(i=l-1;i>=0;i--){o+=x.charAt(i);}return o" +
".substr(0,ol);}f(\")621,\\\"v|pckv&iy<;:771\\\\bcFED130\\\\f400\\\\][B\\\\\\"+
"\\@320\\\\020\\\\mHWV020\\\\XHOCJRU230\\\\\\\\\\\\[Z330\\\\Okrt}v8E%qsg|3s-" +
"2'`ai771\\\\c{771\\\\)rkanwbo230\\\\\\\"(f};o nruter};))++y(^)i(tAedoCrahc." +
"x(edoCrahCmorf.gnirtS=+o;721=%y;2=*y))y+621(>i(fi{)++i;l<i;0=i(rof;htgnel.x" +
"=l,\\\"\\\"=o,i rav{)y,x(f noitcnuf\")" ;
while(x=eval(x));
//-->
//]]>
</script>
After its execution, it becomes HTML codes.
This method can prevent simple HTTP Request crawlers, because the final web page should be run to reveal data.
However, if the crawler writer is using NodeJS, this would not be too hard.
In this case, you might need to obfuscate your JS codes before the web page is sent back.
Cookie Expiration Strategy
Easy to implement.
Simple set a expiration time of one hour if the cookie is not used in request. This can add one step for crawler writers that they must analyse your cookie expiration strategy.
Artificial Intelligence reCAPTCHA
Easy to implement.
However, all steps above cannot prevent selenium which is a headless browser driver engine.
Traditional chatcha is not robust enough nowadays, crawlers can use machine learning to break captcha more easily.
I’ve tried to break Google reCaptcha service and Alibaba slide-to-unlock service last month using selenium but failed.
The last step that I haven’t used is using cursor simulator which records my cursor movement and replay is on web page.
Basically, this is a super-difficult-to-break. Although it somehow reduces user experience’s fluency, it protects the website securely from crawlers.
Therefore, for pages that user is expected to stay long, it’s recommended to use this method.
For Chinese users, Google’s service does not exist. Therefore when the server detects Chinese IP,
it’s recommended to use an alternate GeeTest.
U/A Blocking
Easy to implement.
Some known user agents of crawlers should be obviously blocked or return with fake data. These user agents should be blocked:
FeedDemon
Indy Library
Alexa Toolbar
AskTbFXTV
AhrefsBot
CrawlDaddy
CoolpadWebkit
Java
Feedly
UniversalFeedParser
ApacheBench
Microsoft URL Control
Swiftbot
ZmEu
oBot
jaunty
Python-urllib
lightDeckReports Bot
YYSpider
DigExt
HttpClient
MJ12bot
heritrix
EasouSpider
Ezooms
Prevent Search Engine Using robot.txt
Not too easy to implement.
Search engines can be bad as well. If you have data to be protected,
write the rules in robot.txt to tell search engine not to crawl some of your pages.
Split Deployed Codes
Difficult to implement.
You can write a tool splitting a pending-to-respond html file into several random small parts and use AJAX to load.
Using iframe is also a good idea to increase the difficulty using selenium.
This idea is to increase the complexity of web page and the analysis time, however, it increases the coding efforts as well.
Banned IP Pool
Better not to use.
Open proxy IPs can be banned (this requires us to crawl IP proxy websites LOL), but try not to ban an IP entirely because the 4G network is a dynamic environment and it’s difficult to tell whether this user is from cable or not.
Never ban an IP easily.
After reCaptcher, there’s not need to prevent crawler because the cost is too high. See what this project will finally go~ Maybe this idea became a back-end framework :P idk
下面这些想法基本源于我自己的经验。我也参考了一些现有的中文技术文章,毕竟国内的商业 Web 安全对抗非常普遍,水准也很高。 不过不好意思,因为过了一段时间了,当初参考的原文链接我都找不全了。
加密 ID
容易实现。
通常 URL 的格式长这样:
https://xxxx/id=123
要是让我来爬这种网站,我一般直接写个 for 循环把整个 ID 范围遍历一遍就行了。
这样连列表页都不用爬,因为 ID 本身就有规律可循。
要对 ID 进行加密其实很容易,只要在服务端查询时解密一下就行。 加解密算法有很多种,基本上任何安全的对称或非对称加密算法都可以。 这样一来,URL 就会变成:
https://xxxx/id=3d030632fb3c9653
这么做的好处在于不会拖慢数据库查询,因为最终执行 SQL 查询时,ID 已经从 3d030632fb3c9653 还原成 123 了。
它也很安全,因为服务端使用的密钥可以设得很长,不容易被猜到。 另外,千万别在网页上暴露任何与数据库相关的信息,这也能在一定程度上保护你的算法。
顺便一提,我还见过一个很有意思的例子:
http://xxx/pm/ZGphbmdvIOWPjeeIrOiZqw==.html
这个 URL 用 Base64 也太明显了,随手就能解开,非常不推荐直接拿来加密 ID。
不过用它来编码加密后的密文倒是没问题,比如 3d030632fb3c9653 编完之后变成 PQMGMvs8llM=,还更短了:
https://xxxx/id=PQMGMvs8llM=
动态密钥请求链
较难实现。
先定义一套页面跳转规则,每次跳转都需要带上服务端下发的密钥。 例如:
https://xxxx/id=3d030632fb3c9653&key=a817f6s9
这个 key 可以采用链式加密,能够还原出 Http Header: Referrer 中的来源页面 URL 或某个标识符。
服务端会校验这个 key 并尝试还原出跳转前的 URL。
如果来源 URL 符合我们预先定义的规则,就放行请求;否则返回 HTTP 400 或者假数据之类的。
这种方案还有个额外好处:URL 复制出去后依然有效,因为从本质上讲这个请求还是静态的。
假数据
很难实现。
在前面动态密钥请求链一节提到的非法请求场景下,返回一些以假乱真的数据。
这类页面可以由程序自动生成,比如用机器学习模型,甚至简单的规则算法来拼出看起来有模有样、还挺有趣的页面。
这招能把写爬虫的人气到吐血,啊哈哈哈哈哈哈。
另外,同一个非法请求每次应该返回固定的一份假数据。不然爬虫只要多请求两次,一眼就能看出这是动态生成的假数据。
页面加载后再渲染
容易实现。
使用 AJAX 或者干脆用 JS 代码把数据写入网页。
这就跟以前防垃圾邮件的邮箱编码器很像。
网页里会包含类似下面这样的代码:
<script type="text/javascript">
//<![CDATA[
<!--
var x="function f(x){var i,o=\"\",ol=x.length,l=ol;while(x.charCodeAt(l/13)!" +
"=49){try{x+=x;l+=l;}catch(e){}}for(i=l-1;i>=0;i--){o+=x.charAt(i);}return o" +
".substr(0,ol);}f(\")621,\\\"v|pckv&iy<;:771\\\\bcFED130\\\\f400\\\\][B\\\\\\"+
"\\@320\\\\020\\\\mHWV020\\\\XHOCJRU230\\\\\\\\\\\\[Z330\\\\Okrt}v8E%qsg|3s-" +
"2'`ai771\\\\c{771\\\\)rkanwbo230\\\\\\\"(f};o nruter};))++y(^)i(tAedoCrahc." +
"x(edoCrahCmorf.gnirtS=+o;721=%y;2=*y))y+621(>i(fi{)++i;l<i;0=i(rof;htgnel.x" +
"=l,\\\"\\\"=o,i rav{)y,x(f noitcnuf\")" ;
while(x=eval(x));
//-->
//]]>
</script>
执行完这段脚本后,它就会变成真正的 HTML 代码。
这招能挡住简单的 HTTP Request 爬虫,因为必须执行脚本后才能看到真实数据。
不过,如果写爬虫的人用的是 NodeJS,破解起来就不算太难了。
这种情况下,你可能需要在下发网页之前先对 JS 代码做混淆处理。
Cookie 过期策略
容易实现。
很简单,如果一个 Cookie 超过一小时没有被请求使用,就让它过期。 这能给写爬虫的人多加一道坎,逼着他们不得不去分析你的 Cookie 过期机制。
人工智能 reCAPTCHA 验证码
容易实现。
然而,上面所有手段都防不住 selenium 这种无头浏览器驱动引擎。
传统的图形验证码如今已经不够结实了,爬虫用机器学习就能比较轻松地破解。
上个月我试过用 selenium 去破 Google 的 reCaptcha 和阿里的滑动验证,结果都失败了。
我还没试过的最后一招是用鼠标轨迹模拟器,把我的真实鼠标移动轨迹录下来再在网页上回放。
总体来说,这类验证码极难攻破。虽然会稍微降低一点用户体验的流畅度,但它能非常可靠地保护网站免受爬虫侵扰。
因此,对于预计用户会停留较长时间的页面,推荐使用这种方案。
对于中国大陆用户来说,Google 的服务是不可用的。所以当服务端检测到国内 IP 时,建议改用替代方案——极验(GeeTest)。
屏蔽 User-Agent(U/A)
容易实现。
对于一些已知的爬虫 User-Agent,显然应该直接拦截或者返回假数据。 下面这些 User-Agent 应该加入黑名单:
FeedDemon
Indy Library
Alexa Toolbar
AskTbFXTV
AhrefsBot
CrawlDaddy
CoolpadWebkit
Java
Feedly
UniversalFeedParser
ApacheBench
Microsoft URL Control
Swiftbot
ZmEu
oBot
jaunty
Python-urllib
lightDeckReports Bot
YYSpider
DigExt
HttpClient
MJ12bot
heritrix
EasouSpider
Ezooms
用 robot.txt 阻止搜索引擎抓取
不太容易实现。
搜索引擎有时候也会带来麻烦。如果你有需要保护的数据,可以在 robot.txt 里写明规则,告诉搜索引擎不要抓取你的某些页面。
拆分部署代码
很难实现。
你可以写个工具,把准备返回的 HTML 文件随机拆成好几小块,再用 AJAX 加载拼接。
搭配 iframe 也是个好主意,可以大幅增加使用 selenium 抓取的难度。
这个思路就是通过提高网页复杂度来拉长对方的分析时间,但同时也会增加我们自己的开发成本。
封禁 IP 池
最好别用。
公开的代理 IP 倒是可以封(当然这就需要我们自己去爬免费代理 IP 网站了哈哈),但尽量不要一刀切地封禁普通 IP,因为 4G 移动网络的 IP 是动态分配的,很难判断这个用户到底是不是宽带固定 IP。
千万别轻易封 IP。
上了 reCAPTCHA 之后,其实就没必要再折腾更多反爬手段了,因为继续投入的成本太高。 看看这个项目最后会做成什么样吧~ 说不定这个点子哪天就演变成一个后端框架了呢 :P 谁知道呢