@mherod/get-cookie 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/prettier.xml +8 -0
- package/dist/cli.js +3 -0
- package/dist/cli.js.map +1 -0
- package/dist/main.js +430 -0
- package/dist/main.js.map +1 -0
- package/package.json +37 -5
- package/src/cli.js +58 -0
- package/src/global.js +3 -0
- package/src/index.js +462 -0
- package/src/utils.js +185 -0
- package/cli.js +0 -48
- package/index.js +0 -424
- package/utils.js +0 -175
package/src/utils.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// noinspection JSUnusedGlobalSymbols
|
|
2
|
+
|
|
3
|
+
const { exec } = require("child_process");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
|
|
6
|
+
async function execSimple(command) {
|
|
7
|
+
if (typeof command !== "string") {
|
|
8
|
+
throw new TypeError("execSimple: command must be a string");
|
|
9
|
+
}
|
|
10
|
+
if (process.env.VERBOSE) {
|
|
11
|
+
console.log(command);
|
|
12
|
+
}
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
exec(
|
|
15
|
+
command,
|
|
16
|
+
{ encoding: "binary", maxBuffer: 5 * 1024 },
|
|
17
|
+
(error, stdout, stderr) => {
|
|
18
|
+
if (error) {
|
|
19
|
+
reject(error);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (stderr) {
|
|
23
|
+
reject(error);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (stdout) {
|
|
27
|
+
resolve(stdout.toString("utf8").trim());
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function execAsBuffer(command) {
|
|
35
|
+
if (typeof command !== "string") {
|
|
36
|
+
throw new TypeError("execAsBuffer: command must be a string");
|
|
37
|
+
}
|
|
38
|
+
if (process.env.VERBOSE) {
|
|
39
|
+
console.log(command);
|
|
40
|
+
}
|
|
41
|
+
return await new Promise((resolve, reject) => {
|
|
42
|
+
exec(
|
|
43
|
+
command,
|
|
44
|
+
{ encoding: "binary", maxBuffer: 5 * 1024 },
|
|
45
|
+
(error, stdout, stderr) => {
|
|
46
|
+
if (error) {
|
|
47
|
+
reject(error);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (stderr) {
|
|
51
|
+
reject(error);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
let stdoutAsBuffer = stdout;
|
|
55
|
+
if (typeof stdoutAsBuffer === "string" && stdoutAsBuffer.length > 0) {
|
|
56
|
+
// noinspection JSCheckFunctionSignatures
|
|
57
|
+
stdoutAsBuffer = Buffer.from(stdoutAsBuffer, "binary").slice(0, -1);
|
|
58
|
+
}
|
|
59
|
+
if (stdoutAsBuffer && stdoutAsBuffer.length > 0) {
|
|
60
|
+
resolve(stdoutAsBuffer);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function toStringValue(r) {
|
|
68
|
+
if (process.env.VERBOSE) {
|
|
69
|
+
console.log("Printing value", r);
|
|
70
|
+
}
|
|
71
|
+
if (r) {
|
|
72
|
+
if (typeof r === "string") {
|
|
73
|
+
return r;
|
|
74
|
+
} else if (r.toString) {
|
|
75
|
+
// noinspection JSCheckFunctionSignatures
|
|
76
|
+
return r.toString("utf8");
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function printStringValue(r) {
|
|
82
|
+
if (process.env.VERBOSE) {
|
|
83
|
+
console.log("Printing value", r);
|
|
84
|
+
}
|
|
85
|
+
if (r) {
|
|
86
|
+
if (typeof r === "string") {
|
|
87
|
+
console.log(r);
|
|
88
|
+
} else if (r.toString) {
|
|
89
|
+
// noinspection JSCheckFunctionSignatures
|
|
90
|
+
console.log(r.toString("utf8"));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
*
|
|
97
|
+
* @param result
|
|
98
|
+
* @returns {string|null}
|
|
99
|
+
*/
|
|
100
|
+
function toStringOrNull(result) {
|
|
101
|
+
if (result == null) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
if (process.env.VERBOSE) {
|
|
105
|
+
console.log("result", result);
|
|
106
|
+
}
|
|
107
|
+
if (typeof result === "string" && result.length > 0) {
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
if (result.slice && result.toString) {
|
|
111
|
+
// noinspection JSCheckFunctionSignatures
|
|
112
|
+
return result.toString("utf8");
|
|
113
|
+
}
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
*
|
|
119
|
+
* @param {string} file
|
|
120
|
+
* @param {string} sql
|
|
121
|
+
* @returns {Promise<Buffer[]>}
|
|
122
|
+
*/
|
|
123
|
+
async function doSqliteQuery1(file, sql) {
|
|
124
|
+
if (typeof sql !== "string") {
|
|
125
|
+
throw new TypeError("doSqliteQuery1: sql must be a string");
|
|
126
|
+
}
|
|
127
|
+
if (typeof file !== "string") {
|
|
128
|
+
throw new TypeError("doSqliteQuery1: file must be a string");
|
|
129
|
+
}
|
|
130
|
+
if (!fs.existsSync(file)) {
|
|
131
|
+
throw new Error(`doSqliteQuery1: file ${file} does not exist`);
|
|
132
|
+
}
|
|
133
|
+
if (process.env.VERBOSE) {
|
|
134
|
+
console.log(`doSqliteQuery1: file ${file}`);
|
|
135
|
+
console.log(`doSqliteQuery1: sql ${sql}`);
|
|
136
|
+
}
|
|
137
|
+
const sqlite3 = require("sqlite3");
|
|
138
|
+
const db = new sqlite3.Database(file);
|
|
139
|
+
return new Promise((resolve, reject) => {
|
|
140
|
+
db.all(sql, (err, rows) => {
|
|
141
|
+
if (err) {
|
|
142
|
+
if (process.env.VERBOSE) {
|
|
143
|
+
console.log(`doSqliteQuery1: error ${err}`);
|
|
144
|
+
}
|
|
145
|
+
reject(err);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const rows1 = rows;
|
|
149
|
+
if (rows1 == null || rows1.length === 0) {
|
|
150
|
+
if (process.env.VERBOSE) {
|
|
151
|
+
console.log(`doSqliteQuery1: no rows`);
|
|
152
|
+
}
|
|
153
|
+
resolve([]);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (Array.isArray(rows1)) {
|
|
157
|
+
if (process.env.VERBOSE) {
|
|
158
|
+
console.log(`doSqliteQuery1: ${rows1.length} rows`);
|
|
159
|
+
}
|
|
160
|
+
// noinspection JSCheckFunctionSignatures
|
|
161
|
+
const buffers = rows1
|
|
162
|
+
.flatMap((row) => Object.values(row))
|
|
163
|
+
.map((v) => Buffer.from(v, "binary"));
|
|
164
|
+
if (process.env.VERBOSE) {
|
|
165
|
+
console.log(`doSqliteQuery1: ${buffers.length} buffers`);
|
|
166
|
+
}
|
|
167
|
+
resolve(buffers);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (process.env.VERBOSE) {
|
|
171
|
+
console.log(`doSqliteQuery1: rows ${JSON.stringify(rows1)}`);
|
|
172
|
+
}
|
|
173
|
+
resolve([rows1]);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
module.exports = {
|
|
179
|
+
execSimple: execSimple,
|
|
180
|
+
execAsBuffer: execAsBuffer,
|
|
181
|
+
printStringValue: printStringValue,
|
|
182
|
+
toStringValue,
|
|
183
|
+
toStringOrNull: toStringOrNull,
|
|
184
|
+
doSqliteQuery1,
|
|
185
|
+
};
|
package/cli.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const {getCookie} = require("./index");
|
|
4
|
-
|
|
5
|
-
if (process.argv) {
|
|
6
|
-
if (process.argv.length > 2) {
|
|
7
|
-
const name = process.argv[2];
|
|
8
|
-
|
|
9
|
-
let domain;
|
|
10
|
-
if (process.argv[3] != null && process.argv[3].indexOf(".") > -1) {
|
|
11
|
-
domain = process.argv[3];
|
|
12
|
-
} else {
|
|
13
|
-
domain = '%';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (process.argv.includes("--require-jwt")) {
|
|
17
|
-
process.env.REQUIRE_JWT = "true";
|
|
18
|
-
}
|
|
19
|
-
if (process.argv.includes('--verbose')) {
|
|
20
|
-
process.env.VERBOSE = "true";
|
|
21
|
-
}
|
|
22
|
-
if (process.argv.includes("--chrome-only")) {
|
|
23
|
-
process.env.CHROME_ONLY = "true";
|
|
24
|
-
}
|
|
25
|
-
if (process.argv.includes("--firefox-only")) {
|
|
26
|
-
process.env.FIREFOX_ONLY = "true";
|
|
27
|
-
}
|
|
28
|
-
if (process.argv.includes("--ignore-expired")) {
|
|
29
|
-
process.env.IGNORE_EXPIRED = "true";
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (process.env.VERBOSE) {
|
|
33
|
-
console.log("Verbose mode", process.argv);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
getCookie({
|
|
37
|
-
name: name,
|
|
38
|
-
domain: domain,
|
|
39
|
-
requireJwt: (process.env.REQUIRE_JWT === "true"),
|
|
40
|
-
}).then(cookie => {
|
|
41
|
-
if (typeof cookie === "string") {
|
|
42
|
-
console.log(cookie);
|
|
43
|
-
}
|
|
44
|
-
}).catch(err => {
|
|
45
|
-
console.error(err);
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
}
|
package/index.js
DELETED
|
@@ -1,424 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
if (process.platform !== 'darwin') {
|
|
4
|
-
throw new Error('This script only works on macOS');
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
const crypto = require('crypto');
|
|
8
|
-
const fs = require("fs");
|
|
9
|
-
const {execSimple, toStringOrNull, doSqliteQuery1, printStringValue, toStringValue} = require("./utils");
|
|
10
|
-
const jsonwebtoken = require('jsonwebtoken');
|
|
11
|
-
|
|
12
|
-
function isValidJwt(token) {
|
|
13
|
-
if (typeof token !== 'string') {
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
try {
|
|
17
|
-
const result = jsonwebtoken.decode(token, {complete: true});
|
|
18
|
-
if (process.env.VERBOSE) {
|
|
19
|
-
console.log(result);
|
|
20
|
-
}
|
|
21
|
-
return true;
|
|
22
|
-
} catch (err) {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
*
|
|
29
|
-
* @returns {Promise<string>}
|
|
30
|
-
*/
|
|
31
|
-
async function getChromePassword() {
|
|
32
|
-
return await execSimple("security find-generic-password -w -s \"Chrome Safe Storage\"");
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
*
|
|
37
|
-
* @param params
|
|
38
|
-
* @returns {Promise<string>}
|
|
39
|
-
*/
|
|
40
|
-
async function getCookie(params) {
|
|
41
|
-
if (process.env.CHROME_ONLY) {
|
|
42
|
-
if (process.env.VERBOSE) {
|
|
43
|
-
console.log('chrome only');
|
|
44
|
-
}
|
|
45
|
-
return await getChromeCookie(params)
|
|
46
|
-
.then(cookie => {
|
|
47
|
-
if (cookie && cookie.length > 0) {
|
|
48
|
-
return toStringValue(cookie);
|
|
49
|
-
}
|
|
50
|
-
})
|
|
51
|
-
.catch(err => {
|
|
52
|
-
if (process.env.VERBOSE) {
|
|
53
|
-
console.error(err);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
} else if (process.env.FIREFOX_ONLY) {
|
|
57
|
-
if (process.env.VERBOSE) {
|
|
58
|
-
console.log('firefox only');
|
|
59
|
-
}
|
|
60
|
-
return await getFirefoxCookie(params)
|
|
61
|
-
.then(b => b.toString('utf8'))
|
|
62
|
-
.then(cookie => {
|
|
63
|
-
if (cookie && cookie.length > 0) {
|
|
64
|
-
return toStringValue(cookie);
|
|
65
|
-
} else {
|
|
66
|
-
console.log('No cookie found');
|
|
67
|
-
}
|
|
68
|
-
})
|
|
69
|
-
.catch(err => {
|
|
70
|
-
if (process.env.VERBOSE) {
|
|
71
|
-
console.error("Error getting Firefox cookie", err);
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
} else {
|
|
75
|
-
return await getChromeCookie(params)
|
|
76
|
-
.catch(err => {
|
|
77
|
-
if (process.env.VERBOSE) {
|
|
78
|
-
console.error("Error getting Chrome cookie", err);
|
|
79
|
-
}
|
|
80
|
-
})
|
|
81
|
-
.then(r => {
|
|
82
|
-
if (typeof r === 'string' && r.trim().length > 0) {
|
|
83
|
-
return r;
|
|
84
|
-
} else {
|
|
85
|
-
return getFirefoxCookie(params)
|
|
86
|
-
.catch(err => {
|
|
87
|
-
if (process.env.VERBOSE) {
|
|
88
|
-
console.error("Error getting Firefox cookie", err);
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
})
|
|
93
|
-
.catch((e) => {
|
|
94
|
-
if (process.env.VERBOSE) {
|
|
95
|
-
console.error("Error getting Chrome or Firefox cookie", e);
|
|
96
|
-
}
|
|
97
|
-
})
|
|
98
|
-
.then(toStringValue);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
*
|
|
104
|
-
* @param name
|
|
105
|
-
* @param domain
|
|
106
|
-
* @returns {Promise<Buffer>}
|
|
107
|
-
*/
|
|
108
|
-
async function getFirefoxCookie({name, domain}) {
|
|
109
|
-
if (name && typeof name !== 'string') {
|
|
110
|
-
throw new Error('name must be a string');
|
|
111
|
-
}
|
|
112
|
-
if (domain && typeof domain !== 'string') {
|
|
113
|
-
throw new Error('domain must be a string');
|
|
114
|
-
}
|
|
115
|
-
const [file] = await findAllFiles({
|
|
116
|
-
path: `${process.env.HOME}/Library/Application Support/Firefox/Profiles`,
|
|
117
|
-
name: "cookies.sqlite"
|
|
118
|
-
})
|
|
119
|
-
if (file && !fs.existsSync(file)) {
|
|
120
|
-
throw new Error(`File ${file} does not exist`);
|
|
121
|
-
}
|
|
122
|
-
if (process.env.VERBOSE) {
|
|
123
|
-
console.log(`Trying Firefox cookie ${name} for domain ${domain}`);
|
|
124
|
-
}
|
|
125
|
-
let sql;
|
|
126
|
-
sql = "SELECT value FROM moz_cookies";
|
|
127
|
-
if (typeof name === 'string' || typeof domain === 'string') {
|
|
128
|
-
sql += ` WHERE `;
|
|
129
|
-
if (typeof name === 'string') {
|
|
130
|
-
sql += `name = '${name}'`;
|
|
131
|
-
if (typeof domain === 'string') {
|
|
132
|
-
sql += ` AND `;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (typeof domain === 'string') {
|
|
136
|
-
sql += `host LIKE '${domain}';`;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
return await doSqliteQuery1(file, sql).then(rows => {
|
|
140
|
-
return rows.map(toStringOrNull).find(v => v !== null);
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const defaultChromeRoot = `${process.env.HOME}/Library/Application Support/Google/Chrome`;
|
|
145
|
-
const defaultChromeCookies = `${defaultChromeRoot}/Default/Cookies`;
|
|
146
|
-
|
|
147
|
-
async function getEncryptedChromeCookie({name, domain, file = defaultChromeCookies}) {
|
|
148
|
-
if (name && typeof name !== 'string') {
|
|
149
|
-
throw new Error('name must be a string');
|
|
150
|
-
}
|
|
151
|
-
if (domain && typeof domain !== 'string') {
|
|
152
|
-
throw new Error('domain must be a string');
|
|
153
|
-
}
|
|
154
|
-
if (file && typeof file !== 'string') {
|
|
155
|
-
throw new Error('file must be a string');
|
|
156
|
-
}
|
|
157
|
-
if (!fs.existsSync(file)) {
|
|
158
|
-
throw new Error(`File ${file} does not exist`);
|
|
159
|
-
}
|
|
160
|
-
if (process.env.VERBOSE) {
|
|
161
|
-
console.log(`Trying Chrome (at ${file.split('/').slice(-3).join('/')}) cookie ${name} for domain ${domain}`);
|
|
162
|
-
}
|
|
163
|
-
let sql;
|
|
164
|
-
sql = `SELECT encrypted_value FROM cookies`;
|
|
165
|
-
if (typeof name === 'string' || typeof domain === 'string') {
|
|
166
|
-
sql += ` WHERE `;
|
|
167
|
-
if (typeof name === 'string') {
|
|
168
|
-
sql += `name = '${name}'`;
|
|
169
|
-
if (typeof domain === 'string') {
|
|
170
|
-
sql += ` AND `;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
if (typeof domain === 'string') {
|
|
174
|
-
sql += `host_key LIKE '${domain}';`;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
return await doSqliteQuery1(file, sql);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
*
|
|
182
|
-
* @param {string} password
|
|
183
|
-
* @param {Buffer} encryptedData
|
|
184
|
-
* @returns {Promise<string>}
|
|
185
|
-
*/
|
|
186
|
-
async function decrypt(password, encryptedData) {
|
|
187
|
-
if (typeof password !== 'string') {
|
|
188
|
-
throw new Error('password must be a string: ' + password);
|
|
189
|
-
}
|
|
190
|
-
let encryptedData1;
|
|
191
|
-
encryptedData1 = encryptedData;
|
|
192
|
-
if (encryptedData1 == null || typeof encryptedData1 !== 'object') {
|
|
193
|
-
throw new Error('encryptedData must be a object: ' + encryptedData1);
|
|
194
|
-
}
|
|
195
|
-
if (!(encryptedData1 instanceof Buffer)) {
|
|
196
|
-
if (Array.isArray(encryptedData1) && encryptedData1[0] instanceof Buffer) {
|
|
197
|
-
[encryptedData1] = encryptedData1;
|
|
198
|
-
if (process.env.VERBOSE) {
|
|
199
|
-
console.log(`encryptedData is an array of buffers, selected first: ${encryptedData1}`);
|
|
200
|
-
}
|
|
201
|
-
} else {
|
|
202
|
-
throw new Error('encryptedData must be a Buffer: ' + encryptedData1);
|
|
203
|
-
}
|
|
204
|
-
encryptedData1 = Buffer.from(encryptedData1);
|
|
205
|
-
}
|
|
206
|
-
if (process.env.VERBOSE) {
|
|
207
|
-
console.log(`Trying to decrypt with password ${password}`);
|
|
208
|
-
}
|
|
209
|
-
return await new Promise((resolve, reject) => {
|
|
210
|
-
crypto.pbkdf2(password, 'saltysalt', 1003, 16, 'sha1', (error, buffer) => {
|
|
211
|
-
try {
|
|
212
|
-
if (error) {
|
|
213
|
-
if (process.env.VERBOSE) {
|
|
214
|
-
console.log("Error doing pbkdf2", error);
|
|
215
|
-
}
|
|
216
|
-
reject(error);
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
if (buffer.length !== 16) {
|
|
221
|
-
if (process.env.VERBOSE) {
|
|
222
|
-
console.log("Error doing pbkdf2, buffer length is not 16", buffer.length);
|
|
223
|
-
}
|
|
224
|
-
reject(new Error('Buffer length is not 16'));
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const iv = new Buffer.from(new Array(17).join(' '), 'binary');
|
|
229
|
-
const decipher = crypto.createDecipheriv('aes-128-cbc', buffer, iv);
|
|
230
|
-
decipher.setAutoPadding(false);
|
|
231
|
-
|
|
232
|
-
if (encryptedData1 && encryptedData1.slice) {
|
|
233
|
-
encryptedData1 = encryptedData1.slice(3);
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
if (encryptedData1.length % 16 !== 0) {
|
|
237
|
-
if (process.env.VERBOSE) {
|
|
238
|
-
console.log("Error doing pbkdf2, encryptedData length is not a multiple of 16", encryptedData1.length);
|
|
239
|
-
}
|
|
240
|
-
reject(new Error('encryptedData length is not a multiple of 16'));
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
let decoded = decipher.update(encryptedData1);
|
|
245
|
-
try {
|
|
246
|
-
decipher.final('utf-8');
|
|
247
|
-
} catch (e) {
|
|
248
|
-
if (process.env.VERBOSE) {
|
|
249
|
-
console.log("Error doing decipher.final()", e);
|
|
250
|
-
}
|
|
251
|
-
reject(e);
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
let padding = decoded[decoded.length - 1];
|
|
256
|
-
if (padding) {
|
|
257
|
-
decoded = decoded.slice(0, 0 - padding);
|
|
258
|
-
}
|
|
259
|
-
// noinspection JSCheckFunctionSignatures
|
|
260
|
-
decoded = decoded.toString('utf8');
|
|
261
|
-
resolve(decoded);
|
|
262
|
-
} catch (e) {
|
|
263
|
-
reject(e);
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
*
|
|
271
|
-
* @param {string|undefined} name
|
|
272
|
-
* @param {string} domain
|
|
273
|
-
* @param {boolean} requireJwt
|
|
274
|
-
* @returns {Promise<string>}
|
|
275
|
-
*/
|
|
276
|
-
async function getChromeCookie({name, domain = '%', requireJwt = false}) {
|
|
277
|
-
if (name && typeof name !== 'string') {
|
|
278
|
-
throw new Error('name must be a string');
|
|
279
|
-
}
|
|
280
|
-
if (typeof domain !== 'string') {
|
|
281
|
-
throw new Error('domain must be a string');
|
|
282
|
-
}
|
|
283
|
-
const encryptedDataItems = await findAllFiles({
|
|
284
|
-
path: defaultChromeRoot,
|
|
285
|
-
name: 'Cookies'
|
|
286
|
-
}).then(files => {
|
|
287
|
-
const promises = files.map(file => {
|
|
288
|
-
return getEncryptedChromeCookie({
|
|
289
|
-
name: name,
|
|
290
|
-
domain: domain,
|
|
291
|
-
file: file
|
|
292
|
-
}).catch(e => {
|
|
293
|
-
if (process.env.VERBOSE) {
|
|
294
|
-
console.log("Error getting encrypted cookie", e);
|
|
295
|
-
}
|
|
296
|
-
return [];
|
|
297
|
-
});
|
|
298
|
-
});
|
|
299
|
-
return Promise.all(promises).then(results => results.flat()).then(results => {
|
|
300
|
-
if (process.env.VERBOSE) {
|
|
301
|
-
console.log("getEncryptedChromeCookie results", results);
|
|
302
|
-
}
|
|
303
|
-
return results.filter(result => {
|
|
304
|
-
return result;
|
|
305
|
-
});
|
|
306
|
-
});
|
|
307
|
-
}).catch(error => {
|
|
308
|
-
if (process.env.VERBOSE) {
|
|
309
|
-
console.log('error', error);
|
|
310
|
-
}
|
|
311
|
-
return [];
|
|
312
|
-
});
|
|
313
|
-
const password = await getChromePassword();
|
|
314
|
-
if (process.env.VERBOSE) {
|
|
315
|
-
console.log('encryptedDataItems', encryptedDataItems);
|
|
316
|
-
}
|
|
317
|
-
const decrypted = encryptedDataItems.filter(encryptedData => {
|
|
318
|
-
return encryptedData != null && encryptedData.length > 0;
|
|
319
|
-
}).map(async encryptedData => {
|
|
320
|
-
if (process.env.VERBOSE) {
|
|
321
|
-
console.log("Received encrypted", encryptedData);
|
|
322
|
-
}
|
|
323
|
-
let decrypted;
|
|
324
|
-
try {
|
|
325
|
-
decrypted = await decrypt(password, encryptedData);
|
|
326
|
-
} catch (e) {
|
|
327
|
-
if (process.env.VERBOSE) {
|
|
328
|
-
console.log("Error decrypting cookie", e);
|
|
329
|
-
}
|
|
330
|
-
return null;
|
|
331
|
-
}
|
|
332
|
-
if (decrypted) {
|
|
333
|
-
if (process.env.VERBOSE) {
|
|
334
|
-
console.log("Decrypted", decrypted);
|
|
335
|
-
}
|
|
336
|
-
return decrypted;
|
|
337
|
-
}
|
|
338
|
-
return null;
|
|
339
|
-
});
|
|
340
|
-
const results = await Promise.all(decrypted);
|
|
341
|
-
if (process.env.VERBOSE) {
|
|
342
|
-
console.log('results', results);
|
|
343
|
-
}
|
|
344
|
-
return results.map(toStringOrNull).find(result => {
|
|
345
|
-
return typeof result === 'string' && result.length > 0 && (requireJwt === false || isValidJwt(result));
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
*
|
|
351
|
-
* @param path
|
|
352
|
-
* @param name
|
|
353
|
-
* @param rootSegments
|
|
354
|
-
* @param maxDepth
|
|
355
|
-
* @returns {Promise<[string]>}
|
|
356
|
-
*/
|
|
357
|
-
async function findAllFiles({path, name, rootSegments = path.split('/').length, maxDepth = 2}) {
|
|
358
|
-
if (typeof path !== 'string') {
|
|
359
|
-
throw new Error('path must be a string');
|
|
360
|
-
}
|
|
361
|
-
if (typeof name !== 'string') {
|
|
362
|
-
throw new Error('name must be a string');
|
|
363
|
-
}
|
|
364
|
-
if (process.env.VERBOSE) {
|
|
365
|
-
console.log(`Searching for ${name} in ${path}`);
|
|
366
|
-
}
|
|
367
|
-
const files = [];
|
|
368
|
-
let readdirSync;
|
|
369
|
-
try {
|
|
370
|
-
readdirSync = fs.readdirSync(path);
|
|
371
|
-
} catch (e) {
|
|
372
|
-
if (process.env.VERBOSE) {
|
|
373
|
-
console.log(`Error reading ${path}`, e);
|
|
374
|
-
}
|
|
375
|
-
return files;
|
|
376
|
-
}
|
|
377
|
-
for (const file of readdirSync) {
|
|
378
|
-
const filePath = path + '/' + file;
|
|
379
|
-
let stat;
|
|
380
|
-
try {
|
|
381
|
-
stat = fs.statSync(filePath);
|
|
382
|
-
} catch (e) {
|
|
383
|
-
if (process.env.VERBOSE) {
|
|
384
|
-
console.error(`Error getting stat for ${filePath}`, e);
|
|
385
|
-
}
|
|
386
|
-
continue;
|
|
387
|
-
}
|
|
388
|
-
if (stat.isDirectory()) {
|
|
389
|
-
if (filePath.split('/').length < rootSegments + maxDepth) {
|
|
390
|
-
try {
|
|
391
|
-
const subFiles = await findAllFiles({
|
|
392
|
-
path: filePath,
|
|
393
|
-
name: name,
|
|
394
|
-
rootSegments: rootSegments,
|
|
395
|
-
maxDepth: 2
|
|
396
|
-
});
|
|
397
|
-
files.push(...subFiles);
|
|
398
|
-
} catch (e) {
|
|
399
|
-
if (process.env.VERBOSE) {
|
|
400
|
-
console.error(e);
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
} else if (file === name) {
|
|
405
|
-
files.push(filePath);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
if (process.env.VERBOSE) {
|
|
409
|
-
if (files.length > 0) {
|
|
410
|
-
console.log(`Found ${(files.length)} ${name} files`);
|
|
411
|
-
console.log(files);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
return files;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
// noinspection JSUnusedGlobalSymbols
|
|
418
|
-
module.exports = {
|
|
419
|
-
getDecryptedCookie: getChromeCookie,
|
|
420
|
-
getChromeCookie,
|
|
421
|
-
getFirefoxCookie,
|
|
422
|
-
getCookie,
|
|
423
|
-
decrypt
|
|
424
|
-
};
|