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:
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
#!/usr/bin/env ruby# KING SABRI | @KINGSABRI# require'readline'# Prevent Ctrl+C for exitingtrap('INT','SIG_IGN')# List of commandsCMDS= [ 'help','rubyfu','ls','pwd','exit' ].sortcompletion =proc { |line|CMDS.grep( /^#{Regexp.escape( line )}/ ) }# Console SettingsReadline.completion_proc = completion # Set completion processReadline.completion_append_character =' '# Make sure to add a space after completionwhile line =Readline.readline('-> ',true)puts line unless line.nil? or line.squeeze.empty?breakif line =~/^quit.*/ior line =~/^exit.*/iend
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
require'readline'# Prevent Ctrl+C for exitingtrap('INT','SIG_IGN')# List of commandsCMDS= [ 'help','rubyfu','ls','exit' ].sortcompletion =procdo|str|casewhenReadline.line_buffer =~/help.*/iputs"Available commands:\n"+"#{CMDS.join("\t")}"whenReadline.line_buffer =~/rubyfu.*/iputs"Rubyfu, where Ruby goes evil!"whenReadline.line_buffer =~/ls.*/iputs`ls`whenReadline.line_buffer =~/exit.*/iputs'Exiting..'exit0elseCMDS.grep( /^#{Regexp.escape(str)}/i ) unless str.nil?endendReadline.completion_proc = completion # Set completion processReadline.completion_append_character =' '# Make sure to add a space after completionwhile line =Readline.readline('-> ',true) # Start console with character -> and make add_hist = trueputs completion.callbreakif line =~/^quit.*/ior line =~/^exit.*/iend
Things can go much farther, like msfconsole, maybe?