Repository navigation
Expand file tree
/
Copy pathexplore.ts
More file actions
200 lines (173 loc) · 5.67 KB
/
Copy pathexplore.ts
File metadata and controls
200 lines (173 loc) · 5.67 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
import type { Layer, RecipeStep } from '../types';
import { getOperation } from '../operations';
import { byteLength, previewText } from '../core/bytes';
import { randomId } from '../core/uid';
import { detect } from '../detection/detect';
import { findEmbedded } from '../detection/embedded';
import type { AnalysisNode, AnalyseOptions } from './types';
interface Decoded {
output: string;
format: string;
confidence: number;
step: RecipeStep;
}
/**
* The chain the decoder already worked out, or null to work it out here.
*
* Deriving the same chain twice is the most expensive thing this tool does. On
* a 1.9 MB file wrapped forty-four times, the decoder and the analysis were
* each unwrapping all forty-four layers — the same work, on the same bytes, to
* the same answer — and neither finished inside its time budget, because the
* other was spending it.
*
* An empty array is not the same as null. It means the chain was supplied and
* has run out, so there is nothing further down to look for.
*/
export type Spine = readonly Layer[] | null;
interface Budget {
nodes: number;
deadline: number;
seen: Set<string>;
truncated: boolean;
}
function exhausted(budget: Budget, options: AnalyseOptions): boolean {
if (budget.nodes >= options.maxNodes || performance.now() > budget.deadline) {
budget.truncated = true;
return true;
}
return false;
}
async function decodeBest(text: string, options: AnalyseOptions): Promise<Decoded | null> {
const candidates = await detect(text);
const best = candidates[0];
if (!best || best.confidence < options.threshold) return null;
const step = best.steps[0];
if (!step) return null;
const operation = getOperation(step.opId);
if (!operation) return null;
try {
const output = await Promise.resolve(operation.run(text, step.args));
if (output === text || output.length === 0) return null;
return { output, format: best.format, confidence: best.confidence, step };
} catch {
return null;
}
}
/**
* The parent's path with one more format on the end, repeats counted.
*
* Written out in full, forty-four layers of Base64 is six lines of the same
* word in every finding, every search hit and every panel that shows where a
* layer came from.
*/
function extendPath(parentPath: string, format: string): string {
if (parentPath.length === 0) return format;
const parts = parentPath.split(' → ');
const last = parts[parts.length - 1] ?? '';
const repeated = /^(.*) ×(\d+)$/.exec(last);
if (repeated && repeated[1] === format) {
parts[parts.length - 1] = `${format} ×${Number(repeated[2]) + 1}`;
} else if (last === format) {
parts[parts.length - 1] = `${format} ×2`;
} else {
parts.push(format);
}
return parts.join(' → ');
}
function makeNode(
format: string,
confidence: number,
depth: number,
parentPath: string,
output: string,
steps: RecipeStep[],
origin?: { label: string; offset: number },
): AnalysisNode {
const path = extendPath(parentPath, format);
return {
id: randomId(`${format}-${depth}`),
format,
confidence,
depth,
path,
byteLength: byteLength(output),
output,
preview: previewText(output, 120),
steps,
children: [],
...(origin ? { origin } : {}),
};
}
function fromSpine(spine: Spine): Decoded | null {
const next = spine?.[0];
const step = next?.steps[next.steps.length - 1];
if (!next || !step) return null;
return { output: next.output, format: next.format, confidence: next.confidence, step };
}
async function walk(
node: AnalysisNode,
options: AnalyseOptions,
budget: Budget,
spine: Spine,
): Promise<void> {
if (node.depth >= options.maxDepth) return;
if (exhausted(budget, options)) return;
const whole = spine === null ? await decodeBest(node.output, options) : fromSpine(spine);
if (whole && !budget.seen.has(whole.output)) {
budget.seen.add(whole.output);
budget.nodes++;
const child = makeNode(
whole.format,
whole.confidence,
node.depth + 1,
node.path,
whole.output,
[...node.steps, whole.step],
);
node.children.push(child);
await walk(child, options, budget, spine === null ? null : spine.slice(1));
}
/*
* Then the parts. A layer is rarely the whole story: a JSON body with one
* Base64 field, a log line with a JWT in it, or plain text with a payload
* glued to the end all decode in part and not in whole.
*
* These children carry the steps that cut the payload out before decoding
* it, so a branch of the tree is a recipe that runs, not just a label.
*/
for (const find of findEmbedded(node.output)) {
if (exhausted(budget, options)) return;
if (budget.seen.has(find.decoded)) continue;
budget.seen.add(find.decoded);
budget.nodes++;
const child = makeNode(
find.format,
find.confidence,
node.depth + 1,
node.path,
find.decoded,
[...node.steps, ...find.steps],
{ label: find.format, offset: find.offset },
);
node.children.push(child);
// A branch is nothing the decoder walked, so it is explored from here.
await walk(child, options, budget, null);
}
}
export async function explore(
input: string,
options: AnalyseOptions,
spine: Spine = null,
): Promise<{ root: AnalysisNode; nodes: number; truncated: boolean }> {
const root = makeNode('Input', 1, 0, '', input, []);
root.path = 'Input';
const budget: Budget = {
nodes: 1,
deadline: performance.now() + options.budgetMs,
seen: new Set([input]),
truncated: false,
};
if (input.trim().length > 0) await walk(root, options, budget, spine);
return { root, nodes: budget.nodes, truncated: budget.truncated };
}
export { flatten } from './report';