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
|
2015-04-09 20:01:23 +00:00
|
|
|
# license: All rights reserved
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
module Inspec::Resources
|
|
|
|
class Processes < Inspec.resource(1)
|
|
|
|
name 'processes'
|
|
|
|
desc 'Use the processes InSpec audit resource to test properties for programs that are running on the system.'
|
|
|
|
example "
|
|
|
|
describe processes('mysqld') do
|
|
|
|
its('list.length') { should eq 1 }
|
|
|
|
its('users') { should eq ['mysql'] }
|
|
|
|
its('states') { should include 'S' }
|
|
|
|
end
|
|
|
|
"
|
2015-04-09 20:01:23 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
attr_reader :list,
|
|
|
|
:users,
|
|
|
|
:states
|
2015-12-07 08:39:48 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def initialize(grep)
|
|
|
|
# turn into a regexp if it isn't one yet
|
|
|
|
if grep.class == String
|
|
|
|
grep = '(/[^/]*)*'+grep if grep[0] != '/'
|
|
|
|
grep = Regexp.new('^' + grep + '(\s|$)')
|
|
|
|
end
|
|
|
|
all_cmds = ps_aux
|
|
|
|
@list = all_cmds.find_all do |hm|
|
|
|
|
hm[:command] =~ grep
|
|
|
|
end
|
2015-12-07 08:39:48 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
{ users: :user,
|
|
|
|
states: :stat }.each do |var, key|
|
|
|
|
instance_variable_set("@#{var}", @list.map { |l| l[key] }.uniq)
|
|
|
|
end
|
2015-12-07 08:39:48 +00:00
|
|
|
end
|
2015-09-09 16:52:27 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
|
|
|
'Processes'
|
|
|
|
end
|
2015-10-12 11:01:58 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
private
|
2015-09-09 16:52:27 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def ps_aux
|
2016-05-09 19:19:56 +00:00
|
|
|
os = inspec.os
|
|
|
|
|
|
|
|
if os.linux?
|
|
|
|
command = 'ps auxZ'
|
|
|
|
regex = /^([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+(.*)$/
|
|
|
|
else
|
|
|
|
command = 'ps aux'
|
|
|
|
regex = /^([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+(.*)$/
|
|
|
|
end
|
|
|
|
build_process_list(command, regex, os)
|
|
|
|
end
|
|
|
|
|
2016-05-13 09:16:45 +00:00
|
|
|
Process = Struct.new(:label, :user, :pid,
|
|
|
|
:cpu, :mem, :vsz,
|
|
|
|
:rss, :tty, :stat,
|
|
|
|
:start, :time, :command)
|
|
|
|
|
|
|
|
def build_process_list(command, regex, os)
|
2016-05-09 19:19:56 +00:00
|
|
|
cmd = inspec.command(command)
|
2016-03-08 18:06:55 +00:00
|
|
|
all = cmd.stdout.split("\n")[1..-1]
|
|
|
|
return [] if all.nil?
|
2015-09-09 16:52:27 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
lines = all.map do |line|
|
2016-05-09 19:19:56 +00:00
|
|
|
line.match(regex)
|
2016-03-08 18:06:55 +00:00
|
|
|
end.compact
|
2015-09-09 16:52:27 +00:00
|
|
|
|
2016-05-13 09:16:45 +00:00
|
|
|
lines.map do |m|
|
|
|
|
a = m.to_a[1..-1] # grab all matching groups
|
|
|
|
a.unshift(nil) unless os.linux?
|
|
|
|
a[2] = a[2].to_i
|
|
|
|
a[5] = a[5].to_i
|
|
|
|
a[6] = a[6].to_i
|
|
|
|
Process.new(*a)
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2015-04-09 20:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
2015-08-28 19:27:35 +00:00
|
|
|
end
|