sekiro-ipc的使用

doc https://sekiro.iinti.cn/sekiro-doc/

   使用sekiro ipc 框架完成js的逆向代理工作 。

sekiro环境配置

  1. 需要java 1.8以上并配置java_home
  2. 下载服务,https://oss.iinti.cn/sekiro/sekiro-demo/
  3. 解压并运行/bin下的可执行文件

备用服务地址

访问:http://127.0.0.1:5612/business-demo/groupList

返回以下json就说明服务启动成功了

1
2
{"data":["ws-group"],"ok":true,"status":0}

注意端口号: 默认5612 可在/conf/config.properties 查看修改

浏览器注入js

同样需要注意端口号一致。
可在console控制台输入,也可通过油猴使用

代码注入后访问 http://127.0.0.1:5612/business-demo/invoke?group=ws-group&action=clientTime 如果返回一下结果代表成功

1
2
3
4
5
{
"clientId": "dd60a2e0-2d51-f177-158b-0c849ebf4e77",
"data": "Mon Dec 18 2023 18:16:04 GMT+0800 (中国标准时间)",
"status": 0
}

脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 2023-12-18
// @description try to take over the world!
// @author You
// @match https://www.kugou.com/yy/rank/home/1-8888.html?from=rank
// @icon https://www.google.com/s2/favicons?sz=64&domain=kugou.com
// @grant none
// ==/UserScript==

function SekiroClient(wsURL) {
this.wsURL = wsURL;
this.handlers = {};
this.socket = {};
// check
if (!wsURL) {
throw new Error('wsURL can not be empty!!')
}
this.webSocketFactory = this.resolveWebSocketFactory();
this.connect()
}

SekiroClient.prototype.resolveWebSocketFactory = function () {
if (typeof window === 'object') {
var theWebSocket = window.WebSocket ? window.WebSocket : window.MozWebSocket;
return function (wsURL) {

function WindowWebSocketWrapper(wsURL) {
this.mSocket = new theWebSocket(wsURL);
}

WindowWebSocketWrapper.prototype.close = function () {
this.mSocket.close();
};

WindowWebSocketWrapper.prototype.onmessage = function (onMessageFunction) {
this.mSocket.onmessage = onMessageFunction;
};

WindowWebSocketWrapper.prototype.onopen = function (onOpenFunction) {
this.mSocket.onopen = onOpenFunction;
};
WindowWebSocketWrapper.prototype.onclose = function (onCloseFunction) {
this.mSocket.onclose = onCloseFunction;
};

WindowWebSocketWrapper.prototype.send = function (message) {
this.mSocket.send(message);
};

return new WindowWebSocketWrapper(wsURL);
}
}
if (typeof weex === 'object') {
// this is weex env : https://weex.apache.org/zh/docs/modules/websockets.html
try {
console.log("test webSocket for weex");
var ws = weex.requireModule('webSocket');
console.log("find webSocket for weex:" + ws);
return function (wsURL) {
try {
ws.close();
} catch (e) {
}
ws.WebSocket(wsURL, '');
return ws;
}
} catch (e) {
console.log(e);
//ignore
}
}
//TODO support ReactNative
if (typeof WebSocket === 'object') {
return function (wsURL) {
return new theWebSocket(wsURL);
}
}
// weex 鍜� PC鐜鐨剋ebsocket API涓嶅畬鍏ㄤ竴鑷达紝鎵€浠ュ仛浜嗘娊璞″吋瀹�
throw new Error("the js environment do not support websocket");
};

SekiroClient.prototype.connect = function () {
console.log('sekiro: begin of connect to wsURL: ' + this.wsURL);
var _this = this;
// 涓峜heck close锛岃
// if (this.socket && this.socket.readyState === 1) {
// this.socket.close();
// }
try {
this.socket = this.webSocketFactory(this.wsURL);
} catch (e) {
console.log("sekiro: create connection failed,reconnect after 2s");
setTimeout(function () {
_this.connect()
}, 2000)
}

this.socket.onmessage(function (event) {
_this.handleSekiroRequest(event.data)
});

this.socket.onopen(function (event) {
console.log('sekiro: open a sekiro client connection')
});

this.socket.onclose(function (event) {
console.log('sekiro: disconnected ,reconnection after 2s');
setTimeout(function () {
_this.connect()
}, 2000)
});
};

