From 39b4f0a7a5a3817cd26a8eed90569c8eba0a5aed Mon Sep 17 00:00:00 2001 From: Sonu Saha Date: Wed, 1 Jun 2022 13:09:30 +0530 Subject: [PATCH] CFINSPEC-291: Initial fix to consider processes without path on Windows Signed-off-by: Sonu Saha --- lib/inspec/resources/processes.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/inspec/resources/processes.rb b/lib/inspec/resources/processes.rb index 0e4772535..a7a679277 100644 --- a/lib/inspec/resources/processes.rb +++ b/lib/inspec/resources/processes.rb @@ -41,9 +41,10 @@ module Inspec::Resources grep = Regexp.new(grep) end + # require "byebug"; byebug all_cmds = ps_axo @list = all_cmds.find_all do |hm| - hm[:command] =~ grep + hm[:command] =~ grep || hm[:process_name] =~ grep end end @@ -84,6 +85,7 @@ module Inspec::Resources .register_column(:time, field: "time") .register_column(:users, field: "user") .register_column(:commands, field: "command") + .register_column(:process_name, field: "process_name") .install_filter_methods_on_resource(self, :filtered_processes) private @@ -98,9 +100,9 @@ module Inspec::Resources if os.linux? command, regex, field_map = ps_configuration_for_linux elsif os.windows? - command = '$Proc = Get-Process -IncludeUserName | Where-Object {$_.Path -ne $null } | Select-Object PriorityClass,Id,CPU,PM,VirtualMemorySize,NPM,SessionId,Responding,StartTime,TotalProcessorTime,UserName,Path | ConvertTo-Csv -NoTypeInformation;$Proc.Replace("""","").Replace("`r`n","`n")' + command = '$Proc = Get-Process -IncludeUserName | Select-Object PriorityClass,Id,CPU,PM,VirtualMemorySize,NPM,SessionId,Responding,StartTime,TotalProcessorTime,UserName,Path,ProcessName | ConvertTo-Csv -NoTypeInformation;$Proc.Replace("""","").Replace("`r`n","`n")' # Wanted to use /(?:^|,)([^,]*)/; works on rubular.com not sure why here? - regex = /^(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+),(.+)$/ + regex = /^(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*),(.*)$/ field_map = { pid: 2, cpu: 3, @@ -113,6 +115,7 @@ module Inspec::Resources time: 10, user: 11, command: 12, + process_name: 13, } else command = "ps axo pid,pcpu,pmem,vsz,rss,tty,stat,start,time,user,command" @@ -187,6 +190,7 @@ module Inspec::Resources end def build_process_list(command, regex, field_map) + # require "byebug"; byebug cmd = inspec.command(command) all = cmd.stdout.split("\n")[1..-1] return [] if all.nil? @@ -204,7 +208,7 @@ module Inspec::Resources # build a hash of process data that we'll turn into a struct for FilterTable process_data = {} - %i{label pid cpu mem vsz rss tty stat start time user command}.each do |param| + %i{label pid cpu mem vsz rss tty stat start time user command process_name}.each do |param| # not all operating systems support all fields, so skip the field if we don't have it process_data[param] = line[field_map[param]] if field_map.key?(param) end