-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_memory.js
91 lines (76 loc) · 3.04 KB
/
scan_memory.js
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
// Scan memory for a specific pattern
function scanMemory(stringToSearch, protection = 'r--') {
// convert the string to a hex pattern
let pattern = stringToHexWithSpaces(stringToSearch);
// print what we are searching for
send(`[BEGIN] Scanning memory for {string: ${stringToSearch}, hex: ${pattern}}`);
let ranges = Process.enumerateRanges({ protection: protection, coalesce: false });
let totalRanges = ranges.length;
let foundCount = 0;
send(`[INFO] Located ${totalRanges} memory ranges matching protection: ${protection}`);
ranges.forEach(function (range) {
Memory.scan(range.base, range.size, pattern, {
onMatch: function (address, size) {
send(`[+] Pattern found at: ${address.toString()}`);
try {
// Read data before the found pattern
let preStringSize = 4096; // Number of bytes to read before the pattern
let preString = getASCIIString(address.sub(preStringSize), preStringSize);
send(`[PRECEDING ASCII] ${preString}`);
// Read the matched pattern + length
let asciiString = getASCIIString(address, 4096);
send(`[MATCH ASCII] ${asciiString}`);
} catch (error) {
send(`[!] Runtime error: ${error.message}`);
}
foundCount++;
},
onError: function (reason) {
send(`[!] Error scanning memory range: ${reason}`);
},
onComplete: function () {
// No action needed here
}
});
});
send(`[FINISH] Scanning complete. Found pattern ${foundCount} times.`);
}
// Convert the string to hex
function stringToHexWithSpaces(str) {
let hex = "";
for (let i = 0; i < str.length; i++) {
// Convert character to hex and pad with a 0 if needed
const hexChar = str.charCodeAt(i).toString(16).padStart(2, "0");
hex += hexChar + " ";
}
// Remove trailing space
return hex.trim();
}
// Get only the ASCII so we dont have to deal with the hex dump
function getASCIIString(buffPtr, buffSize) {
let asciiString = "";
for (let i = 0; i < buffSize; i++) {
try {
let byte = buffPtr.add(i).readU8();
if (byte >= 32 && byte <= 126) { // Check if the byte is a printable ASCII character
asciiString += String.fromCharCode(byte);
}
} catch (error) {
send(`[!] Error reading memory at offset ${i}: ${error.message}`);
break;
}
}
return asciiString;
}
// Continuous scan with sleep of every 1 sec
function startContinuousScan(pattern, protection = 'r--') {
setInterval(function () {
try {
scanMemory(pattern, protection);
} catch (error) {
send(`[!] Error during scanning: ${error.message}`);
}
}, 1000); // Scan every 1 second
}
// Scan for specified string
startContinuousScan("secretTunnel");