-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjsretk-strings.js
280 lines (250 loc) · 9.9 KB
/
jsretk-strings.js
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
#!/usr/bin/env node
// Author: Sean Pesce
//
// This script extracts string literals from JavaScript code. Useful for quickly analyzing minified
// or obfuscated code. Similar to running "strings" against a compiled binary executable.
// Optionally also extracts JavaScript comments and RegEx literals.
//
// System package requirements:
// curl
// nodejs 18.3+
//
// NodeJS package requirements:
// esprima
const { parseArgs } = require('util'); // Requires nodejs 18.3+
const fs = require('fs');
const path = require('path');
const jsretk = require('./lib/jsretk');
function printUsage() {
console.log(`Usage:\n\n${path.basename(process.argv[0])} ${path.basename(process.argv[1])} [OPTIONS] <JS_FILE_1> [[JS_FILE_2] ...]` +
'\n\nOptions:\n' +
'\n\t[-h|--help]\t\tPrint usage and exit' +
'\n\t[-P|--stdin]\t\tPipe data from stdin' +
'\n\t[-c|--comments]\t\tInclude JavaScript comments in output' +
'\n\t[-C|--comments-only]\tFind ONLY JavaScript comments (no string/RegEx literals; overrides "-c")' +
'\n\t[-r|--regex]\t\tInclude Regular Expression (RegEx) literals in output' +
'\n\t[-R|--regex-only]\tFind ONLY RegEx literals (no comments/string literals; overrides "-r")' +
'\n\t[-T|--templates-only]\tFind ONLY template strings (no static string/RegEx literals or comments)' +
'\n\t[-m|--min]\t\tFind strings of this length or longer (inclusive)' +
'\n\t[-M|--max]\t\tFind strings of this length or shorter (inclusive)' +
'\n\t[-x|--match-regex] <ex>\tFind strings that match the given Regular Expression' +
'\n\t[-k|--insecure]\t\tDon\'t verify TLS/SSL certificates for connections when fetching remotely-hosted JS files' +
'\n\t[-p|--curl-path] <path>\tNon-standard path/name for the curl command' +
`\n\t[-B|--max-buffer] <n>\tMaximum size (in bytes) for remotely-fetched JS files (default: ${Math.floor(jsretk.DEFAULT_MAX_BUF_SZ/jsretk.ONE_MEGABYTE)}MB)` +
`\n\t[-E|--encoding] <enc>\tText encoding for local input/output files (default: "${jsretk.DEFAULT_TEXT_ENCODING}")` +
'\n\t[-i|--interactive]\tEnter interactive NodeJS prompt after completion'
);
process.exit();
}
function main(isInteractiveMode) {
// Parse command-line arguments
const argsConfig = {
args: process.argv.slice(2),
strict: true,
allowPositionals: true,
options: {
// Print usage instructions
'help': {
type: 'boolean',
short: 'h',
default: false,
},
// Pipe data from stdin
'stdin': {
type: 'boolean',
short: 'P',
default: false,
},
// Check whether to verify TLS/SSL certificates for remotely-fetched JS files
'insecure': {
type: 'boolean',
short: 'k',
default: false,
},
// Custom curl path/command
'curl-path': {
type: 'string',
short: 'p',
default: 'curl',
},
// Maximum buffer size for remotely-fetched JS files
'max-buffer': {
type: 'string',
short: 'B',
default: jsretk.DEFAULT_MAX_BUF_SZ.toString(),
},
// Text encoding for local input/output files
'encoding': {
type: 'string',
short: 'E',
default: jsretk.DEFAULT_TEXT_ENCODING,
},
// Optional interactive mode to play with the data after execution (e.g., for troubleshooting)
'interactive': {
type: 'boolean',
short: 'i',
default: false,
},
// Check whether to extract JavaScript comments
'comments': {
type: 'boolean',
short: 'c',
default: false,
},
'comments-only': {
type: 'boolean',
short: 'C',
default: false,
},
// Check whether to extract Regular Expressions (RegEx)
'regex': {
type: 'boolean',
short: 'r',
default: false,
},
'regex-only': {
type: 'boolean',
short: 'R',
default: false,
},
'templates-only': {
type: 'boolean',
short: 'T',
default: false,
},
// Minimum string length (inclusive)
'min': {
type: 'string',
short: 'm',
default: '0',
},
// Maximum string length (inclusive); negative means no maximum
'max': {
type: 'string',
short: 'M',
default: '-1',
},
// Find strings that match the given Regular Expression
'match-regex': {
type: 'string',
short: 'x',
default: '',
},
}
};
const parsedArgs = parseArgs(argsConfig);
const args = parsedArgs.values;
const inputFiles = parsedArgs.positionals;
var includeStringLiterals = true;
var includeTemplateLiterals = true;
if (args['help'] || (inputFiles.length < 1 && !args['stdin'])) {
printUsage();
}
const exclusiveFlagsErrMsg = 'Error: Only one of -C|--comments-only, -R|--regex-only, -T|--templates-only can be provided simultaneously';
if (args['comments-only']) {
args['comments'] = true;
args['regex'] = false;
includeStringLiterals = false;
includeTemplateLiterals = false;
if (args['regex-only'] || args['templates-only']) {
console.error(exclusiveFlagsErrMsg);
process.exit(2);
}
}
if (args['regex-only']) {
args['regex'] = true;
args['comments'] = false;
includeStringLiterals = false;
includeTemplateLiterals = false;
if (args['comments-only'] || args['templates-only']) {
console.error(exclusiveFlagsErrMsg);
process.exit(2);
}
}
if (args['templates-only']) {
args['comments'] = false;
args['regex'] = false;
includeStringLiterals = false;
includeTemplateLiterals = true;
if (args['comments-only'] || args['regex-only']) {
console.error(exclusiveFlagsErrMsg);
process.exit(2);
}
}
args['min'] = parseInt(args['min']);
if (args['min'] < 0) {
throw RangeError(`-m|--min must be non-negative (received ${args['min']})`);
}
args['max'] = parseInt(args['max']);
if (args['max'] >= 0 && args['min'] > args['max']) {
throw RangeError(`-M|--max must be greater than or equal to -m|--min (received min=${args['min']}, max=${args['max']})`);
}
args['max-buffer'] = parseInt(args['max-buffer']);
if (args['max-buffer'] <= 0) {
throw RangeError(`-B|--max-buffer must be non-negative (received ${args['max-buffer']})`);
}
if (args['match-regex'] == '') {
args['match-regex'] = null;
}
var parsedFileCount = 0;
var inFilePath = null;
var inFileData = null;
if (args['stdin']) {
if (inputFiles.length === 0 || inputFiles[0] !== process.stdin.fd) {
inputFiles.unshift(process.stdin.fd);
}
}
// For each input file, extract the data
for (var i = 0; i < inputFiles.length; i++) {
inFilePath = inputFiles[i];
if (inFilePath !== process.stdin.fd && (inFilePath.toLowerCase().startsWith('http://') || inFilePath.toLowerCase().startsWith('https://'))) {
// Remote JS file
inFileData = jsretk.httpGetSync(inFilePath, {verifyCert: !args['insecure'], curlCmd: args['curl-path'], maxBuffer: args['max-buffer']});
} else {
// Local JS file
inFileData = fs.readFileSync(inFilePath, args['encoding']);
}
// If the first line of the file is a hashbang/shebang statement, comment it out before parsing
// (otherwise esprima throws an error, as hashbangs are not valid JS)
if (inFileData.startsWith('#!')) {
inFileData = `//${inFileData.slice(2)}`;
}
jsretk.getStringTokens(inFileData, {
doPrint: true,
includeStringLiterals: includeStringLiterals,
includeTemplateLiterals: includeTemplateLiterals,
includeComments: args['comments'],
includeRegex: args['regex'],
minLength: args['min'],
maxLength: args['max'],
matchRegex: args['match-regex']
});
parsedFileCount += 1;
}
if (parsedFileCount === 0) {
printUsage();
}
// Optional interactive mode to play with the data after execution (e.g., for troubleshooting)
if (isInteractiveMode == null) {
// Prevent recursive REPL
isInteractiveMode = true;
}
if (!isInteractiveMode && args['interactive']) {
var prompt = require('repl').start('> ');
prompt.context.parseArgs = parseArgs;
prompt.context.main = main;
prompt.context.printUsage = printUsage;
prompt.context.jsretk = jsretk;
prompt.context.parsedArgs = parsedArgs;
prompt.context.args = args;
prompt.context.inputFiles = inputFiles;
prompt.context.inFilePath = inFilePath;
prompt.context.inFileData = inFileData;
prompt.context.parsedFileCount = parsedFileCount;
prompt.context.includeStringLiterals = includeStringLiterals;
prompt.context.includeTemplateLiterals = includeTemplateLiterals;
}
}
if (require.main === module) {
main(false);
}