@glassbox-ai/engine 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +31 -2
- package/dist/action/acc.d.ts +48 -0
- package/dist/action/acc.js +117 -0
- package/dist/action/inline.d.ts +14 -0
- package/dist/action/inline.js +54 -0
- package/dist/action/paths.d.ts +71 -0
- package/dist/action/paths.js +398 -0
- package/dist/action/powershell.d.ts +9 -0
- package/dist/action/powershell.js +848 -0
- package/dist/action/protected-write.d.ts +11 -0
- package/dist/action/protected-write.js +113 -0
- package/dist/action/schedule.d.ts +43 -0
- package/dist/action/schedule.js +213 -0
- package/dist/action/shell/analyze.d.ts +28 -0
- package/dist/action/shell/analyze.js +2030 -0
- package/dist/action/shell/parse.d.ts +74 -0
- package/dist/action/shell/parse.js +1037 -0
- package/dist/action/shell/view.d.ts +6 -0
- package/dist/action/shell/view.js +89 -0
- package/dist/action/sql.d.ts +3 -0
- package/dist/action/sql.js +259 -0
- package/dist/action/tools.d.ts +20 -0
- package/dist/action/tools.js +240 -0
- package/dist/action/upload.d.ts +17 -0
- package/dist/action/upload.js +72 -0
- package/dist/analysis/fields.d.ts +14 -0
- package/dist/analysis/fields.js +136 -0
- package/dist/analysis/findings.d.ts +25 -0
- package/dist/analysis/findings.js +82 -0
- package/dist/analysis/gate.d.ts +16 -0
- package/dist/analysis/gate.js +660 -0
- package/dist/analysis/manifest.d.ts +3 -0
- package/dist/analysis/manifest.js +369 -0
- package/dist/analysis/match.d.ts +54 -0
- package/dist/analysis/match.js +352 -0
- package/dist/analysis/scan.d.ts +17 -0
- package/dist/analysis/scan.js +97 -0
- package/dist/canon/confusables.generated.d.ts +3 -0
- package/dist/canon/confusables.generated.js +6 -0
- package/dist/canon/decode.d.ts +11 -0
- package/dist/canon/decode.js +147 -0
- package/dist/canon/views.d.ts +36 -0
- package/dist/canon/views.js +283 -0
- package/dist/evidence/evidence.d.ts +12 -0
- package/dist/evidence/evidence.js +149 -0
- package/dist/fingerprint/diff.d.ts +3 -0
- package/dist/fingerprint/diff.js +273 -0
- package/dist/fingerprint/fingerprint.d.ts +4 -0
- package/dist/fingerprint/fingerprint.js +77 -0
- package/dist/grade/score.d.ts +19 -0
- package/dist/grade/score.js +89 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +148 -0
- package/dist/intake/json.d.ts +17 -0
- package/dist/intake/json.js +284 -0
- package/dist/intake/snapshot.d.ts +14 -0
- package/dist/intake/snapshot.js +130 -0
- package/dist/limits.d.ts +4 -0
- package/dist/limits.js +30 -0
- package/dist/policy/decide.d.ts +26 -0
- package/dist/policy/decide.js +74 -0
- package/dist/rules/ids.d.ts +2 -0
- package/dist/rules/ids.js +99 -0
- package/dist/ruleset/baseline-tests.generated.d.ts +6 -0
- package/dist/ruleset/baseline-tests.generated.js +2935 -0
- package/dist/ruleset/baseline.d.ts +3 -0
- package/dist/ruleset/baseline.generated.d.ts +3 -0
- package/dist/ruleset/baseline.generated.js +5 -0
- package/dist/ruleset/baseline.js +44 -0
- package/dist/ruleset/fakes.d.ts +3 -0
- package/dist/ruleset/fakes.js +26 -0
- package/dist/ruleset/load.d.ts +14 -0
- package/dist/ruleset/load.js +173 -0
- package/dist/ruleset/schema.d.ts +120 -0
- package/dist/ruleset/schema.js +54 -0
- package/dist/ruleset/selftest.d.ts +18 -0
- package/dist/ruleset/selftest.js +42 -0
- package/dist/ruleset-entry.d.ts +5 -0
- package/dist/ruleset-entry.js +7 -0
- package/dist/secrets/detect.d.ts +17 -0
- package/dist/secrets/detect.js +236 -0
- package/dist/secrets/redact.d.ts +19 -0
- package/dist/secrets/redact.js +116 -0
- package/dist/types.d.ts +434 -0
- package/dist/types.js +10 -0
- package/dist/util/hash.d.ts +4 -0
- package/dist/util/hash.js +16 -0
- package/dist/util/initclock.d.ts +3 -0
- package/dist/util/initclock.js +17 -0
- package/dist/util/jcs.d.ts +4 -0
- package/dist/util/jcs.js +91 -0
- package/dist/util/misc.d.ts +17 -0
- package/dist/util/misc.js +77 -0
- package/dist/util/re.d.ts +14 -0
- package/dist/util/re.js +47 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +55 -3
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type NormPath, type PathCtx } from "./paths.js";
|
|
2
|
+
/** Shell-style glob match of one path segment. `*` and `?` don't match a leading dot. */
|
|
3
|
+
export declare function segMatch(pat: string, s: string): boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Is `np` (written, moved, deleted, chmod-ed or linked) a protected path, an ancestor
|
|
6
|
+
* directory of one, or a glob pattern that could match one?
|
|
7
|
+
* Project-level files (<cwd>/.mcp.json, <cwd>/.claude/settings*.json) count for ancestors
|
|
8
|
+
* only strictly inside the project, so `chmod -R u+w .` on the project root stays allowed.
|
|
9
|
+
*/
|
|
10
|
+
export declare function isProtectedWrite(np: NormPath, ctx: PathCtx, isGlob?: boolean): boolean;
|
|
11
|
+
//# sourceMappingURL=protected-write.d.ts.map
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Write targets that reach protected paths indirectly (red team, Phase 0 Step 4):
|
|
2
|
+
// - an ancestor directory (moving or deleting ~/.claude moves its settings and hooks)
|
|
3
|
+
// - a glob that could match a protected file or a path inside a protected directory
|
|
4
|
+
import { trimEndChars } from "../util/misc.js";
|
|
5
|
+
import { ANYWHERE, entries, fold, isProtected, isProtectedRead, normalizeFull, readProtectedEntries } from "./paths.js";
|
|
6
|
+
/** Concrete protected paths a write could hit. `project` marks the cwd's "anywhere" files. */
|
|
7
|
+
function candidates(ctx) {
|
|
8
|
+
const out = entries(ctx).map((e) => ({ p: e.p, dir: e.dir, project: false }));
|
|
9
|
+
for (const e of readProtectedEntries(ctx))
|
|
10
|
+
out.push({ p: e.p.endsWith("/*.key") ? e.p.slice(0, -6) : e.p, dir: e.p.endsWith("/*.key"), project: false });
|
|
11
|
+
if (ctx.cwd !== null) {
|
|
12
|
+
const cwd = normalizeFull(ctx.cwd, ctx);
|
|
13
|
+
if (cwd)
|
|
14
|
+
for (const a of ANYWHERE)
|
|
15
|
+
out.push({ p: cwd + fold(a, ctx.caseInsensitive), dir: false, project: true });
|
|
16
|
+
}
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
/** Shell-style glob match of one path segment. `*` and `?` don't match a leading dot. */
|
|
20
|
+
export function segMatch(pat, s) {
|
|
21
|
+
if (s.startsWith(".") && !pat.startsWith("."))
|
|
22
|
+
return false;
|
|
23
|
+
let p = 0, i = 0, star = -1, mark = 0;
|
|
24
|
+
while (i < s.length) {
|
|
25
|
+
const pc = pat[p];
|
|
26
|
+
const sc = s[i];
|
|
27
|
+
if (pc === "[") {
|
|
28
|
+
const close = pat.indexOf("]", p + 1);
|
|
29
|
+
if (close > p) {
|
|
30
|
+
const set = pat.slice(p + 1, close);
|
|
31
|
+
const neg = set.startsWith("!") || set.startsWith("^");
|
|
32
|
+
const body = neg ? set.slice(1) : set;
|
|
33
|
+
let hit = false;
|
|
34
|
+
for (let k = 0; k < body.length; k++) {
|
|
35
|
+
const lo = body[k];
|
|
36
|
+
const hi = body[k + 2];
|
|
37
|
+
if (body[k + 1] === "-" && hi !== undefined) {
|
|
38
|
+
if (sc >= lo && sc <= hi)
|
|
39
|
+
hit = true;
|
|
40
|
+
k += 2;
|
|
41
|
+
}
|
|
42
|
+
else if (lo === sc)
|
|
43
|
+
hit = true;
|
|
44
|
+
}
|
|
45
|
+
if (hit !== neg) {
|
|
46
|
+
p = close + 1;
|
|
47
|
+
i++;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else if (pc === "?" || (pc !== undefined && pc !== "*" && pc === sc)) {
|
|
53
|
+
p++;
|
|
54
|
+
i++;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (pc === "*") {
|
|
58
|
+
star = p++;
|
|
59
|
+
mark = i;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (star >= 0) {
|
|
63
|
+
p = star + 1;
|
|
64
|
+
i = ++mark;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
while (pat[p] === "*")
|
|
70
|
+
p++;
|
|
71
|
+
return p === pat.length;
|
|
72
|
+
}
|
|
73
|
+
function segsMatch(ps, xs) {
|
|
74
|
+
return ps.length === xs.length && ps.every((seg, k) => segMatch(seg, xs[k]));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Is `np` (written, moved, deleted, chmod-ed or linked) a protected path, an ancestor
|
|
78
|
+
* directory of one, or a glob pattern that could match one?
|
|
79
|
+
* Project-level files (<cwd>/.mcp.json, <cwd>/.claude/settings*.json) count for ancestors
|
|
80
|
+
* only strictly inside the project, so `chmod -R u+w .` on the project root stays allowed.
|
|
81
|
+
*/
|
|
82
|
+
export function isProtectedWrite(np, ctx, isGlob = false) {
|
|
83
|
+
if (isProtected(np, ctx) || isProtectedRead(np, ctx))
|
|
84
|
+
return true;
|
|
85
|
+
if (np.path === null)
|
|
86
|
+
return false;
|
|
87
|
+
const p = trimEndChars(np.path, "/");
|
|
88
|
+
if (p.length === 0)
|
|
89
|
+
return false;
|
|
90
|
+
const cwd = ctx.cwd !== null ? normalizeFull(ctx.cwd, ctx) : null;
|
|
91
|
+
const insideProject = cwd !== null && p.startsWith(cwd + "/");
|
|
92
|
+
const cands = candidates(ctx);
|
|
93
|
+
if (cands.some((c) => c.p.startsWith(p + "/") && (!c.project || insideProject)))
|
|
94
|
+
return true;
|
|
95
|
+
if (!isGlob)
|
|
96
|
+
return false;
|
|
97
|
+
const ps = p.split("/");
|
|
98
|
+
for (const c of cands) {
|
|
99
|
+
const cs = c.p.split("/");
|
|
100
|
+
// The pattern names the candidate or one of its ancestors…
|
|
101
|
+
for (let n = cs.length; n > 1; n--) {
|
|
102
|
+
if (n === cs.length || !c.project || ps.length > (cwd?.split("/").length ?? 0)) {
|
|
103
|
+
if (segsMatch(ps, cs.slice(0, n)))
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// …or a path inside a protected directory.
|
|
108
|
+
if (c.dir && ps.length > cs.length && segsMatch(ps.slice(0, cs.length), cs))
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=protected-write.js.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type JobKind = "cron" | "systemd" | "at" | "launchd" | "autostart" | "startup-folder" | "task-scheduler" | "service" | "boot-script";
|
|
2
|
+
export interface ScheduledJob {
|
|
3
|
+
kind: JobKind;
|
|
4
|
+
verb: "creates" | "changes" | "removes";
|
|
5
|
+
/** The job definition file, when the job is a file (a unit, plist, .desktop, cron.d entry…). */
|
|
6
|
+
path: string | null;
|
|
7
|
+
/** How often or when it runs, as written; null when it isn't known statically. */
|
|
8
|
+
schedule: string | null;
|
|
9
|
+
/** Files the job runs (programs and scripts), as written in the command or file. */
|
|
10
|
+
files: string[];
|
|
11
|
+
}
|
|
12
|
+
export declare const JOB_LABEL: Record<JobKind, string>;
|
|
13
|
+
/** Cron lines in free text (a crontab file, or `echo '* * * * * cmd' | crontab -`). */
|
|
14
|
+
export declare function cronLines(text: string): {
|
|
15
|
+
schedule: string;
|
|
16
|
+
command: string;
|
|
17
|
+
}[];
|
|
18
|
+
/** Program and script paths in a command line: anything path-like or with a script extension. */
|
|
19
|
+
export declare function commandFiles(command: string): string[];
|
|
20
|
+
/** A launchd plist: ProgramArguments/Program and StartInterval, StartCalendarInterval, RunAtLoad… */
|
|
21
|
+
export declare function plistJob(xml: string): {
|
|
22
|
+
schedule: string | null;
|
|
23
|
+
files: string[];
|
|
24
|
+
};
|
|
25
|
+
export declare function unitJob(text: string): {
|
|
26
|
+
schedule: string | null;
|
|
27
|
+
files: string[];
|
|
28
|
+
};
|
|
29
|
+
export declare function desktopJob(text: string): {
|
|
30
|
+
schedule: string | null;
|
|
31
|
+
files: string[];
|
|
32
|
+
};
|
|
33
|
+
/** schtasks /create arguments → schedule and the program it runs. */
|
|
34
|
+
export declare function schtasksJob(vals: string[]): {
|
|
35
|
+
schedule: string | null;
|
|
36
|
+
files: string[];
|
|
37
|
+
};
|
|
38
|
+
/** Register-ScheduledTask scripts: New-ScheduledTaskAction -Execute/-Argument and New-ScheduledTaskTrigger. */
|
|
39
|
+
export declare function psTaskJob(script: string): {
|
|
40
|
+
schedule: string | null;
|
|
41
|
+
files: string[];
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=schedule.d.ts.map
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// Scheduled jobs (spec rev 9): cron, systemd timers and units, at, launchd, XDG autostart, the
|
|
2
|
+
// Windows Startup folder, Task Scheduler and services. Creating, changing or removing one asks
|
|
3
|
+
// (CP-SELF-006, deny in autopilot) and the ask shows the schedule and the full command. The
|
|
4
|
+
// files a job runs are recorded as a session signal; later agent edits, moves or deletes of
|
|
5
|
+
// those files ask too (CP-SELF-007). String code only: no regex with nested quantifiers.
|
|
6
|
+
export const JOB_LABEL = {
|
|
7
|
+
cron: "cron job",
|
|
8
|
+
systemd: "systemd unit or timer",
|
|
9
|
+
at: "one-off job (at)",
|
|
10
|
+
launchd: "launchd agent or daemon",
|
|
11
|
+
autostart: "login autostart entry",
|
|
12
|
+
"startup-folder": "Windows Startup folder entry",
|
|
13
|
+
"task-scheduler": "Windows scheduled task",
|
|
14
|
+
service: "service",
|
|
15
|
+
"boot-script": "boot script",
|
|
16
|
+
};
|
|
17
|
+
const CRON_MACROS = new Set(["@reboot", "@yearly", "@annually", "@monthly", "@weekly", "@daily", "@midnight", "@hourly"]);
|
|
18
|
+
const isCronField = (t) => t.length > 0 && t.length <= 40 && [...t].every((ch) => "0123456789*/,-".includes(ch) || (ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z"));
|
|
19
|
+
/** Cron lines in free text (a crontab file, or `echo '* * * * * cmd' | crontab -`). */
|
|
20
|
+
export function cronLines(text) {
|
|
21
|
+
const out = [];
|
|
22
|
+
// Split on newlines and on quote characters, so quoted echo arguments become lines.
|
|
23
|
+
const parts = [];
|
|
24
|
+
let cur = "";
|
|
25
|
+
for (const ch of text.slice(0, 20_000)) {
|
|
26
|
+
if (ch === "\n" || ch === "'" || ch === '"' || ch === "`") {
|
|
27
|
+
parts.push(cur);
|
|
28
|
+
cur = "";
|
|
29
|
+
}
|
|
30
|
+
else
|
|
31
|
+
cur += ch;
|
|
32
|
+
}
|
|
33
|
+
parts.push(cur);
|
|
34
|
+
for (const raw of parts) {
|
|
35
|
+
const line = raw.trim();
|
|
36
|
+
if (line === "" || line.startsWith("#"))
|
|
37
|
+
continue;
|
|
38
|
+
const toks = line.split(/\s+/);
|
|
39
|
+
if (toks[0] !== undefined && CRON_MACROS.has(toks[0].toLowerCase()) && toks.length > 1) {
|
|
40
|
+
out.push({ schedule: toks[0], command: toks.slice(1).join(" ") });
|
|
41
|
+
}
|
|
42
|
+
else if (toks.length > 5 && toks.slice(0, 5).every(isCronField) && toks.slice(0, 5).some((t) => t.includes("*") || /\d/.test(t))) {
|
|
43
|
+
// Skip "VAR=value" environment lines and words that only look like fields.
|
|
44
|
+
if (toks[0]?.includes("="))
|
|
45
|
+
continue;
|
|
46
|
+
out.push({ schedule: toks.slice(0, 5).join(" "), command: toks.slice(5).join(" ") });
|
|
47
|
+
}
|
|
48
|
+
if (out.length >= 20)
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
/** Strips leading `"'(` and trailing `"');,` without a regex (a trailing `[…]+$` is quadratic in V8). */
|
|
54
|
+
function cleanToken(t) {
|
|
55
|
+
let a = 0, b = t.length;
|
|
56
|
+
while (a < b && "\"'(".includes(t[a]))
|
|
57
|
+
a++;
|
|
58
|
+
while (b > a && "\"');,".includes(t[b - 1]))
|
|
59
|
+
b--;
|
|
60
|
+
return t.slice(a, b);
|
|
61
|
+
}
|
|
62
|
+
const INTERPRETERS = new Set(["sh", "bash", "zsh", "dash", "python", "python3", "node", "perl", "ruby", "powershell", "powershell.exe", "pwsh", "cmd", "cmd.exe", "env", "wscript", "cscript", "nohup", "sudo"]);
|
|
63
|
+
const SCRIPT_EXT = [".sh", ".bash", ".zsh", ".py", ".pl", ".rb", ".js", ".mjs", ".ps1", ".bat", ".cmd", ".exe", ".vbs", ".lnk", ".jar", ".php"];
|
|
64
|
+
/** Program and script paths in a command line: anything path-like or with a script extension. */
|
|
65
|
+
export function commandFiles(command) {
|
|
66
|
+
const out = [];
|
|
67
|
+
for (const raw of command.trim().split(/\s+/).slice(0, 200)) {
|
|
68
|
+
const t = cleanToken(raw);
|
|
69
|
+
if (t === "" || t.includes("://") || t.startsWith("-"))
|
|
70
|
+
continue;
|
|
71
|
+
if (t.length === 2 && t.startsWith("/"))
|
|
72
|
+
continue; // cmd-style switches like /c
|
|
73
|
+
if (INTERPRETERS.has(t.toLowerCase()) || INTERPRETERS.has(t.slice(t.lastIndexOf("/") + 1).toLowerCase()))
|
|
74
|
+
continue;
|
|
75
|
+
const lower = t.toLowerCase();
|
|
76
|
+
if (t.includes("/") || t.includes("\\") || SCRIPT_EXT.some((e) => lower.endsWith(e)))
|
|
77
|
+
out.push(t);
|
|
78
|
+
if (out.length >= 8)
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
return [...new Set(out)];
|
|
82
|
+
}
|
|
83
|
+
function between(text, open, close, from = 0) {
|
|
84
|
+
const a = text.indexOf(open, from);
|
|
85
|
+
if (a < 0)
|
|
86
|
+
return null;
|
|
87
|
+
const b = text.indexOf(close, a + open.length);
|
|
88
|
+
if (b < 0)
|
|
89
|
+
return null;
|
|
90
|
+
return { value: text.slice(a + open.length, b).trim(), end: b + close.length };
|
|
91
|
+
}
|
|
92
|
+
/** A launchd plist: ProgramArguments/Program and StartInterval, StartCalendarInterval, RunAtLoad… */
|
|
93
|
+
export function plistJob(xml) {
|
|
94
|
+
const text = xml.slice(0, 200_000);
|
|
95
|
+
const files = [];
|
|
96
|
+
const keyAt = (k) => text.indexOf(`<key>${k}</key>`);
|
|
97
|
+
const prog = keyAt("Program");
|
|
98
|
+
if (prog >= 0) {
|
|
99
|
+
const s = between(text, "<string>", "</string>", prog);
|
|
100
|
+
if (s)
|
|
101
|
+
files.push(s.value);
|
|
102
|
+
}
|
|
103
|
+
const pa = keyAt("ProgramArguments");
|
|
104
|
+
if (pa >= 0) {
|
|
105
|
+
const arr = between(text, "<array>", "</array>", pa);
|
|
106
|
+
if (arr) {
|
|
107
|
+
let pos = 0;
|
|
108
|
+
for (let n = 0; n < 8; n++) {
|
|
109
|
+
const s = between(arr.value, "<string>", "</string>", pos);
|
|
110
|
+
if (!s)
|
|
111
|
+
break;
|
|
112
|
+
files.push(s.value);
|
|
113
|
+
pos = s.end;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const sched = [];
|
|
118
|
+
const si = keyAt("StartInterval");
|
|
119
|
+
if (si >= 0) {
|
|
120
|
+
const v = between(text, "<integer>", "</integer>", si);
|
|
121
|
+
if (v)
|
|
122
|
+
sched.push(`every ${v.value} s`);
|
|
123
|
+
}
|
|
124
|
+
if (keyAt("StartCalendarInterval") >= 0)
|
|
125
|
+
sched.push("on a calendar schedule (StartCalendarInterval)");
|
|
126
|
+
const ral = keyAt("RunAtLoad");
|
|
127
|
+
if (ral >= 0 && text.slice(ral, ral + 40).includes("<true/>"))
|
|
128
|
+
sched.push("at load and login");
|
|
129
|
+
if (keyAt("KeepAlive") >= 0)
|
|
130
|
+
sched.push("kept alive");
|
|
131
|
+
if (keyAt("WatchPaths") >= 0)
|
|
132
|
+
sched.push("when watched paths change");
|
|
133
|
+
return { schedule: sched.length > 0 ? sched.join(", ") : null, files: [...new Set([...commandFiles(files.slice(0, 1).join(" ")), ...files.slice(1).filter((f) => f.includes("/"))])] };
|
|
134
|
+
}
|
|
135
|
+
/** INI-style files: systemd units and .desktop autostart entries. */
|
|
136
|
+
function iniValues(text, keys) {
|
|
137
|
+
const out = {};
|
|
138
|
+
for (const line of text.slice(0, 200_000).split("\n").slice(0, 2000)) {
|
|
139
|
+
const eq = line.indexOf("=");
|
|
140
|
+
if (eq <= 0)
|
|
141
|
+
continue;
|
|
142
|
+
const k = line.slice(0, eq).trim();
|
|
143
|
+
if (keys.includes(k))
|
|
144
|
+
(out[k] ??= []).push(line.slice(eq + 1).trim());
|
|
145
|
+
}
|
|
146
|
+
return out;
|
|
147
|
+
}
|
|
148
|
+
export function unitJob(text) {
|
|
149
|
+
const v = iniValues(text, ["ExecStart", "ExecStartPre", "ExecStartPost", "OnCalendar", "OnBootSec", "OnStartupSec", "OnUnitActiveSec", "OnUnitInactiveSec", "OnActiveSec", "WantedBy"]);
|
|
150
|
+
const files = (v["ExecStart"] ?? []).concat(v["ExecStartPre"] ?? [], v["ExecStartPost"] ?? []).flatMap((c) => commandFiles(c.replace(/^[-@:+!]+/, "")));
|
|
151
|
+
const sched = ["OnCalendar", "OnBootSec", "OnStartupSec", "OnUnitActiveSec", "OnUnitInactiveSec", "OnActiveSec"].flatMap((k) => (v[k] ?? []).map((x) => `${k}=${x}`));
|
|
152
|
+
if (sched.length === 0 && (v["WantedBy"] ?? []).length > 0)
|
|
153
|
+
sched.push(`when ${(v["WantedBy"] ?? []).join(", ")} starts`);
|
|
154
|
+
return { schedule: sched.length > 0 ? sched.join(", ") : null, files: [...new Set(files)] };
|
|
155
|
+
}
|
|
156
|
+
export function desktopJob(text) {
|
|
157
|
+
const v = iniValues(text, ["Exec"]);
|
|
158
|
+
return { schedule: "at login", files: [...new Set((v["Exec"] ?? []).flatMap((c) => commandFiles(c)))] };
|
|
159
|
+
}
|
|
160
|
+
/** schtasks /create arguments → schedule and the program it runs. */
|
|
161
|
+
export function schtasksJob(vals) {
|
|
162
|
+
const get = (flag) => { const i = vals.findIndex((v) => v.toLowerCase() === flag); return i >= 0 ? vals[i + 1] ?? null : null; };
|
|
163
|
+
const sched = [["/sc", "schedule"], ["/mo", "modifier"], ["/d", "days"], ["/st", "start time"], ["/sd", "start date"], ["/ri", "repeat every (min)"]]
|
|
164
|
+
.map(([f, label]) => { const v = get(f); return v ? `${label} ${v}` : null; }).filter((x) => x !== null);
|
|
165
|
+
const tr = get("/tr");
|
|
166
|
+
return { schedule: sched.length > 0 ? sched.join(", ") : null, files: tr ? commandFiles(tr) : [] };
|
|
167
|
+
}
|
|
168
|
+
/** Register-ScheduledTask scripts: New-ScheduledTaskAction -Execute/-Argument and New-ScheduledTaskTrigger. */
|
|
169
|
+
export function psTaskJob(script) {
|
|
170
|
+
const text = script.slice(0, 50_000);
|
|
171
|
+
const lower = text.toLowerCase();
|
|
172
|
+
const files = [];
|
|
173
|
+
const valueAfter = (from, param) => {
|
|
174
|
+
const i = lower.indexOf(param, from);
|
|
175
|
+
if (i < 0)
|
|
176
|
+
return null;
|
|
177
|
+
let j = i + param.length;
|
|
178
|
+
while (j < text.length && (text[j] === " " || text[j] === "\t"))
|
|
179
|
+
j++;
|
|
180
|
+
const q = text[j];
|
|
181
|
+
if (q === '"' || q === "'") {
|
|
182
|
+
const e = text.indexOf(q, j + 1);
|
|
183
|
+
return e > j ? text.slice(j + 1, e) : null;
|
|
184
|
+
}
|
|
185
|
+
let e = j;
|
|
186
|
+
while (e < text.length && !" \t\n;)".includes(text[e]))
|
|
187
|
+
e++;
|
|
188
|
+
return text.slice(j, e) || null;
|
|
189
|
+
};
|
|
190
|
+
let pos = 0;
|
|
191
|
+
for (let n = 0; n < 5; n++) {
|
|
192
|
+
const a = lower.indexOf("new-scheduledtaskaction", pos);
|
|
193
|
+
if (a < 0)
|
|
194
|
+
break;
|
|
195
|
+
const exe = valueAfter(a, "-execute");
|
|
196
|
+
const arg = valueAfter(a, "-argument");
|
|
197
|
+
files.push(...commandFiles(`${exe ?? ""} ${arg ?? ""}`));
|
|
198
|
+
pos = a + 1;
|
|
199
|
+
}
|
|
200
|
+
const t = lower.indexOf("new-scheduledtasktrigger");
|
|
201
|
+
let schedule = null;
|
|
202
|
+
if (t >= 0) {
|
|
203
|
+
let end = text.indexOf("\n", t);
|
|
204
|
+
if (end < 0)
|
|
205
|
+
end = text.length;
|
|
206
|
+
const semi = text.indexOf(";", t);
|
|
207
|
+
if (semi >= 0 && semi < end)
|
|
208
|
+
end = semi;
|
|
209
|
+
schedule = text.slice(t + "new-scheduledtasktrigger".length, Math.min(end, t + 300)).trim() || null;
|
|
210
|
+
}
|
|
211
|
+
return { schedule, files: [...new Set(files)] };
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=schedule.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Acc } from "../acc.js";
|
|
2
|
+
import { type PathCtx } from "../paths.js";
|
|
3
|
+
import { type Script, type SimpleCommand, type Word } from "./parse.js";
|
|
4
|
+
export interface ShellState {
|
|
5
|
+
pctx: PathCtx;
|
|
6
|
+
depth: number;
|
|
7
|
+
maxBytes: number;
|
|
8
|
+
}
|
|
9
|
+
export interface WordInfo {
|
|
10
|
+
word: Word;
|
|
11
|
+
/** Fully literal value (quotes removed), or null if anything is dynamic. Globs keep their text. */
|
|
12
|
+
value: string | null;
|
|
13
|
+
/** Value for path checks: $HOME/~ kept for the normalizer; null if other dynamic parts. */
|
|
14
|
+
pathValue: string | null;
|
|
15
|
+
/** Literal text after the last dynamic part, if any. */
|
|
16
|
+
suffix: string;
|
|
17
|
+
dynamic: boolean;
|
|
18
|
+
glob: boolean;
|
|
19
|
+
substs: Script[];
|
|
20
|
+
}
|
|
21
|
+
export declare function wordInfo(w: Word): WordInfo;
|
|
22
|
+
export declare function analyzeShellCommand(src: string, st: ShellState): Acc;
|
|
23
|
+
export declare function analyzeScript(script: Script, st: ShellState, acc: Acc): void;
|
|
24
|
+
/** Classify a native program invocation (used by the PowerShell analyzer). */
|
|
25
|
+
export declare function classifyNative(name: string, args: WordInfo[], cmd: SimpleCommand, st: ShellState, acc: Acc, cwd: {
|
|
26
|
+
cwd: string | null;
|
|
27
|
+
}): void;
|
|
28
|
+
//# sourceMappingURL=analyze.d.ts.map
|