jdi_hook

by Eric Monti
http://github.com/emonti/jdi_hook

DESCRIPTION:

JdiHook is a ruby-scriptable Java debugger based on and around Sun’s Java Debugging Interface (JDI) API.

FEATURES/PROBLEMS:

  • JdiHook aims to provide a scriptable engine around Sun’s Java Debugging Interface (JDI) using JRuby. The uses of such an engine are intentionally open-ended, but the initial rationale was the need for custom instrumentation and dynamic analysis tools for Java applications with a reverse engineering and vulnerability testing mindset.

  • You already use JAD or JODE for class decompiling, but you need a quick and painless way to observe call trees, hit traces, and other target behaviors during runtime. Create bespoke Java runtime debugging scripts just like you would using PyDbg or Ragweed on a native target.

SYNOPSIS:

require 'rubygems'
require 'jdi_hook'

# Start a java target class. Kind of equivalent to running 'jdb HelloWorld'
vm = JdiHook.command_line_launch("HelloWorld")

# Instantiate and attach debugging event handler. MethodTracer is geared
# for attaching hooks to method entry and exit events.
dbg = JdiHook::MethodTracer.new vm, :redirect_stdio => true

# Define some handlers for Java method entry and exit events to dump
# some information about the method invocation. Event handlers for 
# MethodTracer are supplied as Ruby Proc objects (or blocks if you will)
en_proc = lambda {|this, evt| puts " [*] " << this.notify_entry(evt.method) }
ex_proc = lambda {|this, evt| puts " [*] " << this.notify_exit(evt.method) }

# Configure some event hooks to fire on regex pattern matches by method name
dbg.meth_hooks = { 
  /\.main$/ => { :on_entry => en_proc, :on_exit => ex_proc },
  /.*/      => { :on_entry => en_proc },
}

# "Continue" the target from the debugger
dbg.go

REQUIREMENTS:

INSTALL:

  • jruby -S gem sources -a gems.github.com # only have to do this once

  • jruby -S gem install emonti-rbkb

LICENSE:

(The MIT License)

Copyright © 2008 Eric Monti - Matasano Security

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.