Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/spec/support/ui_support.rb
1154 views
1
#
2
# Copyright (c) 2006-2025 Wade Alcorn - [email protected]
3
# Browser Exploitation Framework (BeEF) - https://beefproject.com
4
# See the file 'doc/COPYING' for copying permission
5
#
6
require 'rspec'
7
require 'rest-client'
8
require 'spec/support/constants.rb'
9
10
def start_beef_and_hook_browser()
11
reset_beef_db
12
pid = start_beef_server_and_wait
13
beef_session = BeefTest.login
14
hooked_browser = BeefTest.new_victim
15
16
expect(hooked_browser).not_to be_nil
17
expect(hooked_browser).to be_a(Capybara::Session)
18
expect(hooked_browser).to have_content('BeEF', wait: PAGE_LOAD_TIMEOUT)
19
20
expect(beef_session).not_to be_nil
21
expect(beef_session).to be_a(Capybara::Session)
22
expect(beef_session).to have_content('Hooked Browsers', wait: PAGE_LOAD_TIMEOUT)
23
24
navigate_to_hooked_browser(beef_session)
25
26
expect(beef_session).to have_content('Commands', wait: PAGE_LOAD_TIMEOUT)
27
beef_session.click_on('Commands')
28
29
return pid, beef_session, hooked_browser
30
end
31
32
def stop_beef_and_unhook_browser(pid, beef_session, hooked_browser)
33
stop_beef_server(pid)
34
beef_session.driver.browser.close
35
hooked_browser.driver.browser.close
36
end
37
38
def navigate_to_hooked_browser(session, hooked_browser_text = nil)
39
expect(session).to have_content('Hooked Browsers', wait: PAGE_LOAD_TIMEOUT)
40
41
hooked_browser_text = '127.0.0.1' if hooked_browser_text.nil?
42
expect(session).to have_content(hooked_browser_text, wait: BROWSER_HOOKING_TIMEOUT)
43
44
# click on the hooked browser in the leaf
45
session.all('a', text: hooked_browser_text)[1].click
46
expect(session).to have_content('Commands', wait: PAGE_LOAD_TIMEOUT)
47
end
48
49
def navigate_to_category(session, category_name = nil)
50
expect(category_name).not_to be_nil
51
expect(category_name).to be_a(String)
52
53
navigate_to_hooked_browser unless session.has_content?('Current Browser')
54
55
# ensure the command module tree is visible
56
session.click_on('Commands')
57
expect(session).to have_content(category_name, wait: PAGE_LOAD_TIMEOUT)
58
59
session.first(:link_or_button, category_name + " ").click
60
end
61
62
def expand_category_tree(session, category, module_name = nil)
63
if category.is_a?(Array)
64
category.each do |category_name|
65
# find the category element and scroll to it
66
session.all('div', text: category_name).each do |element|
67
begin
68
element_text = element.text
69
next unless element_text.start_with?(category_name)
70
match_data = element_text.match(/\A([\w\s]+)\s\((\d+)\)\z/)
71
next unless match_data
72
73
# scroll to the element
74
session.scroll_to(element)
75
rescue Selenium::WebDriver::Error::StaleElementReferenceError => e
76
77
puts "StaleElementReferenceError: #{element_text}"
78
puts e.message
79
next
80
end
81
end
82
83
expect(session).to have_content(category_name, wait: PAGE_LOAD_TIMEOUT)
84
navigate_to_category(session, category_name) unless session.has_content?(module_name)
85
end
86
else
87
navigate_to_category(session, category) unless session.has_content?(module_name)
88
expect(session).to have_content(category, wait: PAGE_LOAD_TIMEOUT)
89
end
90
expect(session).to have_content(module_name, wait: PAGE_LOAD_TIMEOUT)
91
end
92
93
def collapse_category_tree(session, category)
94
if category.is_a?(Array)
95
category.reverse.each do |category_name|
96
# Collapse the sub-folder
97
session.scroll_to(category_name)
98
session.first(:link_or_button, category_name + " ").click
99
end
100
else
101
session.scroll_to(category)
102
session.first(:link_or_button, category + " ").click
103
end
104
end
105
106
def click_on_module(session, category, module_name)
107
# expand the category tree to make the module visible
108
expand_category_tree(session, category, module_name)
109
110
# click on the module in the expanded tree
111
session.scroll_to(module_name)
112
expect(session).to have_content(module_name, wait: PAGE_LOAD_TIMEOUT)
113
modules = session.all(:link_or_button, module_name)
114
modules[0].click
115
end
116