Skip to content

Commit 13f5c22

Browse files
committed
Land rapid7#3129 - Fix 2782 with 2961 and stop stack-tracing download_exec
2 parents 0a141f1 + 3d36818 commit 13f5c22

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

lib/msf/core/post/common.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,55 @@ def report_vm(vm)
153153
report_host(vm_data)
154154
end
155155

156+
#
157+
# Returns the value of the environment variable +env+
158+
#
159+
def get_env(env)
160+
case session.type
161+
when /meterpreter/
162+
return session.sys.config.getenv(env)
163+
when /shell/
164+
if session.platform =~ /win/
165+
if env[0,1] == '%'
166+
unless env[-1,1] == '%'
167+
env << '%'
168+
end
169+
else
170+
env = "%#{env}%"
171+
end
172+
173+
return cmd_exec("echo #{env}")
174+
else
175+
unless env[0,1] == '$'
176+
env = "$#{env}"
177+
end
178+
179+
return cmd_exec("echo \"#{env}\"")
180+
end
181+
end
182+
183+
nil
184+
end
185+
186+
#
187+
# Returns a hash of environment variables +envs+
188+
#
189+
def get_envs(*envs)
190+
case session.type
191+
when /meterpreter/
192+
return session.sys.config.getenvs(*envs)
193+
when /shell/
194+
result = {}
195+
envs.each do |env|
196+
res = get_env(env)
197+
result[env] = res unless res.blank?
198+
end
199+
200+
return result
201+
end
202+
203+
nil
204+
end
205+
156206
end
207+

modules/post/linux/manage/download_exec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,19 @@ def cmd_exec_vprint(cmd)
4545
end
4646

4747
def exists_exe?(exe)
48-
path = session.sys.config.getenv("PATH")
48+
vprint_status "Searching for #{exe} in the current $PATH..."
49+
path = get_env("PATH")
4950
if path.nil? or path.empty?
5051
return false
52+
vprint_error "No local $PATH set!"
53+
else
54+
vprint_status "$PATH is #{path.strip!}"
5155
end
5256

5357
path.split(":").each{ |p|
54-
return true if file_exist?(p + "/" + exe)
58+
full_path = p + "/" + exe
59+
vprint_status "Searching for '#{full_path}' ..."
60+
return true if file_exist?(full_path)
5561
}
5662

5763
return false

test/modules/post/test/get_env.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
$:.push "test/lib" unless $:.include? "test/lib"
3+
require 'module_test'
4+
5+
#load 'test/lib/module_test.rb'
6+
#load 'lib/rex/text.rb'
7+
#load 'lib/msf/core/post/common.rb'
8+
9+
class Metasploit4 < Msf::Post
10+
11+
include Msf::ModuleTest::PostTest
12+
include Msf::Post::Common
13+
14+
def initialize(info={})
15+
super( update_info( info,
16+
'Name' => 'Testing Get Envs',
17+
'Description' => %q{ This module will test Post::Common get envs API methods },
18+
'License' => MSF_LICENSE,
19+
'Author' => [ 'Ben Campbell'],
20+
'Platform' => [ 'windows', 'linux', 'java', 'python' ],
21+
'SessionTypes' => [ 'meterpreter', 'shell' ]
22+
))
23+
end
24+
25+
def test_get_env_windows
26+
if session.platform =~ /win/i
27+
it "should return windows path" do
28+
path = get_env('WINDIR')
29+
path =~ /windows/i
30+
end
31+
32+
it "should handle % signs" do
33+
path = get_env('%WINDIR%')
34+
path =~ /windows/i
35+
end
36+
end
37+
end
38+
39+
def test_get_env_nix
40+
unless session.platform =~ /win/i
41+
it "should return user" do
42+
user = get_env('USER')
43+
!user.blank?
44+
end
45+
46+
it "should handle $ sign" do
47+
user = get_env('$USER')
48+
!user.blank?
49+
end
50+
end
51+
end
52+
53+
def test_get_envs
54+
it "should return multiple envs" do
55+
res = get_envs('PATH','USERNAME')
56+
!res['PATH'].blank? && !res['USERNAME'].blank?
57+
end
58+
end
59+
60+
end
61+

0 commit comments

Comments
 (0)