-
Notifications
You must be signed in to change notification settings - Fork 3
/
kmb-patch.ts
427 lines (338 loc) · 14.6 KB
/
kmb-patch.ts
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
const tempy = require('tempy');
const {execSync} = require('child_process');
const os = require('os');
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const ProgressBar = require('progress');
const extract = require("extract-zip");
const cheerio = require('cheerio');
const glob = require("glob");
const tar = require('tar-stream');
/**
* KMB Patch Class
*/
export class KMBPatch {
public inputAPK : string;
public outputAPK : string;
public tempDir : string;
public forceOverwrite = true;
public mapKey = "";
public signKey = "tools/sign-key.jks";
public keystorePassword = "";
public keyAlias = "louislam";
public keyPassword = "";
public downloadJava = true;
public java = '"tools/jdk-11.0.8+10-jre/bin/java"';
constructor(inputAPK : string, outputAPK = "patched-kmb.apk", tempDir = "tmp") {
this.mapKey = Buffer.from("QUl6YVN5QXR6Y2t0UzFfb1RBOHJ5dXBXdjFqcENCUXJZRjNHVVJr", 'base64').toString('utf8');
this.keystorePassword = Buffer.from("Q25oUWJuZ3U4YUxGVlI2ckhHczZ6a29yeVpjSlZlREY=", 'base64').toString('utf8');
this.keyPassword = this.keystorePassword;
this.inputAPK = inputAPK;
this.outputAPK = outputAPK;
if (tempDir == null) {
this.tempDir = tempy.directory();
this.forceOverwrite = true;
} else {
this.tempDir = tempDir;
}
console.log("Temp Dir: " + this.tempDir);
console.log("OS: " + os.platform());
if (os.platform() !== "win32") {
this.downloadJava = false;
this.java = "java";
}
}
/**
* Patch the apk
*/
async patch() {
let exitCode = 0;
let self = this;
try {
await this.downloadTools();
if (fs.existsSync(this.tempDir)){
fs.rmSync(this.tempDir, {
recursive: true
});
}
let escapedTempDir = escapeShellArg(this.tempDir);
let escapedInputAPK = escapeShellArg(this.inputAPK);
let escapedOutputAPK;
let f = "";
let escapedSignKey = escapeShellArg(this.signKey);
let escapedKeystonePassword = escapeShellArg(this.keystorePassword);
let escapedKeyAlias = escapeShellArg(this.keyAlias);
let escapedKeyPassword = escapeShellArg(this.keyPassword);
if (this.forceOverwrite) {
f = "-f";
}
console.log("Extracting APK");
let output : string = execSync(`${this.java} -Xmx512m -jar tools/apktool_2.4.1.jar d -o ${escapedTempDir} ${f} ${escapedInputAPK}`).toString();
console.log(output);
// Patch AndroidManifest.xml
console.log("Patch AndroidManifest.xml");
let xmlPath = this.tempDir + "/AndroidManifest.xml";
let androidManifestXML : string = fs.readFileSync(xmlPath);
let $ = cheerio.load(androidManifestXML, {
xmlMode: true
});
escapedOutputAPK = escapeShellArg(this.outputAPK);
// Update MAP Key
$("application meta-data").each(function () {
if ($(this).attr("android:name") == "com.google.android.maps.v2.API_KEY") {
$(this).attr("android:value", self.mapKey);
}
});
// Remove Splash Screen
console.log("Remove Splash Screen");
$("activity").each(function () {
if ($(this).attr("android:name") == "com.mobilesoft.mybus.KMBMainView") {
$(this).append(`
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
`);
} else if ($(this).attr("android:name") == "com.mobilesoft.mybus.KMBSplashScreen") {
$(this).empty();
}
});
fs.writeFileSync(xmlPath, $.xml());
// Remove AdView in xml
let resXMLPath = this.tempDir + '/res/**/*.xml';
let fileList = glob.sync(resXMLPath);
for (let i = 0; i < fileList.length; i++) {
let filename = fileList[i];
let text = fs.readFileSync(filename).toString();
if (text.includes("com.google.android.gms.ads.AdView")) {
console.log("Remove AdView in " + filename);
$ = cheerio.load(text, {
xmlMode: true
});
$("com\\.google\\.android\\.gms\\.ads\\.AdView").remove();
fs.writeFileSync(filename, $.xml());
}
}
// smali_classes2 folder
let path = this.tempDir + '/smali_classes2/**/*.smali';
fileList = glob.sync(path);
for (let i = 0; i < fileList.length; i++) {
let updated = false;
let filename = fileList[i];
let text = fs.readFileSync(filename).toString();
let lines = text.split(/\r?\n/);
for (let j = 0; j < lines.length; j++) {
let line = lines[j];
// Disable check update
if (line.includes(", 0x2712")) {
console.log("Remove force update in " + filename);
lines[j] = line.replace(", 0x2712", ", 0x2760");
updated = true;
}
// Remove loadAd code
if (line.includes("->loadAd(")) {
console.log("Remove loadAd code in " + filename);
lines[j] = "#" + line;
// Also comment previous line if is .line
if (lines[j - 1].trim().startsWith(".line")) {
lines[j - 1] = "#" + lines[j - 1];
}
updated = true;
}
// Remove setVisibility code
if (line.includes("AdView;->setVisibility")) {
console.log("Remove setVisibility code in " + filename);
lines[j] = "#" + line;
// Also comment previous line if is .line
if (lines[j - 1].trim().startsWith(".line")) {
lines[j - 1] = "#" + lines[j - 1];
}
updated = true;
}
// Remove InterstitialAd
// Smali: InterstitialAd;->load ==== JAVA: InterstitialAd.load(...)
if (line.includes("InterstitialAd;->load")) {
console.log("Remove InterstitialAd code in " + filename);
lines[j] = "#" + line;
// Also comment previous line if is .line
if (lines[j - 1].trim().startsWith(".line")) {
lines[j - 1] = "#" + lines[j - 1];
}
updated = true;
}
}
if (updated) {
fs.writeFileSync(filename, lines.join(os.EOL));
}
}
// /smali/ folder
path = this.tempDir + '/smali/**/*.smali';
fileList = glob.sync(path);
for (let i = 0; i < fileList.length; i++) {
let updated = false;
let filename = fileList[i];
let text = fs.readFileSync(filename).toString();
let lines = text.split(/\r?\n/);
for (let j = 0; j < lines.length; j++) {
let line = lines[j];
// Remove Builtin Ads
if (line.includes("https://app.kmb.hk/app1933/index.php")) {
console.log("Remove Builtin Ads in " + filename);
// Keep finding `/mybus/manager/m` (JAVA: mybus.manager.m(...)), if found, add # at the beginning to comment it
let k = j;
let foundTheCall = false;
while (k >= 0 && k < lines.length) {
if (lines[k].includes("/mybus/manager/m")) {
lines[k] = "#" + lines[k];
foundTheCall = true;
break;
}
k++;
}
if (!foundTheCall) {
console.error("Failed to remove Builtin Ads in " + filename);
} else {
updated = true;
}
}
}
if (updated) {
fs.writeFileSync(filename, lines.join(os.EOL));
}
}
console.log("Build APK");
output = execSync(`${this.java} -Xmx512m -jar tools/apktool_2.4.1.jar b ${escapedTempDir} -o ${escapedOutputAPK}`).toString();
console.log(output);
console.log("Sign the APK");
output = execSync(`${this.java} -Xmx512m -jar tools/uber-apk-signer-1.1.0.jar -a ${escapedOutputAPK} --allowResign --overwrite --ks ${escapedSignKey} --ksPass ${escapedKeystonePassword} --ksAlias ${escapedKeyAlias} --ksKeyPass ${escapedKeyPassword}`).toString();
console.log(output);
console.log("Patched successfully! The patch apk file located in " + this.outputAPK);
} catch (error) {
console.error(error.message);
exitCode = 1;
}
if (fs.existsSync(this.tempDir)){
fs.rmSync(this.tempDir, {
recursive: true
});
}
return exitCode;
}
/**
* Download all tools
*/
async downloadTools() {
console.log("Download Tools");
if (! fs.existsSync("tools/apktool_2.4.1.jar")) {
await this.downloadFile("https://github.com/iBotPeaches/Apktool/releases/download/v2.4.1/apktool_2.4.1.jar", "apktool_2.4.1.jar");
}
if (! fs.existsSync("tools/uber-apk-signer-1.1.0.jar")) {
await this.downloadFile("https://github.com/patrickfav/uber-apk-signer/releases/download/v1.1.0/uber-apk-signer-1.1.0.jar", "uber-apk-signer-1.1.0.jar");
}
if (! fs.existsSync("tools/abe.jar")) {
await this.downloadFile("https://github.com/nelenkov/android-backup-extractor/releases/download/20181012025725-d750899/abe-all.jar", "abe.jar")
}
// JRE Download link from https://adoptopenjdk.net/archive.html
if (this.downloadJava && ! fs.existsSync("tools/jdk-11.0.8+10-jre")) {
await this.downloadFile("https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.8%2B10/OpenJDK11U-jre_x64_windows_hotspot_11.0.8_10.zip", "jre.zip");
await extract("tools/jre.zip", {
dir: path.resolve("tools")
});
}
console.log("Downloaded all tools");
}
/**
* Download a file
* Copy from https://futurestud.io/tutorials/axios-download-progress-in-node-js
*/
async downloadFile(url, filename) {
console.log('Download ' + path.basename(url))
const { data, headers } = await axios({
url,
method: 'GET',
responseType: 'stream'
});
const totalLength = parseInt(headers['content-length']);
const progressBar = new ProgressBar('downloading [:bar] :percent :etas', {
width: 40,
complete: '=',
incomplete: ' ',
renderThrottle: 1,
total: totalLength
});
const writer = fs.createWriteStream(path.resolve(__dirname, 'tools', filename));
data.on('data', (chunk) => {
progressBar.tick(chunk.length);
data.finished
});
await new Promise<void>((resolve) => {
writer.on("finish", () => {
console.log("Downloaded and renamed to " + filename);
resolve();
});
data.pipe(writer)
});
}
/**
* Restore and patch the appdata.ab
*/
async restore() {
try {
if (! fs.existsSync("tmp")) {
fs.mkdirSync("tmp");
}
if (! fs.existsSync("tmp/appdata")) {
fs.mkdirSync("tmp/appdata");
}
if (! fs.existsSync("appdata.ab")) {
throw "appdata.ab not found";
}
console.log("Patching backup")
execSync(`${this.java} -jar tools/abe.jar unpack appdata.ab tmp/appdata.tar`);
// Stream is fun
// oldTarballStream -> extract (Stream) -> only replace "_manifest" -> pack (Stream) -> newTarballStream
let oldTarballStream = fs.createReadStream("tmp/appdata.tar");
let newTarballStream = fs.createWriteStream("tmp/patched-appdata.tar");
var pack = tar.pack();
var extract = tar.extract();
extract.on('entry', function(header, stream, callback) {
if (header.name == "apps/com.kmb.app1933/_manifest") {
let stat = fs.statSync("tools/_manifest");
let manifestStream = fs.createReadStream("tools/_manifest");
header.size = stat.size;
manifestStream.pipe(pack.entry(header, callback))
} else {
stream.pipe(pack.entry(header, callback))
}
})
extract.on('finish', function () {
pack.finalize()
})
await new Promise<void>((resolve) => {
newTarballStream.on("finish", function () {
newTarballStream.end();
resolve();
});
oldTarballStream.pipe(extract);
pack.pipe(newTarballStream);
});
execSync(`${this.java} -jar tools/abe.jar pack tmp/patched-appdata.tar patched-appdata.ab`);
console.log("Connect your phone to your PC and accept restore");
execSync(`adb restore patched-appdata.ab`);
} catch (error) {
console.error(error.message);
return 1;
}
return 0;
}
}
function escapeShellArg(arg) {
let quote;
if (os.platform() == 'win32') {
quote = '"';
} else {
quote = "'";
}
return quote + `${arg.replace(/'/g, `'\\''`)}` + quote;
}