#!/usr/bin/env ruby

require 'fileutils'
require 'yaml'


msfbase = ARGV.shift() || "/usr/lib/msf3"
outp    = ARGV.shift || "msfexport.yaml"

# Load MSF from the specified directory
$:.unshift(File.join(msfbase, 'lib'))
require 'rex'
require 'msf/ui'

# Initialize the simplified framework instance.
$framework = Msf::Simple::Framework.create

info = {}

[ 
	[ $framework.exploits,   'exploit'   ], 
	[ $framework.auxiliary,  'auxiliary' ],
	[ $framework.payloads,   'payload'   ],
	[ $framework.encoders,   'encoder'   ],
	[ $framework.nops,       'nop'       ]	
].each do |mtype|

	mtype[0].each_module do |name,mod|
		x = mod.new
		link = mtype[1] + "/" + x.refname
		
		info[link] ||= {}
		info[link][:refs] ||= []


		info[link][:name]     = x.name
		info[link][:desc]     = x.description.split(/\n+/).map{|a| a.strip }.join(" ").strip.gsub(/\s+/, ' ')
		info[link][:version]  = x.version
		info[link][:arch]     = x.arch
		info[link][:license]  = x.license
		info[link][:authors]  = [x.author].flatten.map  {|a| [a.email, a.name]}
		info[link][:platform] = x.platform.names
		
		x.references.each { |r|	info[link][:refs] << [r.ctx_id, r.ctx_val, r.to_s] }

		info[link][:options]  = []
		x.options.each do |opt|
			info[link][:options] << [
				opt[0], opt[1].desc, opt[1].type, opt[1].required, opt[1].default, 
				opt[1].advanced, opt[1].evasion, opt[1].enums 
			]
		end

		case mtype[1]
		when 'exploit'
			info[link][:privileged] = x.privileged
			info[link][:targets] = []
			if(x.default_target)
				info[link][:default_target] = x.default_target
			end
			x.targets.each_index do |i|
				info[link][:targets] << x.targets[i].name
			end
		when 'auxiliary'
		when 'payload'
		when 'encoder'
		when 'nop'
		end
	end
end


File.open(outp, "wb") do |fd|
	fd.write(info.to_yaml)
end

