zhaoboy666 发表于 2021-5-29 17:41:53

安卓直播间websocket协议破解还原

前言
现如今直播间协议很多已经很少使用http轮询的方式获取直播间的弹幕、礼物、关注等,在用的目前知道的是某音的直播,其他的一些app已经使用socket或者websoceket获取直播间的弹幕等,本次主要针对websocket提供协议破解思路。
设备:小米note8、windows
案例:具体不提供,仅提供破解思路。
抓包分析
本次抓包使用的Charles进行抓包websocket,Charles配置简单,抓包方便。https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9cf473ab2fba4ec3aa661f49dba01e1b~tplv-k3u1fbpfcp-zoom-1.image打开该app的直播间,在Charles中可以很清晰的看到,websocket发送的数据以及接受的推送信息。
ws://chat.**.com:3185

ws://也就是该websocket的明显特征,除过ws://还有wss://,而wss是对应ws的加密版本,类似http和https的关系。
脱壳分析定位
该app是经过加壳的,这里使用youpk进行脱壳,具体流程可看github,https://github.com/Youlor/Youpk
定位关键位置
通过抓包的关键词ctor.entryHandler.enter在该app定位。https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/edd4ee9377c54773ba22eb9a2ba9fe99~tplv-k3u1fbpfcp-zoom-1.image这个sdk就告诉我们所有了,com.netease.pomelo,通过相关资料
pomelo是网易12年底出品的一个基于node.js的游戏服务器框架,其设计初衷是游戏服务器, 不过在设计、开发完成后发现pomelo是个通用的分布式实时应用开发框架。

根据网易github,https://github.com/netease/pomelo-androidclient 相关资料,其使用socket.io进行发包,并且在logcat中可以很清晰的看到相关发送数据的日志。https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4500ebe210324640886452b7b69df16b~tplv-k3u1fbpfcp-zoom-1.image但是在该数据包中直接发现有乱码,那就只能找该数据包的原始字节。
进一步的hook分析,定位到如下图位置。https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/b3b6d09722134b978bafa3cf9d39d44d~tplv-k3u1fbpfcp-zoom-1.imagehttps://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6f0f4d1555e44d88956768d66d6dea46~tplv-k3u1fbpfcp-zoom-1.image通过frida进行hook打印该类,默认调用tostring方法
var FramedataImpl1 = Java.use("org.java_websocket.framing.FramedataImpl1")
FramedataImpl1.b.overload('boolean').implementation = function (q1) {
this.b(q1)
console.log(this)
}

https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0f7621060a6d4615bf03709f4400160d~tplv-k3u1fbpfcp-zoom-1.image得出如下字节数组
//51, 58, 58, 58, 0, 0, 0, 1, 31,
//51, 58, 58, 58, 0, 0, 0, 2, 28,
//51, 58, 58, 58, 0, 0, 0, 3, 35,
//51, 58, 58, 58, 0, 0, 0, 4, 32,
//51, 58, 58, 58, 0, 0, 0, 5, 32,
//50, 58, 58

剩下就是发现规律,包的顺序以及发那些包,其中51, 58, 58, 58,就是3:::,50, 58, 58是2::,那些乱码的字符,也就是对应的后半部分的字节,其中包括了发包的顺序,从0-9等。
还原协议
使用python的websocket-client,进行还原直播间的协议通讯。
关于上述的字节,可以在java中使用base64进行编码,然后python中进行解码使用。https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3d8a8cfe16214f4b8d473c3361a8d0dc~tplv-k3u1fbpfcp-zoom-1.image

def send_msg(ws):
a = base64.b64decode('Mzo6OgAAAAEf')
enter = {"sid": "", "rid": str(room_id), "userid": "0",
"brand": "Redmi Note 8", "cores": 8, "memory": 5638, "screen": 1080, "trid": 666, "re_enter": 1,
"true_ip": "210*12*195*3", "sktime": 1622114102, "sign": sign,
"version_code": 507,
"is_intl_pack": "0", "channel": "9700288", "client_code_version": "23", "client_side": 2, "sys_sdk": 29,
"country_code": "CN", "pkg_channel": "9700288", "is_market": "1", "timestamp": 1622114101589}
ws.send(a + bytes('sioconnector.entryHandler.enter' + json.dumps(enter), encoding='utf8'))
b = base64.b64decode('Mzo6OgAAAAIc')
ws.send(b + bytes('chat.chatHandler.getTopThree' + json.dumps({"timestamp": 1622113988470}), encoding='utf8'))
c = base64.b64decode('Mzo6OgAAAAMj')
ws.send(c + bytes('chat.chatHandler.checkUserStatusMix' + json.dumps({"uid": "0", "timestamp": 1622113988484}),
encoding='utf8'))
d = base64.b64decode('Mzo6OgAAAAQg')
ws.send(d + bytes('chat.chatHandler.getAnchorCdnMix' + json.dumps({"timestamp": 1622113988496}),
encoding='utf8'))
e = base64.b64decode('Mzo6OgAAAAcg')
ws.send(e + bytes('chat.chatHandler.getGuardinfo' + json.dumps({"timestamp": 1622113988496}),
encoding='utf8'))
f = base64.b64decode('Mzo6OgAAAAYd')
ws.send(f + bytes('chat.chatHandler.onlineGoldPhone' + json.dumps({"timestamp": 1622113988496}),
encoding='utf8'))
while 1:
ws.send(b'2::')
sleep(1)


def on_open(ws):
print('on_open', ws)
threading.Thread(target=send_msg, args=(ws,)).start()


def on_message(ws, message):
print(message)
message = message
print('on_message', message)


def on_close(ws, close_status_code, close_reason):
print('on_close', ws, close_status_code, close_reason)


def start_websocket(chat_url, token):
# websocket.enableTrace(True)
ws = websocket.WebSocketApp(
f"ws://{chat_url}/socket.io/1/websocket/{token}",
on_message=on_message,
on_open=on_open,
on_close=on_close
)
ws.run_forever()
小结
该直播app的直播间协议,在获取弹幕过程中,有部分数据是乱码形式,这部分就可以使用hook将其发送的数据以字节的形式拿到,在通过对比发送字节的不同,构造其发送的数据,最后完成协议还原。https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8bc408809c234e5185552bf539deb034~tplv-k3u1fbpfcp-zoom-1.imagehttps://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/66bcd8e890034299893f544c2c5d5ae2~tplv-k3u1fbpfcp-zoom-1.image



页: [1]
查看完整版本: 安卓直播间websocket协议破解还原