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