Skip to content

Commit f036a0e

Browse files
committed
Dump strings on the stack in logs
These strings help to identify the detection point explained in JingMatrix/NeoZygisk#42.
1 parent eb5e3ff commit f036a0e

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

app/src/main/cpp/include/vmap.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ struct MapInfo {
3434
};
3535

3636
MapInfo *DetectInjection();
37+
38+
void DumpStackStrings();
3739
} // namespace VirtualMap

app/src/main/cpp/native-lib.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Java_org_matrix_demo_MainActivity_stringFromJNI(JNIEnv *env,
1616
SoList::SoInfo *abnormal_soinfo = SoList::DetectInjection();
1717
VirtualMap::MapInfo *abnormal_vmap = VirtualMap::DetectInjection();
1818
size_t module_injected = SoList::DetectModules();
19+
VirtualMap::DumpStackStrings();
1920

2021
if (abnormal_soinfo != nullptr) {
2122
solist_detection =
@@ -32,8 +33,8 @@ Java_org_matrix_demo_MainActivity_stringFromJNI(JNIEnv *env,
3233
}
3334

3435
if (module_injected > 0) {
35-
counter_detection =
36-
std::format("Module counter: {} shared libraries unloaded", module_injected);
36+
counter_detection = std::format(
37+
"Module counter: {} shared libraries unloaded", module_injected);
3738
}
3839

3940
return env->NewStringUTF(

app/src/main/cpp/vmap.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,58 @@
99

1010
namespace VirtualMap {
1111

12+
void logPossibleStrings(const char *start, size_t size,
13+
size_t min_string_length = 4) {
14+
const char *end = start + size;
15+
const char *ptr = start;
16+
17+
LOGD("--- Starting String Dump (min length: %zu, range size: %zu) ---",
18+
min_string_length, size);
19+
20+
while (ptr < end) {
21+
// Find the beginning of a potential string (a printable character)
22+
if (isprint(static_cast<unsigned char>(*ptr))) {
23+
const char *string_start = ptr;
24+
const char *string_end = ptr + 1;
25+
26+
// Find the end of the sequence of printable characters
27+
while (string_end < end &&
28+
isprint(static_cast<unsigned char>(*string_end))) {
29+
string_end++;
30+
}
31+
32+
size_t length = string_end - string_start;
33+
34+
// If the sequence meets our minimum length, log it
35+
if (length >= min_string_length) {
36+
// Safely create a std::string from the non-null-terminated segment
37+
std::string found_str(string_start, length);
38+
39+
// Log the string and its memory offset using LOGD's format string
40+
LOGI("Offset 0x%zx: \"%s\"", (size_t)(string_start - start),
41+
found_str.c_str());
42+
}
43+
44+
// Advance the main pointer past the sequence we just processed
45+
ptr = string_end;
46+
} else {
47+
// Not a printable character, just move to the next byte
48+
ptr++;
49+
}
50+
}
51+
LOGD("--- Finished String Dump ---");
52+
}
53+
54+
void DumpStackStrings() {
55+
for (auto &map : MapInfo::Scan()) {
56+
if (map.dev == 0 && map.inode == 0 && map.offset == 0 &&
57+
map.path == "[anon:stack_and_tls:main]") {
58+
logPossibleStrings(reinterpret_cast<const char *>(map.start),
59+
map.end - map.start, 3);
60+
}
61+
}
62+
}
63+
1264
MapInfo *DetectInjection() {
1365
int jit_cache_count = 0;
1466
int jit_zygote_cache_count = 0;

0 commit comments

Comments
 (0)