Path: blob/master/test/integration/tc_debug_modules.rb
1154 views
#1# Copyright (c) 2006-2025 Wade Alcorn - [email protected]2# Browser Exploitation Framework (BeEF) - https://beefproject.com3# See the file 'doc/COPYING' for copying permission4#5require 'test/unit'6require 'rest-client'7require 'json'8require '../common/test_constants'9require '../common/beef_test'1011class TC_DebugModules < Test::Unit::TestCase1213@@token = nil14@@hb_session = nil1516@@mod_debug_long_string = nil17@@mod_debug_ascii_chars = nil18@@mod_debug_test_network = nil1920# NOTE: Tests within the same test class are called in the order they are defined.21# NOTE: However, test classes are run in alphabetical order by classname.22# That's why we use the prefix x_N_y, with N being the order of execution.23#2425# Test RESTful API authentication with default credentials, returns the API token to be used later.26def test_1_restful_auth27response = RestClient.post "#{RESTAPI_ADMIN}/login",28{ 'username' => "#{BEEF_USER}",29'password' => "#{BEEF_PASSWD}"}.to_json,30:content_type => :json,31:accept => :json32assert_equal 200, response.code33assert_not_nil response.body34result = JSON.parse(response.body)35success = result['success']36@@token = result['token']37assert(success)38end3940# Test RESTful API hooks handler hooking a victim browser, and then retrieving his BeEF session41def test_2_restful_hooks42BeefTest.new_victim43sleep 5.044response = RestClient.get "#{RESTAPI_HOOKS}", {:params => {:token => @@token}}45assert_equal 200, response.code46assert_not_nil response.body47result = JSON.parse(response.body)48@@hb_session = result["hooked-browsers"]["online"]["0"]["session"]49assert_not_nil @@hb_session50end5152# Test RESTful API modules handler, retrieving the IDs of the 3 debug modules currently in the framework53def test_3_restful_modules54response = RestClient.get "#{RESTAPI_MODULES}", {:params => {:token => @@token}}55assert_equal 200, response.code56assert_not_nil response.body57result = JSON.parse(response.body)58result.each do |mod|59case mod[1]["class"]60when "Test_return_long_string"61@@mod_debug_long_string = mod[1]["id"]62when "Test_return_ascii_chars"63@@mod_debug_ascii_chars = mod[1]["id"]64when "Test_network_request"65@@mod_debug_test_network = mod[1]["id"]66end67end68assert_not_nil @@mod_debug_long_string69assert_not_nil @@mod_debug_ascii_chars70assert_not_nil @@mod_debug_test_network71end72#73## Test debug module "Test_return_long_string" using the RESTful API74def test_return_long_string75repeat_string = "BeEF"76repeat_count = 207778response = RestClient.post "#{RESTAPI_MODULES}/#{@@hb_session}/#{@@mod_debug_long_string}?token=#{@@token}",79{ 'repeat_string' => repeat_string,80'repeat' => repeat_count}.to_json,81:content_type => :json,82:accept => :json83assert_equal 200, response.code84assert_not_nil response.body85result = JSON.parse(response.body)86success = result['success']87assert success8889cmd_id = result['command_id']90count = 091response = RestClient.get "#{RESTAPI_MODULES}/#{@@hb_session}/#{@@mod_debug_long_string}/#{cmd_id}?token=#{@@token}"9293#TODO if the response is empty, the body size is 2, basically an empty Hash.94# don't know why empty?, nil and other checks are not working.95while(response.body.size <= 2 && count < 10)96response = RestClient.get "#{RESTAPI_MODULES}/#{@@hb_session}/#{@@mod_debug_long_string}/#{cmd_id}?token=#{@@token}"97sleep 298count += 199end100assert_equal 200, response.code101assert_not_nil response.body102result = JSON.parse(response.body)103data = JSON.parse(result['0']['data'])['data']104assert_not_nil data105assert_equal (repeat_string * repeat_count),data106end107#108## Test debug module "Test_return_ascii_chars" using the RESTful API109def test_return_ascii_chars110response = RestClient.post "#{RESTAPI_MODULES}/#{@@hb_session}/#{@@mod_debug_ascii_chars}?token=#{@@token}",111{}.to_json, # module does not expect any input112:content_type => :json,113:accept => :json114assert_equal 200, response.code115assert_not_nil response.body116result = JSON.parse(response.body)117success = result['success']118assert success119cmd_id = result['command_id']120count = 0121response = RestClient.get "#{RESTAPI_MODULES}/#{@@hb_session}/#{@@mod_debug_ascii_chars}/#{cmd_id}?token=#{@@token}"122123#TODO if the response is empty, the body size is 2, basically an empty Hash.124# don't know why empty?, nil and other checks are not working.125while(response.body.size <= 2 && count < 10)126response = RestClient.get "#{RESTAPI_MODULES}/#{@@hb_session}/#{@@mod_debug_ascii_chars}/#{cmd_id}?token=#{@@token}"127sleep 2128count += 1129end130assert_equal 200, response.code131assert_not_nil response.body132result = JSON.parse(response.body)133data = JSON.parse(result['0']['data'])['data']134assert_not_nil data135ascii_chars = ""136(32..127).each do |i| ascii_chars << i.chr end137assert_equal ascii_chars,data138end139140# Test debug module "Test_network_request" using the RESTful API141def test_return_network_request142143# Test same-origin request (response code and content of secret_page.html)144response = RestClient.post "#{RESTAPI_MODULES}/#{@@hb_session}/#{@@mod_debug_test_network}?token=#{@@token}",145#override only a few parameters, the other ones will have default values from modules's module.rb definition146{"domain" => ATTACK_DOMAIN, "port" => "3000", "path" => "/demos/secret_page.html"}.to_json,147:content_type => :json,148:accept => :json149assert_equal 200, response.code150assert_not_nil response.body151result = JSON.parse(response.body)152success = result['success']153assert success154155cmd_id = result['command_id']156count = 0157response = RestClient.get "#{RESTAPI_MODULES}/#{@@hb_session}/#{@@mod_debug_test_network}/#{cmd_id}?token=#{@@token}"158159#TODO if the response is empty, the body size is 2, basically an empty Hash.160# don't know why empty?, nil and other checks are not working.161while(response.body.size <= 2 && count < 10)162response = RestClient.get "#{RESTAPI_MODULES}/#{@@hb_session}/#{@@mod_debug_test_network}/#{cmd_id}?token=#{@@token}"163sleep 2164count += 1165end166assert_equal 200, response.code167assert_not_nil response.body168result = JSON.parse(response.body)169data = JSON.parse(result['0']['data'])['data']170assert_not_nil data171assert_equal 200, JSON.parse(data)["status_code"]172assert JSON.parse(data)["port_status"].include?("open")173174end175end176177178