From d0d383c8db0fa24aaa657f5605fe43fd45c989b3 Mon Sep 17 00:00:00 2001 From: William Vu Date: Thu, 31 Jan 2019 22:04:29 -0600 Subject: [PATCH 1/2] Move command_exists? to Msf::Post::Common --- lib/msf/core/post/common.rb | 10 +++++++++- lib/msf/core/post/linux/system.rb | 10 ---------- lib/msf/core/post/solaris/system.rb | 10 ---------- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/lib/msf/core/post/common.rb b/lib/msf/core/post/common.rb index 47ea697b16..1fdfd1e3bf 100644 --- a/lib/msf/core/post/common.rb +++ b/lib/msf/core/post/common.rb @@ -235,6 +235,14 @@ module Msf::Post::Common nil end - private + # + # Checks if the `cmd` is installed on the system + # @return [Boolean] + # + def command_exists?(cmd) + cmd_exec("command -v #{cmd} && echo true").to_s.include? 'true' + rescue + raise "Unable to check if command `#{cmd}' exists" + end end diff --git a/lib/msf/core/post/linux/system.rb b/lib/msf/core/post/linux/system.rb index 47c5968667..dd216f574a 100644 --- a/lib/msf/core/post/linux/system.rb +++ b/lib/msf/core/post/linux/system.rb @@ -225,16 +225,6 @@ module System raise 'Unable to check for gcc' end - # - # Checks if the `cmd` is installed on the system - # @return [Boolean] - # - def command_exists?(cmd) - cmd_exec("command -v #{cmd} && echo true").to_s.include? 'true' - rescue - raise "Unable to check if command `#{cmd}` exists" - end - # # Gets the process id(s) of `program` # @return [Array] diff --git a/lib/msf/core/post/solaris/system.rb b/lib/msf/core/post/solaris/system.rb index 4e3870c5c9..c6524e776a 100644 --- a/lib/msf/core/post/solaris/system.rb +++ b/lib/msf/core/post/solaris/system.rb @@ -118,16 +118,6 @@ module System raise 'Unable to check for gcc' end - # - # Checks if the `cmd` is installed on the system - # @return [Boolean] - # - def command_exists?(cmd) - cmd_exec("command -v #{cmd} && echo true").to_s.include? 'true' - rescue - raise "Unable to check if command `#{cmd}` exists" - end - # # Gets the process id(s) of `program` # @return [Array] From 62560f958119d843f4f65325386878f52804820b Mon Sep 17 00:00:00 2001 From: William Vu Date: Thu, 31 Jan 2019 22:07:30 -0600 Subject: [PATCH 2/2] Add rudimentary Windows support to command_exists? --- lib/msf/core/post/common.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/msf/core/post/common.rb b/lib/msf/core/post/common.rb index 1fdfd1e3bf..1ba1c663b3 100644 --- a/lib/msf/core/post/common.rb +++ b/lib/msf/core/post/common.rb @@ -240,7 +240,13 @@ module Msf::Post::Common # @return [Boolean] # def command_exists?(cmd) - cmd_exec("command -v #{cmd} && echo true").to_s.include? 'true' + if session.platform == 'windows' + # https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/where_1 + # https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/if + cmd_exec("cmd /c where /q #{cmd} & if not errorlevel 1 echo true").to_s.include? 'true' + else + cmd_exec("command -v #{cmd} && echo true").to_s.include? 'true' + end rescue raise "Unable to check if command `#{cmd}' exists" end