Repository navigation
Expand file tree
/
Copy pathdecrypt.js
More file actions
578 lines (437 loc) · 13.7 KB
/
Copy pathdecrypt.js
File metadata and controls
578 lines (437 loc) · 13.7 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/* SPDX-License-Identifier: 0BSD */
/**
Decrypt TAPO 'KLAP' protocol communications saved as wireshark packet dissection json:
File | Export Packet Dissections | As JSON...
Tested with:
[Tapo P110 Firmware: 1.3.1 Build 240621 Rel.162048] <--> [TP-Link Tapo Android app v3.13.512]
Captured with Wireshark 4.0.17
*/
import fs from 'node:fs/promises';
import path from 'node:path';
import { createHash, createDecipheriv } from 'node:crypto';
if (process.argv.length < 3) {
console.log('Usage: ' + path.basename(process.argv[1]) + ' <filename>');
process.exit();
}
const cred_file = './.credentials.js';
let cred_module = null;
try {
await fs.access(cred_file, fs.constants.F_OK | fs.constants.R_OK);
console.log(' * Loading credentials from', cred_file);
cred_module = await import(cred_file);
} catch { console.log(' * Credentials file not found:', cred_file); }
const credentials = cred_module ? cred_module.credentials : [];
// for debugging
const SAVE_FILTERED = false;
const filename = process.argv[2];
const auth = {};
const fd = path.parse(filename);
const filterFilename = fd.dir + path.sep + fd.name + '.filtered' + fd.ext;
const decryptFilename = fd.dir + path.sep + fd.name + '.decrypted' + fd.ext;
const dissections = await read_dissections(filename);
console.log(` * Loaded ${dissections.length} dissections`);
console.log(' * Filtering data ...');
const filtered = filter(dissections);
if (SAVE_FILTERED) {
await write_filtered(filtered, filterFilename);
}
console.log(' * Checking filtered data ...');
check(filtered);
console.log(' * Decrypting ...');
const decrypted = parse(filtered, credentials);
if (decrypted === false) {
console.log(' * No decrypted results to save');
} else {
await write_results(decrypted, decryptFilename);
}
console.log(' * Done!');
process.exit();
// ---------------------------------------------
/**
* @param {*} filtered
* @param {String[][]} credentials
* @returns {String|False}
*/
function parse(filtered, credentials) {
const device_default_creds = [
['test@tp-link.net', 'test'],
['kasa@tp-link.net', 'kasaSetup'],
];
auth.hashes = [];
[...credentials, ...device_default_creds].forEach((cred) => {
auth.hashes.push([cred[0], sha256(Buffer.concat([sha1(cred[0]), sha1(cred[1])]))]);
});
let got_auth = false;
let output = '';
let skipped = 0;
let count = 0;
for (const stream_id of Object.keys(filtered)) {
const stream = filtered[stream_id];
for (const frame_id of Object.keys(stream.frames)) {
const frame = stream.frames[frame_id];
const id = `[Stream:${stream_id} Frame:${frame_id}]`;
if (!frame.is_http_request) {
continue;
}
switch (frame.type) {
case 'handshake1':
// console.log('HANDSHAKE1', id);
got_auth = handshake1(stream, frame, auth);
break;
case 'handshake2':
// console.log('HANDSHAKE2', id);
// @TODO: verify clientProof and check for HTTP200 response from server
break;
case 'request':
// console.log('REQUEST', id);
count++;
if (!got_auth) {
skipped++;
continue;
}
// block scope due to variable declaration
{
const data = request(filtered, stream_id, frame_id, auth);
if (data === false) {
continue;
}
if (output === '') {
output += '[\n' + data;
} else {
output += ',\n\n' + data;
}
}
break;
case '':
console.error('[ERROR] empty type', id);
break;
default:
console.error('[ERROR] unknown type:', frame.type, id);
break;
}
}
}
if (skipped > 0) {
console.log(`[WARNING] Skipped ${skipped} of ${count} requests due to no credentials`);
}
if (output === '') {
return false;
}
output += '\n]\n';
return output;
}
/**
* @returns {String}
*/
function request(streams, stream_id, frame_id, auth) {
const id = `Stream: ${stream_id} Frame: ${frame_id}`;
const stream = streams[stream_id];
const frame = stream.frames[frame_id];
const requestPayload = decode_payload(frame.payload);
const request = decrypt(requestPayload, frame.seq, auth);
if (request === false) {
console.error('[ERROR] Failed to decrypt request', id);
return false;
}
if (request.substring(0, 23) === '{"method":"set_qs_info"') {
parse_qs_info(request);
}
if (frame.resp_in === false) {
console.error('[WARNING] request has no response', id);
return request;
}
const respFrame = stream.frames[frame.resp_in];
const responsePayload = decode_payload(respFrame.payload);
const response = decrypt(responsePayload, frame.seq, auth);
if (response === false) {
console.error('[ERROR] Failed to decrypt response', id);
return false;
}
return request + ',\n' + response;
}
function parse_qs_info(request) {
try {
const obj = JSON.parse(request);
const username_b64 = obj.params.account.username ?? null;
const password_b64 = obj.params.account.password ?? null;
if (username_b64 === null || password_b64 === null) {
return;
}
const username = Buffer.from(username_b64, 'base64').toString();
const password = Buffer.from(password_b64, 'base64').toString();
auth.hashes.push([username, sha256(Buffer.concat([sha1(username), sha1(password)]))]);
console.log(' * Discovered credentials for username', username);
} catch (err) {
console.error('[ERROR] failed to parse set_qs_info', err);
return;
}
}
function handshake1(stream, frame, auth) {
console.log(' * Found handshake1, checking for valid credentials');
const clientNonce = decode_payload(frame.payload);
if (clientNonce.length !== 16) {
console.error(`[ERROR] Invalid length for clientNonce: ${clientNonce.length}`);
return false;
}
if (frame.resp_in === false) {
console.error('[ERROR] Handshake1 packet has no response');
return false;
}
const resp = stream.frames[frame.resp_in];
const serverData = decode_payload(resp.payload);
if (serverData.length !== 48) {
console.error(`[ERROR] Invalid length for serverData: ${serverData.length}`);
return false;
}
const serverNonce = serverData.subarray(0, 16);
const serverProof = serverData.subarray(16);
let found_hash = false;
let base;
for (const hash of auth.hashes) {
base = Buffer.concat([clientNonce, serverNonce, hash[1]]);
const verifyHash = sha256(base);
if (serverProof.equals(verifyHash)) {
console.log(' * Valid credentials found for username', hash[0]);
found_hash = true;
break;
}
}
if (found_hash !== true) {
console.error('[ERROR] none of the available credentials are correct for this handshake');
return false;
}
auth.key = sha256(Buffer.concat([text2Buffer('lsk'), base])).subarray(0, 16);
const iv = sha256(Buffer.concat([text2Buffer('iv'), base]));
auth.iv = Buffer.alloc(12); // 96 bits + 32 bits from seq = 128 bits
iv.copy(auth.iv, 0, 0, 12);
const sig = sha256(Buffer.concat([text2Buffer('ldk'), base]));
auth.sig = Buffer.alloc(28);
sig.copy(auth.sig, 0, 0, 28);
return true;
}
/**
* @param {Buffer} data
* @param {Number} seq
* @param {Object} auth
* @returns {String|false}
*/
function decrypt(data, seq, auth) {
if (data.length < 32) {
console.error(`[ERROR] decrypt: Not enough data (${data.length} bytes)`);
return false;
}
const seqBuf = Buffer.alloc(4);
seqBuf.writeUInt32BE(seq);
const signature = data.subarray(0, 32);
const ciphertext = data.subarray(32);
const checkSignature = sha256(Buffer.concat([auth.sig, seqBuf, ciphertext]));
if (!signature.equals(checkSignature)) {
console.error('[ERROR] decrypt: Invalid signature');
return false;
}
const ivAndSeq = Buffer.concat([auth.iv, seqBuf]);
const cipher = createDecipheriv('aes-128-cbc', auth.key, ivAndSeq);
let plaintext = cipher.update(ciphertext);
plaintext = Buffer.concat([plaintext, cipher.final()]);
return plaintext.toString();
}
/**
* Filter the Wireshark dissections, extracting just the information needed
* @param {Array} dissections
* @returns
*/
function filter(dissections) {
const urlRegExp = /.*?\/app\/(handshake(?:1|2)|request)(?:\?seq=(\d+)|)/;
const result = {};
let idx = 0;
dissections.forEach((packet) => {
idx++;
let id = `[Entry: ${idx}]`;
const source = packet._source || null;
if (source === null) {
console.error('[ERROR] No source property', id);
return;
}
const layers = source.layers || null;
if (layers === null) {
console.error('[ERROR] No layers property', id);
return;
}
const frame_layer = layers.frame || null;
if (frame_layer === null) {
console.error('[ERROR] No frame layer property', id);
return;
}
const frame_num = getInt(frame_layer['frame.number']);
if (frame_num === false) {
console.error('[ERROR] Invalid frame number', frame_layer['frame.number'], id);
return;
}
id = `[Frame: ${frame_num}]`;
const ip_layer = layers.ip || null;
if (ip_layer === null) {
console.error('[ERROR] No ip layer property', id);
return;
}
const tcp_layer = layers.tcp || null;
if (tcp_layer === null) {
console.error('[ERROR] No tcp layer property', id);
return;
}
const tcp_stream = getInt(tcp_layer['tcp.stream']);
if (tcp_stream === false) {
console.error('[ERROR] Invalid stream number', tcp_layer['tcp.stream'], id);
return;
}
id = `[Stream: ${tcp_stream} Frame: ${frame_num}]`;
const tcp_segments = layers['tcp.segments'] || null;
const http_layer = layers.http || null;
if (http_layer === null) {
console.error('[ERROR] No http layer property', id);
return;
}
if (result[tcp_stream] === undefined) {
result[tcp_stream] = {};
result[tcp_stream].srcIP = ip_layer['ip.src'] || '';
result[tcp_stream].srcPort = getInt(tcp_layer['tcp.srcport']);
result[tcp_stream].destIP = ip_layer['ip.dst'] || '';
result[tcp_stream].destPort = getInt(tcp_layer['tcp.dstport']);
result[tcp_stream].frames = {};
}
const details = {};
details.content_length = getInt(http_layer['http.content_length_header']);
if (details.content_length === false) {
console.error('[WARNING] No http.content_length_header (assuming 0)', id);
details.content_length = 0;
}
/** @type {String} */
let tcp_payload;
if (tcp_segments !== null) {
tcp_payload = tcp_segments['tcp.reassembled.data'] || '';
} else {
tcp_payload = tcp_layer['tcp.payload'] || '';
}
// keep just the http body part of the payload (3 characters per byte e.g. ":FF")
let offset = tcp_payload.length - (details.content_length * 3);
if (offset < 0) {
console.warn(
'[WARNING] payload appears too small for http body',
id, details.content_length * 3, tcp_payload.length
);
offset = 0;
}
details.payload = tcp_payload.substring(offset);
if (http_layer['http.request_number'] !== undefined) {
details.is_http_request = true;
details.resp_in = getInt(http_layer['http.response_in']);
if (details.resp_in === false) {
console.error('[WARNING] Request has no reply', id);
}
/** @type {String} */
const uri = http_layer['http.request.full_uri'] || '';
const res = uri.match(urlRegExp);
if (res === null) {
console.error('[ERROR] Failed to parse uri', uri, id);
return;
}
details.type = res[1];
details.seq = getInt(res[2]);
if (details.seq === false) {
// perfectly valid for handshake packets to not have a seq
details.seq = 0;
}
} else {
details.is_http_request = false;
}
result[tcp_stream].frames[frame_num] = details;
});
return result;
}
/**
* Check for unused frames
* @param {Object} filtered
*/
function check(filtered) {
for (const stream of Object.values(filtered)) {
for (const frame of Object.values(stream.frames)) {
if (frame.is_http_request) {
frame.consumed = true;
if (frame.resp_in !== false) {
stream.frames[frame.resp_in].consumed = true;
}
}
}
}
let error = false;
for (const stream_id of Object.keys(filtered)) {
const stream = filtered[stream_id];
for (const frame_id of Object.keys(stream.frames)) {
const consumed = stream.frames[frame_id].consumed || false;
if (consumed !== true) {
const id = `[Stream: ${stream_id} Frame: ${frame_id}]`;
console.error('[WARNING] Unreferenced frame', id);
error = true;
}
}
}
return !error;
}
function getInt(input) {
const t = typeof input;
if (t !== 'string' && t !== 'number') {
return false;
}
return parseInt(input || 0, 10);
}
function text2Buffer(text) { return Buffer.from(text, 'utf-8'); }
function sha1(data) { return createHash('sha1').update(data).digest(); }
function sha256(data) { return createHash('sha256').update(data).digest(); }
/**
* Create a buffer from a wireshark hex string
* @param {String} hexString
* @returns {Buffer}
*/
function decode_payload(hexString) {
return Buffer.from(hexString.replaceAll(':', ''), 'hex');
}
async function read_dissections(filename) {
try {
const fp = await fs.open(filename);
const fileContents = await fp.readFile();
fp.close();
const dissections = JSON.parse(fileContents);
if (dissections.length === undefined) {
console.error('[ERROR] Invalid data in file (no length property)');
process.exit();
}
return dissections;
} catch (err) {
console.error('[ERROR] Failed to load dissections: "' + filename + '"', err.message);
process.exit();
}
}
async function write_filtered(obj, filename) {
console.log(' * Writing filtered data to:', filename);
try {
const fp = await fs.open(filename, 'w');
const fileContents = JSON.stringify(obj, null, 2);
await fp.writeFile(fileContents);
fp.close();
} catch (err) {
console.error('[WARNING] Failed to write file:', filename, err.message);
return false;
}
return true;
}
async function write_results(txt, filename) {
console.log(' * Writing results to:', filename);
try {
const fp = await fs.open(filename, 'w');
await fp.writeFile(txt);
fp.close();
} catch (err) {
console.error('[WARNING] Failed to write file:', filename, err.message);
return false;
}
return true;
}