58
“Greyhat Ruby” Ruby for Reverse Engineers, Vulnerability Researchers, and Infosec Professionals SourceBoston April 2011 Stephen A. Ridley Independent Security Researcher & Director of Information Security (Financial Services Firm) @s7ephen [email protected] http://www.dontstuffbeansupyournose.com

Stephen Ridley - Greyhat Ruby

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Stephen Ridley - Greyhat Ruby

“Greyhat Ruby”Ruby for Reverse Engineers, Vulnerability Researchers, and Infosec Professionals

SourceBoston April 2011

Stephen A. RidleyIndependent Security Researcher &

Director of Information Security (Financial Services Firm)@s7ephen

[email protected]://www.dontstuffbeansupyournose.com

Page 2: Stephen Ridley - Greyhat Ruby

Who Am I?• Currently: Independent Security Researcher and Director of Information

Security at Financial Services Firm

• Previously: Senior Consultant at Matasano

• Senior Security Architect at McAfee and founding member of Security Architecture Group

• Researcher at major U.S. Defense contractor directly supporting U.S. Defense and Intelligence communities in realm of software exploitation and software reverse engineering

• Columnist for/interviewed by IT magazines (Wired, Ping!, Washington Post)

• Speaker at Blackhat, ReCON, EuSecWest, PacSec, SysCan, etc.

• Founding member of Kenshoto DefCon CTF organizer

• http://www.dontstuffbeansupyournose.com

• Guest Lecturer/Instructor (New York University, Netherlands Forensics Institute, Department of Defense, Google, et al)

Page 3: Stephen Ridley - Greyhat Ruby

What am I talking about?•Why use Ruby? : 12 Reasons for C and Python Coders

•“Interactive Coding” with Ruby (some neat tricks)

• Interfacing with the Real World

•Using Ruby to “make your own language”

•Ruxxer2 Fuzzing Framework: A Case Study

• Total Browser automation (3 major browsers on 3 major OSes)

• Distributed Programming with Ruby (Building a Fuzz Farm)

• A completely custom “Domain Specific Language” for protocols

Page 4: Stephen Ridley - Greyhat Ruby

Why use Ruby?

Twelve Good Reasons for C/C++/Python coders.

Page 5: Stephen Ridley - Greyhat Ruby

Ruby’s Equally Useful Interactive Interpreter

•Many use Python’s Interactive Interpreter as “Suisse Army Knife” for everything from calculator to ascii chart:

Page 6: Stephen Ridley - Greyhat Ruby

Ruby’s Equally Useful Interactive Interpreter

•Simple Hex Calculator

Page 7: Stephen Ridley - Greyhat Ruby

Ruby’s Equally Useful Interactive Interpreter

•URL Encode strings

Page 8: Stephen Ridley - Greyhat Ruby

Ruby’s Equally Useful Interactive Interpreter

•Other useful day-to-day string encoding/decoding

Page 9: Stephen Ridley - Greyhat Ruby

Ruby has real case/switch statements

•No more HUGE cumbersome pages of if/then/break statements like in Python

Page 10: Stephen Ridley - Greyhat Ruby

Ruby has C-style ternary statements

Page 11: Stephen Ridley - Greyhat Ruby

Ruby has “public” and “private” namespace.

Page 12: Stephen Ridley - Greyhat Ruby

Ruby (like C++ and Java) let’s you define classes in “pieces”.

•In Python you have to do it in one contiguous stretch of code so class definitions have to live in same file.

• In Ruby you can edit a class definition piecemeal.

Page 13: Stephen Ridley - Greyhat Ruby

Ruby has a “container” class that lets you control your namespace

•Modules act like “directories” for classes, methods, and variables... allowing you to flexibly organize your own namespace as you wish.

Page 14: Stephen Ridley - Greyhat Ruby

Ruby doesn’t automatically make a namespace for an

“included” file.

Page 15: Stephen Ridley - Greyhat Ruby

In Ruby doesn’t automatically make a namespace for an

“included” file.

•Allows you to control your own namespace

•No more namespace collisions from module imports!

Page 16: Stephen Ridley - Greyhat Ruby

A Better “sprintf”.

•No more fumbling with awkwardly long “%” output lines

Page 17: Stephen Ridley - Greyhat Ruby

Ruby has strong OOP paradigm and convenient “getter/setter” syntax

•The intuitive way to create a getter/setter

Page 18: Stephen Ridley - Greyhat Ruby

Ruby has strong OOP paradigm and convenient “getter/setter” syntax

•The Ruby convenience way: attr_accessor

•creates instance variable, getter and setter of same name

