- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个普通的 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
$
我们需要找出main
和function
的地址,使用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
$
关于c - 如何破解 elf 文件以调用其他 function() 而不是 main?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18633880/
因此,当使用“智能”时,当我想创建一个类时,它还会创建另外两个函数(不确定我是否正确调用了它们): class main { private: /* data */ public: m
我确实知道 C/C++ 和 Java 中使用的 main() 方法,但由于 main() 是用户定义的(因为 main() 中的代码是由用户定义的,它不能是预定义的方法) & 在 C/C++ 中,ma
这个问题在这里已经有了答案: What is a NullPointerException, and how do I fix it? (12 个答案) 关闭 7 年前。 我意识到这是一个常见错误,
您好,我是 jquery 和 javascript 的新手。我想做的是我有 3 个独立的 Main-Divs ex main-div1, main-div2, main-div-3 它们都是一个大盒子
我知道以前曾有人问过有关此错误的问题,但我的情况与其他人不同。我正在编写计算数据集的平均值、方差和标准差的代码。编译代码时我没有收到任何错误,但是当我尝试运行代码时,我收到如下错误: Exceptio
这个问题已经有答案了: What should main() return in C and C++? (19 个回答) Why is the type of the main function in
无效的输入流不起作用 - 每次我给出负的月份值时,它都会返回此异常。 代码: import java.util.Scanner; public class Main { public stat
我在 main() 中调用 main(),递归 10 次。现在,在使用 gdb (bt/backtrace) 进行调试时,我没有看到 main() 的多个帧。为什么? #include int mai
我有一个类 - A - 没有方法,只有主要方法。 在其他类(class) - B - 我需要调用那个 main.做什么最好?从使用的资源、时间和功耗以及效率来看? 从类 A 创建一个“a”对象并执行
鉴于 documentation以及对 earlier question 的评论,根据要求,我制作了一个最小的可重现示例,演示了这两个语句之间的区别: my %*SUB-MAIN-OPTS = :na
我有一个在 main 中声明并初始化的数组,名为 Edges。 我还在 main 中声明了一些访问名为 Edges 的数组的函数。 代码编译并运行。 为什么它有效?我认为 main 中声明的变量不是全
如果定义内容主要部分的最具语义性和可访问性的方式是标准,那么使用 ARIA 地标似乎是多余的元素。正在添加 role="main"到元素真的有必要吗? 最佳答案 并非所有现代浏览器都已经映射了 ari
我是 C 语言的新手(6 小时前开始),我知道有大量的在线引用资料,我应该(并且将会)详细查看,但现在,我有紧急情况需要帮助。我有一个包含以下文件的项目文件夹: boundary_val.c boun
我正在审查许多不同的 Java 程序,并试图弄清楚如何只更新一次而不是两次更新对程序名称的引用。有没有办法在单个终端命令中使用变量? :S 我试图改进的命令是这样的形式: javac Main.jav
我已经创建了一个工作线程, Thread thread= new Thread(runnable); thread.start(); 我在工作线程中打印这个; Log.d("SessionTh
import java.awt.*; import java.awt.event.*; import java.io.*; import java.lang.*; public class Main
这是我的 Main.java,它位于服务器套接字“get().logger().tag();”之后的部分我已经在实例中添加了所有这些,我真的不确定它出了什么问题。 public class Main
我在 http://www.hackerearth.com/problem/algorithm/roys-life-cycle/ 上测试了我的程序。但是,我总是收到错误:在类 ActivityTime
我想要一个脚本来运行从模块导出的子例程,导出的子程序在脚本中作为 MAIN 运行。该子例程做了我想做的所有事情,除了它返回结果而不是打印它。 RUN-MAIN 似乎实现了我的大部分目标,但我不确定如何
java中有什么具体原因吗,main方法应该是小写字母 是的“主要”和“主要” 编译完成 public class ManiMethod { public static void main(S
我是一名优秀的程序员,十分优秀!