@acepaste/ingress 0.0.0-stage → 0.7.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 +105 -0
- package/README.md +15 -2
- package/dist/src/acepaste-alphabet.d.ts +26 -0
- package/dist/src/acepaste-alphabet.js +47 -0
- package/dist/src/acquire.d.ts +24 -0
- package/dist/src/acquire.js +146 -0
- package/dist/src/adapters.d.ts +30 -0
- package/dist/src/adapters.js +160 -0
- package/dist/src/character.d.ts +22 -0
- package/dist/src/character.js +291 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +63 -0
- package/dist/src/color.d.ts +26 -0
- package/dist/src/color.js +111 -0
- package/dist/src/engine.d.ts +108 -0
- package/dist/src/engine.js +612 -0
- package/dist/src/evidence.d.ts +21 -0
- package/dist/src/evidence.js +77 -0
- package/dist/src/index.d.ts +26 -0
- package/dist/src/index.js +19 -0
- package/dist/src/meaning.d.ts +28 -0
- package/dist/src/meaning.js +226 -0
- package/dist/src/netpolicy.d.ts +34 -0
- package/dist/src/netpolicy.js +122 -0
- package/dist/src/normalize.d.ts +34 -0
- package/dist/src/normalize.js +204 -0
- package/dist/src/policyfile.d.ts +2 -0
- package/dist/src/policyfile.js +50 -0
- package/dist/src/sandbox.d.ts +45 -0
- package/dist/src/sandbox.js +231 -0
- package/dist/src/script.d.ts +23 -0
- package/dist/src/script.js +220 -0
- package/dist/src/secrets.d.ts +24 -0
- package/dist/src/secrets.js +192 -0
- package/dist/src/supplychain.d.ts +24 -0
- package/dist/src/supplychain.js +182 -0
- package/dist/src/taint.d.ts +37 -0
- package/dist/src/taint.js +39 -0
- package/dist/src/types.d.ts +176 -0
- package/dist/src/types.js +25 -0
- package/dist/src/visibility.d.ts +41 -0
- package/dist/src/visibility.js +424 -0
- package/package.json +64 -4
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text normalization for the meaning pass: produces the variants an attacker
|
|
3
|
+
* uses to dodge literal matching. Each variant is scored; a hit on a variant
|
|
4
|
+
* other than the original carries a small confidence discount and names the
|
|
5
|
+
* transform in the finding.
|
|
6
|
+
*
|
|
7
|
+
* Also the decoders the character pass calls for encoded payloads.
|
|
8
|
+
*/
|
|
9
|
+
const LEET = { "0": "o", "1": "i", "3": "e", "4": "a", "5": "s", "7": "t", "@": "a", "$": "s", "|": "l", "!": "i" };
|
|
10
|
+
/** 1gn0r3 -> ignore, only inside tokens that mix letters and leet digits */
|
|
11
|
+
export function deLeet(text) {
|
|
12
|
+
return text.replace(/\b(?=[a-z0-9@$|!]*[a-z])(?=[a-z0-9@$|!]*[0-9@$|!])[a-z0-9@$|!]{3,}\b/gi, (w) => [...w].map((c) => LEET[c] ?? c).join(""));
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* i.g.n.o.r.e / i-g_n.o*r:e / i g n o r e -> ignore.
|
|
16
|
+
* Stage 1 folds runs of single letters joined by any non-space separators (mixed separators allowed).
|
|
17
|
+
* Stage 2 folds runs joined by single spaces. Doing spaces last keeps the word break in
|
|
18
|
+
* "i.g.n.o.r.e p.r.e.v" while still folding "a l l".
|
|
19
|
+
*/
|
|
20
|
+
export function deSpace(text) {
|
|
21
|
+
const stage1 = text.replace(/(?<![a-z])[a-z](?:[^a-z0-9\s\u00C0-\u024F][a-z]){2,}(?![a-z])/gi, (run) => run.replace(/[^a-z]/gi, ""));
|
|
22
|
+
return stage1.replace(/(?<![a-z])[a-z](?: [a-z]){2,}(?![a-z])/gi, (run) => run.replace(/ /g, ""));
|
|
23
|
+
}
|
|
24
|
+
export function reverse(text) {
|
|
25
|
+
return [...text].reverse().join("");
|
|
26
|
+
}
|
|
27
|
+
export function rot13(text) {
|
|
28
|
+
return text.replace(/[a-z]/gi, (c) => {
|
|
29
|
+
const base = c <= "Z" ? 65 : 97;
|
|
30
|
+
return String.fromCharCode(((c.charCodeAt(0) - base + 13) % 26) + base);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/** Fold the look-alike scripts that have Latin twins: Armenian, Cherokee, Lisu, Greek, Cyrillic (extended), Georgian, Latin letter-likes. */
|
|
34
|
+
const SKELETON = {
|
|
35
|
+
// Armenian
|
|
36
|
+
"օ": "o", "ո": "n", "ս": "s", "ւ": "u", "ց": "g", "հ": "h", "յ": "j", "ր": "n", "ա": "w", "զ": "q", "է": "t", "թ": "p", "լ": "l", "մ": "u", "ն": "u", "չ": "z", "ռ": "n", "ք": "p",
|
|
37
|
+
"Տ": "S", "Օ": "O", "Ո": "N", "Մ": "U", "Լ": "L", "Ե": "t", "Ց": "Q",
|
|
38
|
+
// Cherokee
|
|
39
|
+
"Ꭰ": "D", "Ꭱ": "R", "Ꭲ": "T", "Ꭵ": "i", "Ꭺ": "A", "Ꭻ": "J", "Ꭼ": "E", "Ꮃ": "W", "Ꮇ": "M", "Ꮋ": "H", "Ꮍ": "Y", "Ꮐ": "G", "Ꮒ": "h", "Ꮓ": "Z", "Ꮟ": "b", "Ꮢ": "R", "Ꮩ": "V", "Ꮪ": "S", "Ꮮ": "L", "Ꮯ": "C", "Ꮲ": "P", "Ꮶ": "K", "Ꮼ": "J", "Ᏻ": "G", "Ᏼ": "B", "Ꭹ": "y", "Ꮅ": "O", "Ꭴ": "O", "Ꭾ": "I",
|
|
40
|
+
// Lisu
|
|
41
|
+
"ꓐ": "B", "ꓑ": "P", "ꓒ": "d", "ꓓ": "D", "ꓔ": "T", "ꓖ": "G", "ꓗ": "K", "ꓙ": "J", "ꓚ": "C", "ꓛ": "Z", "ꓜ": "F", "ꓝ": "M", "ꓞ": "N", "ꓟ": "L", "ꓠ": "S", "ꓡ": "R", "ꓣ": "V", "ꓤ": "H", "ꓦ": "W", "ꓧ": "X", "ꓨ": "Y", "ꓩ": "A", "ꓪ": "E", "ꓫ": "U", "ꓬ": "I", "ꓭ": "O",
|
|
42
|
+
// Greek extras beyond the character pass table
|
|
43
|
+
"α": "a", "ε": "e", "ι": "i", "κ": "k", "ν": "v", "ρ": "p", "τ": "t", "υ": "u", "χ": "x", "γ": "y",
|
|
44
|
+
// Cyrillic extras
|
|
45
|
+
"в": "b", "г": "r", "к": "k", "н": "h", "п": "n", "т": "t", "ч": "y", "ь": "b", "Ѳ": "o", "ӏ": "l", "ѡ": "w", "с": "c", "ѕ": "s", "і": "i", "ј": "j", "һ": "h", "ԁ": "d", "ԛ": "q", "а": "a", "е": "e", "о": "o", "р": "p", "у": "y", "х": "x",
|
|
46
|
+
// Coptic
|
|
47
|
+
"\u2C9F": "o", "\u2C81": "a", "\u2C89": "e", "\u2C93": "i", "\u2C95": "k", "\u2C99": "m", "\u2C9B": "n", "\u2CA1": "p", "\u2CA3": "r", "\u2CA5": "s", "\u2CA7": "t", "\u2CA9": "u", "\u2CAD": "x", "\u2C8F": "z", "\u2C83": "b", "\u2C85": "g", "\u2C87": "d", "\u2C8D": "h", "\u2C97": "l", "\u2CB1": "w", "\u2CAF": "y",
|
|
48
|
+
"\u2C9E": "O", "\u2C80": "A", "\u2C88": "E", "\u2C92": "I", "\u2C94": "K", "\u2C98": "M", "\u2C9A": "N", "\u2CA0": "P", "\u2CA2": "R", "\u2CA4": "S", "\u2CA6": "T", "\u2CA8": "Y", "\u2CAC": "X", "\u2C8E": "Z", "\u2C82": "B", "\u2C86": "D", "\u2C8C": "H",
|
|
49
|
+
// Tifinagh
|
|
50
|
+
"\u2D54": "o", "\u2D53": "u", "\u2D59": "s", "\u2D3F": "c", "\u2D4F": "i", "\u2D55": "q", "\u2D30": "a", "\u2D44": "e", "\u2D3E": "k", "\u2D4D": "l", "\u2D3B": "x", "\u2D4B": "z",
|
|
51
|
+
// Canadian syllabics
|
|
52
|
+
"\u1455": "c", "\u15DE": "d", "\u157C": "h", "\u148D": "j", "\u14AA": "l", "\u146D": "p", "\u15B4": "f", "\u144C": "u", "\u142F": "v", "\u15EF": "w", "\u166D": "x", "\u1431": "a", "\u15AE": "e", "\u15F7": "b", "\u1587": "r", "\u1400": "e", "\u146B": "b", "\u1401": "o", "\u1450": "o", "\u14BF": "l", "\u1468": "o", "\u15E9": "w", "\u1466": "o", "\u14EC": "n",
|
|
53
|
+
// Deseret, Glagolitic, Georgian lookalikes
|
|
54
|
+
"\u10432": "o", "\u10419": "O", "\u2C3B": "o", "\u2C3E": "s", "\u10FF": "o", "\u10D5": "o", "\u10E7": "y", "\u10DA": "m",
|
|
55
|
+
// Latin-like symbols and letters
|
|
56
|
+
"ı": "i", "ȷ": "j", "ɪ": "i", "ɴ": "n", "ʀ": "r", "ᴀ": "a", "ᴄ": "c", "ᴇ": "e", "ᴏ": "o", "ᴘ": "p", "ᴛ": "t", "ᴜ": "u", "ᴠ": "v", "ᴡ": "w", "ᴢ": "z",
|
|
57
|
+
"ß": "ss", "ł": "l", "đ": "d", "ħ": "h", "ŧ": "t", "ɍ": "r", "ℓ": "l", "K": "K", "Ω": "O", "Å": "A",
|
|
58
|
+
};
|
|
59
|
+
/** NFKC fold + script skeleton + strip combining marks: the shape a human reads. */
|
|
60
|
+
export function skeleton(text) {
|
|
61
|
+
const nfkc = text.normalize("NFKC");
|
|
62
|
+
let out = "";
|
|
63
|
+
for (const ch of nfkc)
|
|
64
|
+
out += SKELETON[ch] ?? ch;
|
|
65
|
+
return out.normalize("NFD").replace(/\p{M}+/gu, "").normalize("NFC");
|
|
66
|
+
}
|
|
67
|
+
/** Variants of the text the meaning pass should score. The original is always first. */
|
|
68
|
+
export function variants(text) {
|
|
69
|
+
const out = [{ name: "original", text, discount: 1 }];
|
|
70
|
+
const sk = skeleton(text);
|
|
71
|
+
if (sk !== text)
|
|
72
|
+
out.push({ name: "skeleton", text: sk, discount: 0.95 });
|
|
73
|
+
const ds = deSpace(sk);
|
|
74
|
+
if (ds !== sk)
|
|
75
|
+
out.push({ name: "despaced", text: ds, discount: 0.9 });
|
|
76
|
+
const dl = deLeet(ds);
|
|
77
|
+
if (dl !== ds)
|
|
78
|
+
out.push({ name: "deleet", text: dl, discount: 0.9 });
|
|
79
|
+
// reversed and rot13 only matter if they contain Latin letters at all
|
|
80
|
+
if (/[a-z]{4,}/i.test(sk)) {
|
|
81
|
+
out.push({ name: "reversed", text: reverse(sk), discount: 0.85 });
|
|
82
|
+
out.push({ name: "rot13", text: rot13(sk), discount: 0.85 });
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
// ---------- decoders ----------
|
|
87
|
+
const ASCII_TEXT = /^[\x09\x0A\x0D\x20-\x7E]+$/;
|
|
88
|
+
const looksLikeWords = (s) => s.length >= 12 && ASCII_TEXT.test(s) && (s.match(/[a-z]{2,}/gi) ?? []).length >= 3 && /\s/.test(s);
|
|
89
|
+
function fromCodes(codes) {
|
|
90
|
+
let out = "";
|
|
91
|
+
for (let i = 0; i < codes.length && i < 200000; i += 8192)
|
|
92
|
+
out += String.fromCharCode(...codes.slice(i, i + 8192));
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
export function decodePayloads(text) {
|
|
96
|
+
const out = [];
|
|
97
|
+
const seen = new Set();
|
|
98
|
+
const push = (scheme, dec, source) => { if (looksLikeWords(dec) && !seen.has(dec)) {
|
|
99
|
+
seen.add(dec);
|
|
100
|
+
out.push({ scheme, text: dec, source });
|
|
101
|
+
} };
|
|
102
|
+
// base64: contiguous runs >= 20, plus runs broken by whitespace/newlines
|
|
103
|
+
for (const run of text.match(/(?<![A-Za-z0-9+/])[A-Za-z0-9+/]{20,}={0,2}(?![A-Za-z0-9+/])/g) ?? []) {
|
|
104
|
+
try {
|
|
105
|
+
push("base64", Buffer.from(run, "base64").toString("utf8"), run.slice(0, 60));
|
|
106
|
+
}
|
|
107
|
+
catch { /* not base64 */ }
|
|
108
|
+
}
|
|
109
|
+
// chunk lengths are bounded (base64 lines are at most 76 chars) so a long unbroken run cannot make this quadratic
|
|
110
|
+
for (const run of text.match(/(?<![A-Za-z0-9+/])(?:[A-Za-z0-9+/]{8,76}[ \t\r\n]{1,4}){1,40}[A-Za-z0-9+/]{4,76}={0,2}(?![A-Za-z0-9+/])/g) ?? []) {
|
|
111
|
+
const joined = run.replace(/\s+/g, "");
|
|
112
|
+
if (joined.length >= 24) {
|
|
113
|
+
try {
|
|
114
|
+
push("base64-split", Buffer.from(joined, "base64").toString("utf8"), run.slice(0, 60));
|
|
115
|
+
}
|
|
116
|
+
catch { /* ignore */ }
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// base64 chunked by short line breaks (4-8 char chunks): bounded groups, bounded whitespace
|
|
120
|
+
for (const run of text.match(/(?<![A-Za-z0-9+/])[A-Za-z0-9+/]{2,76}(?:[ \t\r\n]{1,2}[A-Za-z0-9+/]{2,76}){3,400}={0,2}(?![A-Za-z0-9+/])/g) ?? []) {
|
|
121
|
+
const joined = run.replace(/\s+/g, "");
|
|
122
|
+
const ws = run.length - joined.length;
|
|
123
|
+
if (joined.length >= 24 && ws / run.length < 0.4) {
|
|
124
|
+
try {
|
|
125
|
+
push("base64-chunked", Buffer.from(joined, "base64").toString("utf8"), run.slice(0, 60));
|
|
126
|
+
}
|
|
127
|
+
catch { /* ignore */ }
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// quoted-printable
|
|
131
|
+
for (const run of text.match(/(?:=[0-9A-F]{2}){6,}/g) ?? [])
|
|
132
|
+
push("quoted-printable", Buffer.from(run.replace(/=/g, ""), "hex").toString("utf8"), run.slice(0, 60));
|
|
133
|
+
// percent-encoding, short runs and UTF-8
|
|
134
|
+
for (const run of text.match(/(?:%[0-9a-fA-F]{2}){4,}/g) ?? []) {
|
|
135
|
+
try {
|
|
136
|
+
push("percent", decodeURIComponent(run), run.slice(0, 60));
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
push("percent", Buffer.from(run.replace(/%/g, ""), "hex").toString("latin1"), run.slice(0, 60));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// base64url
|
|
143
|
+
for (const run of text.match(/(?<![A-Za-z0-9_-])[A-Za-z0-9_-]{24,}(?![A-Za-z0-9_-])/g) ?? []) {
|
|
144
|
+
if (/[_-]/.test(run)) {
|
|
145
|
+
try {
|
|
146
|
+
push("base64url", Buffer.from(run, "base64url").toString("utf8"), run.slice(0, 60));
|
|
147
|
+
}
|
|
148
|
+
catch { /* ignore */ }
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// base32
|
|
152
|
+
for (const run of text.match(/(?<![A-Z2-7])[A-Z2-7]{16,}={0,6}(?![A-Z2-7])/g) ?? [])
|
|
153
|
+
push("base32", base32Decode(run), run.slice(0, 60));
|
|
154
|
+
// hex: \x.., %.., 0x.., bare pairs
|
|
155
|
+
for (const run of text.match(/(?:\\x[0-9a-fA-F]{2}){8,}|(?:%[0-9a-fA-F]{2}){8,}|(?:0x[0-9a-fA-F]{2}[\s,]*){8,}/g) ?? []) {
|
|
156
|
+
const hex = run.replace(/\\x|%|0x|[\s,]/g, "");
|
|
157
|
+
push("hex", Buffer.from(hex, "hex").toString("utf8"), run.slice(0, 60));
|
|
158
|
+
}
|
|
159
|
+
for (const run of text.match(/\b(?:[0-9a-f]{2}){12,}\b/gi) ?? [])
|
|
160
|
+
push("hex", Buffer.from(run, "hex").toString("utf8"), run.slice(0, 60));
|
|
161
|
+
// decimal / hex-pair / octal char codes: try each reading of a run of 2-3 digit tokens
|
|
162
|
+
for (const run of text.match(/(?:\b[0-9a-fA-F]{2,3}[\s,;]+){8,}\b[0-9a-fA-F]{2,3}\b/g) ?? []) {
|
|
163
|
+
const toks = run.split(/[\s,;]+/).filter(Boolean);
|
|
164
|
+
const tryCodes = (scheme, parse) => {
|
|
165
|
+
const codes = toks.map(parse).filter((n) => Number.isFinite(n) && n >= 9 && n < 127);
|
|
166
|
+
if (codes.length >= toks.length * 0.9)
|
|
167
|
+
push(scheme, fromCodes(codes), run.slice(0, 60));
|
|
168
|
+
};
|
|
169
|
+
if (toks.every((t) => /^\d+$/.test(t)))
|
|
170
|
+
tryCodes("charcodes-decimal", (t) => parseInt(t, 10));
|
|
171
|
+
if (toks.every((t) => /^[0-7]{2,3}$/.test(t)))
|
|
172
|
+
tryCodes("charcodes-octal", (t) => parseInt(t, 8));
|
|
173
|
+
if (toks.every((t) => /^[0-9a-fA-F]{2}$/.test(t)))
|
|
174
|
+
tryCodes("charcodes-hex", (t) => parseInt(t, 16));
|
|
175
|
+
}
|
|
176
|
+
for (const run of text.match(/(?:[01]{8}[ \t]{0,2}){6,2000}/g) ?? []) {
|
|
177
|
+
const bits = run.replace(/\s/g, "");
|
|
178
|
+
let s = "";
|
|
179
|
+
for (let i = 0; i + 8 <= bits.length; i += 8)
|
|
180
|
+
s += String.fromCharCode(parseInt(bits.slice(i, i + 8), 2));
|
|
181
|
+
push("binary", s, run.slice(0, 60));
|
|
182
|
+
}
|
|
183
|
+
// unicode escapes i and HTML numeric entities in non-HTML text
|
|
184
|
+
if (/\\u[0-9a-f]{4}/i.test(text))
|
|
185
|
+
push("unicode-escape", text.replace(/\\u([0-9a-f]{4})/gi, (_, h) => String.fromCharCode(parseInt(h, 16))).replace(/[^\x09\x0A\x0D\x20-\x7E]/g, ""), "\\u escapes");
|
|
186
|
+
// rot13 of the whole text is handled by variants(); nothing to do here
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
function base32Decode(s) {
|
|
190
|
+
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
191
|
+
let bits = 0, value = 0, out = "";
|
|
192
|
+
for (const c of s.replace(/=+$/, "")) {
|
|
193
|
+
const idx = alphabet.indexOf(c);
|
|
194
|
+
if (idx < 0)
|
|
195
|
+
return "";
|
|
196
|
+
value = (value << 5) | idx;
|
|
197
|
+
bits += 5;
|
|
198
|
+
if (bits >= 8) {
|
|
199
|
+
out += String.fromCharCode((value >>> (bits - 8)) & 255);
|
|
200
|
+
bits -= 8;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Policy file loader. JSON, validated loosely; unknown keys are ignored so
|
|
3
|
+
* older engines can read newer files.
|
|
4
|
+
*
|
|
5
|
+
* {
|
|
6
|
+
* "thresholds": { "suspect": 0.25, "hostile": 0.7 },
|
|
7
|
+
* "net": { "denyHosts": [".internal.example"], "allowHosts": [], "extraPorts": [8080] },
|
|
8
|
+
* "domains": [
|
|
9
|
+
* { "match": ".trusted-docs.example", "suspectThreshold": 0.5 },
|
|
10
|
+
* { "match": "pastebin.com", "hostileThreshold": 0.4 },
|
|
11
|
+
* { "match": ".evil.example", "deny": true }
|
|
12
|
+
* ],
|
|
13
|
+
* "sandbox": "escalate"
|
|
14
|
+
* }
|
|
15
|
+
*/
|
|
16
|
+
import { readFileSync } from "node:fs";
|
|
17
|
+
export function loadPolicyFile(path) {
|
|
18
|
+
const raw = JSON.parse(readFileSync(path, "utf8"));
|
|
19
|
+
const out = {};
|
|
20
|
+
const num = (v) => (typeof v === "number" && v >= 0 && v <= 1 ? v : undefined);
|
|
21
|
+
if (raw.thresholds) {
|
|
22
|
+
const s = num(raw.thresholds.suspect), h = num(raw.thresholds.hostile);
|
|
23
|
+
if (s !== undefined)
|
|
24
|
+
out.suspectThreshold = s;
|
|
25
|
+
if (h !== undefined)
|
|
26
|
+
out.hostileThreshold = h;
|
|
27
|
+
}
|
|
28
|
+
if (raw.net && typeof raw.net === "object") {
|
|
29
|
+
out.net = {
|
|
30
|
+
denyHosts: Array.isArray(raw.net.denyHosts) ? raw.net.denyHosts.filter((x) => typeof x === "string") : [],
|
|
31
|
+
allowHosts: Array.isArray(raw.net.allowHosts) ? raw.net.allowHosts.filter((x) => typeof x === "string") : [],
|
|
32
|
+
extraPorts: Array.isArray(raw.net.extraPorts) ? raw.net.extraPorts.filter((x) => Number.isInteger(x)) : [],
|
|
33
|
+
allowPrivate: raw.net.allowPrivate === true,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
if (Array.isArray(raw.domains)) {
|
|
37
|
+
out.policy = {
|
|
38
|
+
domains: raw.domains
|
|
39
|
+
.filter((d) => d && typeof d.match === "string")
|
|
40
|
+
.map((d) => ({ match: d.match, deny: d.deny === true, suspectThreshold: num(d.suspectThreshold), hostileThreshold: num(d.hostileThreshold), cloakingCheck: typeof d.cloakingCheck === "boolean" ? d.cloakingCheck : undefined })),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
if (raw.sandbox === "off" || raw.sandbox === "escalate" || raw.sandbox === "always")
|
|
44
|
+
out.sandbox = raw.sandbox;
|
|
45
|
+
if (typeof raw.cloakingCheck === "boolean")
|
|
46
|
+
out.cloakingCheck = raw.cloakingCheck;
|
|
47
|
+
if (typeof raw.fetchStylesheets === "boolean")
|
|
48
|
+
out.fetchStylesheets = raw.fetchStylesheets;
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sandbox pass.
|
|
3
|
+
*
|
|
4
|
+
* Runs the page in a disposable headless Chromium context with no cookies,
|
|
5
|
+
* no storage, no credentials, and egress limited to the page's own origin
|
|
6
|
+
* plus stylesheets/scripts it references (so it renders) while every
|
|
7
|
+
* other request is blocked and recorded. Runs twice:
|
|
8
|
+
*
|
|
9
|
+
* automation-visible — navigator.webdriver true, headless signals intact
|
|
10
|
+
* stealth — webdriver hidden, plugin/language counts normal,
|
|
11
|
+
* a desktop user agent
|
|
12
|
+
*
|
|
13
|
+
* Compares the rendered text of the two runs (webdriver-keyed cloaking),
|
|
14
|
+
* the rendered text against the static extraction (text injected after
|
|
15
|
+
* load), and labels text by computed style (true visibility).
|
|
16
|
+
*
|
|
17
|
+
* Playwright is an optional dependency: if it is not installed the pass
|
|
18
|
+
* reports itself as not run and the engine proceeds statically.
|
|
19
|
+
*/
|
|
20
|
+
import type { Finding, TextSegment } from "./types.js";
|
|
21
|
+
export interface SandboxResult {
|
|
22
|
+
ran: boolean;
|
|
23
|
+
reason?: string;
|
|
24
|
+
findings: Finding[];
|
|
25
|
+
/** segments as the browser computed them (stealth run); replaces static segments when present */
|
|
26
|
+
segments?: TextSegment[];
|
|
27
|
+
/** text present only in the automation-visible render: what the attacker shows agents and hides from humans */
|
|
28
|
+
automationOnlyText?: string;
|
|
29
|
+
renderedVisibleText?: string;
|
|
30
|
+
blockedRequests: string[];
|
|
31
|
+
timings: {
|
|
32
|
+
total: number;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** cross-origin script hosts the sandbox may load so ordinary pages render; everything else is blocked and recorded */
|
|
36
|
+
export declare const DEFAULT_SCRIPT_HOSTS: string[];
|
|
37
|
+
export declare function runSandbox(opts: {
|
|
38
|
+
html: string;
|
|
39
|
+
url: string;
|
|
40
|
+
staticVisibleText: string;
|
|
41
|
+
timeoutMs: number;
|
|
42
|
+
allowedScriptHosts?: string[];
|
|
43
|
+
/** path to a Chromium binary; defaults to INGRESS_CHROMIUM or Playwright's bundled browser */
|
|
44
|
+
executablePath?: string;
|
|
45
|
+
}): Promise<SandboxResult>;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sandbox pass.
|
|
3
|
+
*
|
|
4
|
+
* Runs the page in a disposable headless Chromium context with no cookies,
|
|
5
|
+
* no storage, no credentials, and egress limited to the page's own origin
|
|
6
|
+
* plus stylesheets/scripts it references (so it renders) while every
|
|
7
|
+
* other request is blocked and recorded. Runs twice:
|
|
8
|
+
*
|
|
9
|
+
* automation-visible — navigator.webdriver true, headless signals intact
|
|
10
|
+
* stealth — webdriver hidden, plugin/language counts normal,
|
|
11
|
+
* a desktop user agent
|
|
12
|
+
*
|
|
13
|
+
* Compares the rendered text of the two runs (webdriver-keyed cloaking),
|
|
14
|
+
* the rendered text against the static extraction (text injected after
|
|
15
|
+
* load), and labels text by computed style (true visibility).
|
|
16
|
+
*
|
|
17
|
+
* Playwright is an optional dependency: if it is not installed the pass
|
|
18
|
+
* reports itself as not run and the engine proceeds statically.
|
|
19
|
+
*/
|
|
20
|
+
const RENDER_SCRIPT = `
|
|
21
|
+
(() => {
|
|
22
|
+
const out = [];
|
|
23
|
+
const walker = document.createTreeWalker(document.body || document.documentElement, NodeFilter.SHOW_TEXT);
|
|
24
|
+
let n;
|
|
25
|
+
while ((n = walker.nextNode())) {
|
|
26
|
+
const t = n.nodeValue;
|
|
27
|
+
if (!t || !t.trim()) continue;
|
|
28
|
+
const el = n.parentElement;
|
|
29
|
+
if (!el) continue;
|
|
30
|
+
const tag = el.tagName.toLowerCase();
|
|
31
|
+
if (tag === 'script' || tag === 'style' || tag === 'noscript' || tag === 'template') continue;
|
|
32
|
+
let hidden = false, why = '';
|
|
33
|
+
let e = el;
|
|
34
|
+
while (e && e !== document.documentElement) {
|
|
35
|
+
const cs = getComputedStyle(e);
|
|
36
|
+
if (cs.display === 'none') { hidden = true; why = 'display:none'; break; }
|
|
37
|
+
if (cs.visibility === 'hidden' || cs.visibility === 'collapse') { hidden = true; why = 'visibility:hidden'; break; }
|
|
38
|
+
if (parseFloat(cs.opacity) === 0) { hidden = true; why = 'opacity:0'; break; }
|
|
39
|
+
if (cs.clipPath === 'inset(100%)' || cs.clip === 'rect(0px, 0px, 0px, 0px)') { hidden = true; why = 'clipped'; break; }
|
|
40
|
+
e = e.parentElement;
|
|
41
|
+
}
|
|
42
|
+
if (!hidden) {
|
|
43
|
+
const cs = getComputedStyle(el);
|
|
44
|
+
const fs = parseFloat(cs.fontSize);
|
|
45
|
+
if (fs <= 1) { hidden = true; why = 'font-size:' + cs.fontSize; }
|
|
46
|
+
else if ((() => {
|
|
47
|
+
// text colour against the nearest painted background up the tree, alpha-composited; unknown (image/gradient) never flags
|
|
48
|
+
const rgba = (c) => { const m = c.match(/rgba?\(([^)]+)\)/); if (!m) return null; const p = m[1].split(/[\s,\/]+/).filter(Boolean).map(Number); return { r: p[0], g: p[1], b: p[2], a: p.length > 3 ? p[3] : 1 }; };
|
|
49
|
+
const mix = (t, u) => ({ r: t.r * t.a + u.r * (1 - t.a), g: t.g * t.a + u.g * (1 - t.a), b: t.b * t.a + u.b * (1 - t.a), a: 1 });
|
|
50
|
+
const lum = (c) => { const f = (x) => { x /= 255; return x <= 0.03928 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4); }; return 0.2126 * f(c.r) + 0.7152 * f(c.g) + 0.0722 * f(c.b); };
|
|
51
|
+
const layers = []; let p = el; let unknown = false;
|
|
52
|
+
while (p) { const s = getComputedStyle(p); if (s.backgroundImage && s.backgroundImage !== 'none') { unknown = true; break; } const c = rgba(s.backgroundColor); if (c && c.a > 0) { layers.push(c); if (c.a >= 0.999) break; } p = p.parentElement; }
|
|
53
|
+
if (unknown) return false;
|
|
54
|
+
let bg = { r: 255, g: 255, b: 255, a: 1 };
|
|
55
|
+
for (let i = layers.length - 1; i >= 0; i--) bg = mix(layers[i], bg);
|
|
56
|
+
const fill = cs.webkitTextFillColor && cs.webkitTextFillColor !== cs.color ? rgba(cs.webkitTextFillColor) : null;
|
|
57
|
+
const fg0 = fill || rgba(cs.color); if (!fg0) return false;
|
|
58
|
+
const fg = mix(fg0, bg);
|
|
59
|
+
const a = lum(fg), b = lum(bg);
|
|
60
|
+
const ratio = (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
|
|
61
|
+
if (ratio <= 1.25) { why = 'color matches background (contrast ' + ratio.toFixed(2) + ':1)'; return true; }
|
|
62
|
+
return false;
|
|
63
|
+
})()) { hidden = true; }
|
|
64
|
+
else {
|
|
65
|
+
const r = el.getBoundingClientRect();
|
|
66
|
+
const docW = Math.max(document.documentElement.scrollWidth, innerWidth);
|
|
67
|
+
const docH = Math.max(document.documentElement.scrollHeight, innerHeight);
|
|
68
|
+
if (r.width === 0 && r.height === 0) { hidden = true; why = 'zero box'; }
|
|
69
|
+
else if (r.right < 0 || r.bottom < 0 || r.left > docW || r.top > docH) { hidden = true; why = 'offscreen'; }
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const path = (() => { const parts = []; let p = el; let d = 0; while (p && d < 5) { parts.unshift(p.tagName.toLowerCase() + (p.id ? '#' + p.id : '')); p = p.parentElement; d++; } return parts.join('/'); })();
|
|
73
|
+
out.push({ text: t, hidden, why, path });
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
})()
|
|
77
|
+
`;
|
|
78
|
+
/** cross-origin script hosts the sandbox may load so ordinary pages render; everything else is blocked and recorded */
|
|
79
|
+
export const DEFAULT_SCRIPT_HOSTS = [
|
|
80
|
+
"cdnjs.cloudflare.com", "cdn.jsdelivr.net", "unpkg.com", "ajax.googleapis.com", "code.jquery.com", "cdn.tailwindcss.com", "esm.sh", "fonts.googleapis.com", "fonts.gstatic.com",
|
|
81
|
+
];
|
|
82
|
+
export async function runSandbox(opts) {
|
|
83
|
+
const t0 = performance.now();
|
|
84
|
+
let pw;
|
|
85
|
+
try {
|
|
86
|
+
pw = await import("playwright");
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return { ran: false, reason: "playwright not installed", findings: [], blockedRequests: [], timings: { total: performance.now() - t0 } };
|
|
90
|
+
}
|
|
91
|
+
const findings = [];
|
|
92
|
+
const blocked = [];
|
|
93
|
+
let origin = "";
|
|
94
|
+
try {
|
|
95
|
+
origin = new URL(opts.url).origin;
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
origin = "null";
|
|
99
|
+
}
|
|
100
|
+
let browser;
|
|
101
|
+
try {
|
|
102
|
+
browser = await pw.chromium.launch({
|
|
103
|
+
headless: true,
|
|
104
|
+
executablePath: opts.executablePath ?? process.env.INGRESS_CHROMIUM ?? undefined,
|
|
105
|
+
args: ["--disable-gpu", "--no-sandbox", "--disable-dev-shm-usage", "--disable-extensions", "--no-first-run"],
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
catch (e) {
|
|
109
|
+
return { ran: false, reason: `browser launch failed: ${e.message.split("\n")[0]}`, findings: [], blockedRequests: [], timings: { total: performance.now() - t0 } };
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
const runOnce = async (stealth) => {
|
|
113
|
+
const ctx = await browser.newContext({
|
|
114
|
+
javaScriptEnabled: true,
|
|
115
|
+
bypassCSP: false,
|
|
116
|
+
userAgent: stealth
|
|
117
|
+
? "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
|
|
118
|
+
: undefined,
|
|
119
|
+
viewport: { width: 1280, height: 900 },
|
|
120
|
+
locale: "en-US",
|
|
121
|
+
offline: false,
|
|
122
|
+
storageState: { cookies: [], origins: [] },
|
|
123
|
+
});
|
|
124
|
+
if (stealth) {
|
|
125
|
+
await ctx.addInitScript(() => {
|
|
126
|
+
Object.defineProperty(navigator, "webdriver", { get: () => false });
|
|
127
|
+
Object.defineProperty(navigator, "languages", { get: () => ["en-US", "en"] });
|
|
128
|
+
Object.defineProperty(navigator, "plugins", { get: () => [1, 2, 3, 4, 5] });
|
|
129
|
+
// @ts-ignore
|
|
130
|
+
window.chrome = { runtime: {} };
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
await ctx.route("**/*", (route) => {
|
|
134
|
+
const req = route.request();
|
|
135
|
+
const u = req.url();
|
|
136
|
+
const type = req.resourceType();
|
|
137
|
+
const sameOrigin = u.startsWith(origin);
|
|
138
|
+
// allow the document itself (served from memory), same-origin subresources, and cross-origin css/scripts so the page renders; block everything else
|
|
139
|
+
if (u === opts.url || u === opts.url + "/")
|
|
140
|
+
return route.fulfill({ status: 200, contentType: "text/html; charset=utf-8", body: opts.html });
|
|
141
|
+
if (type === "document")
|
|
142
|
+
return route.abort("blockedbyclient");
|
|
143
|
+
let host = "";
|
|
144
|
+
try {
|
|
145
|
+
host = new URL(u).hostname;
|
|
146
|
+
}
|
|
147
|
+
catch { /* ignore */ }
|
|
148
|
+
const cdn = (opts.allowedScriptHosts ?? DEFAULT_SCRIPT_HOSTS).some((h) => host === h || host.endsWith("." + h));
|
|
149
|
+
if (sameOrigin && (type === "stylesheet" || type === "script" || type === "font" || type === "image"))
|
|
150
|
+
return route.continue();
|
|
151
|
+
if (cdn && (type === "stylesheet" || type === "script" || type === "font"))
|
|
152
|
+
return route.continue();
|
|
153
|
+
blocked.push(u.slice(0, 200));
|
|
154
|
+
return route.abort("blockedbyclient");
|
|
155
|
+
});
|
|
156
|
+
const page = await ctx.newPage();
|
|
157
|
+
page.setDefaultTimeout(opts.timeoutMs);
|
|
158
|
+
try {
|
|
159
|
+
await page.goto(opts.url, { waitUntil: "domcontentloaded", timeout: opts.timeoutMs });
|
|
160
|
+
await page.waitForTimeout(Math.min(1500, opts.timeoutMs / 4));
|
|
161
|
+
}
|
|
162
|
+
catch { /* a page that never settles still gets evaluated */ }
|
|
163
|
+
let segs = [];
|
|
164
|
+
try {
|
|
165
|
+
segs = (await page.evaluate(RENDER_SCRIPT));
|
|
166
|
+
}
|
|
167
|
+
catch { /* evaluation blocked */ }
|
|
168
|
+
await ctx.close();
|
|
169
|
+
const text = segs.filter((s) => !s.hidden).map((s) => s.text).join(" ").replace(/\s+/g, " ").trim();
|
|
170
|
+
return { segs, text };
|
|
171
|
+
};
|
|
172
|
+
const [auto, stealth] = await Promise.all([runOnce(false), runOnce(true)]);
|
|
173
|
+
// 1. webdriver-keyed divergence
|
|
174
|
+
const sim = jaccard(auto.text, stealth.text);
|
|
175
|
+
if (sim < 0.6 && (auto.text.length > 200 || stealth.text.length > 200)) {
|
|
176
|
+
findings.push({
|
|
177
|
+
pass: "sandbox", kind: "sandbox.webdriver_keyed_divergence", confidence: Math.min(0.95, 0.55 + (0.6 - sim)), severity: 0.9,
|
|
178
|
+
message: `rendered text differs between an automation-visible browser and a stealth browser (${(sim * 100).toFixed(0)}% overlap)`,
|
|
179
|
+
excerpt: onlyIn(auto.text, stealth.text), action: "none",
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
// 2. text injected after load: in the stealth render but not in the static extraction
|
|
183
|
+
const injected = onlyIn(stealth.text, opts.staticVisibleText, 40);
|
|
184
|
+
const injectedWords = injected.split(/\s+/).filter(Boolean).length;
|
|
185
|
+
if (injectedWords >= 8) {
|
|
186
|
+
findings.push({
|
|
187
|
+
pass: "sandbox", kind: "sandbox.text_injected_after_load", confidence: Math.min(0.9, 0.4 + injectedWords / 100), severity: 0.7,
|
|
188
|
+
message: `${injectedWords}+ words appear only after scripts run`, excerpt: injected.slice(0, 200), action: "none",
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
// 3. computed-hidden text that static analysis thought visible
|
|
192
|
+
const hiddenSegs = stealth.segs.filter((s) => s.hidden && s.text.trim().split(/\s+/).length >= 6);
|
|
193
|
+
for (const h of hiddenSegs.slice(0, 10)) {
|
|
194
|
+
findings.push({
|
|
195
|
+
pass: "sandbox", kind: "sandbox.computed_hidden_text", confidence: 0.75, severity: 0.6,
|
|
196
|
+
message: `text hidden by computed style (${h.why})`, excerpt: h.text.trim().slice(0, 160), location: h.path, action: "quarantined",
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
// 4. blocked egress
|
|
200
|
+
const egress = [...new Set(blocked)];
|
|
201
|
+
if (egress.length) {
|
|
202
|
+
findings.push({
|
|
203
|
+
pass: "sandbox", kind: "sandbox.blocked_egress_attempt", confidence: egress.length > 3 ? 0.5 : 0.3, severity: 0.4,
|
|
204
|
+
message: `${egress.length} cross-origin requests blocked during render`, excerpt: egress.slice(0, 4).join(" "), action: "none",
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
// segment-level diff keeps the attacker's sentences intact for the meaning pass
|
|
208
|
+
const stealthSet = new Set(stealth.segs.map((x) => x.text.trim()));
|
|
209
|
+
const automationOnlyText = auto.segs.filter((x) => !x.hidden && !stealthSet.has(x.text.trim())).map((x) => x.text.trim()).join("\n").slice(0, 4000);
|
|
210
|
+
const segments = stealth.segs.map((s) => ({ text: s.text, visibility: s.hidden ? (s.why === "offscreen" ? "offscreen" : s.why.startsWith("font-size") || s.why.includes("background") || s.why === "opacity:0" ? "tiny_or_transparent" : "css_hidden") : "visible", path: s.path }));
|
|
211
|
+
return { ran: true, findings, segments, automationOnlyText, renderedVisibleText: stealth.text, blockedRequests: egress, timings: { total: performance.now() - t0 } };
|
|
212
|
+
}
|
|
213
|
+
finally {
|
|
214
|
+
await browser.close();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function jaccard(a, b) {
|
|
218
|
+
const A = new Set(a.toLowerCase().split(/\W+/).filter((w) => w.length > 2));
|
|
219
|
+
const B = new Set(b.toLowerCase().split(/\W+/).filter((w) => w.length > 2));
|
|
220
|
+
if (!A.size && !B.size)
|
|
221
|
+
return 1;
|
|
222
|
+
let inter = 0;
|
|
223
|
+
for (const w of A)
|
|
224
|
+
if (B.has(w))
|
|
225
|
+
inter++;
|
|
226
|
+
return inter / (A.size + B.size - inter);
|
|
227
|
+
}
|
|
228
|
+
function onlyIn(a, b, max = 30) {
|
|
229
|
+
const B = new Set(b.toLowerCase().split(/\W+/));
|
|
230
|
+
return a.split(/\s+/).filter((w) => w.length > 2 && !B.has(w.toLowerCase().replace(/\W/g, ""))).slice(0, max).join(" ");
|
|
231
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Script pass (underlight lineage).
|
|
3
|
+
*
|
|
4
|
+
* Static, millisecond-budget analysis of every script on the page. It does
|
|
5
|
+
* not execute anything. A high score marks the page for sandbox escalation
|
|
6
|
+
* (sandbox not yet implemented; the hook is in engine.ts).
|
|
7
|
+
*
|
|
8
|
+
* Surfaces, from the underlight list: obfuscation and minification signatures,
|
|
9
|
+
* runtime code generation, typed-array / binary-buffer decoding, automation
|
|
10
|
+
* detection (agent-targeted cloaking), post-load text injection, and outbound
|
|
11
|
+
* beacons that could carry exfiltrated context.
|
|
12
|
+
*/
|
|
13
|
+
import type { Finding, ScriptUnit } from "./types.js";
|
|
14
|
+
export interface ScriptResult {
|
|
15
|
+
findings: Finding[];
|
|
16
|
+
/** 0..1, the maximum single-script suspicion; drives sandbox escalation */
|
|
17
|
+
maxSuspicion: number;
|
|
18
|
+
}
|
|
19
|
+
export declare function analyzeScript(unit: ScriptUnit): {
|
|
20
|
+
findings: Finding[];
|
|
21
|
+
suspicion: number;
|
|
22
|
+
};
|
|
23
|
+
export declare function analyzeScripts(units: ScriptUnit[]): ScriptResult;
|