I'm practicing reverse engineering C object files. Suppose I have an object file of the C program:
#include <stdio.h>
#include <string.h>
int main (int argc, char ** argv) {
char * input = argv[1];
int result = strcmp(input, "text_to_compare");
if (result == 0) {
printf("%s\n", "text matches");
}
else {
printf("%s\n", "text doeesn't match");
}
return 0;
}
How would I go about finding "text_to_compare" from the object file given it was compiled with a -g flag and an x86-64 architecture?
Running
stringson a binary file will all sequences of four or more printable characters in the file. For a simple file this might be sufficient, but for a larger file you can end up with a lot of false positives. For example, compiling your code withgccand runningstringson the resulting binary will return 295 results.We can start by using the
objdumpcommand to disassemble the code in your sample file:Looking at the disassembly, we can see a call to
strcmpat offset 40115d:If we look a couple of lines before that, we can see a instruction that is moving data from an address outside of this section (
0x402010):If we look at the output of
objdump -h a.out, we see that this address falls in the.rodatasection (we're looking for sections for which the given address is in the block of memory starting at the address in the VMA column):We can extract the data in that section using the
objcopycommand:And we can see that the string at address
0x402010istext_to_compare.