-
Notifications
You must be signed in to change notification settings - Fork 13.8k
/
cmd_exec.rb
114 lines (102 loc) · 3.84 KB
/
cmd_exec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
require 'rex'
lib = File.join(Msf::Config.install_root, "test", "lib")
$LOAD_PATH.push(lib) unless $LOAD_PATH.include?(lib)
require 'module_test'
class MetasploitModule < Msf::Post
include Msf::ModuleTest::PostTest
include Msf::Post::File
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Meterpreter cmd_exec test',
'Description' => %q( This module will test the meterpreter cmd_exec API ),
'License' => MSF_LICENSE,
'Platform' => [ 'windows', 'linux', 'unix', 'java', 'osx' ],
'SessionTypes' => ['meterpreter', 'shell', 'powershell']
)
)
end
def test_cmd_exec
# we are inconsistent reporting windows session types
windows_strings = ['windows', 'win']
vprint_status("Starting cmd_exec tests")
it "should return the result of echo" do
test_string = Rex::Text.rand_text_alpha(4)
if windows_strings.include? session.platform and session.type.eql? 'meterpreter'
vprint_status("meterpreter?")
output = cmd_exec('cmd.exe', "/c echo #{test_string}")
else
output = cmd_exec("echo #{test_string}")
end
output == test_string
end
# Powershell supports this, but not windows meterpreter (unsure about windows shell)
if not windows_strings.include? session.platform or session.type.eql? 'powershell'
it "should return the full response after sleeping" do
test_string = Rex::Text.rand_text_alpha(4)
output = cmd_exec("sleep 1; echo #{test_string}")
output == test_string
end
it "should return the full response after sleeping" do
test_string = Rex::Text.rand_text_alpha(4)
test_string2 = Rex::Text.rand_text_alpha(4)
output = cmd_exec("echo #{test_string}; sleep 1; echo #{test_string2}")
output.delete("\r") == "#{test_string}\n#{test_string2}"
end
it "should return the result of echo 10 times" do
10.times do
test_string = Rex::Text.rand_text_alpha(4)
output = cmd_exec("echo #{test_string}")
return false unless output == test_string
end
true
end
else
vprint_status("Session does not support sleep, skipping sleep tests")
end
vprint_status("Finished cmd_exec tests")
end
def test_cmd_exec_quotes
vprint_status("Starting cmd_exec quote tests")
it "should return the result of echo with single quotes" do
test_string = Rex::Text.rand_text_alpha(4)
if session.platform.eql? 'windows' and session.arch == ARCH_PYTHON
output = cmd_exec("cmd.exe", "/c echo \"#{test_string}\"")
output == test_string
elsif session.platform.eql? 'windows'
output = cmd_exec("cmd.exe", "/c echo '#{test_string}'")
output == "'" + test_string + "'"
else
output = cmd_exec("echo '#{test_string}'")
output == test_string
end
end
it "should return the result of echo with double quotes" do
test_string = Rex::Text.rand_text_alpha(4)
if session.platform.eql? 'windows' and session.arch == ARCH_PYTHON
output = cmd_exec("cmd.exe", "/c echo \"#{test_string}\"")
output == test_string
elsif session.platform.eql? 'windows'
output = cmd_exec("cmd.exe", "/c echo \"#{test_string}\"")
output == "\"" + test_string + "\""
else
output = cmd_exec("echo \"#{test_string}\"")
output == test_string
end
end
end
def test_cmd_exec_stderr
vprint_status("Starting cmd_exec stderr tests")
it "should return the stderr output" do
test_string = Rex::Text.rand_text_alpha(4)
if session.platform.eql? 'windows'
output = cmd_exec("cmd.exe", "/c echo #{test_string} 1>&2")
output.rstrip == test_string
else
output = cmd_exec("echo #{test_string} 1>&2")
output == test_string
end
end
end
end