在数据爬取和自动化测试过程中,人机验证(如滑块、点选、短信验证等)是常见的反爬手段。贝壳网(ke.com)作为国内领先的房产平台,其人机验证机制较为复杂,涉及前端JS加密、动态Token、行为检测等技术。本文将通过逆向分析贝壳网的人机验证JS加密逻辑,并给出Python实现方案,帮助开发者绕过验证机制,实现高效数据采集。
贝壳网的人机验证通常包括以下几种形式:
本文重点分析动态Token加密逻辑,即前端如何生成加密参数(如 token
、signature
等),并模拟该过程实现自动化绕过。
/api/captcha/verify
)。关键参数识别 通常包含以下字段:
{
"token": "xxxxxx",
"signature": "yyyyyy",
"timestamp": 1234567890,
"behavior_data": "{...}"
}
这些参数由前端JS生成,需逆向其加密逻辑。
token
、signature
、encrypt
)。假设发现以下关键函数:
function generateToken() {
var e = Date.now();
var t = encryptAES(e + "|" + Math.random());
return t;
}
说明 token
由 AES加密(时间戳|随机数)
生成。
encryptAES
函数,提取密钥和加密模式(如 CBC
、PKCS7Padding
)。逆向完成后,用Python实现相同的加密逻辑。以下是一个示例:
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
import base64
import time
import random
def encrypt_aes(data, key, iv):
cipher = AES.new(key.encode(), AES.MODE_CBC, iv.encode())
padded_data = pad(data.encode(), AES.block_size)
encrypted = cipher.encrypt(padded_data)
return base64.b64encode(encrypted).decode()
# 贝壳网AES加密参数(需根据实际逆向结果调整)
KEY = "xxxxxx" # 替换为实际密钥
IV = "yyyyyy" # 替换为实际IV
def generate_token():
timestamp = int(time.time() * 1000)
rand_num = random.random()
raw_data = f"{timestamp}|{rand_num}"
token = encrypt_aes(raw_data, KEY, IV)
return token
import requests
def get_captcha_params():
url = "https://ke.com/api/captcha/get"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
response = requests.get(url, headers=headers)
return response.json()
def submit_captcha(token, signature):
url = "https://ke.com/api/captcha/verify"
data = {
"token": token,
"signature": signature,
"timestamp": int(time.time() * 1000)
}
response = requests.post(url, json=data, headers=headers)
return response.json()
# 1. 获取验证参数
captcha_data = get_captcha_params()
# 2. 生成加密Token
token = generate_token()
# 3. 计算签名(假设signature由HMAC-SHA256生成)
import hmac
import hashlib
def generate_signature(token, secret_key):
return hmac.new(secret_key.encode(), token.encode(), hashlib.sha256).hexdigest()
SECRET_KEY = "zzzzzz" # 替换为实际密钥
signature = generate_signature(token, SECRET_KEY)
# 4. 提交验证
result = submit_captcha(token, signature)
print("验证结果:", result)
本文通过逆向分析贝壳网人机验证的JS加密逻辑,提取了AES加密和HMAC签名算法,并用Python模拟生成合法参数。该方法适用于需要绕过动态Token验证的爬虫场景,但需注意:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。