r/AskReverseEngineering • • Oct 22 '24

Reverse engineering a dumped C program

I have a container a C program that is read protected. I need to modify that program a bit, to patch a certain behaviour that I want to change.
It's read/write protected, but I can still execute it, and inject my own code with LD_PRELOAD to simply read most sections from /proc/self/maps. I then tried to reverse it in ghidra. Here is an exemple of what I have:

For a simple C program:
Source:

#include <stdio.h>

int main()
{
    printf("test\n");
    FILE *f = fopen("./output", "w+");
    fwrite("test", 4, 1, f);
    fclose(f);
}

Compiled and dumped using the method above gives me this in ghidra:

undefined8 FUN_001011a9(void)
{
  undefined8 uVar1;
  FUN_00101080(&DAT_00102004);
  uVar1 = FUN_001010a0("./output",&DAT_00102009);
  FUN_001010b0(&DAT_00102004,4,1,uVar1);
  FUN_00101090(uVar1);
  return 0;
}

So I clearly have something, all the function calls/static strings match. Execpt when following a call (here to printf for exemple) ghidra only shows me this:

void FUN_00101080(void)
{
                    /* WARNING: Treating indirect jump as call */
  (*(code *)0x1030)();
  return;
}

From my understanding, that's a call from to a dynamically loaded library (libc). My question is: Is there a way for me to have ghidra automatically resolve thoses calls to libraries ? Do I need to rearrange some sections that I grabbed from the dump ?

5 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Oct 25 '24

Sadly no ptrace allowed either...
I don't really get how a core dump would help me here. Correct me if I'm wrong, because core dumps are something I've never really used, but isn't it just a fancy copy of the program's memory into specific NOTE sections ?
If it is, then I'm already mostly doing that: I read the whole /proc/self/map file, and for each regions I just memcpy(buffer, region_start, region_end - region_start) and write the buffer into my dump.

I made some tests, and from inside the program I can prctl(PR_SET_DUMPABLE, 1) and then raise(SIGSEGV), which generated a core dump. I looked into from ghidra, and it contains 3 functions, that I don't really know what they mean. I don't know what steps I have to take to use the core dump to help me reverse my original binary.

Very sorry if anything is unclear, don't hesitate to ask questions if it is.

1

u/anaccountbyanyname Oct 25 '24

A core dump is a correctly formatted ELF file. It has all the information in it that gdb needs to present you with an environment as though you just attached to the running process and suspended it. That may or may not be best suited to what you're doing, but it's usually the most complete view you can get of a running process you can't attach to live