转载自 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
-
#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/