2015-06-07 15:09:02 +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: Christoph Hartmann
|
2015-06-07 15:09:02 +00:00
|
|
|
# license: All rights reserved
|
|
|
|
|
2015-04-17 13:37:17 +00:00
|
|
|
require 'json'
|
|
|
|
|
2015-09-05 20:36:32 +00:00
|
|
|
# Usage:
|
|
|
|
# describe registry_key('Task Scheduler','HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule') do
|
|
|
|
# its('Start') { should eq 2 }
|
|
|
|
# end
|
|
|
|
|
2015-10-26 03:04:18 +00:00
|
|
|
class RegistryKey < Inspec.resource(1)
|
2015-08-28 18:52:22 +00:00
|
|
|
name 'registry_key'
|
2015-04-17 13:37:17 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
attr_accessor :reg_key
|
2015-04-17 13:37:17 +00:00
|
|
|
|
2015-08-28 18:52:22 +00:00
|
|
|
def initialize(name, reg_key = nil)
|
|
|
|
# if we have one parameter, we use it as name
|
2015-09-09 16:52:27 +00:00
|
|
|
reg_key ||= name
|
2015-08-28 18:52:22 +00:00
|
|
|
@name = name
|
|
|
|
@reg_key = reg_key
|
|
|
|
end
|
|
|
|
|
2015-09-09 16:52:27 +00:00
|
|
|
def registry_value(path, key)
|
2015-07-26 10:30:12 +00:00
|
|
|
cmd = "(Get-Item 'Registry::#{path}').GetValue('#{key}')"
|
2015-10-26 03:04:18 +00:00
|
|
|
command_result ||= inspec.command(cmd)
|
2015-09-05 14:07:54 +00:00
|
|
|
val = { exit_code: command_result.exit_status.to_i, data: command_result.stdout }
|
2015-07-26 10:30:12 +00:00
|
|
|
val
|
|
|
|
end
|
2015-04-17 13:37:17 +00:00
|
|
|
|
2015-09-09 16:52:27 +00:00
|
|
|
def convert_value(value)
|
2015-07-26 10:30:12 +00:00
|
|
|
val = value.strip
|
|
|
|
val = val.to_i if val.match(/^\d+$/)
|
2015-09-04 07:59:30 +00:00
|
|
|
val
|
2015-07-26 10:30:12 +00:00
|
|
|
end
|
2015-04-17 13:37:17 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
# returns nil, if not existant or value
|
|
|
|
def method_missing(meth)
|
|
|
|
# get data
|
2015-09-09 16:52:27 +00:00
|
|
|
val = registry_value(@reg_key, meth)
|
2015-04-17 13:37:17 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
# verify data
|
2015-11-13 00:03:15 +00:00
|
|
|
if val[:exit_code] == 0
|
2015-09-09 16:52:27 +00:00
|
|
|
return convert_value(val[:data])
|
2015-07-26 10:30:12 +00:00
|
|
|
else
|
2015-09-04 07:59:30 +00:00
|
|
|
return nil
|
2015-07-26 10:30:12 +00:00
|
|
|
end
|
|
|
|
end
|
2015-04-17 13:37:17 +00:00
|
|
|
|
2015-07-26 10:30:12 +00:00
|
|
|
def to_s
|
|
|
|
"Registry Key #{@name}"
|
|
|
|
end
|
|
|
|
end
|