pwnable.kr之input


                                                                                       input

这道题有点麻烦。

一共有五个阶段,需要突破,参考链接里讲的很详细了,自己没有太多的理解。感觉基础差太多了,像进程间通信,Linux管道技术,网络 编程,都需要加强。

 

在这里记录几个自己的学到的姿势吧:

1   argv['A'] = "\x00";

     argv['B'] = "\x20\x0a\x0d";

     argv['C'] ="55555"; 

这里可以用’A’作为参数的索引,这是原来没有见过的,默认把字符转换成ASCII码了。


2  execve函数

execve(执行文件)在父进程中fork一个子进程,在子进程中调用exec函数启动新的程序。exec函数一共有六个,其中execve为内核级系统调用,其他(execl,execle,execlp,execv,execvp)都是调用execve的库函数。

int execve(const char * filename,char *const argv[ ],char * const envp[ ]);

execve()用来执行参数filename字符串所代表的文件路径

第二个参数是利用指针数组来传递给执行文件,并且需要以空指针(NULL)结束

最后一个参数则为传递给执行文件的新环境变量数组

 

3 进程间通信(以后再补上)

 

4 socket编程

一般的模式:

(1)建立套接字:sockfd = socket(AF_INET,SOCK_STREAM,0)

AF_INET:IPV4

SOCK_STREAM:TCP

最后一个参数一般为0

(2)设置socket_in结构中的参数(套接字地址),包括Ip、端口和IPV4or6

http://www.cnblogs.com/hnrainll/archive/2011/04/24/2026432.html

(3)bind监听函数

http://blog.chinaunix.net/uid-24954950-id-2956469.html

(4)listen函数

http://blog.chinaunix.net/uid-25749806-id-348681.html

(5)accpet函数

http://blog.chinaunix.net/uid-25749806-id-348689.html

(6)recv/send函数

http://www.cnblogs.com/blankqdb/archive/2012/08/30/2663859.html


代码见inputpwn.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
 
int main (){
    //Stage 1
    char *argv[101] = {"/home/input/input", [1 ... 99] = "A", NULL};
    argv['A'] = "\x00";
    argv['B'] = "\x20\x0a\x0d";
    argv['C'] = "55555";
     
    //Stage 2
    int pipe2stdin[2] = {-1,-1};
    int pipe2stderr[2] = {-1,-1};
    pid_t childpid;
 
    if ( pipe(pipe2stdin) < 0 || pipe(pipe2stderr) < 0){
        perror("Cannot create the pipe");
        exit(1);
    }
     
    //Stage 4
    FILE* fp = fopen("\x0a","w");
    fwrite("\x00\x00\x00\x00",4,1,fp);
    fclose(fp);
 
    if ( ( childpid = fork() ) < 0 ){
        perror("Cannot fork");
        exit(1);
    }
     
    if ( childpid == 0 ){
        /* Parent process */
        close(pipe2stdin[0]); close(pipe2stderr[0]); // Close pipes for reading 
        write(pipe2stdin[1],"\x00\x0a\x00\xff",4);
        write(pipe2stderr[1],"\x00\x0a\x02\xff",4);
         
    } 
    else {
        /* Child process */
        close(pipe2stdin[1]); close(pipe2stderr[1]);   // Close pipes for writing
        dup2(pipe2stdin[0],0); dup2(pipe2stderr[0],2); // Map to stdin and stderr 
        close(pipe2stdin[0]); close(pipe2stderr[1]);   // Close write end (the fd has been copied before)
        // Stage 3
        char *env[2] = {"\xde\xad\xbe\xef=\xca\xfe\xba\xbe", NULL};
        execve("/home/input/input",argv,env);   // Execute the program  
        perror("Fail to execute the program");
        exit(1);
    }
 
        // Stage 5
        sleep(5);
        int sockfd;
        struct sockaddr_in server;
        sockfd = socket(AF_INET,SOCK_STREAM,0);
        if ( sockfd < 0){
            perror("Cannot create the socket");
            exit(1);
        }
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = inet_addr("127.0.0.1");
        server.sin_port = htons(55555);
        if ( connect(sockfd, (struct sockaddr*) &server, sizeof(server)) < 0 ){
            perror("Problem connecting");
            exit(1);
        }
        printf("Connected\n");
        char buf[4] = "\xde\xad\xbe\xef";
        write(sockfd,buf,4);
        close(sockfd);
        return 0;
}


把代码复制到服务器,放在/tmp目录,需要在/tmp下连接flag

ln /home/input/flag flag


然后gcc编译运行程序。Ok!!!

 

flag:Mommy! I learned how to pass variousinput in linux :)

 

参考: https://werewblog.wordpress.com/2016/01/11/pwnable-kr-input/

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值