|
| 1 | +## |
| 2 | +# $Id$ |
| 3 | +## |
| 4 | + |
| 5 | +## |
| 6 | +# This file is part of the Metasploit Framework and may be subject to |
| 7 | +# redistribution and commercial restrictions. Please see the Metasploit |
| 8 | +# Framework web site for more information on licensing and terms of use. |
| 9 | +# http://metasploit.com/framework/ |
| 10 | +## |
| 11 | + |
| 12 | + |
| 13 | +require 'msf/core' |
| 14 | +require 'yaml' |
| 15 | + |
| 16 | +class Metasploit3 < Msf::Auxiliary |
| 17 | + |
| 18 | + include Msf::Exploit::Remote::MSSQL |
| 19 | + include Msf::Auxiliary::Report |
| 20 | + |
| 21 | + include Msf::Auxiliary::Scanner |
| 22 | + |
| 23 | + def initialize |
| 24 | + super( |
| 25 | + 'Name' => 'MSSQL Schema Dump', |
| 26 | + 'Description' => %Q{ |
| 27 | + This module attempts to extract the schema from a MSSQL Server |
| 28 | + Instance. It will disregard builtin and example DBs such |
| 29 | + as master,model,msdb, and tempdb. The module will create |
| 30 | + a note for each DB found, and store a YAML formatted output |
| 31 | + as loot for easy reading. |
| 32 | + }, |
| 33 | + 'Author' => ['TheLightCosine <thelightcosine[at]gmail.com>'], |
| 34 | + 'License' => MSF_LICENSE |
| 35 | + ) |
| 36 | + end |
| 37 | + |
| 38 | + def run_host(ip) |
| 39 | + |
| 40 | + if (not mssql_login_datastore) |
| 41 | + print_error("#{rhost}:#{rport} - Invalid SQL Server credentials") |
| 42 | + return |
| 43 | + end |
| 44 | + |
| 45 | + #Grabs the Instance Name and Version of MSSQL(2k,2k5,2k8) |
| 46 | + instancename = mssql_query(mssql_enumerate_servername())[:rows][0][0].split('\\')[1] |
| 47 | + print_status("Instance Name: #{instancename.inspect}") |
| 48 | + version = mssql_query(mssql_sql_info())[:rows][0][0] |
| 49 | + output = "Microsoft SQL Server Schema \n Host: #{datastore['RHOST']} \n Port: #{datastore['RPORT']} \n Instance: #{instancename} \n Version: #{version} \n====================\n\n" |
| 50 | + |
| 51 | + #Grab all the DB schema and save it as notes |
| 52 | + mssql_schema = get_mssql_schema |
| 53 | + return nil if mssql_schema.nil? or mssql_schema.empty? |
| 54 | + mssql_schema.each do |db| |
| 55 | + report_note( |
| 56 | + :host => rhost, |
| 57 | + :type => "mssql.db.schema", |
| 58 | + :data => db, |
| 59 | + :port => rport, |
| 60 | + :proto => 'tcp', |
| 61 | + :update => :unique_data |
| 62 | + ) |
| 63 | + end |
| 64 | + output << YAML.dump(mssql_schema) |
| 65 | + this_service = report_service( |
| 66 | + :host => datastore['RHOST'], |
| 67 | + :port => datastore['RPORT'], |
| 68 | + :name => 'mssql', |
| 69 | + :proto => 'tcp' |
| 70 | + ) |
| 71 | + store_loot('mssql_schema', "text/plain", datastore['RHOST'], output, "#{datastore['RHOST']}_mssql_schema.txt", "MS SQL Schema", this_service) |
| 72 | + print_good output |
| 73 | + end |
| 74 | + |
| 75 | + def get_mssql_schema |
| 76 | + mssql_db_names = get_db_names() |
| 77 | + mssql_schema=[] |
| 78 | + unless mssql_db_names.nil? |
| 79 | + mssql_db_names.each do |dbname| |
| 80 | + next if dbname[0] == 'model' or dbname[0] == 'master' or dbname[0] == 'msdb' or dbname[0] == 'tempdb' |
| 81 | + tmp_db = {} |
| 82 | + tmp_tblnames = get_tbl_names(dbname[0]) |
| 83 | + unless tmp_tblnames.nil? |
| 84 | + tmp_db['DBName']= dbname[0] |
| 85 | + tmp_db['Tables'] = [] |
| 86 | + tmp_tblnames.each do |tblname| |
| 87 | + next if tblname[0].nil? |
| 88 | + tmp_tbl = {} |
| 89 | + tmp_tbl['TableName'] = tblname[0] |
| 90 | + tmp_tbl['Columns'] = [] |
| 91 | + tmp_columns = get_columns(dbname[0], tblname[1]) |
| 92 | + unless tmp_columns.nil? |
| 93 | + tmp_columns.each do |column| |
| 94 | + next if column[0].nil? |
| 95 | + tmp_column = {} |
| 96 | + tmp_column['ColumnName'] = column[0] |
| 97 | + tmp_column['ColumnType'] = column[1] |
| 98 | + tmp_column['ColumnLength'] = column[2] |
| 99 | + tmp_tbl['Columns'] << tmp_column |
| 100 | + end |
| 101 | + end |
| 102 | + tmp_db['Tables'] << tmp_tbl |
| 103 | + end |
| 104 | + end |
| 105 | + mssql_schema << tmp_db |
| 106 | + end |
| 107 | + end |
| 108 | + return mssql_schema |
| 109 | + end |
| 110 | + |
| 111 | + |
| 112 | + #Gets all of the Databases on this Instance |
| 113 | + def get_db_names |
| 114 | + results = mssql_query(mssql_db_names())[:rows] |
| 115 | + return results |
| 116 | + end |
| 117 | + |
| 118 | + #Gets all the table names for the given DB |
| 119 | + def get_tbl_names(db_name) |
| 120 | + results = mssql_query("SELECT name,id FROM #{db_name}..sysobjects WHERE xtype = 'U'")[:rows] |
| 121 | + return results |
| 122 | + end |
| 123 | + |
| 124 | + def get_columns(db_name, table_id) |
| 125 | + results = 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] |
| 126 | + return results |
| 127 | + end |
| 128 | + |
| 129 | + |
| 130 | +end |
| 131 | + |
0 commit comments