在 unix 管道中发送空字节

Pax*_*Pax 9 bash io-redirection null

我正在尝试将 python 生成的输入重定向到 bash 5.0.3 中的 ELF 64 位可执行文件。我正进入(状态:

> ./bf <<< $(python2 -c "print('c'*6+b'\x00'+'c'*6)")
bash: warning: command substitution: ignored null byte in input
Enter password: Password didn't match
input: cccccccccccc
Run Code Online (Sandbox Code Playgroud)

如何在输入中允许空字节?

Kus*_*nda 25

您可以通过管道传递空字节(就像您在标题中所说的那样),但是bashshell 不允许在扩展中使用空字节。它不允许在扩展中使用空字节,因为 shell 使用 C 字符串来表示扩展的结果,而 C 字符串以空字节终止

$ hexdump -C <<< $( python2 -c "print('c'*6+b'\x00'+'c'*6)" )
bash: warning: command substitution: ignored null byte in input
00000000  63 63 63 63 63 63 63 63  63 63 63 63 0a           |cccccccccccc.|
0000000d
Run Code Online (Sandbox Code Playgroud)

通过管道传递数据很好:

$ python2 -c "print('c'*6+b'\x00'+'c'*6)" | hexdump -C
00000000  63 63 63 63 63 63 00 63  63 63 63 63 63 0a        |cccccc.cccccc.|
0000000e
Run Code Online (Sandbox Code Playgroud)

重定向进程替换也有效,因为进程替换不会扩展到命令生成的数据,而是扩展到包含该数据的文件的名称:

$ hexdump -C < <( python2 -c "print('c'*6+b'\x00'+'c'*6)" )
00000000  63 63 63 63 63 63 00 63  63 63 63 63 63 0a        |cccccc.cccccc.|
0000000e
Run Code Online (Sandbox Code Playgroud)

因此,解决方案是避免让 shell 将包含空字节的数据存储在字符串中,而是通过管道传递数据,而不使用命令替换。在你的情况下

$ python2 -c "print('c'*6+b'\x00'+'c'*6)" | ./bf
Run Code Online (Sandbox Code Playgroud)

有关的:


或者切换到zsh确实允许字符串空字节:

$ hexdump -C <<< $( python2 -c "print('c'*6+b'\x00'+'c'*6)" )
00000000  63 63 63 63 63 63 00 63  63 63 63 63 63 0a        |cccccc.cccccc.|
0000000e
Run Code Online (Sandbox Code Playgroud)

  • 它也适用于进程替换:`od -c &lt; &lt;(python2 -c "p​​rint('c'*6+b'\x00'+'c'*6)")` (5认同)