2015-04-09 20:01:23 +00:00
|
|
|
# encoding: utf-8
|
2015-07-15 13:15:18 +00:00
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
2015-10-06 16:55:44 +00:00
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
2017-05-06 00:06:28 +00:00
|
|
|
# author: Aaron Lippold
|
2015-04-09 20:01:23 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
module Inspec::Resources
|
|
|
|
class MysqlSession < Inspec.resource(1)
|
|
|
|
name 'mysql_session'
|
|
|
|
desc 'Use the mysql_session InSpec audit resource to test SQL commands run against a MySQL database.'
|
|
|
|
example "
|
2017-05-06 00:06:28 +00:00
|
|
|
sql = mysql_session('my_user','password','host')
|
2016-03-08 18:06:55 +00:00
|
|
|
describe sql.query('show databases like \'test\';') do
|
2016-05-03 22:14:33 +00:00
|
|
|
its('stdout') { should_not match(/test/) }
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
|
|
|
"
|
|
|
|
|
2017-06-23 15:28:15 +00:00
|
|
|
def initialize(user = nil, pass = nil, host = 'localhost', port = nil, socket = nil)
|
2016-03-08 18:06:55 +00:00
|
|
|
@user = user
|
|
|
|
@pass = pass
|
2017-05-06 00:06:28 +00:00
|
|
|
@host = host
|
2017-06-23 15:28:15 +00:00
|
|
|
@port = port
|
|
|
|
@socket = socket
|
2016-03-08 18:06:55 +00:00
|
|
|
init_fallback if user.nil? or pass.nil?
|
|
|
|
skip_resource("Can't run MySQL SQL checks without authentication") if @user.nil? or @pass.nil?
|
2015-11-27 13:02:38 +00:00
|
|
|
end
|
2015-08-28 19:41:48 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def query(q, db = '')
|
|
|
|
# TODO: simple escape, must be handled by a library
|
|
|
|
# that does this securely
|
|
|
|
escaped_query = q.gsub(/\\/, '\\\\').gsub(/"/, '\\"').gsub(/\$/, '\\$')
|
|
|
|
|
|
|
|
# run the query
|
2017-06-23 15:28:15 +00:00
|
|
|
command = "mysql -u#{@user} -p#{@pass}"
|
2017-06-27 10:26:47 +00:00
|
|
|
if !@socket.nil?
|
2017-06-23 15:28:15 +00:00
|
|
|
command += " -S #{@socket}"
|
|
|
|
else
|
|
|
|
command += " -h #{@host}"
|
|
|
|
end
|
|
|
|
command += " --port #{@port}" unless @port.nil?
|
|
|
|
command += " #{db} -s -S #{@socket} -e \"#{escaped_query}\""
|
|
|
|
|
|
|
|
cmd = inspec.command(command)
|
2016-03-08 18:06:55 +00:00
|
|
|
out = cmd.stdout + "\n" + cmd.stderr
|
2017-06-27 10:26:47 +00:00
|
|
|
if out =~ /Can't connect to .* MySQL server/ || out.downcase =~ /^error/
|
2016-03-08 18:06:55 +00:00
|
|
|
# skip this test if the server can't run the query
|
2017-06-27 10:26:47 +00:00
|
|
|
warn("Can't connect to MySQL instance for SQL checks.")
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# return the raw command output
|
|
|
|
cmd
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|
2015-09-26 09:56:49 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
|
|
|
'MySQL Session'
|
|
|
|
end
|
2015-10-12 11:01:58 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
private
|
2015-06-22 13:24:35 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def init_fallback
|
|
|
|
# support debian mysql administration login
|
|
|
|
debian = inspec.command('test -f /etc/mysql/debian.cnf && cat /etc/mysql/debian.cnf').stdout
|
|
|
|
return if debian.empty?
|
2015-09-09 16:37:16 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
user = debian.match(/^\s*user\s*=\s*([^ ]*)\s*$/)
|
|
|
|
pass = debian.match(/^\s*password\s*=\s*([^ ]*)\s*$/)
|
|
|
|
return if user.nil? or pass.nil?
|
|
|
|
@user = user[1]
|
|
|
|
@pass = pass[1]
|
|
|
|
end
|
2015-06-22 13:24:35 +00:00
|
|
|
end
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|