过程比较艰辛,具体看代码吧
python
"""
框架源代码,源代码
前端外层一个html,内层嵌套一个
1. 找到未加密的参数
2. 想办法把参数加密(必须参考网易的逻辑),params,encSeckey; 都是window.arsea函数加密的
3. 请求到网易,拿到评论信息
F12查看发起程序的请求堆栈, 自下往上越来越新
js代码可能经过压缩,加密,变量名替换
看到有个send,打断点,一步步找
"""
import requests
url = 'https://music.163.com/weapi/comment/resource/comments/get?csrf_token='
# 原始数据,请求方式是post
data = {
"rid": "R_SO_4_1869390986",
"threadId": "R_SO_4_1869390986",
"pageNo": "1",
"pageSize": "20",
"cursor": "-1",
"offset": "0",
"orderType": "1",
"csrf_token": ""
}
# 处理加密过程
"""
function a(a = 16) {
var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = "";
for (d = 0; a > d; d += 1)
e = Math.random() * b.length, # 循环16次,随机数,取整,取字符串中的位置
e = Math.floor(e),
c += b.charAt(e);
return c
}
function b(a, b) { # a是要加密的内容,e是数据, AES(CBC)加密
var c = CryptoJS.enc.Utf8.parse(b) # b是秘钥
, d = CryptoJS.enc.Utf8.parse("0102030405060708")
, e = CryptoJS.enc.Utf8.parse(a)
, f = CryptoJS.AES.encrypt(e, c, {
iv: d,
mode: CryptoJS.mode.CBC
});
return f.toString()
}
function c(a, b, c) {
var d, e;
return setMaxDigits(131),
d = new RSAKeyPair(b,"",c),
e = encryptedString(d, a)
}
function d(d, e, f, g) { d:数据,e:010001, f:很长, g:定值
var h = {}
, i = a(16); # 16位随机值
return h.encText = b(d, g), # g是秘钥
h.encText = b(h.encText, i), # i是秘钥
h.encSecKey = c(i, e, f), # 得到encSeckey,e和f是定死的, 如果把i固定那得到的encSeckey就是定值
h
# h 塞点东西然后返回
# encText两次加密,数据+g => b => 第一次加密+i => b => params
}
function e(a, b, d, e) {
var f = {};
return f.encText = c(a + e, b, d),
f
}
"""
# 服务于D的
e = "010001"
f = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
g = '0CoJUm6Qyw8W8jud'
i = "CSc0ukfzGqLt3kmr" # 手动固定的,但人家函数中是随机的
ttt = {
"encText": "U7Udb8ATi4zMsjVtcCiBTuofmEU/BLLb8VYst3XQxGoENrmhp9aPbB5OMRmqab6KWsJ7XTDjlYvjbmMjHN5mB5lPTmsr/oRdNCNVlI5DtTXs/0Ll5L5S7vfbcbFKKVEh",
"encSecKey": "8356016f73fac1d2d2d0fa3e915b7e132df0e1711361bb572d679b0e266c22a0536ea0291db1aad59b7edc033856dd558709504f098727e56512a1ce554729cdde1ddc59c2208c2ebf7f689308d3cc1da96bbbef523fc025f64c0c68ef28d4da677bc547d2db71a6e12e68f0ad262d653bf28037c94fb9aed6d6ff3e84aea71c"
}
def get_encKey():
return ttt["encSecKey"]
from Crypto.Cipher import AES
from base64 import b64encode
def to_16(data):
pad = 16-len(data) %16
data += chr(pad) * pad
return data
def enc_params(data, key):
aes = AES.new(key.encode('utf-8'), iv="0102030405060708".encode('utf-8'), mode=AES.MODE_CBC)
bs = aes.encrypt(to_16(data).encode('utf-8')) # 加密,内容长度必须是16倍数
return str(b64encode(bs), 'utf-8') # 转化成字符串返回
# 数据加密
def get_params(data):
# 默认收到的是字符串
first = enc_params(data,g)
second = enc_params(first, i)
return second
import json
resp = requests.post(url, data={
"params": get_params(json.dumps(data)),
"encSecKey": get_encKey()
}, verify=False)
resp.close()
dict = json.loads(resp.text)
for com in dict['data']['comments']:
print(repr(com['content']))