SekiroClient.prototype.handleSekiroRequest = function (requestJson) {
console.log("receive sekiro request: " + requestJson);
var request = JSON.parse(requestJson);
var seq = request['__sekiro_seq__'];

if (!request['action']) {
this.sendFailed(seq, 'need request param {action}');
return
}
var action = request['action'];
if (!this.handlers[action]) {
this.sendFailed(seq, 'no action handler: ' + action + ' defined');
return
}

var theHandler = this.handlers[action];
var _this = this;
try {
theHandler(request, function (response) {
try {
_this.sendSuccess(seq, response)
} catch (e) {
_this.sendFailed(seq, "e:" + e);
}
}, function (errorMessage) {
_this.sendFailed(seq, errorMessage)
})
} catch (e) {
console.log("error: " + e);
_this.sendFailed(seq, ":" + e);
}
};

SekiroClient.prototype.sendSuccess = function (seq, response) {
var responseJson;
if (typeof response == 'string') {
try {
responseJson = JSON.parse(response);
} catch (e) {
responseJson = {};
responseJson['data'] = response;
}
} else if (typeof response == 'object') {
responseJson = response;
} else {
responseJson = {};
responseJson['data'] = response;
}


if (Array.isArray(responseJson)) {
responseJson = {
data: responseJson,
code: 0
}
}

if (responseJson['code']) {
responseJson['code'] = 0;
} else if (responseJson['status']) {
responseJson['status'] = 0;
} else {
responseJson['status'] = 0;
}
responseJson['__sekiro_seq__'] = seq;
var responseText = JSON.stringify(responseJson);
console.log("response :" + responseText);
this.socket.send(responseText);
};

SekiroClient.prototype.sendFailed = function (seq, errorMessage) {
if (typeof errorMessage != 'string') {
errorMessage = JSON.stringify(errorMessage);
}
var responseJson = {};
responseJson['message'] = errorMessage;
responseJson['status'] = -1;
responseJson['__sekiro_seq__'] = seq;
var responseText = JSON.stringify(responseJson);
console.log("sekiro: response :" + responseText);
this.socket.send(responseText)
};

SekiroClient.prototype.registerAction = function (action, handler) {
if (typeof action !== 'string') {
throw new Error("an action must be string");
}
if (typeof handler !== 'function') {
throw new Error("a handler must be function");
}
console.log("sekiro: register action: " + action);
this.handlers[action] = handler;
return this;
};

function guid() {
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

var client = new SekiroClient("ws://127.0.0.1:5612/business-demo/register?group=ws-group&clientId="+guid());

client.registerAction("clientTime",function(request, resolve,reject ){
resolve(""+new Date());
})

使用

新建html文件如下. 需在此页面进行注入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

<script src="http://file.virjar.com/sekiro_web_client.js?_=123"></script>

<script>
function b64(arg) {
return btoa(arg)
}

// 生成唯一标记uuid编号
function guid() {
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
// 连接服务端
var client = new SekiroClient("ws://127.0.0.1:5612/business-demo/register?group=ws-group&clientId="+guid());
// 业务接口
client.registerAction("demo",function(request, resolve, reject){
data = request['page']
console.log(data,'记号')
resolve("我是Forever"+new Date());
})

client.registerAction("demo1",function(request, resolve, reject){
data = request['pwd']
res = b64(data)
resolve(res);
})


</script>


</body>
</html>

接收注入的结果

以python为例

sekiro支持多种语言可根据需要使用。 
1
2
3
4
5
6
7
8
import requests
data = {"group": "ws-group",
"action": "demo",
"page": 10
}
res = requests.get("http://127.0.0.1:5610/business-demo/invoke",params=data )
print(res.text)

线上注入

  1. 找到需要注入的js文件进行本地覆盖保存。
  2. 将上面的脚本文件在文件中写入
  3. 在原函数的调用处进行要hook的函数回调
    1
    2
    3
    4
    5
    6
    client.registerAction("demo",function(request, resolve, reject){
    var data = request['page']
    console.log(data,'记号')
    var value = d(data)
    resolve("kugou: "+value);
    });