muaddib-scanner 2.2.19 → 2.2.22
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/.gitattributes +18 -0
- package/README.fr.md +819 -825
- package/README.md +822 -822
- package/bin/muaddib.js +868 -849
- package/docker/Dockerfile +19 -18
- package/docker/sandbox-runner.sh +313 -292
- package/package.json +61 -61
- package/src/commands/evaluate.js +484 -484
- package/src/diff.js +411 -411
- package/src/hooks-init.js +264 -258
- package/src/index.js +439 -437
- package/src/ioc/scraper.js +1293 -1293
- package/src/monitor.js +1817 -1764
- package/src/sandbox.js +631 -620
- package/src/scanner/ast-detectors.js +946 -933
- package/src/scanner/ast.js +96 -96
- package/src/scanner/github-actions.js +96 -96
- package/src/scanner/module-graph.js +2 -2
- package/src/scanner/obfuscation.js +128 -128
- package/src/scanner/shell.js +48 -48
- package/src/shared/download.js +181 -171
- package/src/webhook.js +413 -353
package/src/sandbox.js
CHANGED
|
@@ -1,620 +1,631 @@
|
|
|
1
|
-
const { execSync, spawn } = require('child_process');
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
'
|
|
20
|
-
'
|
|
21
|
-
'
|
|
22
|
-
'
|
|
23
|
-
'
|
|
24
|
-
'
|
|
25
|
-
'npmjs.
|
|
26
|
-
'
|
|
27
|
-
'
|
|
28
|
-
'
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
{ pattern: /\
|
|
54
|
-
{ pattern: /\
|
|
55
|
-
{ pattern: /
|
|
56
|
-
{ pattern:
|
|
57
|
-
{ pattern: /
|
|
58
|
-
{ pattern:
|
|
59
|
-
{ pattern: /\
|
|
60
|
-
{ pattern:
|
|
61
|
-
{ pattern:
|
|
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
|
-
const
|
|
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
|
-
const
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
let
|
|
154
|
-
let
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
'
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
'--
|
|
163
|
-
'--
|
|
164
|
-
'--
|
|
165
|
-
'--
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
dockerArgs.push('-e', `
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
dockerArgs.push('--tmpfs', '/
|
|
188
|
-
dockerArgs.push('--
|
|
189
|
-
|
|
190
|
-
dockerArgs.push('--
|
|
191
|
-
|
|
192
|
-
dockerArgs.push(
|
|
193
|
-
dockerArgs.push(
|
|
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
|
-
const
|
|
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
|
-
if (
|
|
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
|
-
for (const [
|
|
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
|
-
if (
|
|
380
|
-
score +=
|
|
381
|
-
findings.push({ type: '
|
|
382
|
-
} else if (
|
|
383
|
-
score +=
|
|
384
|
-
findings.push({ type: '
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
if (
|
|
394
|
-
score +=
|
|
395
|
-
findings.push({ type: '
|
|
396
|
-
} else
|
|
397
|
-
score +=
|
|
398
|
-
findings.push({ type: '
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
//
|
|
403
|
-
for (const
|
|
404
|
-
if (
|
|
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
|
-
lines
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
lines.push(
|
|
504
|
-
lines.push(`${BOLD}${
|
|
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
|
-
const
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
if (
|
|
576
|
-
lines.push('');
|
|
577
|
-
lines.push(`${BOLD}${
|
|
578
|
-
for (const
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
1
|
+
const { execSync, execFileSync, spawn } = require('child_process');
|
|
2
|
+
const crypto = require('crypto');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const {
|
|
5
|
+
generateCanaryTokens,
|
|
6
|
+
createCanaryEnvFile,
|
|
7
|
+
createCanaryNpmrc,
|
|
8
|
+
detectCanaryExfiltration,
|
|
9
|
+
detectCanaryInOutput
|
|
10
|
+
} = require('./canary-tokens.js');
|
|
11
|
+
|
|
12
|
+
const { NPM_PACKAGE_REGEX } = require('./shared/constants.js');
|
|
13
|
+
|
|
14
|
+
const DOCKER_IMAGE = 'muaddib-sandbox';
|
|
15
|
+
const CONTAINER_TIMEOUT = 120000; // 120 seconds
|
|
16
|
+
|
|
17
|
+
// Domains excluded from network findings (false positives)
|
|
18
|
+
const SAFE_DOMAINS = [
|
|
19
|
+
'registry.npmjs.org',
|
|
20
|
+
'github.com',
|
|
21
|
+
'objects.githubusercontent.com',
|
|
22
|
+
'api.github.com',
|
|
23
|
+
'raw.githubusercontent.com',
|
|
24
|
+
'codeload.github.com',
|
|
25
|
+
'npmjs.com',
|
|
26
|
+
'npmjs.org',
|
|
27
|
+
'yarnpkg.com',
|
|
28
|
+
'googleapis.com',
|
|
29
|
+
'cloudflare.com'
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
// IPs/ports excluded from connection findings (false positives)
|
|
33
|
+
const SAFE_IPS = ['127.0.0.1'];
|
|
34
|
+
const PROBE_PORTS = [65535]; // Node.js internal connectivity checks
|
|
35
|
+
|
|
36
|
+
// Commands that are always suspicious in a sandbox
|
|
37
|
+
const DANGEROUS_CMDS = ['curl', 'wget', 'nc', 'netcat', 'python', 'python3', 'bash', 'sh'];
|
|
38
|
+
|
|
39
|
+
// Static canary tokens injected by sandbox-runner.sh (fallback honeypots).
|
|
40
|
+
// These are searched in the sandbox report as a complement to the dynamic
|
|
41
|
+
// tokens from canary-tokens.js (which use random suffixes per session).
|
|
42
|
+
const STATIC_CANARY_TOKENS = {
|
|
43
|
+
GITHUB_TOKEN: 'MUADDIB_CANARY_GITHUB_f8k3t0k3n',
|
|
44
|
+
NPM_TOKEN: 'MUADDIB_CANARY_NPM_s3cr3tt0k3n',
|
|
45
|
+
AWS_ACCESS_KEY_ID: 'MUADDIB_CANARY_AKIAIOSFODNN7EXAMPLE',
|
|
46
|
+
AWS_SECRET_ACCESS_KEY: 'MUADDIB_CANARY_wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
|
|
47
|
+
SLACK_WEBHOOK_URL: 'MUADDIB_CANARY_SLACK',
|
|
48
|
+
DISCORD_WEBHOOK_URL: 'MUADDIB_CANARY_DISCORD'
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Patterns indicating data exfiltration in HTTP bodies
|
|
52
|
+
const EXFIL_PATTERNS = [
|
|
53
|
+
{ pattern: /\bNPM_TOKEN\b/i, label: 'npm token', severity: 'CRITICAL' },
|
|
54
|
+
{ pattern: /\bGITHUB_TOKEN\b/i, label: 'GitHub token', severity: 'CRITICAL' },
|
|
55
|
+
{ pattern: /\bAWS_SECRET/i, label: 'AWS credentials', severity: 'CRITICAL' },
|
|
56
|
+
{ pattern: /npmrc/i, label: '.npmrc content', severity: 'CRITICAL' },
|
|
57
|
+
{ pattern: /\bssh-rsa\b|\bssh-ed25519\b/i, label: 'SSH key', severity: 'CRITICAL' },
|
|
58
|
+
{ pattern: /BEGIN (RSA |EC |DSA |OPENSSH )?PRIVATE KEY/, label: 'private key', severity: 'CRITICAL' },
|
|
59
|
+
{ pattern: /\bpassword\b/i, label: 'password', severity: 'CRITICAL' },
|
|
60
|
+
{ pattern: /\btoken\b/i, label: 'token', severity: 'CRITICAL' },
|
|
61
|
+
{ pattern: /\/etc\/passwd/, label: 'passwd file', severity: 'HIGH' },
|
|
62
|
+
{ pattern: /\.env\b/, label: '.env content', severity: 'HIGH' }
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
// ── Docker availability checks ──
|
|
66
|
+
|
|
67
|
+
function isDockerAvailable() {
|
|
68
|
+
try {
|
|
69
|
+
execSync('docker info', { stdio: 'pipe', timeout: 10000 });
|
|
70
|
+
return true;
|
|
71
|
+
} catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function imageExists() {
|
|
77
|
+
try {
|
|
78
|
+
execFileSync('docker', ['image', 'inspect', DOCKER_IMAGE], { stdio: 'pipe', timeout: 10000 });
|
|
79
|
+
return true;
|
|
80
|
+
} catch {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ── Build image (with cache) ──
|
|
86
|
+
|
|
87
|
+
async function buildSandboxImage() {
|
|
88
|
+
if (!isDockerAvailable()) {
|
|
89
|
+
console.log('[SANDBOX] Docker is not installed or not running. Skipping sandbox analysis.');
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (imageExists()) {
|
|
94
|
+
console.log('[SANDBOX] Using cached Docker image.');
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
console.log('[SANDBOX] Building Docker image...');
|
|
99
|
+
|
|
100
|
+
return new Promise((resolve) => {
|
|
101
|
+
const dockerfilePath = path.join(__dirname, '..', 'docker').replace(/\\/g, '/');
|
|
102
|
+
const proc = spawn('docker', ['build', '-t', DOCKER_IMAGE, dockerfilePath], {
|
|
103
|
+
stdio: 'inherit'
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
proc.on('close', (code) => {
|
|
107
|
+
if (code === 0) {
|
|
108
|
+
console.log('[SANDBOX] Image built successfully.');
|
|
109
|
+
resolve(true);
|
|
110
|
+
} else {
|
|
111
|
+
console.log('[SANDBOX] Docker build failed.');
|
|
112
|
+
resolve(false);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
proc.on('error', () => {
|
|
117
|
+
console.log('[SANDBOX] Docker error during build.');
|
|
118
|
+
resolve(false);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ── Run sandbox analysis ──
|
|
124
|
+
|
|
125
|
+
async function runSandbox(packageName, options = {}) {
|
|
126
|
+
const cleanResult = { score: 0, severity: 'CLEAN', findings: [], raw_report: null, suspicious: false };
|
|
127
|
+
|
|
128
|
+
if (!isDockerAvailable()) {
|
|
129
|
+
console.log('[SANDBOX] Docker is not installed or not running. Skipping.');
|
|
130
|
+
return cleanResult;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const strict = options.strict || false;
|
|
134
|
+
const canaryEnabled = options.canary !== false; // enabled by default
|
|
135
|
+
const mode = strict ? 'strict' : 'permissive';
|
|
136
|
+
|
|
137
|
+
// Validate package name before passing to container
|
|
138
|
+
if (!NPM_PACKAGE_REGEX.test(packageName)) {
|
|
139
|
+
console.log('[SANDBOX] Invalid package name: ' + packageName);
|
|
140
|
+
return cleanResult;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Generate canary tokens for this sandbox session
|
|
144
|
+
let canaryTokens = null;
|
|
145
|
+
if (canaryEnabled) {
|
|
146
|
+
const canary = generateCanaryTokens();
|
|
147
|
+
canaryTokens = canary.tokens;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
console.log(`[SANDBOX] Analyzing "${packageName}" in isolated container (mode: ${mode}${canaryEnabled ? ', canary: on' : ''})...`);
|
|
151
|
+
|
|
152
|
+
return new Promise((resolve) => {
|
|
153
|
+
let stdout = '';
|
|
154
|
+
let stderr = '';
|
|
155
|
+
let timedOut = false;
|
|
156
|
+
const containerName = `muaddib-sandbox-${Date.now()}-${crypto.randomBytes(4).toString('hex')}`;
|
|
157
|
+
|
|
158
|
+
const dockerArgs = [
|
|
159
|
+
'run',
|
|
160
|
+
'--rm',
|
|
161
|
+
`--name=${containerName}`,
|
|
162
|
+
'--network=bridge',
|
|
163
|
+
'--memory=512m',
|
|
164
|
+
'--cpus=1',
|
|
165
|
+
'--pids-limit=100',
|
|
166
|
+
'--cap-drop=ALL'
|
|
167
|
+
];
|
|
168
|
+
|
|
169
|
+
// Inject canary tokens as environment variables
|
|
170
|
+
if (canaryTokens) {
|
|
171
|
+
for (const [key, value] of Object.entries(canaryTokens)) {
|
|
172
|
+
dockerArgs.push('-e', `${key}=${value}`);
|
|
173
|
+
}
|
|
174
|
+
// Also inject canary file contents as env vars for the entrypoint to write
|
|
175
|
+
dockerArgs.push('-e', `CANARY_ENV_CONTENT=${createCanaryEnvFile(canaryTokens).replace(/\r?\n/g, '\\n')}`);
|
|
176
|
+
dockerArgs.push('-e', `CANARY_NPMRC_CONTENT=${createCanaryNpmrc(canaryTokens).replace(/\r?\n/g, '\\n')}`);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Both modes need NET_RAW for tcpdump (runs as root in entrypoint).
|
|
180
|
+
// Strict mode also needs NET_ADMIN for iptables network blocking.
|
|
181
|
+
// SYS_PTRACE is not needed: strace traces its own child (npm install via su).
|
|
182
|
+
dockerArgs.push('--cap-add=NET_RAW');
|
|
183
|
+
if (strict) {
|
|
184
|
+
dockerArgs.push('--cap-add=NET_ADMIN');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
dockerArgs.push('--tmpfs', '/tmp:rw,nosuid,size=64m');
|
|
188
|
+
dockerArgs.push('--tmpfs', '/sandbox/install:rw,nosuid,size=256m');
|
|
189
|
+
dockerArgs.push('--tmpfs', '/home/sandboxuser:rw,noexec,nosuid,size=16m');
|
|
190
|
+
dockerArgs.push('--read-only');
|
|
191
|
+
|
|
192
|
+
dockerArgs.push('--security-opt', 'no-new-privileges');
|
|
193
|
+
dockerArgs.push(DOCKER_IMAGE);
|
|
194
|
+
dockerArgs.push(packageName);
|
|
195
|
+
dockerArgs.push(mode);
|
|
196
|
+
|
|
197
|
+
const proc = spawn('docker', dockerArgs);
|
|
198
|
+
|
|
199
|
+
// Timeout: kill container after 120s
|
|
200
|
+
const timer = setTimeout(() => {
|
|
201
|
+
timedOut = true;
|
|
202
|
+
console.log('[SANDBOX] Timeout (120s). Killing container...');
|
|
203
|
+
try {
|
|
204
|
+
execFileSync('docker', ['kill', containerName], { stdio: 'pipe', timeout: 5000 });
|
|
205
|
+
} catch {
|
|
206
|
+
proc.kill('SIGKILL');
|
|
207
|
+
}
|
|
208
|
+
}, CONTAINER_TIMEOUT);
|
|
209
|
+
|
|
210
|
+
proc.stdout.on('data', (data) => {
|
|
211
|
+
stdout += data.toString();
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
proc.stderr.on('data', (data) => {
|
|
215
|
+
stderr += data.toString();
|
|
216
|
+
// Forward sandbox progress logs (sanitize ANSI escape sequences)
|
|
217
|
+
const text = data.toString().replace(/\x1b\[[0-9;]*[a-zA-Z]/g, '');
|
|
218
|
+
for (const line of text.split(/\r?\n/)) {
|
|
219
|
+
if (line.includes('[SANDBOX]')) {
|
|
220
|
+
console.log(line.trim());
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
proc.on('close', () => {
|
|
226
|
+
clearTimeout(timer);
|
|
227
|
+
|
|
228
|
+
if (timedOut) {
|
|
229
|
+
const result = {
|
|
230
|
+
score: 100,
|
|
231
|
+
severity: 'CRITICAL',
|
|
232
|
+
findings: [{
|
|
233
|
+
type: 'timeout',
|
|
234
|
+
severity: 'CRITICAL',
|
|
235
|
+
detail: 'Container exceeded 120s timeout',
|
|
236
|
+
evidence: `Killed after ${CONTAINER_TIMEOUT}ms`
|
|
237
|
+
}],
|
|
238
|
+
raw_report: null,
|
|
239
|
+
suspicious: true
|
|
240
|
+
};
|
|
241
|
+
displayResults(result);
|
|
242
|
+
resolve(result);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Parse JSON from container stdout using delimiter
|
|
247
|
+
let report;
|
|
248
|
+
try {
|
|
249
|
+
const REPORT_DELIMITER = '---MUADDIB-REPORT-START---';
|
|
250
|
+
const delimIdx = stdout.indexOf(REPORT_DELIMITER);
|
|
251
|
+
let jsonStr;
|
|
252
|
+
if (delimIdx !== -1) {
|
|
253
|
+
// Reliable: use delimiter to skip any package output before the report
|
|
254
|
+
jsonStr = stdout.substring(delimIdx + REPORT_DELIMITER.length).trim();
|
|
255
|
+
} else {
|
|
256
|
+
// Fallback: find first '{' (backward compat with older images)
|
|
257
|
+
const jsonStart = stdout.indexOf('{');
|
|
258
|
+
const jsonEnd = stdout.lastIndexOf('}');
|
|
259
|
+
if (jsonStart === -1 || jsonEnd === -1) {
|
|
260
|
+
throw new Error('No JSON found in output');
|
|
261
|
+
}
|
|
262
|
+
jsonStr = stdout.substring(jsonStart, jsonEnd + 1);
|
|
263
|
+
}
|
|
264
|
+
report = JSON.parse(jsonStr);
|
|
265
|
+
} catch (e) {
|
|
266
|
+
console.log('[SANDBOX] Failed to parse container output:', e.message);
|
|
267
|
+
resolve(cleanResult);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const { score, findings } = scoreFindings(report);
|
|
272
|
+
|
|
273
|
+
// Canary token exfiltration detection (dynamic tokens)
|
|
274
|
+
if (canaryTokens) {
|
|
275
|
+
const networkExfil = detectCanaryExfiltration(report.network || {}, canaryTokens);
|
|
276
|
+
const outputExfil = detectCanaryInOutput(stdout, stderr, canaryTokens);
|
|
277
|
+
|
|
278
|
+
for (const exfil of [...networkExfil.exfiltrations, ...outputExfil.exfiltrations]) {
|
|
279
|
+
findings.push({
|
|
280
|
+
type: 'canary_exfiltration',
|
|
281
|
+
severity: 'CRITICAL',
|
|
282
|
+
detail: `Package attempted to exfiltrate ${exfil.token} (${exfil.foundIn})`,
|
|
283
|
+
evidence: exfil.value
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Static canary token detection (fallback for shell-injected tokens)
|
|
289
|
+
const staticExfil = detectStaticCanaryExfiltration(report);
|
|
290
|
+
for (const { token, value } of staticExfil) {
|
|
291
|
+
const alreadyDetected = findings.some(f =>
|
|
292
|
+
f.type === 'canary_exfiltration' && f.detail && f.detail.includes(token)
|
|
293
|
+
);
|
|
294
|
+
if (!alreadyDetected) {
|
|
295
|
+
findings.push({
|
|
296
|
+
type: 'canary_exfiltration',
|
|
297
|
+
severity: 'CRITICAL',
|
|
298
|
+
detail: `Canary token exfiltration detected: ${token}`,
|
|
299
|
+
evidence: value
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const finalScore = Math.min(100, findings.reduce((s, f) => {
|
|
305
|
+
if (f.type === 'canary_exfiltration') return s + 50;
|
|
306
|
+
return s;
|
|
307
|
+
}, score));
|
|
308
|
+
const severity = getSeverity(finalScore);
|
|
309
|
+
const result = { score: finalScore, severity, findings, raw_report: report, suspicious: finalScore > 0 };
|
|
310
|
+
|
|
311
|
+
displayResults(result);
|
|
312
|
+
resolve(result);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
proc.on('error', (err) => {
|
|
316
|
+
clearTimeout(timer);
|
|
317
|
+
if (err.code === 'ENOENT') {
|
|
318
|
+
console.log('[SANDBOX] Docker not found. Please install Docker.');
|
|
319
|
+
} else {
|
|
320
|
+
console.log(`[SANDBOX] Error: ${err.message}`);
|
|
321
|
+
}
|
|
322
|
+
resolve(cleanResult);
|
|
323
|
+
});
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// ── Static canary detection ──
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Detect static canary token exfiltration in a sandbox report.
|
|
331
|
+
* Searches HTTP bodies, DNS queries, HTTP request URLs, TLS domains,
|
|
332
|
+
* filesystem changes, process commands, and install output.
|
|
333
|
+
* @param {object} report - Parsed sandbox report JSON
|
|
334
|
+
* @returns {Array<{token: string, value: string}>} Exfiltrated tokens
|
|
335
|
+
*/
|
|
336
|
+
function detectStaticCanaryExfiltration(report) {
|
|
337
|
+
const exfiltrated = [];
|
|
338
|
+
if (!report) return exfiltrated;
|
|
339
|
+
|
|
340
|
+
const searchable = [];
|
|
341
|
+
|
|
342
|
+
// Network data
|
|
343
|
+
for (const body of (report.network?.http_bodies || [])) if (body) searchable.push(body);
|
|
344
|
+
for (const domain of (report.network?.dns_queries || [])) if (domain) searchable.push(domain);
|
|
345
|
+
for (const req of (report.network?.http_requests || [])) {
|
|
346
|
+
searchable.push(`${req.method || ''} ${req.host || ''}${req.path || ''}`);
|
|
347
|
+
}
|
|
348
|
+
for (const tls of (report.network?.tls_connections || [])) if (tls.domain) searchable.push(tls.domain);
|
|
349
|
+
|
|
350
|
+
// Filesystem + processes
|
|
351
|
+
for (const file of (report.filesystem?.created || [])) if (file) searchable.push(file);
|
|
352
|
+
for (const proc of (report.processes?.spawned || [])) if (proc.command) searchable.push(proc.command);
|
|
353
|
+
|
|
354
|
+
// Install output
|
|
355
|
+
if (report.install_output) searchable.push(report.install_output);
|
|
356
|
+
|
|
357
|
+
const allOutput = searchable.join('\n');
|
|
358
|
+
|
|
359
|
+
for (const [tokenName, tokenValue] of Object.entries(STATIC_CANARY_TOKENS)) {
|
|
360
|
+
if (allOutput.includes(tokenValue)) {
|
|
361
|
+
exfiltrated.push({ token: tokenName, value: tokenValue });
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return exfiltrated;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ── Scoring engine ──
|
|
369
|
+
|
|
370
|
+
function scoreFindings(report) {
|
|
371
|
+
let score = 0;
|
|
372
|
+
const findings = [];
|
|
373
|
+
|
|
374
|
+
// 1. Sensitive file reads
|
|
375
|
+
for (const file of (report.sensitive_files?.read || [])) {
|
|
376
|
+
if (/\.npmrc/.test(file) || /\.ssh/.test(file) || /\.aws/.test(file)) {
|
|
377
|
+
score += 40;
|
|
378
|
+
findings.push({ type: 'sensitive_file_read', severity: 'CRITICAL', detail: `Read credential file: ${file}`, evidence: file });
|
|
379
|
+
} else if (/\/etc\/passwd/.test(file) || /\/etc\/shadow/.test(file)) {
|
|
380
|
+
score += 25;
|
|
381
|
+
findings.push({ type: 'sensitive_file_read', severity: 'HIGH', detail: `Read system file: ${file}`, evidence: file });
|
|
382
|
+
} else if (/\.env/.test(file) || /\.gitconfig/.test(file) || /\.bash_history/.test(file)) {
|
|
383
|
+
score += 15;
|
|
384
|
+
findings.push({ type: 'sensitive_file_read', severity: 'MEDIUM', detail: `Read config file: ${file}`, evidence: file });
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// 2. Sensitive file writes (from strace)
|
|
389
|
+
for (const file of (report.sensitive_files?.written || [])) {
|
|
390
|
+
if (/\.npmrc/.test(file) || /\.ssh/.test(file) || /\.aws/.test(file)) {
|
|
391
|
+
score += 40;
|
|
392
|
+
findings.push({ type: 'sensitive_file_write', severity: 'CRITICAL', detail: `Write to credential file: ${file}`, evidence: file });
|
|
393
|
+
} else if (/\/etc\/passwd/.test(file) || /\/etc\/shadow/.test(file)) {
|
|
394
|
+
score += 25;
|
|
395
|
+
findings.push({ type: 'sensitive_file_write', severity: 'HIGH', detail: `Write to system file: ${file}`, evidence: file });
|
|
396
|
+
} else {
|
|
397
|
+
score += 15;
|
|
398
|
+
findings.push({ type: 'sensitive_file_write', severity: 'MEDIUM', detail: `Write to sensitive file: ${file}`, evidence: file });
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// 3. Filesystem changes — files created in suspicious locations
|
|
403
|
+
for (const file of (report.filesystem?.created || [])) {
|
|
404
|
+
if (/^\/usr\/bin\//.test(file) || /crontab/.test(file) || /\/cron\.d\//.test(file)) {
|
|
405
|
+
score += 50;
|
|
406
|
+
findings.push({ type: 'suspicious_filesystem', severity: 'CRITICAL', detail: `File created in system path: ${file}`, evidence: file });
|
|
407
|
+
} else if (/^\/tmp\//.test(file)) {
|
|
408
|
+
score += 30;
|
|
409
|
+
findings.push({ type: 'suspicious_filesystem', severity: 'HIGH', detail: `File created in /tmp: ${file}`, evidence: file });
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// 4a. DNS queries (exclude safe domains)
|
|
414
|
+
for (const domain of (report.network?.dns_queries || [])) {
|
|
415
|
+
if (isSafeDomain(domain)) continue;
|
|
416
|
+
score += 20;
|
|
417
|
+
findings.push({ type: 'suspicious_dns', severity: 'HIGH', detail: `DNS query to non-registry domain: ${domain}`, evidence: domain });
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// 4b. DNS resolutions — extra detail
|
|
421
|
+
for (const res of (report.network?.dns_resolutions || [])) {
|
|
422
|
+
if (isSafeDomain(res.domain)) continue;
|
|
423
|
+
// Already scored in 4a via dns_queries, but flag the resolution for reporting
|
|
424
|
+
findings.push({ type: 'dns_resolution', severity: 'INFO', detail: `${res.domain} → ${res.ip}`, evidence: `${res.domain}:${res.ip}` });
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// 5a. TCP connections (exclude safe hosts, probe ports, localhost)
|
|
428
|
+
for (const conn of (report.network?.http_connections || [])) {
|
|
429
|
+
if (isSafeHost(conn.host)) continue;
|
|
430
|
+
if (SAFE_IPS.includes(conn.host)) continue;
|
|
431
|
+
if (PROBE_PORTS.includes(conn.port)) continue;
|
|
432
|
+
score += 25;
|
|
433
|
+
findings.push({ type: 'suspicious_connection', severity: 'HIGH', detail: `TCP connection to ${conn.host}:${conn.port}`, evidence: `${conn.host}:${conn.port}` });
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// 5b. TLS connections — non-safe domains
|
|
437
|
+
for (const tls of (report.network?.tls_connections || [])) {
|
|
438
|
+
if (isSafeDomain(tls.domain)) continue;
|
|
439
|
+
score += 20;
|
|
440
|
+
findings.push({ type: 'suspicious_tls', severity: 'HIGH', detail: `TLS connection to ${tls.domain} (${tls.ip}:${tls.port})`, evidence: tls.domain });
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// 5c. HTTP exfiltration detection — scan body snippets for sensitive data
|
|
444
|
+
for (const body of (report.network?.http_bodies || [])) {
|
|
445
|
+
for (const pat of EXFIL_PATTERNS) {
|
|
446
|
+
if (pat.pattern.test(body)) {
|
|
447
|
+
score += 50;
|
|
448
|
+
findings.push({
|
|
449
|
+
type: 'data_exfiltration',
|
|
450
|
+
severity: pat.severity,
|
|
451
|
+
detail: `HTTP body contains ${pat.label}`,
|
|
452
|
+
evidence: body.substring(0, 200)
|
|
453
|
+
});
|
|
454
|
+
break; // One match per body is enough
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// 5d. HTTP requests to non-safe hosts
|
|
460
|
+
for (const req of (report.network?.http_requests || [])) {
|
|
461
|
+
if (isSafeDomain(req.host)) continue;
|
|
462
|
+
score += 20;
|
|
463
|
+
findings.push({ type: 'suspicious_http_request', severity: 'HIGH', detail: `${req.method} ${req.host}${req.path}`, evidence: `${req.method} ${req.host}${req.path}` });
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// 5e. Blocked connections (strict mode)
|
|
467
|
+
for (const blocked of (report.network?.blocked_connections || [])) {
|
|
468
|
+
score += 30;
|
|
469
|
+
findings.push({ type: 'blocked_connection', severity: 'HIGH', detail: `Blocked outbound to ${blocked.ip}:${blocked.port}`, evidence: `${blocked.ip}:${blocked.port}` });
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// 6. Suspicious processes
|
|
473
|
+
for (const p of (report.processes?.spawned || [])) {
|
|
474
|
+
const cmd = p.command || '';
|
|
475
|
+
const basename = path.basename(cmd);
|
|
476
|
+
if (DANGEROUS_CMDS.some(d => basename === d)) {
|
|
477
|
+
score += 40;
|
|
478
|
+
findings.push({ type: 'suspicious_process', severity: 'CRITICAL', detail: `Dangerous command spawned: ${cmd}`, evidence: cmd });
|
|
479
|
+
} else if (cmd) {
|
|
480
|
+
score += 15;
|
|
481
|
+
findings.push({ type: 'unknown_process', severity: 'MEDIUM', detail: `Unknown process spawned: ${cmd}`, evidence: cmd });
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
score = Math.min(100, score);
|
|
486
|
+
return { score, findings };
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// ── Network report (detailed, colored) ──
|
|
490
|
+
|
|
491
|
+
function generateNetworkReport(report) {
|
|
492
|
+
const lines = [];
|
|
493
|
+
const RED = '\x1b[31m';
|
|
494
|
+
const YELLOW = '\x1b[33m';
|
|
495
|
+
const GREEN = '\x1b[32m';
|
|
496
|
+
const CYAN = '\x1b[36m';
|
|
497
|
+
const MAGENTA = '\x1b[35m';
|
|
498
|
+
const BOLD = '\x1b[1m';
|
|
499
|
+
const DIM = '\x1b[2m';
|
|
500
|
+
const RESET = '\x1b[0m';
|
|
501
|
+
|
|
502
|
+
lines.push('');
|
|
503
|
+
lines.push(`${BOLD}${MAGENTA}╔══════════════════════════════════════════════════╗${RESET}`);
|
|
504
|
+
lines.push(`${BOLD}${MAGENTA}║ MUAD'DIB — Sandbox Network Report ║${RESET}`);
|
|
505
|
+
lines.push(`${BOLD}${MAGENTA}╚══════════════════════════════════════════════════╝${RESET}`);
|
|
506
|
+
lines.push('');
|
|
507
|
+
lines.push(` Package: ${BOLD}${report.package}${RESET}`);
|
|
508
|
+
lines.push(` Mode: ${report.mode === 'strict' ? RED + 'STRICT' : GREEN + 'permissive'}${RESET}`);
|
|
509
|
+
lines.push(` Time: ${report.timestamp}`);
|
|
510
|
+
lines.push(` Duration: ${report.duration_ms}ms`);
|
|
511
|
+
|
|
512
|
+
// DNS Resolutions
|
|
513
|
+
const dnsRes = report.network?.dns_resolutions || [];
|
|
514
|
+
lines.push('');
|
|
515
|
+
lines.push(`${BOLD}${CYAN}── DNS Resolutions (${dnsRes.length}) ──${RESET}`);
|
|
516
|
+
if (dnsRes.length === 0) {
|
|
517
|
+
lines.push(` ${DIM}No DNS resolutions captured${RESET}`);
|
|
518
|
+
} else {
|
|
519
|
+
for (const r of dnsRes) {
|
|
520
|
+
const safe = isSafeDomain(r.domain);
|
|
521
|
+
const icon = safe ? GREEN + '[OK]' : YELLOW + '[!!]';
|
|
522
|
+
lines.push(` ${icon}${RESET} ${r.domain} → ${r.ip}`);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// HTTP Requests
|
|
527
|
+
const httpReqs = report.network?.http_requests || [];
|
|
528
|
+
lines.push('');
|
|
529
|
+
lines.push(`${BOLD}${CYAN}── HTTP Requests (${httpReqs.length}) ──${RESET}`);
|
|
530
|
+
if (httpReqs.length === 0) {
|
|
531
|
+
lines.push(` ${DIM}No HTTP requests captured${RESET}`);
|
|
532
|
+
} else {
|
|
533
|
+
for (const req of httpReqs) {
|
|
534
|
+
const safe = isSafeDomain(req.host);
|
|
535
|
+
const icon = safe ? GREEN + '[OK]' : RED + '[!!]';
|
|
536
|
+
lines.push(` ${icon}${RESET} ${req.method} ${req.host}${req.path}`);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// TLS Connections
|
|
541
|
+
const tlsConns = report.network?.tls_connections || [];
|
|
542
|
+
lines.push('');
|
|
543
|
+
lines.push(`${BOLD}${CYAN}── TLS Connections (${tlsConns.length}) ──${RESET}`);
|
|
544
|
+
if (tlsConns.length === 0) {
|
|
545
|
+
lines.push(` ${DIM}No TLS connections captured${RESET}`);
|
|
546
|
+
} else {
|
|
547
|
+
for (const tls of tlsConns) {
|
|
548
|
+
const safe = isSafeDomain(tls.domain);
|
|
549
|
+
const icon = safe ? GREEN + '[OK]' : YELLOW + '[!!]';
|
|
550
|
+
lines.push(` ${icon}${RESET} ${tls.domain} (${tls.ip}:${tls.port})`);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// Blocked Connections (strict mode)
|
|
555
|
+
const blocked = report.network?.blocked_connections || [];
|
|
556
|
+
if (blocked.length > 0) {
|
|
557
|
+
lines.push('');
|
|
558
|
+
lines.push(`${BOLD}${RED}── Blocked Connections (${blocked.length}) ──${RESET}`);
|
|
559
|
+
for (const b of blocked) {
|
|
560
|
+
lines.push(` ${RED}[BLOCKED]${RESET} ${b.ip}:${b.port}`);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// Data Exfiltration Alerts
|
|
565
|
+
const bodies = report.network?.http_bodies || [];
|
|
566
|
+
const exfilAlerts = [];
|
|
567
|
+
for (const body of bodies) {
|
|
568
|
+
for (const pat of EXFIL_PATTERNS) {
|
|
569
|
+
if (pat.pattern.test(body)) {
|
|
570
|
+
exfilAlerts.push({ label: pat.label, severity: pat.severity, snippet: body.substring(0, 100) });
|
|
571
|
+
break;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
if (exfilAlerts.length > 0) {
|
|
576
|
+
lines.push('');
|
|
577
|
+
lines.push(`${BOLD}${RED}── Data Exfiltration Alerts (${exfilAlerts.length}) ──${RESET}`);
|
|
578
|
+
for (const alert of exfilAlerts) {
|
|
579
|
+
lines.push(` ${RED}[${alert.severity}]${RESET} ${alert.label} detected in HTTP body`);
|
|
580
|
+
lines.push(` ${DIM}${alert.snippet}...${RESET}`);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// Raw TCP connections
|
|
585
|
+
const conns = report.network?.http_connections || [];
|
|
586
|
+
if (conns.length > 0) {
|
|
587
|
+
lines.push('');
|
|
588
|
+
lines.push(`${BOLD}${CYAN}── Raw TCP Connections (${conns.length}) ──${RESET}`);
|
|
589
|
+
for (const c of conns) {
|
|
590
|
+
const safe = isSafeHost(c.host);
|
|
591
|
+
const icon = safe ? GREEN + '[OK]' : YELLOW + '[!!]';
|
|
592
|
+
lines.push(` ${icon}${RESET} ${c.host}:${c.port} (${c.protocol})`);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
lines.push('');
|
|
597
|
+
return lines.join('\n');
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// ── Helpers ──
|
|
601
|
+
|
|
602
|
+
function isSafeDomain(domain) {
|
|
603
|
+
return SAFE_DOMAINS.some(safe => domain === safe || domain.endsWith('.' + safe));
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
function isSafeHost(host) {
|
|
607
|
+
return SAFE_DOMAINS.some(safe => host === safe || host.endsWith('.' + safe));
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
function getSeverity(score) {
|
|
611
|
+
if (score === 0) return 'CLEAN';
|
|
612
|
+
if (score <= 20) return 'LOW';
|
|
613
|
+
if (score <= 50) return 'MEDIUM';
|
|
614
|
+
if (score <= 80) return 'HIGH';
|
|
615
|
+
return 'CRITICAL';
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
function displayResults(result) {
|
|
619
|
+
console.log(`\n[SANDBOX] Score: ${result.score}/100 — ${result.severity}`);
|
|
620
|
+
if (result.findings.length === 0) {
|
|
621
|
+
console.log('[SANDBOX] No suspicious behavior detected.');
|
|
622
|
+
} else {
|
|
623
|
+
const actionable = result.findings.filter(f => f.severity !== 'INFO');
|
|
624
|
+
console.log(`[SANDBOX] ${actionable.length} finding(s):`);
|
|
625
|
+
for (const f of actionable) {
|
|
626
|
+
console.log(` [${f.severity}] ${f.type}: ${f.detail}`);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
module.exports = { buildSandboxImage, runSandbox, scoreFindings, generateNetworkReport, EXFIL_PATTERNS, SAFE_DOMAINS, getSeverity, displayResults, isDockerAvailable, imageExists, STATIC_CANARY_TOKENS, detectStaticCanaryExfiltration };
|