Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/auxiliary/scanner/mssql/mssql_schemadump.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##4require 'yaml'56class MetasploitModule < Msf::Auxiliary7include Msf::Exploit::Remote::MSSQL8include Msf::Auxiliary::Report9include Msf::Auxiliary::Scanner10include Msf::OptionalSession::MSSQL1112def initialize13super(14'Name' => 'MSSQL Schema Dump',15'Description' => %Q{16This module attempts to extract the schema from a MSSQL Server17Instance. It will disregard builtin and example DBs such18as master, model, msdb, and tempdb. The module will create19a note for each DB found, and store a YAML formatted output20as loot for easy reading.21},22'Author' => ['theLightCosine'],23'License' => MSF_LICENSE24)2526register_options([27OptBool.new('DISPLAY_RESULTS', [true, "Display the Results to the Screen", true])28])29end3031def run_host(ip)32if session33set_mssql_session(session.client)34else35unless mssql_login_datastore36print_error("#{datastore['RHOST']}:#{datastore['RPORT']} - Invalid SQL Server credentials")37return38end39end4041# Grabs the Instance Name and Version of MSSQL(2k,2k5,2k8)42instance_info = mssql_query(mssql_enumerate_servername())[:rows][0][0].split('\\')43instancename = instance_info[1] || instance_info[0]4445print_status("Instance Name: #{instancename.inspect}")46version = mssql_query(mssql_sql_info())[:rows][0][0]47output = "Microsoft SQL Server Schema \n Host: #{mssql_client.peerhost} \n Port: #{mssql_client.peerport} \n Instance: #{instancename} \n Version: #{version} \n====================\n\n"4849# Grab all the DB schema and save it as notes50mssql_schema = get_mssql_schema51if mssql_schema.nil? or mssql_schema.empty?52print_good output if datastore['DISPLAY_RESULTS']53print_warning('No schema information found')54return nil55end56mssql_schema.each do |db|57report_note(58:host => mssql_client.peerhost,59:type => "mssql.db.schema",60:data => db,61:port => mssql_client.peerport,62:proto => 'tcp',63:update => :unique_data64)65end66output << YAML.dump(mssql_schema)67this_service = report_service(68:host => mssql_client.peerhost,69:port => mssql_client.peerport,70:name => 'mssql',71:proto => 'tcp'72)73store_loot('mssql_schema', "text/plain", mssql_client.peerhost, output, "#{mssql_client.peerhost}_mssql_schema.txt", "MS SQL Schema", this_service)74print_good output if datastore['DISPLAY_RESULTS']75end7677def get_mssql_schema78mssql_db_names = get_db_names()79mssql_schema=[]80unless mssql_db_names.nil?81mssql_db_names.each do |dbname|82next if dbname[0] == 'model' or dbname[0] == 'master' or dbname[0] == 'msdb' or dbname[0] == 'tempdb'83tmp_db = {}84tmp_tblnames = get_tbl_names(dbname[0])85unless tmp_tblnames.nil?86tmp_db['DBName']= dbname[0]87tmp_db['Tables'] = []88tmp_tblnames.each do |tblname|89next if tblname[0].nil?90tmp_tbl = {}91tmp_tbl['TableName'] = tblname[0]92tmp_tbl['Columns'] = []93tmp_columns = get_columns(dbname[0], tblname[1])94unless tmp_columns.nil?95tmp_columns.each do |column|96next if column[0].nil?97tmp_column = {}98tmp_column['ColumnName'] = column[0]99tmp_column['ColumnType'] = column[1]100tmp_column['ColumnLength'] = column[2]101tmp_tbl['Columns'] << tmp_column102end103end104tmp_db['Tables'] << tmp_tbl105end106end107mssql_schema << tmp_db108end109end110return mssql_schema111end112113114# Gets all of the Databases on this Instance115def get_db_names116results = mssql_query(mssql_db_names())[:rows]117return results118end119120# Gets all the table names for the given DB121def get_tbl_names(db_name)122results = mssql_query("SELECT name,id FROM #{db_name}..sysobjects WHERE xtype = 'U'")[:rows]123return results124end125126# TODO: This should be split up, I fear nil problems in these query/response parsings127def get_columns(db_name, table_id)128results = mssql_query("Select syscolumns.name,systypes.name,syscolumns.length from #{db_name}..syscolumns JOIN #{db_name}..systypes ON syscolumns.xtype=systypes.xtype WHERE syscolumns.id=#{table_id}")[:rows]129return results130end131end132133134