sekant-intercept-js 1.0.0 → 1.0.2

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.
@@ -57,14 +57,18 @@ export function compileYaraLike(definition) {
57
57
  }
58
58
 
59
59
  function addOptionsToMatcher(matcher) {
60
- return (data, offset = -1, maxLength = MAX_STRING_MATCH_LENGTH) => {
61
- // If offset is specified, slice the data accordingly and limit length for matching
62
- const singleRun = offset >= 0;
63
- // Start 1 byte before to do fullword checks if needed
64
- offset = Math.max(0, offset - 1);
65
- data = singleRun ? data.slice(offset, offset + maxLength) : data;
66
- return matcher(data, singleRun ? offset : -1);
60
+ // Public contract: `offset < 0` scans the whole buffer, otherwise the search window starts at
61
+ // `offset` (one byte earlier, so fullword checks at the window start can see the preceding byte).
62
+ const scan = (data, offset = -1, maxLength = MAX_STRING_MATCH_LENGTH, exactIndex = -1) => {
63
+ if (offset < 0) return matcher(data, -1);
64
+ const start = Math.max(0, offset - 1);
65
+ return matcher(data.slice(start, start + maxLength), start, exactIndex < 0 ? -1 : exactIndex - start);
67
66
  };
67
+
68
+ // Aho-Corasick candidates ask "is there a match starting exactly here?", which a window scan
69
+ // cannot answer — its first match may be a neighbouring occurrence.
70
+ scan.verifyAt = (data, offset) => scan(data, offset, MAX_STRING_MATCH_LENGTH, offset);
71
+ return scan;
68
72
  }
69
73
 
70
74
  // ╭─────────────────────────────╮
@@ -80,14 +84,21 @@ function makeRegexMatcher(definition) {
80
84
  const literalPrefix = getRegexPrefix(pattern, nocase);
81
85
  const re = new RegExp(pattern, flags);
82
86
 
83
- const matcher = (data, offset = -1) => {
87
+ const matcher = (data, offset = -1, exactIndex = -1) => {
84
88
  const encoder = new TextEncoder();
85
89
  const text = new TextDecoder().decode(data);
86
90
  const results = [];
87
91
  const singleRun = offset >= 0;
92
+ const exact = exactIndex >= 0;
93
+ // `re` outlives a call: the matcher runs once per Aho-Corasick candidate plus once per full
94
+ // scan, and an early `break` (or the MAX_MATCHES cap) leaves `lastIndex` mid-text. Position it
95
+ // explicitly on entry instead of inheriting the previous run's position.
96
+ const runIndex = exact ? exactIndex : 0;
88
97
  offset = offset < 0 ? 0 : offset;
98
+ re.lastIndex = runIndex;
89
99
  let m;
90
100
  while ((m = re.exec(text)) !== null && results.length < MAX_MATCHES) {
101
+ if (exact && m.index !== runIndex) break; // prefix hit the regex rejects
91
102
  const byteIndex = encoder.encode(text.slice(0, m.index)).length;
92
103
  results.push({ offset: byteIndex + offset, length: encoder.encode(m[0]).length });
93
104
  re.lastIndex = m.index + 1; // Move forward to find overlapping matches
@@ -180,18 +191,19 @@ function makeTextMatcher(text, opts) {
180
191
  }
181
192
  }
182
193
 
183
- const matcher = (data, offset = -1) => {
194
+ const matcher = (data, offset = -1, exactIndex = -1) => {
184
195
  let matches = [];
185
196
  const singleRun = offset >= 0;
197
+ // `exactIndex` (relative to the slice) restricts a single run to the candidate's own byte.
198
+ const runIndex = exactIndex >= 0 ? exactIndex : -1;
186
199
  offset = offset < 0 ? 0 : offset;
187
200
 
188
201
  for (const { bytes, isWide } of patterns) {
189
202
  if (opts.xor) {
190
- const xorMatches = findXorMatches(data, bytes, opts.xor, opts.allowKey0, singleRun);
191
- // console.log(xorMatches);
203
+ const xorMatches = findXorMatches(data, bytes, opts.xor, opts.allowKey0, singleRun, runIndex);
192
204
  matches.push(...xorMatches.map((m) => ({ ...m, length: bytes.length, isWide })));
193
205
  } else {
194
- const plainMatches = findPlainMatches(data, bytes, opts.nocase, singleRun);
206
+ const plainMatches = findPlainMatches(data, bytes, opts.nocase, singleRun, runIndex);
195
207
  matches.push(...plainMatches.map((m) => ({ ...m, length: bytes.length, isWide })));
196
208
  }
197
209
  }
@@ -214,15 +226,16 @@ function makeTextMatcher(text, opts) {
214
226
  // ╭─────────────────────────────╮
215
227
  // │ XOR-AWARE MATCHING │
216
228
  // ╰─────────────────────────────╯
217
- function findXorMatches(data, pattern, keyRange, allowKey0 = true, singleRun = false) {
229
+ function findXorMatches(data, pattern, keyRange, allowKey0 = true, singleRun = false, exactIndex = -1) {
218
230
  const [minKey, maxKey] = keyRange;
219
231
  const matches = [];
220
232
  const pLen = pattern.length;
221
233
  const dLen = data.length;
234
+ const from = exactIndex >= 0 ? exactIndex : 0;
222
235
  if (pLen === 0 || pLen > dLen) return matches;
223
236
 
224
237
  for (let candidateKey = minKey; candidateKey <= maxKey; candidateKey++) {
225
- for (let i = 0; i <= dLen - pLen; i++) {
238
+ for (let i = from; i <= dLen - pLen; i++) {
226
239
  let ok = true;
227
240
  for (let j = 0; j < pLen; j++) {
228
241
  if ((data[i + j] ^ candidateKey) !== pattern[j]) {
@@ -231,7 +244,8 @@ function findXorMatches(data, pattern, keyRange, allowKey0 = true, singleRun = f
231
244
  }
232
245
  }
233
246
  if (ok) {
234
- matches.push({ offset: i, key: candidateKey });
247
+ // An exact run only accepts the requested offset; a later hit belongs to another candidate.
248
+ if (exactIndex < 0 || i === exactIndex) matches.push({ offset: i, key: candidateKey });
235
249
  if (singleRun) break;
236
250
  }
237
251
  }
@@ -242,15 +256,17 @@ function findXorMatches(data, pattern, keyRange, allowKey0 = true, singleRun = f
242
256
  // ╭─────────────────────────────╮
243
257
  // │ PLAIN MATCHING │
244
258
  // ╰─────────────────────────────╯
245
- function findPlainMatches(data, pattern, nocase = false, singleRun = false) {
259
+ function findPlainMatches(data, pattern, nocase = false, singleRun = false, exactIndex = -1) {
246
260
  const matches = [];
247
261
  const pLen = pattern.length;
248
262
  const dLen = data.length;
249
- outer: for (let i = 0; i <= dLen - pLen; i++) {
263
+ const from = exactIndex >= 0 ? exactIndex : 0;
264
+ outer: for (let i = from; i <= dLen - pLen; i++) {
250
265
  for (let j = 0; j < pLen; j++) {
251
266
  if ((nocase ? lowercaseByte(data[i + j]) : data[i + j]) !== pattern[j]) continue outer;
252
267
  }
253
- matches.push({ offset: i });
268
+ // An exact run only accepts the requested offset; a later hit belongs to another candidate.
269
+ if (exactIndex < 0 || i === exactIndex) matches.push({ offset: i });
254
270
  if (singleRun) break;
255
271
  }
256
272
  return matches;
@@ -365,6 +381,9 @@ function makeHexMatcher(hexBody, mods) {
365
381
 
366
382
  const matches = [];
367
383
  let m;
384
+ // Same shared-instance hazard as the regex matcher: `regex` lives as long as the matcher, so
385
+ // reset before each run (the MAX_MATCHES cap can leave `lastIndex` mid-string).
386
+ regex.lastIndex = 0;
368
387
  while ((m = regex.exec(hexString)) !== null && matches.length < MAX_MATCHES) {
369
388
  // Convert hex string offset to byte offset
370
389
  if (m.index % 2 !== 0) {
@@ -377,7 +396,7 @@ function makeHexMatcher(hexBody, mods) {
377
396
  // // Skip non-aligned length matches
378
397
  // continue;
379
398
  // }
380
- matches.push({ offset: m.index / 2 });
399
+ matches.push({ offset: m.index / 2, length: Math.ceil(m[0].length / 2) });
381
400
  regex.lastIndex = m.index + 2; // Move forward to find overlapping matches
382
401
  }
383
402
  return matches;