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 ?
View all comments
Show parent comment
1
u/anaccountbyanyname Oct 22 '24
You really want a core dump, which ghidra and gdb can then read just like normal ELF files.
I'm fairly certain you can run this as a regular user as long as debugging is enabled:
https://www.linux.org/docs/man1/gcore.html
You can also do it from inside gdb:
https://sourceware.org/gdb/current/onlinedocs/gdb.html/Core-File-Generation.html
If ptrace is disabled and you can't debug your own processes, but you can preload like you're doing, then you'll need to look at the gcore source and implement the same behavior locally into your preload library (just keep in mind that it *preloads*, so you'd need to spin off a thread that waits a second or two for the process to be mapped before dumping it.) Hopefully you don't have to resort to that because it would be a decent bit of work, but it's a last resort option that's possible
https://fossies.org/linux/gdb/gdb/gcore.c