I am new to systems programming. I was just trying to implement the ret2libc attack on my own. To implement that, I need the address of the start of the libc function "system" in the executable.
I tried to do static linking, but to my surprise, the "system" function is not getting linked in my executable.
Below is a simple program saved as t.c:
#include <stdio.h>
#include <stdlib.h>
int fun()
{
int x;
}int main()
{
fun();
int x;
x=10;
printf("%d\n",x);
return 0;
}
Below is the output which I get when using GDB:
abhishek@abhishek:~$ gcc -g -static t.c -o t
abhishek@abhishek:~$ gdb -q t
Reading symbols from t...
(gdb) print system
No symbol "system" in current context.
(gdb) print memcpy
$1 = {<text gnu-indirect-function variable, no debug info>} 0x422b40 <memcpy>
(gdb) print strcmp
$2 = {<text gnu-indirect-function variable, no debug info>} 0x421b50 <strcmp_ifunc>
(gdb) print printf
$3 = {<text variable, no debug info>} 0x40b5b0 <printf>
(gdb) quit
abhishek@abhishek:~$
libc functions like memcpy, strcmp and even printf is getting linked. But not the function system. The tutorials out on the internet just asks to get the address of system and then proceed accordingly. But I am unable to get the address of system in the first place.
Could anyone help me why the function system is not linked even when I am using the -static flag in GCC?
system()function. Only functions (or actually the compliation unit the function is part of) gets linked in. printf and the libc runtime itself may call other functions such as memcpy/strcmp so those get linked in.systemto your code, so it gets linked into your executable, likevoid foo(void) {system("bar");}