-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatterns.js
More file actions
591 lines (551 loc) · 15.4 KB
/
patterns.js
File metadata and controls
591 lines (551 loc) · 15.4 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
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
* @typedef {Object} PatternMatch
* @property {string} [filePath]
* @property {string} [relativePath]
* @property {{line: number, column: number}} [loc]
* @property {number} [line]
* @property {number} [column]
* @property {string} [detail]
*/
/**
* @typedef {Object} PatternDefinition
* @property {string} id
* @property {"critical" | "high" | "medium" | "low"} severity
* @property {string} category
* @property {"file" | "manifest"} scope
* @property {string} description
* @property {(context: object) => PatternMatch[]} detector
*/
/**
* Severity weights used by the report generator.
* @type {Readonly<Record<"critical" | "high" | "medium" | "low", number>>}
*/
export const SEVERITY_POINTS = Object.freeze({
critical: 40,
high: 20,
medium: 10,
low: 5,
});
/**
* Convert a detector match into a normalized finding object.
* @param {PatternDefinition} pattern
* @param {object} context
* @param {PatternMatch} match
* @returns {object}
*/
function createFinding(pattern, context, match) {
const location = match.loc ?? {
line: match.line ?? 1,
column: match.column ?? 1,
};
return {
id: pattern.id,
severity: pattern.severity,
category: pattern.category,
description: pattern.description,
filePath: match.filePath ?? context.filePath,
relativePath: match.relativePath ?? context.relativePath,
line: location.line,
column: location.column,
detail: match.detail ?? pattern.description,
};
}
/**
* Create a match array when a detector only has one aggregate result.
* @param {string} filePath
* @param {string} relativePath
* @param {{line: number, column: number}} loc
* @param {string} detail
* @returns {PatternMatch[]}
*/
function createAggregateMatch(filePath, relativePath, loc, detail) {
return [
{
filePath,
relativePath,
loc,
detail,
},
];
}
/**
* Determine whether sensitive env access should count as a finding.
* @param {object} context
* @returns {boolean}
*/
function fileHasSuspiciousCompanions(context) {
const hasExternalNetwork = context.evidence.networkCalls.some((entry) => entry.external);
const hasExecution = context.evidence.execution.length > 0;
const hasSuspiciousFs = context.evidence.fsAccesses.some((entry) => entry.classification !== "safe");
return hasExternalNetwork || hasExecution || hasSuspiciousFs;
}
/**
* Normalize detector output into findings.
* @param {PatternDefinition} pattern
* @param {object} context
* @returns {object[]}
*/
function runPattern(pattern, context) {
const matches = pattern.detector(context);
return matches.map((match) => createFinding(pattern, context, match));
}
/**
* Detect dynamic `eval()` calls.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectDynamicEval(context) {
return context.evidence.obfuscation.filter((entry) => entry.type === "eval-dynamic");
}
/**
* Detect `new Function()` with runtime-built arguments.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectDynamicFunction(context) {
return context.evidence.obfuscation.filter((entry) => entry.type === "function-dynamic");
}
/**
* Detect base64 staging patterns.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectBase64Payloads(context) {
return context.evidence.obfuscation.filter((entry) => entry.type === "base64-decode");
}
/**
* Detect executable string assembly.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectExecutableConcat(context) {
return context.evidence.obfuscation.filter((entry) => entry.type === "executable-concat");
}
/**
* Detect escape-heavy strings.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectEscapeHeavyStrings(context) {
return context.evidence.strings.filter((entry) => entry.escapeRatio > 0.3);
}
/**
* Detect plain external network calls.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectExternalNetwork(context) {
return context.evidence.networkCalls.filter(
(entry) =>
entry.external &&
entry.kind !== "websocket" &&
entry.kind !== "dns",
);
}
/**
* Detect external WebSocket connections.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectExternalWebSockets(context) {
return context.evidence.networkCalls.filter(
(entry) => entry.external && entry.kind === "websocket",
);
}
/**
* Detect external DNS lookups.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectExternalDns(context) {
return context.evidence.networkCalls.filter(
(entry) => entry.external && entry.kind === "dns",
);
}
/**
* Detect environment values that are sent over the network.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectEnvToNetwork(context) {
return context.evidence.networkCalls.filter(
(entry) => entry.external && entry.taintedEnvNames.length > 0,
);
}
/**
* Detect environment harvesting plus network usage in the same scope.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectScopeHarvest(context) {
return context.scopeSummaries
.filter(
(entry) =>
entry.hasExternalNetwork &&
entry.envReadCount > 0 &&
!entry.hasTaintedNetworkCall,
)
.map((entry) => ({
filePath: context.filePath,
relativePath: context.relativePath,
loc: entry.loc,
detail: "Reads environment values and makes an external network call in the same scope.",
}));
}
/**
* Detect dynamic `exec()` and `execSync()` usage.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectDynamicExec(context) {
return context.evidence.execution.filter(
(entry) =>
(entry.kind === "exec" || entry.kind === "execSync") &&
entry.dynamic === true,
);
}
/**
* Detect `spawn()` with a non-literal binary.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectDynamicSpawn(context) {
return context.evidence.execution.filter(
(entry) =>
(entry.kind === "spawn" || entry.kind === "spawnSync") &&
entry.dynamic === true,
);
}
/**
* Detect VM context execution.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectVmExecution(context) {
return context.evidence.execution.filter(
(entry) => entry.kind === "runInNewContext" || entry.kind === "runInThisContext",
);
}
/**
* Detect dynamic `require()` calls.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectDynamicRequire(context) {
return context.evidence.execution.filter((entry) => entry.kind === "dynamic-require");
}
/**
* Detect reads of sensitive filesystem locations.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectSensitiveRead(context) {
return context.evidence.fsAccesses.filter((entry) => entry.classification === "sensitive-read");
}
/**
* Detect writes into system directories.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectSystemWrite(context) {
return context.evidence.fsAccesses.filter((entry) => entry.classification === "system-write");
}
/**
* Detect accesses outside the allowed root.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectOutsideRootAccess(context) {
return context.evidence.fsAccesses.filter((entry) => entry.classification === "outside-root");
}
/**
* Detect sensitive env access when the same file already shows suspicious behavior.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectSensitiveEnvAccess(context) {
if (!fileHasSuspiciousCompanions(context)) {
return [];
}
return context.evidence.envReads.filter((entry) => entry.sensitiveNames.length > 0);
}
/**
* Detect prototype pollution writes.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectPrototypePollution(context) {
return context.evidence.prototypeAssignments;
}
/**
* Detect undeclared permissions in `SKILL.md`.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectUndeclaredPermissions(context) {
const missingBins = [...context.summary.actualBins].filter(
(entry) => !context.manifest.declaredBins.has(entry),
);
const missingEnv = [...context.summary.sensitiveEnvVars].filter(
(entry) => !context.manifest.declaredEnv.has(entry),
);
if (missingBins.length === 0 && missingEnv.length === 0) {
return [];
}
const parts = [];
if (missingBins.length > 0) {
parts.push(`undeclared bins: ${missingBins.join(", ")}`);
}
if (missingEnv.length > 0) {
parts.push(`undeclared env vars: ${missingEnv.join(", ")}`);
}
return createAggregateMatch(
context.filePath,
context.relativePath,
context.manifest.metadataLoc,
`SKILL.md metadata does not declare actual capabilities (${parts.join("; ")}).`,
);
}
/**
* Detect suspicious manifest requirements.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectSuspiciousManifestEntries(context) {
const suspiciousBins = [...context.manifest.declaredBins].filter((entry) =>
["bash", "sh", "curl", "wget", "powershell", "pwsh", "python", "ssh", "scp", "nc"].includes(entry),
);
const suspiciousEnv = [...context.manifest.declaredEnv].filter((entry) =>
/(?:key|token|secret|pass|password|private|cookie|session)/i.test(entry),
);
if (suspiciousBins.length === 0 && suspiciousEnv.length === 0) {
return [];
}
const details = [];
if (suspiciousBins.length > 0) {
details.push(`suspicious bins: ${suspiciousBins.join(", ")}`);
}
if (suspiciousEnv.length > 0) {
details.push(`suspicious env entries: ${suspiciousEnv.join(", ")}`);
}
return createAggregateMatch(
context.filePath,
context.relativePath,
context.manifest.metadataLoc,
`Manifest declares high-risk requirements (${details.join("; ")}).`,
);
}
/**
* Detect homepage URLs that do not align with external network destinations.
* @param {object} context
* @returns {PatternMatch[]}
*/
function detectHomepageMismatch(context) {
if (context.manifest.homepageDomains.size === 0 || context.summary.externalHosts.size === 0) {
return [];
}
const sharedDomain = [...context.manifest.homepageDomains].some((entry) =>
context.summary.externalHosts.has(entry),
);
if (sharedDomain) {
return [];
}
return createAggregateMatch(
context.filePath,
context.relativePath,
context.manifest.urlLoc,
"Homepage or documented URLs do not match the code's external network destinations.",
);
}
/**
* Community-extensible registry of findings.
* @type {Readonly<PatternDefinition[]>}
*/
export const patterns = Object.freeze([
{
id: "OBFUSC_001",
severity: "critical",
category: "Obfuscation",
scope: "file",
description: "Dynamic eval() call with non-literal input.",
detector: detectDynamicEval,
},
{
id: "OBFUSC_002",
severity: "high",
category: "Obfuscation",
scope: "file",
description: "new Function() is built from runtime input.",
detector: detectDynamicFunction,
},
{
id: "OBFUSC_003",
severity: "medium",
category: "Obfuscation",
scope: "file",
description: "Base64 decoding pattern that can hide staged payloads.",
detector: detectBase64Payloads,
},
{
id: "OBFUSC_004",
severity: "medium",
category: "Obfuscation",
scope: "file",
description: "Executable code is assembled through string concatenation.",
detector: detectExecutableConcat,
},
{
id: "OBFUSC_005",
severity: "low",
category: "Obfuscation",
scope: "file",
description: "Escape-heavy string suggests hex or unicode obfuscation.",
detector: detectEscapeHeavyStrings,
},
{
id: "EXFIL_001",
severity: "low",
category: "Exfiltration",
scope: "file",
description: "External network call to a non-local destination.",
detector: detectExternalNetwork,
},
{
id: "EXFIL_002",
severity: "medium",
category: "Exfiltration",
scope: "file",
description: "External WebSocket connection.",
detector: detectExternalWebSockets,
},
{
id: "EXFIL_003",
severity: "medium",
category: "Exfiltration",
scope: "file",
description: "DNS lookup targets an external domain.",
detector: detectExternalDns,
},
{
id: "EXFIL_004",
severity: "high",
category: "Exfiltration",
scope: "file",
description: "Environment data is passed into an external network call.",
detector: detectEnvToNetwork,
},
{
id: "EXFIL_005",
severity: "medium",
category: "Exfiltration",
scope: "file",
description: "Environment harvesting and external network activity occur in the same scope.",
detector: detectScopeHarvest,
},
{
id: "EXEC_001",
severity: "high",
category: "Dangerous Execution",
scope: "file",
description: "child_process exec() or execSync() uses runtime input.",
detector: detectDynamicExec,
},
{
id: "EXEC_002",
severity: "high",
category: "Dangerous Execution",
scope: "file",
description: "child_process spawn() uses a non-literal command.",
detector: detectDynamicSpawn,
},
{
id: "EXEC_003",
severity: "high",
category: "Dangerous Execution",
scope: "file",
description: "vm context execution is present.",
detector: detectVmExecution,
},
{
id: "EXEC_004",
severity: "medium",
category: "Dangerous Execution",
scope: "file",
description: "require() resolves a runtime-generated path.",
detector: detectDynamicRequire,
},
{
id: "FS_001",
severity: "high",
category: "File System Abuse",
scope: "file",
description: "Sensitive filesystem path is read.",
detector: detectSensitiveRead,
},
{
id: "FS_002",
severity: "high",
category: "File System Abuse",
scope: "file",
description: "Writes into a system directory.",
detector: detectSystemWrite,
},
{
id: "FS_003",
severity: "medium",
category: "File System Abuse",
scope: "file",
description: "Filesystem access targets a path outside process.cwd() or ~/.openclaw/.",
detector: detectOutsideRootAccess,
},
{
id: "ENV_001",
severity: "low",
category: "Environment Harvesting",
scope: "file",
description: "Sensitive environment variables are accessed.",
detector: detectSensitiveEnvAccess,
},
{
id: "POLL_001",
severity: "high",
category: "Prototype Pollution",
scope: "file",
description: "Writes to __proto__, Object.prototype, or constructor.prototype.",
detector: detectPrototypePollution,
},
{
id: "META_001",
severity: "medium",
category: "SKILL.md Analysis",
scope: "manifest",
description: "SKILL.md permissions do not match actual code capabilities.",
detector: detectUndeclaredPermissions,
},
{
id: "META_002",
severity: "low",
category: "SKILL.md Analysis",
scope: "manifest",
description: "SKILL.md declares suspicious bins or env requirements.",
detector: detectSuspiciousManifestEntries,
},
{
id: "META_003",
severity: "low",
category: "SKILL.md Analysis",
scope: "manifest",
description: "Documented URLs do not align with external code destinations.",
detector: detectHomepageMismatch,
},
]);
/**
* Apply the registered detectors to a file or manifest context.
* @param {object} context
* @returns {object[]}
*/
export function applyPatterns(context) {
return patterns
.filter((pattern) => pattern.scope === context.kind)
.flatMap((pattern) => runPattern(pattern, context));
}