Wednesday, May 13, 2009

Discovering Metasploit API: Structure of installation

I was playing with MSF user cache and overwrote it, accidentally :)

1. To avoid my inevitable sloppiness in the future I am going to try and offload scripts to a more "static" location ( like ~/Code/metasploit) and try and invoke MSF APIs from there. This will allow me to develop and debug scripts outside of ~/.msf or /modules/. I can always move the m there eventually.

For that I have to add the following at the beginning of the script:

$:.unshift("/Users/dimas/framework-3.2/lib")

This line essentially allows me to prepend MSF library path to the search order for useful MSF classes and modules.

2. To better understand how MSF is laid out I also wanted to create a little helper for myself showing what's where. MSF class Msf::Config allows to create such a reference.
I will use calls similar to the following:

Msf::Config.get_config_root


For detailed information see Metasploit API here.

I am also going to use MSF's Rex library to nicely format the table of locations. Like so:


rt=Rex::Ui::Text::Table.new({
"Header" => "Structure of the installation",
"HeaderIndent" => 3,
"Columns" => ["Setting Name", "Location"],
"Indent" => 1

})


Here's what I came up with:



#!/usr/bin/env ruby
#
$:.unshift("/Users/dimas/framework-3.2/lib")

#
require 'rex/ui'
require 'msf/base'

rt=Rex::Ui::Text::Table.new({
"Header" => "Structure of the installation",
"HeaderIndent" => 3,
"Columns" => ["Setting Name", "Location"],
"Indent" => 1

})
rt.add_hr()
copts={
"Config Root" => Msf::Config.get_config_root,
"Install Root" => Msf::Config.install_root,
"Config Directory" => Msf::Config.config_directory,
"Config File" => Msf::Config.config_file,
"Data Directory" => Msf::Config.data_directory,
"Module Directory" => Msf::Config.module_directory,
"Plugin Directory" => Msf::Config.plugin_directory,
"Script Directory" => Msf::Config.script_directory,
"Session Directory" => Msf::Config.session_log_directory,
"User Module Directory" => Msf::Config.user_module_directory,
"User Script Directory" => Msf::Config.user_script_directory,
"Log Directory" => Msf::Config.log_directory
}

copts.each { |k,v| rt.add_row([k,v]) }
rt.add_hr()
rt.print


And I now have a nice reference:

Structure of the installation
=============================

Setting Name Location
------------ --------

Plugin Directory /Users/dimas/framework-3.2/plugins
Script Directory /Users/dimas/framework-3.2/scripts
User Module Directory /Users/dimas/.msf3/modules
Config Directory /Users/dimas/.msf3
Config Root /Users/dimas/.msf3
Data Directory /Users/dimas/framework-3.2/data
Log Directory /Users/dimas/.msf3/logs
User Script Directory /Users/dimas/.msf3/scripts
Session Directory /Users/dimas/.msf3/logs/sessions
Module Directory /Users/dimas/framework-3.2/modules
Install Root /Users/dimas/framework-3.2
Config File /Users/dimas/.msf3/config

0 comments: