2015-04-09 22:01:23 +02:00
|
|
|
# encoding: utf-8
|
2015-07-15 15:15:18 +02:00
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
2015-10-06 18:55:44 +02:00
|
|
|
# author: Dominik Richter
|
|
|
|
# author: Christoph Hartmann
|
2017-05-05 20:06:28 -04:00
|
|
|
# author: Aaron Lippold
|
2015-04-09 22:01:23 +02:00
|
|
|
|
2016-03-08 13:06:55 -05: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-05 20:06:28 -04:00
|
|
|
sql = mysql_session('my_user','password','host')
|
2016-03-08 13:06:55 -05:00
|
|
|
describe sql.query('show databases like \'test\';') do
|
2016-05-03 23:14:33 +01:00
|
|
|
its('stdout') { should_not match(/test/) }
|
2016-03-08 13:06:55 -05:00
|
|
|
end
|
|
|
|
"
|
|
|
|
|
2017-06-23 10:28:15 -05:00
|
|
|
def initialize(user = nil, pass = nil, host = 'localhost', port = nil, socket = nil)
|
2016-03-08 13:06:55 -05:00
|
|
|
@user = user
|
|
|
|
@pass = pass
|
2017-05-05 20:06:28 -04:00
|
|
|
@host = host
|
2017-06-23 10:28:15 -05:00
|
|
|
@port = port
|
|
|
|
@socket = socket
|
2016-03-08 13:06:55 -05: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 14:02:38 +01:00
|
|
|
end
|
2015-08-28 12:41:48 -07:00
|
|
|
|
2016-03-08 13:06:55 -05: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 10:28:15 -05:00
|
|
|
command = "mysql -u#{@user} -p#{@pass}"
|
2017-06-27 03:26:47 -07:00
|
|
|
if !@socket.nil?
|
2017-06-23 10:28:15 -05:00
|
|
|
command += " -S #{@socket}"
|
|
|
|
else
|
|
|
|
command += " -h #{@host}"
|
|
|
|
end
|
|
|
|
command += " --port #{@port}" unless @port.nil?
|
2017-07-18 17:28:56 +02:00
|
|
|
command += " #{db} -s -e \"#{escaped_query}\""
|
2017-06-23 10:28:15 -05:00
|
|
|
|
|
|
|
cmd = inspec.command(command)
|
2016-03-08 13:06:55 -05:00
|
|
|
out = cmd.stdout + "\n" + cmd.stderr
|
2017-06-27 03:26:47 -07:00
|
|
|
if out =~ /Can't connect to .* MySQL server/ || out.downcase =~ /^error/
|
2016-03-08 13:06:55 -05:00
|
|
|
# skip this test if the server can't run the query
|
2017-06-27 03:26:47 -07:00
|
|
|
warn("Can't connect to MySQL instance for SQL checks.")
|
2016-03-08 13:06:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# return the raw command output
|
|
|
|
cmd
|
2015-04-09 22:01:23 +02:00
|
|
|
end
|
2015-09-26 11:56:49 +02:00
|
|
|
|
2016-03-08 13:06:55 -05:00
|
|
|
def to_s
|
|
|
|
'MySQL Session'
|
|
|
|
end
|
2015-10-12 13:01:58 +02:00
|
|
|
|
2016-03-08 13:06:55 -05:00
|
|
|
private
|
2015-06-22 15:24:35 +02:00
|
|
|
|
2016-03-08 13:06:55 -05: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 18:37:16 +02:00
|
|
|
|
2016-03-08 13:06:55 -05: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 15:24:35 +02:00
|
|
|
end
|
2015-04-09 22:01:23 +02:00
|
|
|
end
|