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/admin/mssql/mssql_idf.rb
Views: 11623
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45##6# Author: Robin Wood <[email protected]> <http://www.digininja.org>7# Version: 0.18#9# This module will search the specified MSSQL server for10# 'interesting' columns and data11#12##1314class MetasploitModule < Msf::Auxiliary15include Msf::Exploit::Remote::MSSQL16include Msf::OptionalSession::MSSQL1718def initialize(info = {})19super(update_info(info,20'Name' => 'Microsoft SQL Server Interesting Data Finder',21'Description' => %q{22This module will search the specified MSSQL server for23'interesting' columns and data.2425This module has been tested against the latest SQL Server 2019 docker container image (22/04/2021).26},27'Author' => [ 'Robin Wood <robin[at]digininja.org>' ],28'License' => MSF_LICENSE,29'References' =>30[31[ 'URL', 'http://www.digininja.org/metasploit/mssql_idf.php' ],32]33))3435register_options(36[37OptString.new('NAMES', [ true, 'Pipe separated list of column names', 'passw|bank|credit|card']),38])39end4041def print_with_underline(str)42print_line(str)43print_line("=" * str.length)44end4546def run47headings = [48["Database", "Schema", "Table", "Column", "Data Type", "Row Count"]49]5051sql = ""52sql += "DECLARE @dbname nvarchar(255), @id int, @sql varchar (4000); "53sql += "DECLARE table_cursor CURSOR FOR SELECT name FROM sys.databases "54sql += "OPEN table_cursor "55sql += "FETCH NEXT FROM table_cursor INTO @dbname "56sql += "WHILE (@@FETCH_STATUS = 0) "57sql += "BEGIN "58sql += "SET @sql = 'select ';"59sql += "SET @sql = @sql + ' ''' + @dbname + ''' as ''Database'', ';"60sql += "SET @sql = @sql + 'sys.schemas.name as ''Schema'', ';"61sql += "SET @sql = @sql + 'sys.objects.name as ''Table'', ';"62sql += "SET @sql = @sql + 'sys.columns.name as ''Column'', ';"63sql += "SET @sql = @sql + 'sys.types.name as ''Column Type'' ';"64sql += "SET @sql = @sql + 'from ' + @dbname + '.sys.columns ';"65sql += "SET @sql = @sql + 'inner join ' + @dbname + '.sys.objects on sys.objects.object_id = sys.columns.object_id ';"66sql += "SET @sql = @sql + 'inner join ' + @dbname + '.sys.types on sys.types.user_type_id = sys.columns.user_type_id ';"67sql += "SET @sql = @sql + 'inner join ' + @dbname + '.sys.schemas on sys.schemas.schema_id = sys.objects.schema_id ';"6869list = datastore['Names']70where = "SET @sql = @sql + ' WHERE ("71list.split(/\|/).each { |val|72where += " lower(sys.columns.name) like ''%" + val + "%'' OR "73}7475where.slice!(-3, 4)7677where += ") ';"7879sql += where8081sql += "SET @sql = @sql + 'and sys.objects.type=''U'';';"82sql += "EXEC (@sql);"83sql += "FETCH NEXT FROM table_cursor INTO @dbname "84sql += "END "85sql += "CLOSE table_cursor "86sql += "DEALLOCATE table_cursor "8788begin89if session90set_mssql_session(session.client)91else92unless mssql_login_datastore93print_error('Login failed')94return95end96end97result = mssql_query(sql, false)98rescue Rex::ConnectionRefused => e99print_error("Connection failed: #{e}")100return101end102103column_data = result[:rows]104widths = [0, 0, 0, 0, 0, 9]105total_width = 0106107if result[:errors] && !result[:errors].empty?108result[:errors].each do |err|109print_error(err)110end111end112113if column_data.nil?114print_error("No columns matched the pattern #{datastore['NAMES'].inspect}. Set the NAMES option to change this search pattern.")115return116end117118(column_data|headings).each { |row|1190.upto(4) { |col|120widths[col] = row[col].length if row[col].length > widths[col]121}122}123124widths.each { |a|125total_width += a126}127128print_line129130buffer = ""131headings.each { |row|1320.upto(5) { |col|133buffer += row[col].ljust(widths[col] + 1)134}135print_line(buffer)136print_line137buffer = ""1381390.upto(5) { |col|140buffer += print "=" * widths[col] + " "141}142print_line(buffer)143print_line144}145146column_data.each { |row|147count_sql = "SELECT COUNT(*) AS count FROM "148149full_table = ""150column_name = ""151buffer = ""1520.upto(4) { |col|153full_table += row[col] + '.' if col < 3154column_name = row[col] if col == 3155buffer += row[col].ljust(widths[col] + 1)156}157full_table.slice!(-1, 1)158count_sql += full_table159160result = mssql_query(count_sql, false) if mssql_login_datastore161162count_data = result[:rows]163row_count = count_data[0][0]164165buffer += row_count.to_s166print_line(buffer)167print_line168}169170print_line171disconnect172end173end174175176