2016-11-07 74 views
1

我目前使用python脚本来运行AC可执行类似杀死一个C程序由python脚本

os.system("./a.out \"%s\" " %p) 

有很多可用的二进制指令,我(I1,I2,I3执行.... I10准确地说)。我使用python中的itertools生成这些指令(长度为1,2,3 ... 10)的排列组合。字符串有效载荷p(在上面的片断中)就是这样一种排列。我测量对各排列的时间如下:(这可能不是来测量时间最好的方法,但是这是另外一个问题一个问题。)

​​


现在对于一些排列我得到分割故障并且python脚本继续到另一个排列。但对于一些置换,我没有得到任何响应(如卡在一个无限循环),如:

58 60 452 547 583 649 756 777 932 965 
key Not found 
(Nothing happens after this. This is due to bad combination 
of instructions or bad payload. 
I have to press ctrl C to proceed to next permutation) 
^C---------------[9 8 ]------------ 
The gadget seq is [mov,ret xor eax,eax,ret ] and time taken is 
0.000254 (this is the bad permutation of instructions) 

(Next permutation..) 

我按后按Ctrl + C,Python脚本去下一个排列。更清楚地说明

perm = itertools.permutations(gadget_list,2) #perm is list of all permutations of 2 instructions 
for string in list(perm): 
#generate the payload p from string which is one of the permutation 
#feed it to c program and measure time 
    start = time.clock() 
    os.system("./a.out \"%s\" " %p) 
    print time.clock() - start 

现在,对于更长的置换长度,对每个错误的有效载荷按Ctrl C变得单调乏味。有没有什么办法可以自动化杀死/停止C程序(我按Ctrl C),由于有效负载卡住了,并继续进行下一个排列?

回答

1

为了获得对子进程的更多控制,您需要使用subprocess模块。

import time 
from subprocess import Popen 
from shlex import split 

proc = Popen(split("./a.out '%s'" % p)) 

rtime, timeout = 0, 10 
while rtime < timeout: 
    rc = proc.poll() 
    if rc is not None: 
     break # process finished. 
    time.sleep(1) 
    rtime += 1 
else: 
    proc.kill() 
+0

那么,我要用上面的代码替换我的os.system()行?我应该在哪里写我的start = time.clock并打印time.clock() - 开始? – shane

+0

'开始'''在Popen行之后,'''end'''在底部 –

0

尝试,而像这样:

os.system("timeout 5 ./a.out \"%s\" " %p) 

5秒实例后杀死进程。

只需打开shell并尝试:

timeout 2 yes 

看到的。

+0

使用此解决方案,我不需要键入Ctrl C.但是有什么方法可以知道发生了超时,所以在那个特殊的排列我可以打印这个有效载荷是坏的消息? – shane

+0

@shane是的,返回值应该没问题;比较'{timeout 2 sleep 1; } && echo ok' with'{timeout 2 sleep 3; } && echo ok'并检查超时是否到期,'timeout'命令的返回值是失败的。因此,尝试像'result = os.system(“timeout ...”)''。请参阅https://docs.python.org/2/library/os.html#os.system“在Unix上,返回值是进程的退出状态” –

+0

我试过k = os.system(“timeout 2 sleep 1“)并打印k。 k的值是0.然后我试着k = os.system(“timeout 2 sleep 3”)。 k这次的值是31744.所以我可以用这个差异来找到超时发生的时间?但根据这个链接:http://unix.stackexchange.com/questions/205076/timeout-function-return-value,我认为在第二种情况下k的值将是124而不是31744. – shane