Skip to content

Commit

Permalink
Drys out some code
Browse files Browse the repository at this point in the history
  • Loading branch information
cgranleese-r7 committed Sep 20, 2024
1 parent 5ef3dfd commit 0d7166a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 158 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/command_shell_acceptance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ jobs:
env:
RAILS_ENV: test
HOST_RUNNER_IMAGE: ${{ matrix.os }}
COMMAND_SHELL: ${{ matrix.command_shell.name }}
COMMAND_SHELL_RUNTIME_VERSION: ${{ matrix.command_shell.runtime_version }}
SESSION: 'command_shell/${{ matrix.command_shell.name }}'
SESSION_RUNTIME_VERSION: ${{ matrix.command_shell.runtime_version }}
BUNDLE_WITHOUT: "coverage development"

name: ${{ matrix.command_shell.name }} ${{ matrix.command_shell.runtime_version }} ${{ matrix.os }}
Expand Down
44 changes: 22 additions & 22 deletions spec/acceptance/command_shell_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
include_context 'wait_for_expect'

# Tests to ensure that CMD/Powershell/Linux is consistent across all implementations/operation systems
COMMAND_SHELL_PAYLOADS = Acceptance::CommandShell.with_command_shell_name_merged(
COMMAND_SHELL_PAYLOADS = Acceptance::Session.with_session_name_merged(
{
powershell: Acceptance::CommandShell::POWERSHELL,
cmd: Acceptance::CommandShell::CMD,
linux: Acceptance::CommandShell::LINUX
powershell: Acceptance::Session::POWERSHELL,
cmd: Acceptance::Session::CMD,
linux: Acceptance::Session::LINUX
}
)

allure_test_environment = AllureRspec.configuration.environment_properties

let_it_be(:current_platform) { Acceptance::CommandShell::current_platform }
let_it_be(:current_platform) { Acceptance::Session::current_platform }

# @!attribute [r] port_allocator
# @return [Acceptance::PortAllocator]
Expand Down Expand Up @@ -54,10 +54,10 @@
describe command_shell_runtime_name, focus: command_shell_config[:focus] do
command_shell_config[:payloads].each.with_index do |payload_config, payload_config_index|
describe(
Acceptance::CommandShell.human_name_for_payload(payload_config).to_s,
Acceptance::Session.human_name_for_payload(payload_config).to_s,
if: (
Acceptance::CommandShell.run_command_shell?(command_shell_config) &&
Acceptance::CommandShell.supported_platform?(payload_config)
Acceptance::Session.run_session?(command_shell_config) &&
Acceptance::Session.supported_platform?(payload_config)
)
) do
let(:payload) { Acceptance::Payload.new(payload_config) }
Expand Down Expand Up @@ -182,18 +182,18 @@ def get_file_attachment_contents(path)
console.reset
end

context "#{Acceptance::CommandShell.current_platform}" do
describe "#{Acceptance::CommandShell.current_platform}/#{command_shell_runtime_name} command shell successfully opens a session for the #{payload_config[:name].inspect} payload" do
context "#{Acceptance::Session.current_platform}" do
describe "#{Acceptance::Session.current_platform}/#{command_shell_runtime_name} command shell successfully opens a session for the #{payload_config[:name].inspect} payload" do
it(
"exposes available metasploit commands",
if: (
# Assume that regardless of payload, staged/unstaged/etc, the command shell will have the same commands available
# So only run this test when config_index == 0
payload_config_index == 0 && Acceptance::CommandShell.supported_platform?(payload_config)
# Run if ENV['METERPRETER'] = 'java php' etc
Acceptance::CommandShell.run_command_shell?(command_shell_config) &&
payload_config_index == 0 && Acceptance::Session.supported_platform?(payload_config)
# Run if ENV['SESSION'] = 'java php' etc
Acceptance::Session.run_session?(command_shell_config) &&
# Only run payloads / tests, if the host machine can run them
Acceptance::CommandShell.supported_platform?(payload_config)
Acceptance::Session.supported_platform?(payload_config)
)
) do
begin
Expand Down Expand Up @@ -329,16 +329,16 @@ def get_file_attachment_contents(path)
command_shell_config[:module_tests].each do |module_test|
describe module_test[:name].to_s, focus: module_test[:focus] do
it(
"#{Acceptance::CommandShell.current_platform}/#{command_shell_runtime_name} command shell successfully opens a session for the #{payload_config[:name].inspect} payload and passes the #{module_test[:name].inspect} tests",
"#{Acceptance::Session.current_platform}/#{command_shell_runtime_name} command shell successfully opens a session for the #{payload_config[:name].inspect} payload and passes the #{module_test[:name].inspect} tests",
if: (
Acceptance::CommandShell.run_command_shell?(command_shell_config) &&
# Run if ENV['METERPRETER_MODULE_TEST'] = 'post/test/cmd_exec' etc
Acceptance::CommandShell.run_command_shell_module_test?(module_test[:name]) &&
Acceptance::Session.run_session?(command_shell_config) &&
# Run if ENV['SESSION_MODULE_TEST'] = 'post/test/cmd_exec' etc
Acceptance::Session.run_session_module_test?(module_test[:name]) &&
# Only run payloads / tests, if the host machine can run them
Acceptance::CommandShell.supported_platform?(payload_config) &&
Acceptance::CommandShell.supported_platform?(module_test) &&
Acceptance::Session.supported_platform?(payload_config) &&
Acceptance::Session.supported_platform?(module_test) &&
# Skip tests that are explicitly skipped, or won't pass in the current environment
!Acceptance::CommandShell.skipped_module_test?(module_test, allure_test_environment)
!Acceptance::Session.skipped_module_test?(module_test, allure_test_environment)
),
# test metadata - will appear in allure report
module_test: module_test[:name]
Expand Down Expand Up @@ -410,7 +410,7 @@ def get_file_attachment_contents(path)
end

validated_lines.each do |test_line|
test_line = Acceptance::CommandShell.uncolorize(test_line)
test_line = Acceptance::Session.uncolorize(test_line)
expect(test_line).to_not include('FAILED', '[-] '), "Unexpected error: #{test_line}"
end

Expand Down
101 changes: 0 additions & 101 deletions spec/support/acceptance/command_shell.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/support/acceptance/command_shell/cmd.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Acceptance::CommandShell
module Acceptance::Session
CMD = {
payloads: [
{
Expand Down
2 changes: 1 addition & 1 deletion spec/support/acceptance/command_shell/linux.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Acceptance::CommandShell
module Acceptance::Session
LINUX = {
payloads: [
{
Expand Down
2 changes: 1 addition & 1 deletion spec/support/acceptance/command_shell/powershell.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Acceptance::CommandShell
module Acceptance::Session
POWERSHELL = {
payloads: [
{
Expand Down
34 changes: 4 additions & 30 deletions test/modules/post/test/cmd_exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(info = {})
)
end

def upload_create_process_precompiled_binaries
def upload_show_args_binary
print_status 'Uploading precompiled binaries'
upload_file(show_args_binary[:path], "data/cmd_exec/#{show_args_binary[:path]}")
unless session.platform.eql?('windows')
Expand Down Expand Up @@ -68,7 +68,7 @@ def test_cmd_exec
# we are inconsistent reporting windows session types
windows_strings = ['windows', 'win']
vprint_status("Starting cmd_exec tests")
upload_create_process_precompiled_binaries
upload_show_args_binary

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

# TODO: This can be added back in once Smashery's create process API has been landed
# TODO: These tests are in preparation for Smashery's create process API
# def test_create_process
# upload_create_process_precompiled_binaries
# upload_show_args_binary
#
# test_string = Rex::Text.rand_text_alpha(4)
#
Expand Down Expand Up @@ -281,30 +281,4 @@ def test_cmd_exec_stderr
# end
# end
# end

# TODO: example in case `show_args_binary` needs some more massaging once we are able to test the API changes again
#
# if session.platform.eql? 'windows'
# output = create_process('./show_args.exe', args: [test_string, '', test_string, '', test_string])
# if session.type.eql? 'powershell'
# output.rstrip == "#{pwd}\\show_args.exe\r\n#{test_string}\r\n\r\n#{test_string}\r\n\r\n#{test_string}"
# elsif session.type.eql? 'shell'
# output = create_process('show_args.exe', args: [test_string, '', test_string, '', test_string])
# output.rstrip == "show_args.exe\r\n#{test_string}\r\n\r\n#{test_string}\r\n\r\n#{test_string}"
# elsif (session.type.eql?('meterpreter') && session.arch.eql?('java'))
# output.rstrip == ".\\show_args.exe\r\n#{test_string}\r\n\r\n#{test_string}\r\n\r\n#{test_string}"
# elsif session.arch.eql?("php")
# # output = create_process('.\\show_args.exe', args: [test_string, '', test_string, '', test_string])
# # $stderr.puts output.rstrip.inspect
# # output.rstrip == ".\\show_args.exe\r\n#{test_string}\r\n\r\n#{test_string}\r\n\r\n#{test_string}"
# # TODO: Fix this functionality
# vprint_status("test skipped for PHP - functionality not correct")
# true
# else
# output.rstrip == "./show_args.exe\r\n#{test_string}\r\n\r\n#{test_string}\r\n\r\n#{test_string}"
# end
# else
# output = create_process('./show_args', args: [test_string, '', test_string, '', test_string])
# output.rstrip == "./show_args\n#{test_string}\n\n#{test_string}\n\n#{test_string}"
# end
end

0 comments on commit 0d7166a

Please sign in to comment.