0x00 Puzzle
关于linux编程的技巧,包含5种不同的输入
0x01 Overview
首先查看input.c
0x02 Stage1 - argv
程序需要100个参数
python :
args = ["0"] * 99
args[ord("A")-1] = "\x00"
args[ord("B")-1] = "\x20\x0a\x0d"
os.fork() 运行input
pid = os.fork()
if pid == 0: # child
os.execv("/home/input2/input", ["input"]+args)
还可以用subprocess
subprocess.Popen(["/home/input2/input"]+args)
c :
使用execve() 运行input
int execve(const char *filename, char *const argv[], char *const envp[]);
char *args[]={"/home/input2/input", [1...99]="A", NULL};
args["A"]="\x00";
args["B"]="\x20\x0a\x0d";
execve(args[0], args, NULL);
0x03 Stage2 - stdio
程序需要从stdin中读”\x00\x0a\x00\xff”, 从stderr中读”\x00\x0a\x02\xff” 使用 pipe 实现:
- 创建两个管道: pipe2stdin 和 pipe2stderr
- fork子进程
- 子进程:映射stdin和stderr到pipe2stdin和pipe2stderr
- 父进程:写”\x00\x0a\x00\xff”和”\x00\x0a\x02\xff”到pipe2stdin和pipe2stderr
参考:Mapping UNIX pipe descriptors to stdin and stdout in C
python :
stdinr, stdinw = os.pipe()
stderrr, stderrw = os.pipe()
os.write(stdinw, "\x00\x0a\x00\xff")
os.write(stderrw, "\x00\x0a\x02\xff")
subprocess.Popen(["/home/input2/input"]+args, stdin=stdinr, stderr=stderrr)
c :
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);
}
childpid=fork();
if (childpid == 0) {
close(pipe2stdin[1]);
close(pipe2stderr[1]);
dup2(pipe2stdin[0], 0);
dup2(pipe2stderr[0], 2);
close(pipe2stdin[0]);
close(pipe2stderr[0]);
execve("/home/input2/input", args, NULL);
}
else
{
close(pipe2stdin[0]);
close(pipe2stderr[0]);
write(pipe2stdin[1],"\x00\x0a\x00\xff",4);
write(pipe2stderr[1],"\x00\x0a\x02\xff",4);
}
0x04 Stage3 - env
python :
environ = {"\xde\xad\xbe\xef" : "\xca\xfe\xba\xbe"}
subprocess.Popen(["/home/input2/input"]+args, stdin=stdinr, stderr=stderrr, env=environ)
or
os.putenv(b'\xde\xad\xbe\xef', b'\xca\xfe\xba\xbe')
c :
char *env[2] = {"\xde\xad\xbe\xef=\xca\xfe\xba\xbe", NULL};
execve("/home/input/input",argv,env);
0x05 Stage4 - file
python :
with open("\x0a", "wb") as f:
f.write(b"\x00\x00\x00\x00")
c :
FILE* fp = fopen("\x0a","w");
fwrite("\x00\x00\x00\x00",4,1,fp);
fclose(fp);
0x06 Stage5 - network
python :
port = random.randrange(10000, 20000)
args[ord("C")-1]=str(port)
s.connect(("127.0.0.1", port))
s.send("\xde\xad\xbe\xef")
s.close()
c :
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(4444);
if(connect(sockfd, (struct sockaddr*) &server, sizeof(server))<0) {
perror("Problem connecting");
exit(1);
}
char buf[4]="\xde\xad\xbe\xef";
write(sockfd, buf, 4);
close(sockfd);
}
0x07 End
用ln命令将flag链接到当前目录:
最后附上完整的代码:
python :
import os
import random
import socket
import time
import subprocess
os.system("ln -s /home/input2/flag flag")
port = random.randrange(10000, 20000)
args = ["0"] * 99
args[ord("A")-1] = ""
args[ord("B")-1] = "\x20\x0a\x0d"
args[ord("C")-1] = str(port)
stdinr, stdinw = os.pipe()
stderrr, stderrw = os.pipe()
os.write(stdinw, "\x00\x0a\x00\xff")
os.write(stderrw, "\x00\x0a\x02\xff")
environ = {"\xde\xad\xbe\xef" : "\xca\xfe\xba\xbe"}
with open("\x0a", "wb") as f:
f.write(b"\x00\x00\x00\x00")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
pro = subprocess.Popen(["/home/input2/input"]+args, stdin=stdinr, stderr=stderrr, env=environ)
time.sleep(2)
s.connect(("127.0.0.1", port))
s.send("\xde\xad\xbe\xef")
s.close()
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(void) {
char *env[2] = {"\xde\xad\xbe\xef=\xca\xfe\xba\xbe", NULL};
char *argv[101] = {"/home/input2/input", [1 ... 99] = "A", NULL};
argv['A'] = "\x00";
argv['B'] = "\x20\x0a\x0d";
argv['C'] = "4444";
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);
}
FILE *fp = fopen("\x0a", "w+");
fwrite("\x00\x00\x00\x00", 4, 1, fp);
fclose(fp);
childpid=fork();
if (childpid == 0) {
close(pipe2stdin[1]);
close(pipe2stderr[1]);
dup2(pipe2stdin[0], 0);
dup2(pipe2stderr[0], 2);
close(pipe2stdin[0]);
execve(argv[0], argv, env);
}
else
{
close(pipe2stdin[0]);
close(pipe2stderr[0]);
write(pipe2stdin[1],"\x00\x0a\x00\xff",4);
write(pipe2stderr[1],"\x00\x0a\x02\xff",4);
}
sleep(2);
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(4444);
if(connect(sockfd, (struct sockaddr*) &server, sizeof(server))<0) {
perror("Problem connecting");
exit(1);
}
char buf[4]="\xde\xad\xbe\xef";
write(sockfd, buf, 4);
close(sockfd);
return 0;
}