Page 19: Stephen Ridley - Greyhat Ruby

Modify Ruby Class definitions “on the fly” without cumbersome

“get_attr”/”set_attr”•Called EigenClasses, each object instance carries

around the class definition that created it.

•Each person is physically unique with a unique personality.

• In Ruby each Object instance is a “person” with its own unique eigenclass “personality”.

• In Python every Object instance is unique but all based on a single “personality” class.

•https://gist.github.com/864773

Page 20: Stephen Ridley - Greyhat Ruby

Ruby has “blocks” and “anonymous functions”.

•The basis of “functional programming”.

• I didn’t see the usefulness of “blocks” until I started thinking of them as shellcode that gets executed in the context of another method/class/object/etc.

Page 21: Stephen Ridley - Greyhat Ruby

Lots of other neat things: __END__

• Tells the interpreter to “ignore everything after”.

• Allows you to embed anything in your single script: SQLLite databases, binary blobs, zip files, etc.

Page 22: Stephen Ridley - Greyhat Ruby

Lots of other neat things: “send()”

•Like an “eval” that happens within the context of a class.

•This is virtually impossible in Python without some very clever use of the “code” module.

Page 23: Stephen Ridley - Greyhat Ruby

Interactive Coding:some neat tricks

Page 24: Stephen Ridley - Greyhat Ruby

Build CLI tools•Python’s CMD module

makes building fancy CLIs extremely simple

•Nestable to make Cisco IOS-style clis

•Over the years built many toolkits around this:

• Most recently: http://s7ephen.github.com/SandKit

Page 25: Stephen Ridley - Greyhat Ruby

Ruby has many ways to build interactive CLIs.

Page 26: Stephen Ridley - Greyhat Ruby

PDB: Tinkering under the hood while you drive the car...

Page 27: Stephen Ridley - Greyhat Ruby

PDB: Tinkering under the hood while you drive the car...•With “pdb.set_trace()” you get dropped into a

interactive debugger session wherever set_trace was inserted.

Page 28: Stephen Ridley - Greyhat Ruby

Tinkering under the hood while you drive the car...

•Ruby permits this several ways: IRB

Page 29: Stephen Ridley - Greyhat Ruby

Tinkering under the hood while you drive the car...

•Ruby permits this several ways: IRB

Page 30: Stephen Ridley - Greyhat Ruby

Tinkering under the hood while you drive the car...

•Ruby permits this several ways: ruby-debug

Page 31: Stephen Ridley - Greyhat Ruby

Interfacing with the Real World

Page 32: Stephen Ridley - Greyhat Ruby

HTTP and Socket stuff (simple and easy)

Page 33: Stephen Ridley - Greyhat Ruby

Using compiled shared objects/libraries/DLLs

•Ruby DL is like CTypes for Python

Page 34: Stephen Ridley - Greyhat Ruby

Ruby DL is good, but FFI is better.

Page 35: Stephen Ridley - Greyhat Ruby

Using compiled shared objects/libraries/DLLs

•Using these foreign library interfaces VERY powerful tools can be built

•They are the basis for many of the following tools

Page 36: Stephen Ridley - Greyhat Ruby

Some useful RubyDL/Ruby FFI basedtools:

Debuggers and HitTracers

