Skip to content

Commit 52be1c3

Browse files
Add schemadump module for MySql
1 parent 1a03777 commit 52be1c3

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
require 'msf/core'
13+
require 'yaml'
14+
15+
class Metasploit3 < Msf::Auxiliary
16+
17+
include Msf::Exploit::Remote::MYSQL
18+
include Msf::Auxiliary::Report
19+
20+
include Msf::Auxiliary::Scanner
21+
22+
def initialize
23+
super(
24+
'Name' => 'MYSQL Schema Dump',
25+
'Version' => '$Revision$',
26+
'Description' => %Q{
27+
This module extracts the schema information from a
28+
MySQL DB server.
29+
},
30+
'Author' => ['TheLightCosine <thelightcosine[at]gmail.com>'],
31+
'License' => MSF_LICENSE
32+
)
33+
end
34+
35+
def run_host(ip)
36+
37+
if (not mysql_login_datastore)
38+
print_error("Invalid MySQL Server credentials")
39+
return
40+
end
41+
mysql_schema = get_schema
42+
mysql_schema.each do |db|
43+
report_note(
44+
:host => rhost,
45+
:type => "mysql.db.schema",
46+
:data => db,
47+
:port => rport,
48+
:proto => 'tcp',
49+
:update => :unique_data
50+
)
51+
end
52+
output = "MySQL Server Schema \n Host: #{datastore['RHOST']} \n Port: #{datastore['RPORT']} \n ====================\n\n"
53+
output << YAML.dump(mysql_schema)
54+
this_service = report_service(
55+
:host => datastore['RHOST'],
56+
:port => datastore['RPORT'],
57+
:name => 'mysql',
58+
:proto => 'tcp'
59+
)
60+
store_loot('mysql_schema', "text/plain", datastore['RHOST'], output, "#{datastore['RHOST']}_mysql_schema.txt", "MySQL Schema", this_service)
61+
print_good output
62+
end
63+
64+
65+
def get_schema
66+
mysql_schema=[]
67+
res = mysql_query("show databases")
68+
if res.size > 0
69+
res.each do |row|
70+
next if row[0].nil?
71+
next if row[0].empty?
72+
next if row[0]== "information_schema"
73+
next if row[0]== "mysql"
74+
next if row[0]== "performance_schema"
75+
next if row[0]== "test"
76+
tmp_db ={}
77+
tmp_db['DBName'] = row[0]
78+
tmp_db['Tables'] = []
79+
tmp_tblnames = get_tbl_names(row[0])
80+
unless tmp_tblnames.nil? or tmp_tblnames.empty?
81+
tmp_tblnames.each do |table_name|
82+
tmp_tbl={}
83+
tmp_tbl['TableName'] = table_name
84+
tmp_tbl['Columns'] = []
85+
tmp_clmnames = get_columns(tmp_db['DBName'],table_name)
86+
unless tmp_clmnames.nil? or tmp_clmnames.empty?
87+
tmp_clmnames.each do |column|
88+
tmp_column = {}
89+
tmp_column['ColumnName'] = column[0]
90+
tmp_column['ColumnType'] = column[1]
91+
tmp_tbl['Columns'] << tmp_column
92+
end
93+
end
94+
tmp_db['Tables'] << tmp_tbl
95+
end
96+
end
97+
mysql_schema << tmp_db
98+
end
99+
end
100+
return mysql_schema
101+
end
102+
103+
#Gets all of the Tables names inside the given Database
104+
def get_tbl_names(dbname)
105+
106+
tables=[]
107+
res = mysql_query("SHOW tables from #{dbname}")
108+
if res.size > 0
109+
res.each do |row|
110+
next if row[0].nil?
111+
next if row[0].empty?
112+
tables<<row[0]
113+
end
114+
end
115+
return tables
116+
117+
end
118+
119+
def get_columns(db_name,tbl_name)
120+
tables=[]
121+
res = mysql_query("desc #{db_name}.#{tbl_name}")
122+
if res.size > 0
123+
res.each do |row|
124+
next if row[0].nil?
125+
next if row[0].empty?
126+
tables<< [row[0],row[1]]
127+
end
128+
end
129+
return tables
130+
end
131+
132+
end

0 commit comments

Comments
 (0)