哔哩哔哩私信HTTP协议分析
本教程(源码)仅做交流学习使用,请勿用于非法
使用抓包工具为谷歌自带的开发者工具
1. 抓取私信数据包
发送成功返回数据包
2. 分析数据包
msg[sender_uid] -> 是自己的账号ID
msg[receiver_id] -> 接受信息的ID
msg[receiver_type] -> 暂时不管,表面意思猜测是接受方式,私信
msg[msg_type] -> 同上
msg[msg_status] -> 暂时不管
msg[content] -> 信息JSON包,固定格式 {"content":"私信内容"}
msg[timestamp] -> 时间戳(10位)
msg[new_face_version] -> 暂时不管
msg[dev_id] -> 暂时不管
from_firework -> 暂时不管
build -> 暂时不管
mobi_app -> 私信端(网页是web),固定即可
csrf_token -> 暂时不管
csrf -> 暂时不管
3. 数据包key分析
msg[dev_id]
重新抓包看是否变化
可以看到并没有改变,所以固定即可
csrf_token
未改变,Ctrl+F搜索key值
cookie里的参数,账号退出登录可能会变,发信时不会改变,固定即可
4. 使用python测试
代码
import requests,time,re
def send_msg(content,cookie):
url = 'https://api.vc.bilibili.com/web_im/v1/web_im/send_msg' #发信url
header = {
'Cookie':cookie,
}
reSe = re.compile('bili_jct=(?P<csrf>[^;]+);')
csrf = reSe.findall(cookie)[0]
data = {
'msg[sender_uid]': '', #账号id
'msg[receiver_id]': '', #收信id
'msg[receiver_type]': '1',
'msg[msg_type]': '1',
'msg[msg_status]': '0',
'msg[content]': '{"content": "'+content+'"}',
'msg[timestamp]': int(time.time()),
'msg[new_face_version]': '0',
'msg[dev_id]': 'D477E677 - D3AE - 46E8 - BDCC - 3FE9E6F4EC65',
'from_firework': '0',
'build': '0',
'mobi_app': 'web',
'csrf_token': csrf,
'csrf': csrf
}
response = requests.post(url,data=data,headers=header)
print(response.text)
cookie = input('请输入cookie:')
content = input('请输入私信内容:')
send_msg(content,cookie)