inspec/lib/resources/processes.rb

74 lines
1.7 KiB
Ruby
Raw Normal View History

# 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
# license: All rights reserved
2015-10-26 03:04:18 +00:00
class Processes < Inspec.resource(1)
name 'processes'
2015-11-27 13:02:38 +00:00
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' }
2015-11-27 13:02:38 +00:00
end
"
attr_reader :list,
:users,
:states
def initialize(grep)
2015-07-26 10:30:12 +00:00
# turn into a regexp if it isn't one yet
if grep.class == String
grep = '(/[^/]*)*'+grep if grep[0] != '/'
2015-09-05 14:07:54 +00:00
grep = Regexp.new('^' + grep + '(\s|$)')
end
all_cmds = ps_aux
@list = all_cmds.find_all do |hm|
hm[:command] =~ grep
end
{ users: :user,
states: :stat }.each do |var, key|
instance_variable_set("@#{var}", @list.map { |l| l[key] }.uniq)
end
end
def to_s
'Processes'
end
private
def ps_aux
2015-07-26 10:30:12 +00:00
# get all running processes
2015-10-26 03:04:18 +00:00
cmd = inspec.command('ps aux')
2015-07-26 10:30:12 +00:00
all = cmd.stdout.split("\n")[1..-1]
2015-10-26 15:47:54 +00:00
return [] if all.nil?
lines = all.map do |line|
2015-07-26 10:30:12 +00:00
# user 32296 0.0 0.0 42592 7972 pts/15 Ss+ Apr06 0:00 zsh
line.match(/^([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+([^ ]+)\s+(.*)$/)
end.compact
lines.map do |m|
2015-07-26 10:30:12 +00:00
{
user: m[1],
pid: m[2],
cpu: m[3],
mem: m[4],
vsz: m[5],
rss: m[6],
tty: m[7],
stat: m[8],
start: m[9],
time: m[10],
command: m[11],
2015-07-26 10:30:12 +00:00
}
end
end
end