Skip to the content ForoHackers
Log inJoin

Finding main() in a stripped ELF without symbols

AssemblyBinary analysisReverse engineering
#945 · hexdump_hermit · · Report

No symbols, no main. But main is not magic: the entry point is _start, and _start ends by handing the address of main to __libc_start_main. So read the entry point and look for the argument.

x86-64, glibc, PIE binary, objdump of the entry:

1060: f3 0f 1e fa        endbr64
1064: 31 ed              xor    ebp,ebp
1066: 49 89 d1           mov    r9,rdx
1069: 5e                 pop    rsi
106a: 48 89 e2           mov    rdx,rsp
106d: 48 83 e4 f0        and    rsp,0xfffffffffffffff0
1071: 50                 push   rax
1072: 54                 push   rsp
1073: 45 31 c0           xor    r8d,r8d
1076: 31 c9              xor    ecx,ecx
1078: 48 8d 3d ca 00 00 00   lea rdi,[rip+0xca]
107f: ff 15 53 2f 00 00      call QWORD PTR [rip+0x2f53]
1085: f4                 hlt

First argument goes in rdi. The lea rdi,[rip+0xca] is 1078 + 7 + 0xca = 0x1149. That is main.

How to read the entry point: readelf -h file and look at Entry point address. Non-PIE binaries use mov rdi, imm32 (48 c7 c7 ..) instead of lea.

#1222 · r3l1c_0f_bbs · · Report

This brings me back to poking around with DEBUG.COM, hunting for the real start of a program after the startup junk. Same game, bigger registers.

#1234 · null_terminated_hopes · · Report

Worth adding that on newer glibc versions main may be reached through __libc_start_main still, but the init sequence moved around, so do not hardcode the offsets above. The shape (last lea/mov into rdi before the call) has survived, which is almost a miracle in our line of work.

#1235 · hexdump_hermit · · Report

Agreed. Shape, not offsets.

Log in or join to reply.