c - 如何破解 elf 文件以调用其他 function() 而不是 main?

标签 c

我有一个普通的 C 程序。我已经制作了它的可执行文件。如果我读取一个 elf 文件,它会告诉我入口点是 Entry point address: 0x80482e0。跟踪入口点后,我看到最终调用如下。

080482b0 <__gmon_start__@plt-0x10>:
 80482b0:   ff 35 50 96 04 08       pushl  0x8049650
 80482b6:   ff 25 54 96 04 08       jmp    *0x8049654
 80482bc:   00 00                   add    %al,(%eax)

我如何破解 0x8049654 的值来调用其他函数而不是 main?我相信主要功能地址将存储在地址 - 0x8049654?我对么?我想做的是不是调用 main(),而是想破解它来调用其他函数?可能吗?

main函数地址应该包含在*0x8049654中吗?

最佳答案

main 不是从 __gmon_start__ 调用的:

(gdb) disassemble main 
Dump of assembler code for function main:
0x080483d8 <main+0>:    push   %ebp                       // main() address
0x080483d9 <main+1>:    mov    %esp,%ebp
0x080483db <main+3>:    and    $0xfffffff0,%esp
0x080483de <main+6>:    sub    $0x10,%esp
0x080483e1 <main+9>:    movl   $0x80484c9,(%esp)
0x080483e8 <main+16>:   call   0x80482f8 <puts@plt>
0x080483ed <main+21>:   mov    $0x0,%eax
0x080483f2 <main+26>:   leave  
0x080483f3 <main+27>:   ret    
End of assembler dump.
(gdb) disassemble __gmon_start__
Dump of assembler code for function __gmon_start__@plt:
0x080482d8 <__gmon_start__@plt+0>:  jmp    *0x80495c8
0x080482de <__gmon_start__@plt+6>:  push   $0x0
0x080482e3 <__gmon_start__@plt+11>: jmp    0x80482c8
End of assembler dump.
(gdb) # no call to main

它是从函数 _start 传递过来的:

(gdb) disassemble _start 
Dump of assembler code for function _start:
0x08048310 <_start+0>:  xor    %ebp,%ebp
0x08048312 <_start+2>:  pop    %esi
0x08048313 <_start+3>:  mov    %esp,%ecx
0x08048315 <_start+5>:  and    $0xfffffff0,%esp
0x08048318 <_start+8>:  push   %eax
0x08048319 <_start+9>:  push   %esp
0x0804831a <_start+10>: push   %edx
0x0804831b <_start+11>: push   $0x8048400
0x08048320 <_start+16>: push   $0x8048410
0x08048325 <_start+21>: push   %ecx
0x08048326 <_start+22>: push   %esi
0x08048327 <_start+23>: push   $0x80483d8
0x0804832c <_start+28>: call   0x80482e8 <__libc_start_main@plt>
0x08048331 <_start+33>: hlt    
0x08048332 <_start+34>: nop
...

读取ELF头,可以找到e_entry中存放的_start地址:

   e_entry     This member gives the virtual address to which the system
               first transfers control, thus starting the process.  If
               the file has no associated entry point, this member holds
               zero.

这里有一个获取地址的简单程序:

#include <stdio.h>
#include <elf.h>

int main(int argc, char **argv) {
  FILE *file;
  Elf32_Ehdr hdr;

  if( argc < 2 ) {
    printf("uage: %s [FILE]\n", argv[0]);
    return -1;
  }

  if( (file = fopen(argv[1], "r")) == NULL ) {
    perror("Error");
    return -1;
  }

  fread(&hdr, sizeof(Elf32_Ehdr), 1, file);
  fclose(file);

  if( (hdr.e_ident[EI_MAG0] != ELFMAG0) ||
    (hdr.e_ident[EI_MAG1] != ELFMAG1) ||
    (hdr.e_ident[EI_MAG2] != ELFMAG2) ||
    (hdr.e_ident[EI_MAG3] != ELFMAG3) ) {
    printf("Error: Error: Not a valid ELF file.\n");
    return -1;
  }

  printf("Entry: 0x%.8x\n", hdr.e_entry);

  return 0;
}

所以如果你想将 main 重定向到其他函数,你需要修补这部分:

0x08048327 <_start+23>: push   $0x80483d8

并将其替换为您的函数。这里我有一个简单的程序:

#include <stdio.h>

void function(void) {
  puts("Function");
}

int main(int argc, char **argv) {

  puts("Main");

  return 0;
}

将打印:

$ ./prog1
Main
$

我们需要找出mainfunction的地址,使用readelf:

$ readelf -s prog1

Symbol table '.dynsym' contains 5 entries:
...

Symbol table '.symtab' contains 66 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
...
    61: 080483c4    20 FUNC    GLOBAL DEFAULT   14 function
...
    64: 080483d8    28 FUNC    GLOBAL DEFAULT   14 main
...
$ 

现在修补 push $0x80483d8 并将 main = 080483d8 的地址替换为 function = 080483c4,我使用了十六进制编辑器,不要'忘记按尊崇顺序翻转字节。它将变成:

0x08048327 <_start+23>: push   $0x80483c4

现在测试一下:

$ ./prog1
Function
$ 

引用:How main() is executed on Linux


这是一种快速而肮脏的方式。如果您只想在调用 main 之前调用一些东西,您可以使用 GCC 属性 __attribute__((constructor)) 使 function 成为构造函数,如下所示:

#include <stdio.h>

__attribute__((constructor)) void function(void) {
  puts("Function");
}

int main(int argc, char **argv) {

  puts("Main");

  return 0;
}

现在它将在 main 之前被调用:

$ gcc -Wall prog.c -o prog
$ ./prog
Function
Main
$ 

引用:Declaring Attributes of Functions

关于c - 如何破解 elf 文件以调用其他 function() 而不是 main?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18633880/

相关文章:

CS50 Mario,代码可以编译,但不会执行任何操作

c - 如何将自己的输出通过管道传递给另一个进程?

c++ - 构建一个项目,其中文件待定参数

c - Linux 内核 : Current macro working

c - 如何在代码中抑制 C 中的 Solaris lint 警告

c - 结构声明中的指针与局部变量

CMake:无法打开头文件

c - 如何循环用户输入并将其复制到文件,直到使用 C 输入 "exit"字符串?

c - windows下绑定(bind)C语言

c - C 程序中的奇怪行为,分配内存后变量损坏