diff --git a/lib/msf/core/post/common.rb b/lib/msf/core/post/common.rb index 7bb6195e33..ed4fe536f6 100644 --- a/lib/msf/core/post/common.rb +++ b/lib/msf/core/post/common.rb @@ -153,4 +153,55 @@ module Msf::Post::Common report_host(vm_data) end + # + # Returns the value of the environment variable +env+ + # + def get_env(env) + case session.type + when /meterpreter/ + return session.sys.config.getenv(env) + when /shell/ + if session.platform =~ /win/ + if env[0,1] == '%' + unless env[-1,1] == '%' + env << '%' + end + else + env = "%#{env}%" + end + + return cmd_exec("echo #{env}") + else + unless env[0,1] == '$' + env = "$#{env}" + end + + return cmd_exec("echo \"#{env}\"") + end + end + + nil + end + + # + # Returns a hash of environment variables +envs+ + # + def get_envs(*envs) + case session.type + when /meterpreter/ + return session.sys.config.getenvs(*envs) + when /shell/ + result = {} + envs.each do |env| + res = get_env(env) + result[env] = res unless res.blank? + end + + return result + end + + nil + end + end + diff --git a/modules/post/linux/manage/download_exec.rb b/modules/post/linux/manage/download_exec.rb index 71dd963b7f..ec4d50729b 100644 --- a/modules/post/linux/manage/download_exec.rb +++ b/modules/post/linux/manage/download_exec.rb @@ -45,13 +45,19 @@ class Metasploit3 < Msf::Post end def exists_exe?(exe) - path = session.sys.config.getenv("PATH") + vprint_status "Searching for #{exe} in the current $PATH..." + path = get_env("PATH") if path.nil? or path.empty? return false + vprint_error "No local $PATH set!" + else + vprint_status "$PATH is #{path.strip!}" end path.split(":").each{ |p| - return true if file_exist?(p + "/" + exe) + full_path = p + "/" + exe + vprint_status "Searching for '#{full_path}' ..." + return true if file_exist?(full_path) } return false diff --git a/test/modules/post/test/get_env.rb b/test/modules/post/test/get_env.rb new file mode 100644 index 0000000000..6fdbaae8b6 --- /dev/null +++ b/test/modules/post/test/get_env.rb @@ -0,0 +1,61 @@ + +$:.push "test/lib" unless $:.include? "test/lib" +require 'module_test' + +#load 'test/lib/module_test.rb' +#load 'lib/rex/text.rb' +#load 'lib/msf/core/post/common.rb' + +class Metasploit4 < Msf::Post + + include Msf::ModuleTest::PostTest + include Msf::Post::Common + + def initialize(info={}) + super( update_info( info, + 'Name' => 'Testing Get Envs', + 'Description' => %q{ This module will test Post::Common get envs API methods }, + 'License' => MSF_LICENSE, + 'Author' => [ 'Ben Campbell'], + 'Platform' => [ 'windows', 'linux', 'java', 'python' ], + 'SessionTypes' => [ 'meterpreter', 'shell' ] + )) + end + + def test_get_env_windows + if session.platform =~ /win/i + it "should return windows path" do + path = get_env('WINDIR') + path =~ /windows/i + end + + it "should handle % signs" do + path = get_env('%WINDIR%') + path =~ /windows/i + end + end + end + + def test_get_env_nix + unless session.platform =~ /win/i + it "should return user" do + user = get_env('USER') + !user.blank? + end + + it "should handle $ sign" do + user = get_env('$USER') + !user.blank? + end + end + end + + def test_get_envs + it "should return multiple envs" do + res = get_envs('PATH','USERNAME') + !res['PATH'].blank? && !res['USERNAME'].blank? + end + end + +end +