I have a piece of native code with C in my Android project for detecting emulators. It works perfectly for detecting all Emulators but some real devices are detected as vulnerable too. How can I reduce these false positives?
//Structure to hold the details of the executable section of library
typedef struct stExecSection {
int execSectionCount;
unsigned long offset[2];
unsigned long memsize[2];
unsigned long checksum[2];
unsigned long startAddrinMem;
} execSection;
#define LIBC "libc.so"
static execSection *elfSectionArr[NUM_LIBS] = {NULL};
static const char *libstocheck[NUM_LIBS] = {"libnative-lib.so", LIBC};
__attribute__((always_inline))
static inline void detect_frida_memdiskcompare() {
int fd = 0;
char map[MAX_LINE];
if ((fd = my_openat(AT_FDCWD, PROC_MAPS, O_RDONLY | O_CLOEXEC, 0)) != 0) {
while ((read_one_line(fd, map, MAX_LINE)) > 0) {
for (int i = 0; i < NUM_LIBS; i++) {
if (my_strstr(map, libstocheck[i]) != NULL) {
if (true == scan_executable_segments(map, elfSectionArr[i], libstocheck[i])) {
break;
}
}
}
}
} else {
__android_log_print(ANDROID_LOG_WARN, APPNAME,
"Error opening /proc/self/maps. That's usually a bad sign.");
}
my_close(fd);
}
__attribute__((always_inline))
static inline bool
scan_executable_segments(char *map, execSection *pElfSectArr, const char *libraryName) {
unsigned long start, end;
char buf[MAX_LINE] = "";
char path[MAX_LENGTH] = "";
char tmp[100] = "";
sscanf(map, "%lx-%lx %s %s %s %s %s", &start, &end, buf, tmp, tmp, tmp, path);
//__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "Map [%s]", map);
if (buf[2] == 'x') {
if (buf[0] == 'r') {
uint8_t *buffer = NULL;
buffer = (uint8_t *) start;
for (int i = 0; i < pElfSectArr->execSectionCount; i++) {
if (start + pElfSectArr->offset[i] + pElfSectArr->memsize[i] > end) {
if (pElfSectArr->startAddrinMem != 0) {
buffer = (uint8_t *) pElfSectArr->startAddrinMem;
pElfSectArr->startAddrinMem = 0;
break;
}
}
}
for (int i = 0; i < pElfSectArr->execSectionCount; i++) {
unsigned long output = checksum(buffer + pElfSectArr->offset[i],
pElfSectArr->memsize[i]);
// __android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "Checksum:[%ld][%ld]", output,
// pElfSectArr->checksum[i]);
if (output != pElfSectArr->checksum[i]) {
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME,
"Executable Section Manipulated, "
"maybe due to Frida or other hooking framework."
"Act Now!!!");
exit(0);
}
}
} else {
char ch[10] = "", ch1[10] = "";
__system_property_get("ro.build.version.release", ch);
__system_property_get("ro.system.build.version.release", ch1);
int version = my_atoi(ch);
int version1 = my_atoi(ch1);
if (version < 10 || version1 < 10) {
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "Suspicious to get XOM in "
"version < Android10");
} else {
if (0 == my_strncmp(libraryName, LIBC, my_strlen(LIBC))) {
//If it is not readable, then most likely it is not manipulated by Frida
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "LIBC Executable Section"
" not readable! ");
} else {
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "Suspicious to get XOM "
"for non-system library on "
"Android 10 and above");
}
}
}
return true;
} else {
if (buf[0] == 'r') {
pElfSectArr->startAddrinMem = start;
}
}
return false;
}
The above code is designed to scan executable segments of memory maps, likely for security purposes such as detecting tampering or unauthorized modifications. and validate the integrity of these segments using checksums. After running this code on Samsung SM-A137F with Android 14.0, the libc. so checksum changed, so the code detects the device vulnerable like any other emulator. Also, we have users with Samsung SM-A137F devices, without problem.
after connecting Samsung SM-A137F ( detected vulnerable device ) to Android Studio, we recognized this permission drwxrwx— for path: /storage/emulated/ and we could read all files inside this directory. Is this default permission? Does it indicate the device is vulnerable?
default output of ls command for /storage/emulated is Permission denied for my device and others.