5
\$\begingroup\$

I have created an application that I will be using for pentesting. It creates a list of random IP addresses, pings them, and adds ports to them (but it's not complete).

It works exactly how it should, and I just want to know if there's a better way to write this specific aspect of the program.

require 'colored'
require 'timeout'

def create_possibles
  puts "[INFO]Attempting to ping generated IP addresses.".green.bold
  ports = %w(80 443 1935 2222 3128 3130 7808 8080 8081 8085 8089 8090 8102 8104 8106 8118 8119 8123 8888 8898 9000 9090 9797 9999 10000 10052 10053 10059 10088 12345 18000 18001 18008 37564 40080 55336 59998)
    10.times do 
      ip = Array.new(4){rand(256)}.join('.')
      Timeout::timeout(5) do
        ping = `ping -n 1 #{ip}` 
        if ping =~ /Received = 1/ 
          puts "[SUCCESS]Proxy created for IP: #{ip}".green.bold
          File.open("proxies.txt", "a+") {|s| s.puts("#{ip}:#{ports[0]}",
                                                     "#{ip}:#{ports[1]}",
                                                     "#{ip}:#{ports[2]}",
                                                     "#{ip}:#{ports[3]}",
                                                     "#{ip}:#{ports[4]}",
                                                     "#{ip}:#{ports[5]}",
                                                     "#{ip}:#{ports[6]}",
                                                     "#{ip}:#{ports[7]}",
                                                     "#{ip}:#{ports[8]}",
                                                     "#{ip}:#{ports[9]}",
                                                     "#{ip}:#{ports[10]}",
                                                     "#{ip}:#{ports[11]}",
                                                     "#{ip}:#{ports[12]}",
                                                     "#{ip}:#{ports[13]}",
                                                     "#{ip}:#{ports[14]}",
                                                     "#{ip}:#{ports[15]}",
                                                     "#{ip}:#{ports[16]}",
                                                     "#{ip}:#{ports[17]}",
                                                     "#{ip}:#{ports[18]}",
                                                     "#{ip}:#{ports[19]}",
                                                     "#{ip}:#{ports[20]}",
                                                     "#{ip}:#{ports[21]}",
                                                     "#{ip}:#{ports[22]}",
                                                     "#{ip}:#{ports[23]}",
                                                     "#{ip}:#{ports[24]}",
                                                     "#{ip}:#{ports[25]}",
                                                     "#{ip}:#{ports[26]}",
                                                     "#{ip}:#{ports[27]}",
                                                     "#{ip}:#{ports[28]}",
                                                     "#{ip}:#{ports[29]}",
                                                     "#{ip}:#{ports[30]}",
                                                     "#{ip}:#{ports[31]}",
                                                     "#{ip}:#{ports[32]}",
                                                     "#{ip}:#{ports[33]}",
                                                     "#{ip}:#{ports[34]}",
                                                     "#{ip}:#{ports[35]}",
                                                     "#{ip}:#{ports[36]}")}
        else
          puts "[ERROR]IP failed to ping: #{ip}".red.bold
          next
        end
      end
    end
  end

Usage example:

[INFO]Attempting to ping generated IP addresses.
[ERROR]IP failed to ping: 197.51.232.17
[ERROR]IP failed to ping: 241.205.115.165
[SUCCESS]Proxy created for IP: 217.123.77.145
[ERROR]IP failed to ping: 16.253.31.19
[SUCCESS]Proxy created for IP: 27.209.123.149
[ERROR]IP failed to ping: 154.156.130.254
[ERROR]IP failed to ping: 229.29.20.2
[ERROR]IP failed to ping: 11.57.201.170
[ERROR]IP failed to ping: 254.99.203.92
[ERROR]IP failed to ping: 153.2.170.205

Example of text file:

217.123.77.145:80
217.123.77.145:443
217.123.77.145:1935
217.123.77.145:2222
217.123.77.145:3128
217.123.77.145:3130
217.123.77.145:7808
217.123.77.145:8080
217.123.77.145:8081
217.123.77.145:8085
217.123.77.145:8089
217.123.77.145:8090
217.123.77.145:8102
217.123.77.145:8104
217.123.77.145:8106
217.123.77.145:8118
217.123.77.145:8119
217.123.77.145:8123
217.123.77.145:8888
217.123.77.145:8898
217.123.77.145:9000
217.123.77.145:9090
217.123.77.145:9797
217.123.77.145:9999
217.123.77.145:10000
217.123.77.145:10052
217.123.77.145:10053
217.123.77.145:10059
217.123.77.145:10088
217.123.77.145:12345
217.123.77.145:18000
217.123.77.145:18001
217.123.77.145:18008
217.123.77.145:37564
217.123.77.145:40080
217.123.77.145:55336
217.123.77.145:59998
27.209.123.149:80
27.209.123.149:443
27.209.123.149:1935
27.209.123.149:2222
27.209.123.149:3128
27.209.123.149:3130
27.209.123.149:7808
27.209.123.149:8080
27.209.123.149:8081
27.209.123.149:8085
27.209.123.149:8089
27.209.123.149:8090
27.209.123.149:8102
27.209.123.149:8104
27.209.123.149:8106
27.209.123.149:8118
27.209.123.149:8119
27.209.123.149:8123
27.209.123.149:8888
27.209.123.149:8898
27.209.123.149:9000
27.209.123.149:9090
27.209.123.149:9797
27.209.123.149:9999
27.209.123.149:10000
27.209.123.149:10052
27.209.123.149:10053
27.209.123.149:10059
27.209.123.149:10088
27.209.123.149:12345
27.209.123.149:18000
27.209.123.149:18001
27.209.123.149:18008
27.209.123.149:37564
27.209.123.149:40080
27.209.123.149:55336
27.209.123.149:59998
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! I hope you get some great answers. \$\endgroup\$ – Phrancis Mar 19 '16 at 4:03
2
\$\begingroup\$

Some notes on your code:

  • Wrap lines to 80-100 chars, longer lines are unreadable.
  • ping -n 1 #{ip}. Whenever possible, use specific Ruby libraries instead of external commands, it will be more portable (i.e. net-ping)
  • "#{ip}:#{ports[0]}. That calls for a loop.
  • What is this next doing?

I'd write:

require 'colored'
require 'net/ping'

def create_possibles
  ports = %w(
    80 443 1935 2222 3128 3130 7808 8080 8081 8085 8089 8090 8102 8104 8106 
    8118 8119 8123 8888 8898 9000 9090 9797 9999 10000 10052 10053 10059 
    10088 12345 18000 18001 18008 37564 40080 55336 59998
  )

  puts("[INFO] Attempting to ping generated IP addresses.".green.bold)
  10.times do 
    ip = Array.new(4) { rand(256) }.join(".")

    if Net::Ping::ICMP.new(ip).ping? 
      puts("[SUCCESS] Proxy created for IP: #{ip}".green.bold)
      File.open("proxies.txt", "a+") do |fd|
        ports.each { |port| fd.puts("#{ip}:#{port}") }
      end
    else
      puts("[ERROR] IP failed to ping: #{ip}".red.bold)
    end
  end
end
\$\endgroup\$
  • \$\begingroup\$ Why use map instead of Array.new? \$\endgroup\$ – 13aal Mar 19 '16 at 22:45
  • \$\begingroup\$ I thought it looked more declarative, but I like Array.new(size) too. \$\endgroup\$ – tokland Mar 20 '16 at 10:43
  • \$\begingroup\$ Alright now let me ask you this, it's not one of my stated questions but would it be possible to loop finding the ips until the file containing the proxies gets to a certain size say 71826kb? \$\endgroup\$ – 13aal Mar 20 '16 at 16:59
  • \$\begingroup\$ Yes, there are many alternatives. You can add a while that checks either the file size ( File.size(path)) or keep a variable with the bytes you have written to it. \$\endgroup\$ – tokland Mar 20 '16 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.