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"
Copied!
  • By readline standard library
1
require 'readline'
2
Readline.get_screen_size
Copied!
  • By environment like IRB or Pry
1
[ENV['LINES'].to_i, ENV['COLUMNS'].to_i]
Copied!
  • By tput command line
1
[`tput cols`.to_i , `tput lines`.to_i]
Copied!

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
6
# Prevent Ctrl+C for exiting
7
trap('INT', 'SIG_IGN')
8
9
# List of commands
10
CMDS = [ 'help', 'rubyfu', 'ls', 'pwd', 'exit' ].sort
11
12
13
completion = proc { |line| CMDS.grep( /^#{Regexp.escape( line )}/ ) }
14
15
# Console Settings
16
Readline.completion_proc = completion # Set completion process
17
Readline.completion_append_character = ' ' # Make sure to add a space after completion
18
19
while line = Readline.readline('-> ', true)
20
puts line unless line.nil? or line.squeeze.empty?
21
break if line =~ /^quit.*/i or line =~ /^exit.*/i
22
end
Copied!
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
3
# Prevent Ctrl+C for exiting
4
trap('INT', 'SIG_IGN')
5
6
# List of commands
7
CMDS = [ 'help', 'rubyfu', 'ls', 'exit' ].sort
8
9
10
completion =
11
proc do |str|
12
case
13
when Readline.line_buffer =~ /help.*/i
14
puts "Available commands:\n" + "#{CMDS.join("\t")}"
15
when Readline.line_buffer =~ /rubyfu.*/i
16
puts "Rubyfu, where Ruby goes evil!"
17
when Readline.line_buffer =~ /ls.*/i
18
puts `ls`
19
when Readline.line_buffer =~ /exit.*/i
20
puts 'Exiting..'
21
exit 0
22
else
23
CMDS.grep( /^#{Regexp.escape(str)}/i ) unless str.nil?
24
end
25
end
26
27
28
Readline.completion_proc = completion # Set completion process
29
Readline.completion_append_character = ' ' # Make sure to add a space after completion
30
31
while line = Readline.readline('-> ', true) # Start console with character -> and make add_hist = true
32
puts completion.call
33
break if line =~ /^quit.*/i or line =~ /^exit.*/i
34
end
Copied!
Things can go much farther, like msfconsole, maybe?
Last modified 3yr ago