KittyMemory icon indicating copy to clipboard operation
KittyMemory copied to clipboard

Crash when searching outside the library range with KittyScanner

Open knms360 opened this issue 1 year ago • 15 comments

In the sample, the search range is specified using ELF.baseSegment().startAddress and ELF.baseSegment().endAddress. However, if I use an arbitrary search range, it crashes. It is most likely that the program is crashing because it is reading an unreadable range. There are no errors when compiling. can get the error with Logcat Error: Fatal signal 11 (SIGSEGV), code 2, fault addr 0xf15b6000 in tid 3825 (android.support)

knms360 avatar Apr 14 '24 17:04 knms360

My Code KittyScanner::ElfScanner g_il2cppELF; g_il2cppELF = KittyScanner::ElfScanner::createWithPath("libMyLibName.so"); uintptr_t search_start = g_il2cppELF.baseSegment().startAddress; uintptr_t search_end = g_il2cppELF.baseSegment().endAddress + 0xFFFF; std::vector<uintptr_t> found_at_list; found_at_list = KittyScanner::findHexAll(search_start, search_end, "01 01 01 00 01 00 00 00 01 00 00 00 01 00 00 00 01 01 00 00", "xxxxxxxxxxxxxxxxxxxx");

knms360 avatar Apr 16 '24 14:04 knms360

uintptr_t search_end = g_il2cppELF.baseSegment().endAddress + 0xFFFF; why the + 0xFFFF ?

MJx0 avatar Apr 19 '24 02:04 MJx0

uintptr_t search_end = g_il2cppELF.baseSegment().endAddress + 0xFFFF; why the + 0xFFFF ?

This is to make it clear that it is outside the range. uintptr_t search_start = 0x0000; uintptr_t search_end = 0xFFFF; didn't work either. I get the same error.

knms360 avatar Apr 19 '24 09:04 knms360

there is no memory permissions checks inside scanner functions. you have to check and provide valid readable memory range by yourself. if you want to scan a full library then use the segments array instead of only the base segment, then check which segment is readable

MJx0 avatar Apr 21 '24 03:04 MJx0

Is it possible to read the segment from 0x00 to 0xFFFFFFFF and check if it is readable?

knms360 avatar Apr 21 '24 06:04 knms360

Why would you use hardcoded memory range? you can call getAllMaps() function to get all process memory maps then filter them

MJx0 avatar Apr 22 '24 02:04 MJx0

Because there is no library that can be used to look up byte arrays. (It's difficult to explain in English, so please refer to the image) gamegu1 gamegu2

knms360 avatar Apr 22 '24 11:04 knms360

use termux and print process maps

cat /proc/<pid>/maps

it could be malloc memory or bss.

MJx0 avatar May 05 '24 14:05 MJx0

Ah, um... it seems very difficult, but I'll try it.

knms360 avatar May 06 '24 17:05 knms360

That's right, it was in the range of anon:libc_malloc

knms360 avatar May 09 '24 17:05 knms360

You can get malloc memory path with this, but on older android versions it might be empty

std::string mallocPathname()
{
    void *n = malloc(sizeof(void*));

    if (auto fMaps = fopen("/proc/self/maps", "r"))
    {
        char cLine[512] = { 0 };
        while (fgets(cLine, sizeof(cLine), fMaps) != nullptr)
        {
            unsigned long long start = 0, end = 0;
            char pathanme[0xff] = { 0 };
            sscanf(cLine, "%llx-%llx %*s %*s %*s %*s %s", &start, &end, pathanme);
            if (uintptr_t(n) >= start && uintptr_t(n) < end)
               {
                   fclose(fMaps);
                   return pathanme;
                }
        }
        fclose(fMaps);
    }

    free(n);

    return "";
}

You can scan like this after

auto mallocPath = mallocPathname();
if (!mallocPath.empty())
{
  auto maps = KittyMemory::getMapsEqual(mallocPath);
  for (const auto &it : maps)
  {
     // filter out 
     if (it.offset != 0 && it.perms.compare("rw-p")) continue;

    uintptr_t found_at = KittyScanner::findIdaPatternFirst(it.startAddress, it.endAddress, "33 ? 55 66 ? 77 88 ? 99");
    KITTY_LOGI("found IDA pattern at: %p", (void *)found_at);
  }
}

MJx0 avatar May 12 '24 12:05 MJx0

Thanks!! I will give it a try. Thank you so much.

knms360 avatar Jun 01 '24 04:06 knms360

Hey, An error occurs in it.perms.compare No member named 'perms' in 'KittyMemory::ProcMap'

knms360 avatar Jun 07 '24 13:06 knms360

But... I fixed the code and it worked. Was this ok? if (it.offset != 0) continue; oauhfiae

knms360 avatar Jun 07 '24 16:06 knms360

Hey

knms360 avatar Jul 06 '24 15:07 knms360

offset == 0 is just a check to filter out unnecessary maps to speed up the scan but you don't really need it. for perms you need to check if map has read permission or it will crash upon access. if (!it.readable) continue;

MJx0 avatar Jul 14 '24 07:07 MJx0

OK, Thanks! I'll try later. maybe this issue will be over.

knms360 avatar Jul 15 '24 14:07 knms360

It has been confirmed that it does not work on some devices. Android versions are 7 and 11.

knms360 avatar Sep 07 '24 11:09 knms360