Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/test/functional/framework/msfconsole_spec.rb
Views: 11623
$:.unshift(File.join(File.dirname(__FILE__)))1$:.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))2$:.unshift(File.join(File.dirname(__FILE__), '..', '..', '..', 'lib'))3$:.unshift(File.join(File.dirname(__FILE__), '..', '..', '..', 'test', 'lib'))45require 'fileutils'6require 'msf_matchers'7require 'msf_test_case'89module MsfTest10include MsfTest::MsfMatchers1112## This spec exists to help us describe the behavior of msfconsole - TODO1314describe "Msfconsole" do15###16# Setup!17###1819before :all do20@working_directory = File.dirname(__FILE__)2122## Static specs will make use of RC files here23@static_resource_directory = "#{@working_directory}/msftest/resource"2425## Directories for the generated specs26@temp_directory = "#{@working_directory}/msfconsole_specs"27@temp_input_directory = "#{@temp_directory}/generated_rc"2829## Where all output from the runs will go30@temp_output_directory = "#{@temp_directory}/output"3132## Create a framework object33@framework = ::Msf::Simple::Framework.create34end3536before :each do37end3839after :each do40end4142after :all do43## Clean up44# FileUtils.rm_rf(@temp_directory)45end4647###48# Static Test cases!49###5051it "should start and let us run help" do52data = start_console_and_run_rc("help", "#{@static_resource_directory}/help.rc")5354success_strings = [55'help',56'Database Backend Commands',57'Core Commands'58]59failure_strings = [] | generic_failure_strings60failure_exception_strings = [] | generic_failure_exception_strings6162data.should contain_all_successes(success_strings)63data.should contain_no_failures_except(failure_strings, failure_exception_strings)64end6566it "should generate a meterpreter session against a vulnerable win32 host" do67## Set input & output to something sane68input = Rex::Ui::Text::Input::Stdio.new69output = Rex::Ui::Text::Output::File.new("temp.output")70session = generate_x86_meterpreter_session(input, output)7172session.should_not be_nil7374if session75session.load_stdapi76session.run_cmd("help")77else78flunk "Error interacting with session"79end80end8182###83# Dynamic Test Cases!!84###8586@working_directory = File.dirname(__FILE__)8788## Directories for the generated specs89@temp_directory = "#{@working_directory}/msfconsole_specs"90@temp_input_directory = "#{@temp_directory}/generated_rc"9192## Where all output from the runs will go93@temp_output_directory = "#{@temp_directory}/output"9495if File.directory? @temp_directory96FileUtils.rm_rf(@temp_directory)97end9899Dir.mkdir(@temp_directory)100Dir.mkdir(@temp_input_directory)101Dir.mkdir(@temp_output_directory)102103Dir.glob("#{@working_directory}/msftest/*.msftest").each do |filename|104## Parse this test case105test_case = MsfTestCase.new(filename)106puts "Found #{test_case.name} in: #{filename}"107108## Write the commands back to a temporary RC file109puts "Writing #{@temp_input_directory}/#{test_case.name}.rc"110File.open("#{@temp_input_directory}/#{test_case.name}.rc", 'w') { |f| f.puts test_case.commands }111112## Create the rspec Test Case113it "should #{test_case.name}" do114## Gather the success / failure strings, and combine with the generics115success_strings = test_case.expected_successes116failure_strings = test_case.expected_failures | generic_failure_strings117failure_exception_strings = test_case.expected_failure_exceptions | generic_failure_exception_strings118119## run the commands120data = start_console_and_run_rc(test_case.name, "#{@temp_input_directory}/#{test_case.name}.rc")121122## check the output123data.should contain_all_successes(success_strings)124data.should contain_no_failures_except(failure_strings, failure_exception_strings)125126## Clean up127# File.delete("#{@temp_input_directory}/#{test_case.name}.rc")128# File.delete("#{@temp_output_directory}/#{test_case.name}")129end130end131132###133# Test case helpers:134###135def generic_success_strings136[]137end138139def generic_failure_strings140['fatal', 'fail', 'error', 'exception']141end142143def generic_failure_exception_strings144[]145end146147def start_console_and_run_rc(name, rc_file, database_file = false)148output_file = "#{@temp_output_directory}/#{name}"149150if database_file151msfconsole_string = "ruby #{@working_directory}/../../../msfconsole -o #{output_file} -r #{rc_file} -y #{database_file}"152else153msfconsole_string = "ruby #{@working_directory}/../../../msfconsole -o #{output_file} -r #{rc_file}"154end155156system("#{msfconsole_string}")157158data = hlp_file_to_string("#{output_file}")159end160161def generate_x86_meterpreter_session(input, output)162## Setup for win32163exploit_name = 'windows/smb/psexec'164payload_name = 'windows/meterpreter/bind_tcp'165166## Fire it off against a known-vulnerable host167session = @framework.exploits.create(exploit_name).exploit_simple(168'Options' => { 'RHOST' => "vulnerable", "SMBUser" => "administrator", "SMBPass" => "" },169'Payload' => payload_name,170'LocalInput' => input,171'LocalOutput' => output172)173174## If a session came back, try to interact with it.175if session176return session177else178return nil179end180end181182def generate_win64_meterpreter_session(input, output)183raise "Not Implemented"184end185186def generate_java_meterpreter_session(input, output)187raise "Not Implemented"188end189190def generate_php_meterpreter_session(input, output)191raise "Not Implemented"192end193194def hlp_file_to_string(filename)195data = ""196f = File.open(filename, "r")197f.each_line do |line|198data += line199end200return data201end202end203end204205206