Skip to content

Commit

Permalink
Included tests for create_process API
Browse files Browse the repository at this point in the history
  • Loading branch information
smashery committed Sep 24, 2024
1 parent f74ca43 commit 5c43cf5
Showing 1 changed file with 121 additions and 97 deletions.
218 changes: 121 additions & 97 deletions test/modules/post/test/cmd_exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,38 @@ def initialize(info = {})
)
end

def upload_show_args_binary
def upload_show_args_binary(details)
print_status 'Uploading precompiled binaries'
upload_file(show_args_binary[:path], "data/cmd_exec/#{show_args_binary[:path]}")
upload_file(details[:upload_path], "data/cmd_exec/#{details[:path]}")
unless session.platform.eql?('windows')
chmod(show_args_binary[:path])
chmod(details[:upload_path])
end
end

def show_args_binary_space
result = show_args_binary_base
result[:upload_path] = result[:path].gsub('_',' ')
result[:cmd] = result[:cmd].gsub('_',' ')

result
end

def show_args_binary_special
result = show_args_binary_base
result[:upload_path] = result[:path].gsub('show_args','~!@#$%^&*(){}')
result[:cmd] = result[:cmd].gsub('show_args','~!@#$%^&*(){}')

result
end

def show_args_binary
result = show_args_binary_base
result[:upload_path] = result[:path]

result
end

def show_args_binary_base
if session.platform == 'linux' || session.platform == 'unix'
{ path: 'show_args_linux', cmd: './show_args_linux' }
elsif session.platform == 'osx'
Expand All @@ -56,7 +79,7 @@ def valid_show_args_response?(output, expected:)

# Match the binary name, to support the binary name containing relative or absolute paths, i.e.
# "show_args.exe\r\none\r\ntwo",
match = output_binary.match?(expected[0]) && output_args == expected[1..]
match = output_binary.include?(expected[0]) && output_args == expected[1..]
if !match
vprint_status("#{__method__}: expected: #{expected.inspect} - actual: #{output_lines.inspect}")
end
Expand All @@ -68,7 +91,7 @@ def test_cmd_exec
# we are inconsistent reporting windows session types
windows_strings = ['windows', 'win']
vprint_status("Starting cmd_exec tests")
upload_show_args_binary
upload_show_args_binary(show_args_binary)

it "should return the result of echo" do
test_string = Rex::Text.rand_text_alpha(4)
Expand Down Expand Up @@ -189,96 +212,97 @@ def test_cmd_exec_stderr
end
end

# TODO: These tests are in preparation for Smashery's create process API
# def test_create_process
# upload_show_args_binary
#
# test_string = Rex::Text.rand_text_alpha(4)
#
# it 'should accept blank strings and return the create_process output' do
# if session.arch.eql?("php")
# # TODO: Fix this functionality
#
# vprint_status("test skipped for PHP - functionality not correct")
# true
# end
# output = create_process(show_args_binary[:cmd], args: [test_string, '', test_string, '', test_string])
# valid_show_args_response?(output, expected: [show_args_binary[:path], test_string, '', test_string, '', test_string])
# end
#
# it 'should accept multiple args and return the create_process output' do
# output = create_process(show_args_binary[:cmd], args: [test_string, test_string])
# valid_show_args_response?(output, expected: [show_args_binary[:path], test_string, test_string])
# end
#
# it 'should accept spaces and return the create_process output' do
# output = create_process(show_args_binary[:cmd], args: ['with spaces'])
# valid_show_args_response?(output, expected: [show_args_binary[:path], 'with spaces'])
# end
#
# it 'should accept environment variables and return the create_process output' do
# output = create_process(show_args_binary[:cmd], args: ['$PATH'])
# valid_show_args_response?(output, expected: [show_args_binary[:path], '$PATH'])
# end
#
# it 'should accept environment variables within a string and return the create_process output' do
# output = create_process(show_args_binary[:cmd], args: ["it's $PATH"])
# valid_show_args_response?(output, expected: [show_args_binary[:path], "it's $PATH"])
# end
#
# it 'should accept special characters and return the create_process output' do
# if session.platform.eql? 'windows'
# # TODO: Fix this functionality
# vprint_status('test skipped for Windows - functionality not correct')
# true
# end
# output = create_process(show_args_binary[:cmd], args: ['~!@#$%^&*(){`1234567890[]",.\'<>'])
# valid_show_args_response?(output, expected: [show_args_binary[:path], '~!@#$%^&*(){`1234567890[]",.\'<>'])
# end
#
# it 'should accept command line commands and return the create_process output' do
# if session.arch.eql?("php")
# # TODO: Fix this functionality
# vprint_status("test skipped for PHP - functionality not correct")
# true
# end
#
# output = create_process(show_args_binary[:cmd], args: ['run&echo'])
# valid_show_args_response?(output, expected: [show_args_binary[:path], 'run&echo'])
# end
#
# it 'should accept semicolons to separate multiple command on a single line and return the create_process output' do
# if session.arch.eql?("php")
# # TODO: Fix this functionality
# vprint_status("test skipped for PHP - functionality not correct")
# true
# end
#
# output = create_process(show_args_binary[:cmd], args: ['run&echo;test'])
# valid_show_args_response?(output, expected: [show_args_binary[:path], 'run&echo;test'])
# end
#
# it 'should accept spaces in the filename and return the create_process output' do
# if session.platform.eql? 'windows'
# # TODO: Fix this functionality
# vprint_status('test skipped for Windows CMD - functionality not correct')
# true
# end
#
# output = create_process('./show_args file', args: [test_string, test_string])
# valid_show_args_response?(output, expected: ['./show_args file', test_string, test_string])
# end
#
# it 'should accept special characters in the filename and return the create_process output' do
# if session.platform.eql? 'windows'
# # TODO: Fix this functionality
# vprint_status('test skipped for Windows CMD - functionality not correct')
# true
# end
#
# output = create_process('./~!@#$%^&*(){}', args: [test_string, test_string])
# valid_show_args_response?(output, expected: ['./~!@#$%^&*(){}', test_string, test_string])
# end
# end
# end
def test_create_process
space_filename = 'showargs file'
upload_show_args_binary(show_args_binary)
upload_show_args_binary(show_args_binary_space)
upload_show_args_binary(show_args_binary_special)