•Ragweed (https://github.com/tduehr/ragweed): A scriptable debugger for OSX, Win32, & Linux

•https://github.com/emonti/dnet-ffi

•Aims to be “the Vdb of Ruby” (https://www.kenshoto.com/wiki/index.php/Vdb)

•Nerve (https://github.com/struct/Nerve) A Ruby based hittracer

Page 37: Stephen Ridley - Greyhat Ruby

Some useful RubyDL/Ruby FFI basedtools:

•LibDNet for Ruby: https://github.com/emonti/dnet-ffi

•Frasm: https://github.com/struct/frasm Ruby interface to DiStorm disassembly libraries

Page 38: Stephen Ridley - Greyhat Ruby

IDA interface: IDARub

•Spoonm’s IDA SDK interface (SDK only, no IDC support)

•Useful but not actively maintained.

Page 39: Stephen Ridley - Greyhat Ruby

JRuby and Java RMI•Like many other languages, Ruby was ported to sit on top of the Java Runtime.

•Pentesters will find this useful for interfacing with expose Java RMI interfaces:

http://chargen.matasano.com/chargen/2009/2/6/ruby-for-pen-testers-jruby.html

Page 40: Stephen Ridley - Greyhat Ruby

Ruby and Burp (Buby)•Access “Burp” from Ruby! (http://emonti.github.com/buby/)

•Build scripts to help you automate web-pentesting!

Page 41: Stephen Ridley - Greyhat Ruby

Ruby and Burp (Buby)

Usage

Automated HTTPVerb Tampering

Page 42: Stephen Ridley - Greyhat Ruby

Win32OLE/Win32API/RubyDL: Using Ruby to “script” GUI interactions

via User32

[There was embedded video here in the presentation]

You can view that video here:

http://www.youtube.com/user/dontstuffbeansupyour#p/a/u/0/j1Xb5s75pko

Page 43: Stephen Ridley - Greyhat Ruby

Win32OLE/Win32API/RubyDL: Using Ruby to “script” GUI interactions

via User32

https://github.com/s7ephen/Tamatebako/blob/master/user32.rb

Page 44: Stephen Ridley - Greyhat Ruby

Browser Automation

Page 45: Stephen Ridley - Greyhat Ruby

Browser Automation with Watir

•Instrument all the major browsers like marionettes

•Firefox, IE, Safari (partially Chrome and Opera)

•Used for QA by Facebook, Expedia, Yahoo, etc.

•Do anything a user can do.

• Input data in forms and submit it.

•Look for responses in text

•Click radio-buttons and checkboxes

Page 46: Stephen Ridley - Greyhat Ruby

Browser Automation

Page 47: Stephen Ridley - Greyhat Ruby

Distributed Code with Drb (or Rinda)

•Drb is more “RMI” than task scheduling and distribution: Lightweight and bundled with the runtime

Page 48: Stephen Ridley - Greyhat Ruby

Easy-To-Read CLI views via Hirb (example: TreeViews)

•Hirb module has many helpers to do things like create CLI based tables and treeviews. Dead simple, incredibly useful!

Page 49: Stephen Ridley - Greyhat Ruby

Brainlessly easy Data Visualizations using The Jit

•Takes a JSON blob representing your data structure as input and generates html with fancy visualizations

http://thejit.org

Page 50: Stephen Ridley - Greyhat Ruby

Write your own “language” in Ruby:Domain Specific Languages (DSL)

Page 51: Stephen Ridley - Greyhat Ruby

Why do it?•Write in a more natural and intuitive language

•Not forced to think in abstract “block”s and “primitives”.

• In 2007 released Ruxxer at PacSec which was a Python fuzzing framework with it’s own “language” (DSL).

•Language grammar written using Python Lex-Yacc

•Became difficult to maintain. Too costly for its benefit

Page 52: Stephen Ridley - Greyhat Ruby

How Ruxxer was architected:

Page 53: Stephen Ridley - Greyhat Ruby

Ruxxer 1.0 (Python)•Early Python versions of Ruxxer available:

•Advanced Combinatorics for test case generation

•Slides: https://github.com/s7ephen/Ruxxer/tree/master/presentations

https://github.com/s7ephen/Ruxxer

Page 54: Stephen Ridley - Greyhat Ruby

How Ruxxer code looked: #declarations long typeop; long_lc pkt_len; #byte length calculator string dgram; structure tlv_packet; #assignments typeop = 0x0D030A0D; pkt_len = tlv_packet; dgram = “This is userdata\r\m” push(tlv_packet, typeop, pkt_len, dgram);

Page 55: Stephen Ridley - Greyhat Ruby

Ruby metaprogramming guide

http://www.slideshare.net/d0cs4vage/ruby-meta-fuzzing

•There are many online tutorials and guides for “metaprogramming” with Ruby.

•For using metaprogramming to create fuzzers see Nephi Johnson’s “Less Dumb Fuzzing and Ruby Metaprogramming”

•For an implementation see Matasano Ruckus:https://github.com/tqbf/ruckus

Page 56: Stephen Ridley - Greyhat Ruby

With a DSL protocol definitions are more concise, easier to

write, and intuitive.

Page 57: Stephen Ridley - Greyhat Ruby

Ruxxer 2.0? (Ruby)•Ruby DSL for concisely describing protocols with

something closer to what you see in a .h

•Advanced Combinatorics for automated data mutation (like Ruxxer 1.0).

•Distributed: Fuzzing “server” controls target VMs via Drb based agents (“drones). Drones have:

• Complete browser control via Watir

• Process exception monitoring via RubyFFI/Win32OLE Dbgeng/Dbghelp bindings.

• Protocol definition visualizations using Hirb and The Jit!

Page 58: Stephen Ridley - Greyhat Ruby

“Greyhat Ruby”

More detail, code, and examples:http://www.dontstuffbeansupyournose.com

Twitter: @[email protected]

THANKS FOR LISTENING!!!!!