2006-05-30 15:44:48 +00:00
|
|
|
require 'fileutils'
|
|
|
|
require 'msf/ui/console/command_dispatcher/db'
|
|
|
|
|
2006-09-17 07:17:27 +00:00
|
|
|
|
2006-05-30 15:44:48 +00:00
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class intializes the database db with a shiny new
|
2006-09-17 07:17:27 +00:00
|
|
|
# SQLite3 database instance.
|
2006-05-30 15:44:48 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
|
|
|
|
class Plugin::DBPostgres < Msf::Plugin
|
2006-09-18 00:05:09 +00:00
|
|
|
|
|
|
|
|
2006-05-30 15:44:48 +00:00
|
|
|
#
|
2006-09-18 00:05:09 +00:00
|
|
|
# Command dispatcher for configuring Postgres
|
2006-05-30 15:44:48 +00:00
|
|
|
#
|
2006-09-18 00:05:09 +00:00
|
|
|
class PostgresCommandDispatcher
|
|
|
|
include Msf::Ui::Console::CommandDispatcher
|
|
|
|
|
|
|
|
#
|
|
|
|
# The dispatcher's name.
|
|
|
|
#
|
|
|
|
def name
|
|
|
|
"Postgres Database"
|
2006-05-30 15:44:48 +00:00
|
|
|
end
|
|
|
|
|
2006-09-18 00:05:09 +00:00
|
|
|
#
|
|
|
|
# The initial command set
|
|
|
|
#
|
|
|
|
def commands
|
|
|
|
{
|
2006-09-18 00:16:25 +00:00
|
|
|
"db_connect" => "Connect to an existing database ( user:pass@host:port/db )",
|
|
|
|
"db_disconnect" => "Disconnect from the current database instance",
|
|
|
|
"db_create" => "Create a brand new database ( user:pass@host:port/db )",
|
|
|
|
"db_destroy" => "Drop an existing database ( user:pass@host:port/db )"
|
2006-09-18 00:05:09 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Disconnect from the current Postgres instance
|
|
|
|
#
|
2006-09-18 00:16:25 +00:00
|
|
|
def cmd_db_disconnect(*args)
|
2006-09-18 00:05:09 +00:00
|
|
|
if (framework.db)
|
|
|
|
framework.db.disconnect()
|
2006-09-18 00:29:14 +00:00
|
|
|
driver.remove_dispatcher('Database Backend')
|
2006-09-18 00:05:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Connect to an existing Postgres database
|
|
|
|
#
|
2006-09-18 00:16:25 +00:00
|
|
|
def cmd_db_connect(*args)
|
2006-09-18 00:05:09 +00:00
|
|
|
info = parse_db_uri(args[0])
|
|
|
|
opts = { 'adapter' => 'postgresql' }
|
|
|
|
|
|
|
|
opts['username'] = info[:user] if (info[:user])
|
|
|
|
opts['password'] = info[:pass] if (info[:pass])
|
|
|
|
opts['database'] = info[:name]
|
|
|
|
opts['host'] = info[:host] if (info[:host])
|
|
|
|
opts['port'] = info[:port] if (info[:port])
|
|
|
|
|
|
|
|
if (not framework.db.connect(opts))
|
|
|
|
raise PluginLoadError.new("Failed to connect to the database")
|
|
|
|
end
|
|
|
|
|
|
|
|
driver.append_dispatcher(DatabaseCommandDispatcher)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a new Postgres database instance
|
|
|
|
#
|
2006-09-18 00:16:25 +00:00
|
|
|
def cmd_db_create(*args)
|
|
|
|
cmd_db_disconnect()
|
2006-09-18 00:05:09 +00:00
|
|
|
|
|
|
|
info = parse_db_uri(args[0])
|
|
|
|
opts = { 'adapter' => 'postgresql' }
|
|
|
|
argv = []
|
|
|
|
|
|
|
|
if (info[:user])
|
|
|
|
opts['username'] = info[:user]
|
|
|
|
argv.push('-U')
|
|
|
|
argv.push(info[:user])
|
|
|
|
end
|
|
|
|
|
|
|
|
if (info[:pass])
|
|
|
|
opts['password'] = info[:pass]
|
|
|
|
argv.push('-P')
|
|
|
|
argv.push(info[:pass])
|
|
|
|
end
|
|
|
|
|
|
|
|
if (info[:host])
|
|
|
|
opts['host'] = info[:host]
|
|
|
|
argv.push('-h')
|
|
|
|
argv.push(info[:host])
|
|
|
|
end
|
|
|
|
|
|
|
|
if (info[:port])
|
|
|
|
opts['port'] = info[:port]
|
|
|
|
argv.push('-p')
|
|
|
|
argv.push(info[:port])
|
|
|
|
end
|
|
|
|
|
|
|
|
opts['database'] = info[:name]
|
|
|
|
|
|
|
|
cargs = argv.map{|c| "'#{c}' "}.join
|
|
|
|
|
|
|
|
sql = File.join(Msf::Config.install_root, "data", "sql", "postgres.sql")
|
|
|
|
fd = File.open(sql, 'r')
|
|
|
|
|
|
|
|
system("dropdb #{cargs} #{info[:name]}")
|
|
|
|
system("createdb #{cargs} #{info[:name]}")
|
|
|
|
|
|
|
|
psql = File.popen("psql -q " + cargs + info[:name], "w")
|
|
|
|
psql.write(fd.read)
|
|
|
|
psql.close
|
|
|
|
fd.close
|
|
|
|
|
|
|
|
if (not framework.db.connect(opts))
|
|
|
|
raise PluginLoadError.new("Failed to connect to the database")
|
|
|
|
end
|
|
|
|
driver.append_dispatcher(DatabaseCommandDispatcher)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Drop an existing database
|
|
|
|
#
|
2006-09-18 00:16:25 +00:00
|
|
|
def cmd_db_destroy(*args)
|
2006-09-18 00:05:09 +00:00
|
|
|
|
2006-09-18 00:16:25 +00:00
|
|
|
cmd_db_disconnect()
|
2006-09-18 00:05:09 +00:00
|
|
|
|
|
|
|
info = parse_db_uri(args[0])
|
|
|
|
argv = []
|
|
|
|
|
|
|
|
if (info[:user])
|
|
|
|
argv.push('-U')
|
|
|
|
argv.push(info[:user])
|
|
|
|
end
|
|
|
|
|
|
|
|
if (info[:pass])
|
|
|
|
argv.push('-P')
|
|
|
|
argv.push(info[:pass])
|
|
|
|
end
|
|
|
|
|
|
|
|
if (info[:host])
|
|
|
|
argv.push('-h')
|
|
|
|
argv.push(info[:host])
|
|
|
|
end
|
|
|
|
|
|
|
|
if (info[:port])
|
|
|
|
argv.push('-p')
|
|
|
|
argv.push(info[:port])
|
|
|
|
end
|
|
|
|
|
|
|
|
cargs = argv.map{|c| "'#{c}' "}.join
|
|
|
|
system("dropdb #{cargs} #{info[:name]}")
|
2006-05-30 15:44:48 +00:00
|
|
|
end
|
|
|
|
|
2006-09-18 00:05:09 +00:00
|
|
|
def parse_db_uri(path)
|
|
|
|
res = {}
|
|
|
|
if (path)
|
|
|
|
auth, dest = path.split('@')
|
|
|
|
(dest = auth and auth = nil) if not dest
|
|
|
|
res[:user],res[:pass] = auth.split(':') if auth
|
|
|
|
targ,name = dest.split('/')
|
|
|
|
(name = targ and targ = nil) if not name
|
|
|
|
res[:host],res[:port] = targ.split(':') if targ
|
|
|
|
end
|
|
|
|
res[:name] = name || 'metasploit3'
|
|
|
|
res
|
2006-05-30 15:44:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2006-09-18 00:05:09 +00:00
|
|
|
# Wrapper class for the database command dispatcher
|
2006-05-30 15:44:48 +00:00
|
|
|
#
|
2006-09-18 00:05:09 +00:00
|
|
|
class DatabaseCommandDispatcher
|
2006-05-30 15:44:48 +00:00
|
|
|
include Msf::Ui::Console::CommandDispatcher
|
|
|
|
include Msf::Ui::Console::CommandDispatcher::Db
|
|
|
|
end
|
2006-09-17 07:17:27 +00:00
|
|
|
|
2006-05-30 15:44:48 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Database specific initialization goes here
|
|
|
|
#
|
|
|
|
###
|
2006-09-17 07:17:27 +00:00
|
|
|
|
2006-05-30 15:44:48 +00:00
|
|
|
def initialize(framework, opts)
|
|
|
|
super
|
2006-09-18 00:05:09 +00:00
|
|
|
add_console_dispatcher(PostgresCommandDispatcher)
|
2006-05-30 15:44:48 +00:00
|
|
|
end
|
2006-09-17 07:17:27 +00:00
|
|
|
|
2006-05-30 15:44:48 +00:00
|
|
|
|
|
|
|
def cleanup
|
2006-09-18 00:05:09 +00:00
|
|
|
remove_console_dispatcher('PostgreSQL Database')
|
2006-05-30 15:44:48 +00:00
|
|
|
remove_console_dispatcher('Database Backend')
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method returns a short, friendly name for the plugin.
|
|
|
|
#
|
|
|
|
def name
|
|
|
|
"db_postgres"
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method returns a brief description of the plugin. It should be no
|
|
|
|
# more than 60 characters, but there are no hard limits.
|
|
|
|
#
|
|
|
|
def desc
|
|
|
|
"Loads a new postgres database backend"
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|