test_string = Rex::Text.rand_text_alpha(4)

it 'should accept blank strings and return the create_process output' do
if session.arch.eql?("php")
# TODO: Fix this functionality

vprint_status("test skipped for PHP - functionality not correct")
true
end
output = create_process(show_args_binary[:cmd], args: [test_string, '', test_string, '', test_string])
valid_show_args_response?(output, expected: [show_args_binary[:upload_path], test_string, '', test_string, '', test_string])
end

it 'should accept multiple args and return the create_process output' do
output = create_process(show_args_binary[:cmd], args: [test_string, test_string])
valid_show_args_response?(output, expected: [show_args_binary[:upload_path], test_string, test_string])
end

it 'should accept spaces and return the create_process output' do
output = create_process(show_args_binary[:cmd], args: ['with spaces'])
valid_show_args_response?(output, expected: [show_args_binary[:upload_path], 'with spaces'])
end

it 'should accept environment variables and return the create_process output' do
output = create_process(show_args_binary[:cmd], args: ['$PATH'])
valid_show_args_response?(output, expected: [show_args_binary[:upload_path], '$PATH'])
end

it 'should accept environment variables within a string and return the create_process output' do
output = create_process(show_args_binary[:cmd], args: ["it's $PATH"])
valid_show_args_response?(output, expected: [show_args_binary[:upload_path], "it's $PATH"])
end

it 'should accept special characters and return the create_process output' do
if session.platform.eql? 'windows'
# TODO: Fix this functionality
vprint_status('test skipped for Windows - functionality not correct')
true
end
output = create_process(show_args_binary[:cmd], args: ['~!@#$%^&*(){`1234567890[]",.\'<>'])
valid_show_args_response?(output, expected: [show_args_binary[:upload_path], '~!@#$%^&*(){`1234567890[]",.\'<>'])
end

it 'should accept command line commands and return the create_process output' do
if session.arch.eql?("php")
# TODO: Fix this functionality
vprint_status("test skipped for PHP - functionality not correct")
true
end

output = create_process(show_args_binary[:cmd], args: ['run&echo'])
valid_show_args_response?(output, expected: [show_args_binary[:upload_path], 'run&echo'])
end

it 'should accept semicolons to separate multiple command on a single line and return the create_process output' do
if session.arch.eql?("php")
# TODO: Fix this functionality
vprint_status("test skipped for PHP - functionality not correct")
true
end

output = create_process(show_args_binary[:cmd], args: ['run&echo;test'])
valid_show_args_response?(output, expected: [show_args_binary[:upload_path], 'run&echo;test'])
end

it 'should accept spaces in the filename and return the create_process output' do
if session.platform.eql? 'windows'
# TODO: Fix this functionality
vprint_status('test skipped for Windows CMD - functionality not correct')
true
end

output = create_process(show_args_binary_space[:cmd], args: [test_string, test_string])
valid_show_args_response?(output, expected: [show_args_binary_space[:cmd], test_string, test_string])
end

it 'should accept special characters in the filename and return the create_process output' do
if session.platform.eql? 'windows'
# TODO: Fix this functionality
vprint_status('test skipped for Windows CMD - functionality not correct')
true
end

output = create_process(show_args_binary_special[:cmd], args: [test_string, test_string])
valid_show_args_response?(output, expected: [show_args_binary_special[:cmd], test_string, test_string])
end
end
end

0 comments on commit 5c43cf5

Please sign in to comment.