Tic*_*Tic 17 python subprocess fuzzing segmentation-fault communicate
我在使用子进程模块获取崩溃程序的输出时遇到问题.我正在使用python2.7和subprocess来调用带有奇怪参数的程序以获得一些段错误为了调用程序,我使用以下代码:
proc = (subprocess.Popen(called,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE))
out,err=proc.communicate()
print out,err
Run Code Online (Sandbox Code Playgroud)
called是一个包含程序名称和参数的列表(一个包含随机字节的字符串,除了子进程根本不喜欢的NULL字节)
当程序没有崩溃时代码表现并向我显示stdout和stderr,但是当它崩溃时,out和err是空的而不是显示着名的"Segmentation fault".
即使程序崩溃,我希望找到一种方法来获取和错误.
希望有人在这里作为一个想法:)
PS:我也尝试过check_output/call/check_call方法
编辑:
我在一个python虚拟环境中的Archlinux 64位上运行这个脚本(这里不应该是重要的东西,但你永远不会知道:p)
segfault发生在我正在尝试运行的C程序中,是缓冲区溢出的结果
问题是当发生段错误时,我无法获得子进程发生的输出
我得到了正确的返回码:-11(SIGSEGV)
使用python我得到:
./dumb2 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
('Exit code was:', -11)
('Output was:', '')
('Errors were:', '')
Run Code Online (Sandbox Code Playgroud)在python外面,我得到:
./dumb2 $(perl -e "print 'A'x50")
BEGINNING OF PROGRAM
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
END OF THE PROGRAM
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)shell的返回值是相同的:echo $?返回139所以-11($?&128)
"Segmentation fault"消息可能由shell生成.要弄清楚,该过程是否被杀死SIGSEGV,请检查proc.returncode == -signal.SIGSEGV.
如果要查看消息,可以在shell中运行该命令:
#!/usr/bin/env python
from subprocess import Popen, PIPE
proc = Popen(shell_command, shell=True, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
print out, err, proc.returncode
Run Code Online (Sandbox Code Playgroud)
我已经测试了它shell_command="python -c 'from ctypes import *; memset(0,1,1)'"导致段错误并且捕获了消息err.
如果消息直接打印到终端,那么您可以使用pexpect模块捕获它:
#!/usr/bin/env python
from pipes import quote
from pexpect import run # $ pip install pexpect
out, returncode = run("sh -c " + quote(shell_command), withexitstatus=1)
signal = returncode - 128 # 128+n
print out, signal
Run Code Online (Sandbox Code Playgroud)
或pty直接使用stdlib模块:
#!/usr/bin/env python
import os
import pty
from select import select
from subprocess import Popen, STDOUT
# use pseudo-tty to capture output printed directly to the terminal
master_fd, slave_fd = pty.openpty()
p = Popen(shell_command, shell=True, stdin=slave_fd, stdout=slave_fd,
stderr=STDOUT, close_fds=True)
buf = []
while True:
if select([master_fd], [], [], 0.04)[0]: # has something to read
data = os.read(master_fd, 1 << 20)
if data:
buf.append(data)
else: # EOF
break
elif p.poll() is not None: # process is done
assert not select([master_fd], [], [], 0)[0] # nothing to read
break
os.close(slave_fd)
os.close(master_fd)
print "".join(buf), p.returncode-128
Run Code Online (Sandbox Code Playgroud)
回到这里:它的工作方式就像一个来自 python3 的子进程的魅力,如果你在 Linux 上,有一个名为 python2 的向后移植,subprocess32它工作得很好
旧的解决方案:我使用了 pexpect 并且它有效
def cmd_line_call(name, args):
child = pexpect.spawn(name, args)
# Wait for the end of the output
child.expect(pexpect.EOF)
out = child.before # we get all the data before the EOF (stderr and stdout)
child.close() # that will set the return code for us
# signalstatus and existstatus read as the same (for my purpose only)
if child.exitstatus is None:
returncode = child.signalstatus
else:
returncode = child.exitstatus
return (out, returncode)
Run Code Online (Sandbox Code Playgroud)
PS:有点慢(因为它产生了一个伪tty)
| 归档时间: |
|
| 查看次数: |
10525 次 |
| 最近记录: |