Linux input methods

Learn linux input methods from pwnable.kr problem.

  1. 命令行参数
  2. 管道
  3. 环境变量
  4. 文件
  5. 网络

命令行参数

需要支持非打印字符,可以借用 bash 中的 $’\xff’ 实现。

args = [str(i) for i in range(1, 100)]
args[ord('A')-1] = "$'\x00'"
args[ord('B')-1] = "$'\\x20\\x0a\\x0d'"

linux 中的 xargs 也可以实现这个功能,支持参数包括 ‘\x00’ 以及 ‘\n\r\0d’ 这种特殊字符。如

python -c "args = [str(i) for i in range(100)]; print '@'.join(args)" | xargs -d '@' ./input

但是这样无法继续支持文件重定向,因为重定向会被认为是 xargs 的重定向。虽然也有一些方法可以在一定情况下解决如 ‘xargs “bash -c ./input …”’,但是这里我没有成功。

pipe

with open('input.txt', 'w') as fi:
    fi.write("\x00\x0a\x00\xff")
    fi.write("\x00\x0a\x02\xff")

调用时用重定向即可。将 stderr 重定向到 stdin 可以完成题目的要求,不过不够灵活。

./input 1 2 3 ... 99 <input.txt 2>&0

文件

with open('\x0a', 'w') as fi:
    fi.write('\x00\x00\x00\x00')

网络

from pwn import *;
p = remote('127.0.0.1', 8888);
p.send('\xde\xad\xbe\xef');

环境变量

通过命令行实在没有找到办法实现 “\xde\xad\xbe\xef”="\xca\xfe\xba\xbe" 这种情况。
最后查到正确办法是用 fork子进程来执行并传入对应的参数和环境变量

char *argv[101]={"/home/input2/input",[1 ... 99]="a",NULL};
pid_t pid;
pid = fork();
if (pid == 0) {
	// child process
} else {
	execve("path_to_exe", argv, env)
}

另外也是第一次学到C里面数组可以 [1…99] = “a” 这样初始化。

猜你喜欢

转载自blog.csdn.net/cyz14/article/details/83066510
今日推荐