linux的逆向基础

1.ELF格式
我们先来看看 ELF 文件头,如果想详细了解,可以查看ELF的man page文档。

关于ELF更详细的说明:e_shoff:节头表的文件偏移量(字节)。如果文件没有节头表,则此成员值为零。sh_offset:表示了该section(节)离开文件头部位置的距离

2.可执行头部(Executable Header)
ELF文件的第一部分是可执行文件头部(Executable Header),其中包含有关ELF文件类型的信息。ELF文件在各种平台下都通用,ELF文件有32位版本和64位版本,其文件头内容是一样的,只不过有些成员的大小不一样。它的文件图也有两种版本:分别叫“Elf32_Ehdr”和“Elf64_Ehdr”。这里以32位版本为例:

使用readelf对ELF文件格式进行分析

我们可以使用以下计算方法来计算整个二进制文件的大小:
size = e_shoff + (e_shnum * e_shentsize)size = Start of section headers + (Number of section headers * Size of section headers)size = 137000 + (29*64) = 138856计算结果验证:

3、程序头部(Program Headers)
程序头部是描述文件中的各种segments(段),用来告诉系统如何创建进程映像的。

4、节表头部(Section Headers)
节表头部(Section Headers)包含了描述文件节区的信息,比如大小、偏移等,但这些对二进制文件的执行流程来说并不重要。
- sections 或者 segments:segments是从运行的角度来描述elf文件,sections是从链接的角度来描述elf文件,也就是说,在链接阶段,我们可以忽略program header table来处理此文件,在运行阶段可以忽略section header table来处理此程序(所以很多加固手段删除了section header table)。从图中我们也可以看出, segments与sections是包含的关系,一个segment包含若干个section。


- 5、表(Section)
- 5.1 .bss Section
- 保存未初始化的数据,比如那些未初始化的全局变量。
- 5.2 .data Section
- 保存已初始化的数据。
- 5.3 .rodata Section
- 保存程序中的只读数据。
- 5.4 .text Section
- 本节包含程序的实际代码,逻辑流程。使用readelf查看ELF文件表结构


- 6、完成简单的CTF挑战
- 既然已经对ELF文件有所了解了,那找一个CTF题目来试试吧。
- 二进制文件下载地址:https://ufile.io/blvpm
- 国内下载:www.lanzous.com/i34qg6f
- 1、运行这个程序,并传递一些随机字符给它,得到的结果如下:

- 2、接着使用strings 查看一下程序的字符串,看是否能找到有用的信息

- 我们可以看到 “%c” 是打印flag的字符串,数量是15个。
- 3、我们可以查看“.rodata ”部分的偏移量,可以更好的查看这些字符

- 4、检查符号表(Symbols)nm命令查看库文件的符号

- 说明:-D或–dynamic:显示动态符号。该任选项仅对于动态目标(例如特定类型的共享库)有意义我们可以发现 printf, puts, sprintf, strlen functions.这些函数未定义。5、跟踪系统调用(System Calls)我们可以使用strace之类的工具去跟踪程序的系统调用


- 为了更好地理解,我们可以使用ltrace解码C++来跟踪函数名所做的库调用。我们可以看到正在进行字符串长度检查。

- 为了更好地理解,我们可以使用ltrace解码C++来跟踪函数名所做的库调用。我们可以看到正在进行字符串长度检查。









- 在这个二进制文件中,符号没有被剥离,因此我们可以看到函数名称,这使得它更容易理解。如果你可以阅读汇编代码,你可以很清楚的知道发生了什么。如果不能阅读汇编代码,让我们做一些实时调试,并尝试更好地理解。7、实时调试这里我们使用GDB-Peda进行实时调试我们首先检查二进制文件中的函数。我们可以看到main,comp_key等函数

- 调试方法:首先使用 break main 跳到主函数,使用n来step和ni来执行每条指令


- 让我们来看看程序的逻辑,程序首先尝试比较参数的数量。它存储在ecx寄存器中并移动到esi,它用于将值与0×2进行比较

- 其伪代码看起来是这样的:


- 其代码是这样的:

- 如果你检查这个代码,可以看到有一个循环正在迭代我们输入字符串的每个字符。


- 它到底循环了多少个字符?通常来说,我们的密码长度为7个字符。


- 代码看起来是这样的:

- 可以看出,如果7个字符总和等于801,即可得到flag。您可以使用任何字符,只要总和是801即可。检查完成后,调用comp_key函数并打印出flag。比如这样:114 * 6 + 177 = 801我们找到数字对应的ASCII字符114是 ‘r’ 117 是 ‘u’。

- 然后我们将字符作为输入,执行程序即可得到FLAG

相关推荐
-
第18问:MySQL CPU 高了,怎么办?2025-02-24 10:27:18
-
mysql索引类型 normal, unique, full text
mysql索引类型 normal, unique, full text2025-02-24 10:05:05 -
uwsgi+django+nginx 搭建部分总结2025-02-24 10:03:33
-
使用Docker配置Nginx环境部署Nextcloud2025-02-24 10:02:03
-
Nginx安装和怎么使用2025-02-24 10:00:45