Skip to content

Commit 4d3f871

Browse files
author
Tod Beardsley
committed
Land rapid7#2961, get_env and get_envs Post mixin
This unbreaks the changes introduced by rapid7#2782 by introducing get_env and get_envs for shell sessions (not just meterpreter sessions).
2 parents b79920b + a5cb03e commit 4d3f871

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
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+

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)