Questions about this topic? Sign up to ask in the talk tab.

Ruby

From NetSec
Revision as of 05:51, 24 June 2012 by LashawnSeccombe (Talk | contribs) (ruby cli)

Jump to: navigation, search

Ruby is one of many interpreted languages written in C used in Linux systems for command line tools and serving web applications. Ruby on Rails is an object-oriented MVC framework written in ruby served by WEBRICK and mongrel on Linux systems.

RPU0j.png This article needs immediate attention, and is in desperate need of content.

Development Environment

ruby cli

Type irb at the commandline to use the default interpreter. If you have rubinius, an interpreter optimized for performance similar to how the sbcl or pypy interpreters are, its rbx. If you want to have autocompletion and a nicer interface, installing the pry interpreter gem through your package manager or through rubygems is a good idea.

  • It can be helpful to use the -c option to perform a syntax check:
Terminal

localhost:~ $ ruby -c test.rb


Syntax OK

  • For larger projects:
Terminal

localhost:~ $ find -name \*.rb -exec ruby -c '{}' \;


Syntax OK

Syntax OK

./vm.rb:36: syntax error, unexpected keyword_defined, expecting '('

return nil if @state > 0 || not defined? @machine_name

...


irb

gem

Your first application

Variables and data types

Boolean Logic

Loops

User Input

User-Defined Functions

Security

Examples of Useful Programs

Many times a hacker has stopped to ask how a character is represented in ascii, possibly for alphanumeric shellcode. Rather than having to resort to wikipedia, why not throw together a script? We'll call it asciispeek.rb

 
 
 #!/usr/bin/ruby
 
def asciispeek(x)
       x.each_byte do |c|
    		puts c.to_s(2)
	end
end
 
asciispeek(ARGV.join(' ').to_s)
 
 

This script takes the arguments from the shell (ARGV) and joins them into one string. The act of joining removes the whitespace between them, so we add it back in, and then convert it to a string. The result is then passed to our function. Our function calls an enumerable method of the string class, each_byte, which gets at the byte representation of the character. We then pipe it into a block which converts it into a base 2 number and then prints it. A simple edit could make this spit out the hex representation, or the octal. This is a good example of the idea of reusing code. By encapsulating the actual actions into a function, we can copy this function into a class or script that we make in the future.

Ruby is part of a series on programming.
<center>
</center>