-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex_search.py
More file actions
50 lines (47 loc) · 1.26 KB
/
Copy pathhex_search.py
File metadata and controls
50 lines (47 loc) · 1.26 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
def find_all(data, pattern):
results = []
start = 0
plen = len(pattern)
if plen == 0:
return results
while True:
idx = data.find(pattern, start)
if idx == -1:
break
results.append(idx)
start = idx + 1
return results
def extract_strings(data, min_len=4):
results = []
buf = []
start = -1
for i, b in enumerate(data):
if 0x20 <= b <= 0x7E:
if start == -1:
start = i
buf.append(chr(b))
else:
if len(buf) >= min_len:
results.append((start, ''.join(buf)))
buf = []
start = -1
if len(buf) >= min_len:
results.append((start, ''.join(buf)))
return results
def parse_search_input(text):
text = text.strip()
if not text:
return b""
parts = text.split()
if all(len(p) == 2 and all(c in '0123456789abcdefABCDEF' for c in p) for p in parts):
try:
return bytes(int(p, 16) for p in parts)
except:
pass
return text.encode('latin-1', errors='replace')
def build_search_set(results, pattern_len):
flat = set()
for r in results:
for i in range(pattern_len):
flat.add(r + i)
return flat