# S05_Return_to_Libc.pptx：逐页可提取文字

> [原课件](../../../S05_Return_to_Libc.pptx)；共28页。以下是文字提取，**未经逐句纠错**；图中文字、截图代码、公式对象及动画关系可能无法完整提取。阅读主题笔记后，可按页码回原文件查看。

## 第 1 页

````text
Return-to-libc 攻击
````

## 第 2 页

````text
内容
栈不可执行对策
如何绕过对策
攻击的任务
函数序言和尾声
发起攻击
````

## 第 3 页

````text
不可执行栈

在 C 程序中运行 shellcode

调用 shellcode
````

### 备注区文字

````text
Consider the above code that places a shellcode in a buffer on the stack, casts the buffer as a function and calls the function.
````

## 第 4 页

````text
使用可执行堆栈


使用不可执行堆栈

不可执行栈
````

### 备注区文字

````text
title
````

## 第 5 页

````text
如何对抗NX

跳转到现有代码：例如 libc 库

Function: system(cmd): cmd是一个被执行的命令.
````

## 第 6 页

````text
环境设置

缓冲区溢出问题

此代码在 vul_func（） 中存在潜在的缓冲区溢出问题
````

## 第 7 页

````text
环境设置

打开“不可执行栈”对策，关闭 StackGuard 保护并关闭地址随机化。


root拥有的 Set-UID 程序。
````

## 第 8 页

````text
攻击概述

任务 A ：查找 system（） 的地址。
用 system（） 的地址覆盖返回地址。
任务 B：查找“/bin/sh”字符串的地址。
从 system（） 运行命令“/bin/sh”
任务 C：构造 system（） 的参数
在堆栈中查找放置“/bin/sh”地址的位置（system（）的参数）
````

## 第 9 页

````text
任务 A ：查找 system（） 的地址。
使用 gdb 调试带漏洞的程序
使用 p （print） 命令，打印 system（） 和 exit（） 的地址。
````

### 备注区文字

````text
Libc library is loaded in the memory when a program runs. For the same program, the library is loaded in the same location. Hence, we can use gdb to find the location of the library functions.
````

## 第 10 页

````text
任务 B：查找“/bin/sh”字符串地址
MYSHELL 作为环境变量传递给带漏洞的程序，该变量存储在堆栈上。

导出一个名为“MYSHELL”的环境变量，值为“/bin/sh”。

我们可以找到它的地址。
````

### 备注区文字

````text
The exported environment variables in the shell process is always passed to the child process.Hence, when a new environment variable is exported and vulnerable program is run, the environment variable is passed to the vulenarbale code and hence, in it’s memory.
````

## 第 11 页

````text
任务 B：查找“/bin/sh”字符串地址
显示环境变量地址的代码

导出“MYSHELL”环境变量并执行代码。
````

## 第 12 页

````text
任务 B：一些注意事项

“MYSHELL”环境变量的地址对程序名称的长度很敏感。
如果程序名称从 env55 更改为 env7777，我们将获得不同的地址。
````

### 备注区文字

````text
Environment variables are stored in the stack region of the process, but before the environment variables are pushed, the name of the program is pushed first which affects the memory locations of the environment variables.  It took us quite a while to figure out why simply changing the file name can make the attack fail.
````

## 第 13 页

````text
任务 C：system（） 的参数

访问与 ebp 相关的参数
system（） 的参数需要在堆栈上
system（） 函数的栈帧

在我们“返回”到 system（） 之后，需要知道 ebp 的确切位置，这样我们就可以将参数放在 ebp + 8 上。
````

## 第 14 页

````text
任务 C：system（） 的参数
函数序言

esp : Stack pointer
ebp : Frame Pointer
````

### 备注区文字

````text
The start of the function call contains 3 instructions as mentioned.
When a function is called, return address (RA) is pushed into the stack. This is the beginning of the function before function prologue gets executed. The stack pointer (esp register) points at RA location.
The previous frame pointer is pushed in the stack, so when the function returns, the caller’s frame pointer is recovered.
The stack pointer now points to the previous frame pointer. The frame pointer (ebp) is pointed to the current stack pointer now so that the frame pointer always points to the old frame pointer.
The stack pointer now moves by N bytes to leave space for the local variables of the function.
````

## 第 15 页

````text
任务 C：system（） 的参数

函数后记

esp : Stack pointer
ebp : Frame Pointer
````

### 备注区文字

````text
The stack pointer now points where the frame pointer points to in order to release the stack space allocated for the local variables.
The previous frame pointer is assigned to %ebp to recover the frame pointer of the caller’s function.
The return address is popped from the stack and the program jumps to that address. This instruction moves the stack pointer.
````

## 第 16 页

````text
函数序言和后记示例

1
2
1
2
函数序言

函数后记
8(%ebp) ⇒ %ebp + 8
````

### 备注区文字

````text
The code can be compiled into assembly code using gcc -s.
````

## 第 17 页

````text
如何找到 system() 的参数地址？

Modified Return Address
vul_func() epilogue
system() prologue
Use of system()’s argument
为了找到 system（） 参数，我们需要了解 ebp 和 esp 寄存器如何随着函数调用而变化
在修改返回地址和使用系统参数之间，vul_func () 返回和 system () 序言开始

              Change ebp and esp
````

## 第 18 页

````text
用于理解 system() 参数的内存映射

3被看作ebp, 2被视为 system 的返回地址。1被视为 system的参数
我们可以在2放置 exit 地址，以便在 system返回时调用 exit. 使程序不会崩溃
````

## 第 19 页

````text
Return address is changed to system() address.
ebp is replaced by esp after vul_func() epilogue
Jump to system()
system() prologue is executed
ebp is set to current value of esp
“/bin/sh” is stored in ebp+8
Check the memory map
了解
system() 参数流程图
````

## 第 20 页

````text
计算距离
````

## 第 21 页

````text
恶意代码

ebp + 4
ebp + 8
ebp + 12
````

### 备注区文字

````text
We are inside the vulnerable function (before return occurs), so ebp currently points to the stack frame of the vulnerable function.
````

## 第 22 页

````text
发起攻击

执行漏洞利用代码，然后执行易受攻击的代码
````

## 第 23 页

````text
Return-Oriented Programming
在返回 libc 攻击中，我们只能将两个函数链接在一起
该技术可以概括：
将许多功能链接在一起
将代码块链接在一起
这种广义技术称为Return-Oriented Programming （ROP）
````

## 第 24 页

````text
链接函数调用（不带参数）
````

## 第 25 页

````text
使用参数链接函数调用

想法：
跳过函数序言
````

## 第 26 页

````text
使用参数链接函数调用

想法：使用 leave 和 ret
````

## 第 27 页

````text
链接参数中为零的函数调用

想法：使用函数调用将堆栈上的参数动态更改为零

函数调用顺序（T 是零的地址）：在调用 setuid 函数之前，使用 4 个 sprint（） 将 setuid（） 的参数更改为零。

在调用 system（“/bin/sh”） 之前调用 setuid（0） 可以击败 shell 程序实现的权限删除对策。
````

## 第 28 页

````text
总结

可以绕过不可执行堆栈机制
为了进行攻击，我们需要了解有关函数调用的低级细节
该技术可以进一步推广到面向回报的编程 （ROP）
````
