pwnable.kr之input

转载自 https://blog.youkuaiyun.com/qq_20307987/article/details/51337179


                                                                                       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


 
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <stdlib.h>
  5. #include <sys/wait.h>
  6. #include <arpa/inet.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. int main (){
  10. //Stage 1
  11. char *argv[ 101] = { "/home/input/input", [ 1 ... 99] = "A", NULL};
  12. argv[ 'A'] = "\x00";
  13. argv[ 'B'] = "\x20\x0a\x0d";
  14. argv[ 'C'] = "55555";
  15. //Stage 2
  16. int pipe2stdin[ 2] = { -1, -1};
  17. int pipe2stderr[ 2] = { -1, -1};
  18. pid_t childpid;
  19. if ( pipe(pipe2stdin) < 0 || pipe(pipe2stderr) < 0){
  20. perror( "Cannot create the pipe");
  21. exit( 1);
  22. }
  23. //Stage 4
  24. FILE* fp = fopen( "\x0a", "w");
  25. fwrite( "\x00\x00\x00\x00", 4, 1,fp);
  26. fclose(fp);
  27. if ( ( childpid = fork() ) < 0 ){
  28. perror( "Cannot fork");
  29. exit( 1);
  30. }
  31. if ( childpid == 0 ){
  32. /* Parent process */
  33. close(pipe2stdin[ 0]); close(pipe2stderr[ 0]); // Close pipes for reading
  34. write(pipe2stdin[ 1], "\x00\x0a\x00\xff", 4);
  35. write(pipe2stderr[ 1], "\x00\x0a\x02\xff", 4);
  36. }
  37. else {
  38. /* Child process */
  39. close(pipe2stdin[ 1]); close(pipe2stderr[ 1]); // Close pipes for writing
  40. dup2(pipe2stdin[ 0], 0); dup2(pipe2stderr[ 0], 2); // Map to stdin and stderr
  41. close(pipe2stdin[ 0]); close(pipe2stderr[ 1]); // Close write end (the fd has been copied before)
  42. // Stage 3
  43. char *env[ 2] = { "\xde\xad\xbe\xef=\xca\xfe\xba\xbe", NULL};
  44. execve( "/home/input/input",argv,env); // Execute the program
  45. perror( "Fail to execute the program");
  46. exit( 1);
  47. }
  48. // Stage 5
  49. sleep( 5);
  50. int sockfd;
  51. struct sockaddr_in server;
  52. sockfd = socket(AF_INET,SOCK_STREAM, 0);
  53. if ( sockfd < 0){
  54. perror( "Cannot create the socket");
  55. exit( 1);
  56. }
  57. server.sin_family = AF_INET;
  58. server.sin_addr.s_addr = inet_addr( "127.0.0.1");
  59. server.sin_port = htons( 55555);
  60. if ( connect(sockfd, (struct sockaddr*) &server, sizeof(server)) < 0 ){
  61. perror( "Problem connecting");
  62. exit( 1);
  63. }
  64. printf( "Connected\n");
  65. char buf[ 4] = "\xde\xad\xbe\xef";
  66. write(sockfd,buf, 4);
  67. close(sockfd);
  68. return 0;
  69. }


把代码复制到服务器,放在/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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值