2006-03-21 04:37:48 +00:00
|
|
|
require 'fileutils'
|
2006-05-30 15:44:48 +00:00
|
|
|
require 'msf/ui/console/command_dispatcher/db'
|
|
|
|
|
2007-12-31 04:05:51 +00:00
|
|
|
require 'rubygems'
|
|
|
|
require 'sqlite3'
|
2006-03-21 04:37:48 +00:00
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class intializes the database db with a shiny new
|
|
|
|
# SQLite3 database instance.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
|
|
|
|
class Plugin::DBSQLite3 < Msf::Plugin
|
|
|
|
|
2006-03-21 05:29:26 +00:00
|
|
|
#
|
2006-09-18 00:16:25 +00:00
|
|
|
# Command dispatcher for configuring SQLite
|
2006-03-21 05:29:26 +00:00
|
|
|
#
|
2006-09-18 00:16:25 +00:00
|
|
|
class SQLiteCommandDispatcher
|
|
|
|
include Msf::Ui::Console::CommandDispatcher
|
|
|
|
|
|
|
|
#
|
|
|
|
# The dispatcher's name.
|
|
|
|
#
|
|
|
|
def name
|
|
|
|
"SQLite3 Database"
|
2006-03-21 05:29:26 +00:00
|
|
|
end
|
2006-04-02 22:33:34 +00:00
|
|
|
|
2006-09-18 00:16:25 +00:00
|
|
|
#
|
|
|
|
# The initial command set
|
|
|
|
#
|
|
|
|
def commands
|
|
|
|
{
|
|
|
|
"db_connect" => "Connect to an existing database ( /path/to/db )",
|
|
|
|
"db_disconnect" => "Disconnect from the current database instance",
|
|
|
|
"db_create" => "Create a brand new database ( /path/to/db )",
|
|
|
|
"db_destroy" => "Drop an existing database ( /path/to/db )"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Disconnect from the current SQLite instance
|
|
|
|
#
|
|
|
|
def cmd_db_disconnect(*args)
|
|
|
|
if (framework.db)
|
|
|
|
framework.db.disconnect()
|
2006-09-18 00:29:14 +00:00
|
|
|
driver.remove_dispatcher('Database Backend')
|
2006-09-18 00:16:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Connect to an existing SQLite database
|
|
|
|
#
|
|
|
|
def cmd_db_connect(*args)
|
2007-12-31 04:05:51 +00:00
|
|
|
|
2006-09-18 00:16:25 +00:00
|
|
|
info = parse_db_uri(args[0])
|
|
|
|
opts = { 'adapter' => 'sqlite3' }
|
|
|
|
|
|
|
|
opts['dbfile'] = info[:path]
|
|
|
|
|
2006-09-18 00:29:14 +00:00
|
|
|
if (not File.exists?(opts['dbfile']))
|
|
|
|
print_status("The specified database does not exist")
|
|
|
|
return
|
|
|
|
end
|
2007-12-31 04:05:51 +00:00
|
|
|
|
2006-09-18 00:16:25 +00:00
|
|
|
if (not framework.db.connect(opts))
|
|
|
|
raise PluginLoadError.new("Failed to connect to the database")
|
|
|
|
end
|
|
|
|
|
|
|
|
driver.append_dispatcher(DatabaseCommandDispatcher)
|
2007-12-31 04:05:51 +00:00
|
|
|
|
|
|
|
print_status("Successfully connected to the database")
|
|
|
|
print_status("File: #{opts['dbfile']}")
|
2006-09-18 00:16:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a new SQLite database instance
|
|
|
|
#
|
|
|
|
def cmd_db_create(*args)
|
|
|
|
cmd_db_disconnect()
|
|
|
|
|
|
|
|
info = parse_db_uri(args[0])
|
|
|
|
opts = { 'adapter' => 'sqlite3' }
|
|
|
|
|
|
|
|
opts['dbfile'] = info[:path]
|
|
|
|
|
2007-05-20 06:11:33 +00:00
|
|
|
sql = File.join(Msf::Config.install_root, "data", "sql", "sqlite.sql")
|
2007-12-31 04:05:51 +00:00
|
|
|
|
|
|
|
if (File.exists?(opts['dbfile']))
|
|
|
|
print_status("The specified database already exists, use db_connect or delete this file")
|
|
|
|
print_status("File: #{opts['dbfile']}")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2007-05-20 06:11:33 +00:00
|
|
|
print_status("Creating a new database instance...")
|
2007-12-31 04:05:51 +00:00
|
|
|
|
|
|
|
sqlite3 =
|
|
|
|
Rex::FileUtils.find_full_path("sqlite3") ||
|
|
|
|
Rex::FileUtils.find_full_path("sqlite3.exe")
|
|
|
|
|
|
|
|
if (not sqlite3)
|
|
|
|
print_error("The sqlite3 executable was not found in the system path")
|
|
|
|
print_error("Please install sqlite3")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2007-12-31 08:20:48 +00:00
|
|
|
|
|
|
|
tmp = Tempfile.new("sqlXXXXXXX")
|
2007-12-31 08:27:24 +00:00
|
|
|
tmp.close
|
2007-12-31 08:20:48 +00:00
|
|
|
|
2007-12-31 08:32:05 +00:00
|
|
|
cmd = "\"#{sqlite3}\" \"#{opts['dbfile']}\" < \"#{sql}\" >\"#{tmp.path}\""
|
|
|
|
print_status("exec: #{cmd}")
|
|
|
|
|
2007-12-31 08:31:01 +00:00
|
|
|
system(cmd)
|
2007-12-31 08:20:48 +00:00
|
|
|
|
2007-12-31 08:27:24 +00:00
|
|
|
File.read(tmp.path).each_line do |line|
|
2007-12-31 08:20:48 +00:00
|
|
|
print_status("OUTPUT: #{line.strip}")
|
2007-12-31 04:05:51 +00:00
|
|
|
end
|
2006-09-18 00:16:25 +00:00
|
|
|
|
2007-12-31 08:20:48 +00:00
|
|
|
File.unlink(tmp.path)
|
|
|
|
|
2006-09-18 00:16:25 +00:00
|
|
|
if (not framework.db.connect(opts))
|
|
|
|
raise PluginLoadError.new("Failed to connect to the database")
|
|
|
|
end
|
|
|
|
driver.append_dispatcher(DatabaseCommandDispatcher)
|
2007-12-31 04:05:51 +00:00
|
|
|
|
|
|
|
print_status("Successfully created the database")
|
|
|
|
print_status("File: #{opts['dbfile']}")
|
2006-09-18 00:16:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Drop an existing database
|
|
|
|
#
|
|
|
|
def cmd_db_destroy(*args)
|
|
|
|
cmd_db_disconnect()
|
|
|
|
info = parse_db_uri(args[0])
|
|
|
|
File.unlink(info[:path])
|
2006-04-02 22:33:34 +00:00
|
|
|
end
|
|
|
|
|
2006-09-18 00:16:25 +00:00
|
|
|
def parse_db_uri(path)
|
|
|
|
res = {}
|
2006-09-18 00:29:14 +00:00
|
|
|
res[:path] = path || File.join(Msf::Config.config_directory, 'sqlite3.db')
|
2006-09-18 00:16:25 +00:00
|
|
|
res
|
2006-04-02 22:33:34 +00:00
|
|
|
end
|
2006-03-21 05:29:26 +00:00
|
|
|
end
|
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
#
|
2006-09-18 00:16:25 +00:00
|
|
|
# Wrapper class for the database command dispatcher
|
2006-03-21 04:37:48 +00:00
|
|
|
#
|
2006-09-18 00:16:25 +00:00
|
|
|
class DatabaseCommandDispatcher
|
2006-03-21 04:37:48 +00:00
|
|
|
include Msf::Ui::Console::CommandDispatcher
|
2006-05-30 15:44:48 +00:00
|
|
|
include Msf::Ui::Console::CommandDispatcher::Db
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
|
|
|
|
2006-05-30 15:44:48 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Database specific initialization goes here
|
|
|
|
#
|
|
|
|
###
|
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
def initialize(framework, opts)
|
|
|
|
super
|
2006-09-18 00:16:25 +00:00
|
|
|
add_console_dispatcher(SQLiteCommandDispatcher)
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
2006-09-18 00:16:25 +00:00
|
|
|
|
2006-03-21 04:37:48 +00:00
|
|
|
|
|
|
|
def cleanup
|
2006-09-18 00:16:25 +00:00
|
|
|
remove_console_dispatcher('SQLite3 Database')
|
2006-04-02 22:33:34 +00:00
|
|
|
remove_console_dispatcher('Database Backend')
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method returns a short, friendly name for the plugin.
|
|
|
|
#
|
|
|
|
def name
|
|
|
|
"db_sqlite3"
|
|
|
|
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
|
2006-04-02 22:33:34 +00:00
|
|
|
"Loads a new sqlite3 database backend"
|
2006-03-21 04:37:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|