Module 0x1 | Basic Ruby Kung Fu

Ruby has awesome abilities and tricks for dealing with string and array scenarios. In this chapter we’ll present some tricks we may need in our hacking life.

Terminal

Terminal size

Here are some different ways to get terminal size from ruby:

  • By IO/console standard library
  1. require 'io/console'
  2. rows, columns = $stdin.winsize
  3. # Try this now
  4. print "-" * (columns/2) + "\n" + ("|" + " " * (columns/2 -2) + "|\n")* (rows / 2) + "-" * (columns/2) + "\n"
  • By readline standard library
  1. require 'readline'
  2. Readline.get_screen_size
  • By environment like IRB or Pry
  1. [ENV['LINES'].to_i, ENV['COLUMNS'].to_i]
  • By tput command line
  1. [`tput cols`.to_i , `tput lines`.to_i]

Console with tab completion

We can’t stop being jealous of Metasploit console (msfconsole), where we take a rest from command line switches. Fortunately, here is the main idea of console tab completion in ruby:

  • Readline

The Readline module provides an interface for GNU Readline. This module defines a number of methods to facilitate completion and accesses input history from the Ruby interpreter.

console-basic1.rb

  1. #!/usr/bin/env ruby
  2. # KING SABRI | @KINGSABRI
  3. #
  4. require 'readline'
  5. # Prevent Ctrl+C for exiting
  6. trap('INT', 'SIG_IGN')
  7. # List of commands
  8. CMDS = [ 'help', 'rubyfu', 'ls', 'pwd', 'exit' ].sort
  9. completion = proc { |line| CMDS.grep( /^#{Regexp.escape( line )}/ ) }
  10. # Console Settings
  11. Readline.completion_proc = completion # Set completion process
  12. Readline.completion_append_character = ' ' # Make sure to add a space after completion
  13. while line = Readline.readline('-> ', true)
  14. puts line unless line.nil? or line.squeeze.empty?
  15. break if line =~ /^quit.*/i or line =~ /^exit.*/i
  16. end

Now run it and try out the tab completion!

Well, the main idea for tab completion is to make things easier, not just “press tab”. Here is a simple thought…

console-basic2.rb

  1. require 'readline'
  2. # Prevent Ctrl+C for exiting
  3. trap('INT', 'SIG_IGN')
  4. # List of commands
  5. CMDS = [ 'help', 'rubyfu', 'ls', 'exit' ].sort
  6. completion =
  7. proc do |str|
  8. case
  9. when Readline.line_buffer =~ /help.*/i
  10. puts "Available commands:\n" + "#{CMDS.join("\t")}"
  11. when Readline.line_buffer =~ /rubyfu.*/i
  12. puts "Rubyfu, where Ruby goes evil!"
  13. when Readline.line_buffer =~ /ls.*/i
  14. puts `ls`
  15. when Readline.line_buffer =~ /exit.*/i
  16. puts 'Exiting..'
  17. exit 0
  18. else
  19. CMDS.grep( /^#{Regexp.escape(str)}/i ) unless str.nil?
  20. end
  21. end
  22. Readline.completion_proc = completion # Set completion process
  23. Readline.completion_append_character = ' ' # Make sure to add a space after completion
  24. while line = Readline.readline('-> ', true) # Start console with character -> and make add_hist = true
  25. puts completion.call
  26. break if line =~ /^quit.*/i or line =~ /^exit.*/i
  27. end

Things can go much farther, like msfconsole, maybe?