【问题标题】:While Loops Only Iterates Correctly OnceWhile 循环只正确迭代一次
【发布时间】:2016-09-26 21:34:29
【问题描述】:

我正在开发一个 python 脚本来强力反混淆我在安全事件中发现的恶意 Java 脚本。长话短说,在此过程中的某一时刻,他们使用 XOR 混淆了重定向到有效负载的脚本。所以这就是我的处理方式。 蟒蛇:

#!/usr/bin/python
import os
import subprocess
perl = "perl -pe 's/([;\}\{])/$"
userInput = input("")
tail = (r"\n/g'")
def deobBrute():
        count = 0
        while (count < 101):
                return(str(userInput)+str(perl)+str(count)+str(tail))
                count = count + 1
output = subprocess.Popen(deobBrute(), shell=True).wait
results = 0
while (results < 101):
        print(output)
        results = results + 1

我正在输入的用户输入:

cat elsePageXoffset | 

elsePageXoffest 是我存储混淆 JS 的文本文件。

它只迭代一次,除非它们用 XOR^1 进行混淆,否则对我没有好处。

所有其他迭代的错误消息:

<bound method Popen.wait of <subprocess.Popen object at 0x7fb65c6c9128>>

【问题讨论】:

  • 你忘记给wait打电话了。
  • return 将退出循环和函数。

标签: python python-3.x loops while-loop


【解决方案1】:

你在 while 循环内返回,这意味着它只会运行一次

如果您将 return 移到 while 循环之外,您的代码应该可以正常工作

def deobBrute():
    count = 0
    while (count < 101):
        count = count + 1
    return(str(userInput)+str(perl)+str(count)+str(tail))

【讨论】:

    【解决方案2】:

    如果这是你的方法(你的选项卡搞砸了),那么函数将立即返回(str(userInput)+str(perl)+str(count)+str(tail)),并且函数的其余部分不会执行,如果你想在方法中继续并返回,请考虑使用yield更多的价值。当 yield 返回一个生成器时,您需要遍历 deobBrute 才能访问这些值

    def deobBrute():
        count = 0
        while (count < 101):
            return(str(userInput)+str(perl)+str(count)+str(tail))
            count = count + 1
    
    def deobBrute():
        count = 0
        while (count < 101):
            yield(str(userInput)+str(perl)+str(count)+str(tail))
            count = count + 1
    

    试试这样的:

    #!/usr/bin/python
    import os
    import subprocess
    perl = "perl -pe 's/([;\}\{])/$"
    userInput = input("")
    tail = (r"\n/g'")
    
    def deobBrute():
        for i in range(1, 102):
            yield "{0}{1}{2}{3}".format(userInput, perl, i, tail)
    
    brute = deobBrute()
    
    for i in brute:
        print(subprocess.Popen(i, shell=True))
    

    【讨论】:

    • 这给了我 AttirubteError 'Generator' obejct has not attirbute 'next'
    • Python 脚本可以正常工作,但总体目标却不行。该变量 perl 是我正在使用的 perl 脚本的一部分。我想要做的是让 python 运行那个 perl 脚本或 cat textFileName | perl -pe 's/([;\}]{])/$Python 脚本编号更改\n/g'
    • 它没有在终端中作为命令运行,我输入 python 脚本的文件只是打印了 100 次,而没有在终端中对该文本文件运行 perl 脚本。
    • 我不是个聪明人。我很抱歉我的 perl 脚本需要修复,这个解决方案效果很好。谢谢。