Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/core/main/router/router.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
7
module BeEF
8
module Core
9
module Router
10
# @note This is the main Router parent class.
11
# @note All the HTTP handlers registered on BeEF will extend this class.
12
class Router < Sinatra::Base
13
config = BeEF::Core::Configuration.instance
14
15
configure do
16
set :show_exceptions, false
17
end
18
19
# @note Override default 404 HTTP response
20
not_found do
21
error_page_404
22
end
23
24
before do
25
# @note Override Server HTTP response header
26
headers response_headers
27
28
# @note If CORS is enabled, expose the appropriate headers
29
if config.get('beef.http.restful_api.allow_cors')
30
allowed_origins = config.get('beef.http.restful_api.cors_allowed_origins')
31
if allowed_origins
32
headers 'Access-Control-Allow-Origin' => allowed_origins
33
end
34
headers 'Access-Control-Allow-Methods' => 'POST, GET'
35
36
# Responses to preflight OPTIONS requests need to respond with HTTP 200
37
# and be able to handle requests with a JSON content-type
38
if request.request_method == 'OPTIONS'
39
headers 'Access-Control-Allow-Headers' => 'Content-Type'
40
halt 200
41
end
42
end
43
end
44
45
# @note Default root page
46
get '/' do
47
index_page
48
end
49
50
private
51
52
def response_headers
53
config = BeEF::Core::Configuration.instance
54
55
default_headers = {
56
'Server' => '',
57
'Content-Type' => 'text/html'
58
}
59
60
return default_headers unless config.get('beef.http.web_server_imitation.enable')
61
62
case config.get('beef.http.web_server_imitation.type')
63
when 'apache'
64
{
65
'Server' => 'Apache/2.2.3 (CentOS)',
66
'Content-Type' => 'text/html; charset=UTF-8'
67
}
68
when 'iis'
69
{
70
'Server' => 'Microsoft-IIS/6.0',
71
'X-Powered-By' => 'ASP.NET',
72
'Content-Type' => 'text/html; charset=UTF-8'
73
}
74
when 'nginx'
75
{
76
'Server' => 'nginx',
77
'Content-Type' => 'text/html'
78
}
79
else
80
print_error 'Configuration error in beef.http.web_server_imitation.type!'
81
print_more 'Supported values are: apache, iis, nginx.'
82
default_headers
83
end
84
end
85
86
def index_page
87
config = BeEF::Core::Configuration.instance
88
89
return '' unless config.get('beef.http.web_server_imitation.enable')
90
91
bp = config.get 'beef.extension.admin_ui.base_path'
92
case config.get('beef.http.web_server_imitation.type')
93
when 'apache'
94
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' \
95
'<head>' \
96
'<title>Apache HTTP Server Test Page powered by CentOS</title>' \
97
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' \
98
'<style type="text/css">' \
99
'body {' \
100
'background-color: #fff; ' \
101
'color: #000;' \
102
'font-size: 0.9em;' \
103
'font-family: sans-serif,helvetica;' \
104
'margin: 0;' \
105
'padding: 0; ' \
106
'} ' \
107
':link { ' \
108
'color: #0000FF; ' \
109
'} ' \
110
':visited { ' \
111
'color: #0000FF; ' \
112
'} ' \
113
'a:hover { ' \
114
'color: #3399FF; ' \
115
'} ' \
116
'h1 { ' \
117
"\ttext-align: center; " \
118
"\tmargin: 0; " \
119
"\tpadding: 0.6em 2em 0.4em; " \
120
"\tbackground-color: #3399FF;" \
121
"\tcolor: #ffffff; " \
122
"\tfont-weight: normal; " \
123
"\tfont-size: 1.75em; " \
124
"\tborder-bottom: 2px solid #000; " \
125
'} ' \
126
'h1 strong {' \
127
'font-weight: bold; ' \
128
'} ' \
129
'h2 { ' \
130
"\tfont-size: 1.1em;" \
131
'font-weight: bold; ' \
132
'} ' \
133
'.content { ' \
134
"\tpadding: 1em 5em; " \
135
'} ' \
136
'.content-columns { ' \
137
"\t/* Setting relative positioning allows for " \
138
"\tabsolute positioning for sub-classes */ " \
139
"\tposition: relative; " \
140
"\tpadding-top: 1em; " \
141
'} ' \
142
'.content-column-left { ' \
143
"\t/* Value for IE/Win; will be overwritten for other browsers */" \
144
"\twidth: 47%; " \
145
"\tpadding-right: 3%; " \
146
"\tfloat: left; " \
147
"\tpadding-bottom: 2em; " \
148
'} ' \
149
'.content-column-right { ' \
150
"\t/* Values for IE/Win; will be overwritten for other browsers */" \
151
"\twidth: 47%; " \
152
"\tpadding-left: 3%; " \
153
"\tfloat: left; " \
154
"\tpadding-bottom: 2em; " \
155
'} ' \
156
'.content-columns>.content-column-left, .content-columns>.content-column-right {' \
157
"\t/* Non-IE/Win */" \
158
'} ' \
159
'img { ' \
160
"\tborder: 2px solid #fff; " \
161
"\tpadding: 2px; " \
162
"\tmargin: 2px; " \
163
'} ' \
164
'a:hover img { ' \
165
"\tborder: 2px solid #3399FF; " \
166
'} ' \
167
'</style> ' \
168
'</head> ' \
169
'<body> ' \
170
'<h1>Apache 2 Test Page<br><font size="-1"><strong>powered by</font> CentOS</strong></h1>' \
171
'<div class="content"><div class="content-middle">' \
172
'<p>This page is used to test the proper operation of the Apache HTTP server after it has been installed. If you can read this page it means that the Apache HTTP server installed at this site is working properly.</p>' \
173
'</div>' \
174
'<hr />' \
175
'<div class="content-columns">' \
176
'<div class="content-column-left"> ' \
177
'<h2>If you are a member of the general public:</h2>' \
178
'<p>The fact that you are seeing this page indicates that the website you just visited is either experiencing problems or is undergoing routine maintenance.</p>' \
179
"<p>If you would like to let the administrators of this website know that you've seen this page instead of the page you expected, you should send them e-mail. In general, mail sent to the name \"webmaster\" and directed to the website's domain should reach the appropriate person.</p> " \
180
'<p>For example, if you experienced problems while visiting www.example.com, you should send e-mail to "[email protected]".</p>' \
181
'</div>' \
182
'<div class="content-column-right">' \
183
'<h2>If you are the website administrator:</h2>' \
184
'<p>You may now add content to the directory <tt>/var/www/html/</tt>. Note that until you do so, people visiting your website will see this page and not your content. To prevent this page from ever being used, follow the instructions in the file <tt>/etc/httpd/conf.d/welcome.conf</tt>.</p>' \
185
'<p>You are free to use the images below on Apache and CentOS Linux powered HTTP servers. Thanks for using Apache and CentOS!</p>' \
186
"<p><a href=\"http://httpd.apache.org/\"><img src=\"#{bp}/media/images/icons/apache_pb.gif\" alt=\"[ Powered by Apache ]\"/></a> <a href=\"http://www.centos.org/\"><img src=\"#{bp}/media/images/icons/powered_by_rh.png\" alt=\"[ Powered by CentOS Linux ]\" width=\"88\" height=\"31\" /></a></p>" \
187
'</div>' \
188
'</div>' \
189
'</div>' \
190
' <div class="content">' \
191
'<div class="content-middle"><h2>About CentOS:</h2><b>The Community ENTerprise Operating System</b> (CentOS) is an Enterprise-class Linux Distribution derived from sources freely provided to the public by a prominent North American Enterprise Linux vendor. CentOS conforms fully with the upstream vendors redistribution policy and aims to be 100% binary compatible. (CentOS mainly changes packages to remove upstream vendor branding and artwork.) The CentOS Project is the organization that builds CentOS.</p>' \
192
'<p>For information on CentOS please visit the <a href="http://www.centos.org/">CentOS website</a>.</p>' \
193
'<p><h2>Note:</h2><p>CentOS is an Operating System and it is used to power this website; however, the webserver is owned by the domain owner and not the CentOS Project. <b>If you have issues with the content of this site, contact the owner of the domain, not the CentOS project.</b>' \
194
"<p>Unless this server is on the CentOS.org domain, the CentOS Project doesn't have anything to do with the content on this webserver or any e-mails that directed you to this site.</p> " \
195
'<p>For example, if this website is www.example.com, you would find the owner of the example.com domain at the following WHOIS server:</p>' \
196
'<p><a href="http://www.internic.net/whois.html">http://www.internic.net/whois.html</a></p>' \
197
'</div>' \
198
'</div>' +
199
("<script src='#{config.get('beef.http.hook_file')}'></script>" if config.get('beef.http.web_server_imitation.hook_root')).to_s +
200
'</body>' \
201
'</html>'
202
when 'iis'
203
'<html>' \
204
'<head>' \
205
'<meta HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">' \
206
'<title ID=titletext>Under Construction</title>' \
207
'</head>' \
208
'<body bgcolor=white>' \
209
'<table>' \
210
'<tr>' \
211
'<td ID=tableProps width=70 valign=top align=center>' \
212
"<img ID=pagerrorImg src=\"#{bp}/media/images/icons/pagerror.gif\" width=36 height=48>" \
213
'<td ID=tablePropsWidth width=400>' \
214
'<h1 ID=errortype style="font:14pt/16pt verdana; color:#4e4e4e">' \
215
'<P ID=Comment1><!--Problem--><P ID="errorText">Under Construction</h1>' \
216
'<P ID=Comment2><!--Probable causes:<--><P ID="errordesc"><font style="font:9pt/12pt verdana; color:black">' \
217
'The site you are trying to view does not currently have a default page. It may be in the process of being upgraded and configured.' \
218
'<P ID=term1>Please try this site again later. If you still experience the problem, try contacting the Web site administrator.' \
219
'<hr size=1 color="blue">' \
220
'<P ID=message1>If you are the Web site administrator and feel you have received this message in error, please see &quot;Enabling and Disabling Dynamic Content&quot; in IIS Help.' \
221
'<h5 ID=head1>To access IIS Help</h5>' \
222
'<ol>' \
223
'<li ID=bullet1>Click <b>Start</b>, and then click <b>Run</b>.' \
224
'<li ID=bullet2>In the <b>Open</b> text box, type <b>inetmgr</b>. IIS Manager appears.' \
225
'<li ID=bullet3>From the <b>Help</b> menu, click <b>Help Topics</b>.' \
226
'<li ID=bullet4>Click <b>Internet Information Services</b>.</ol>' \
227
'</td>' \
228
'</tr>' \
229
'</table>' +
230
("<script src='#{config.get('beef.http.hook_file')}'></script>" if config.get('beef.http.web_server_imitation.hook_root')).to_s +
231
'</body>' \
232
'</html>'
233
when 'nginx'
234
"<!DOCTYPE html>\n" \
235
"<html>\n" \
236
"<head>\n" \
237
"<title>Welcome to nginx!</title>\n" \
238
"<style>\n" \
239
" body {\n" \
240
" width: 35em;\n" \
241
" margin: 0 auto;\n" \
242
" font-family: Tahoma, Verdana, Arial, sans-serif;\n" \
243
" }\n" \
244
"</style>\n" \
245
"</head>\n" \
246
"<body>\n" \
247
"<h1>Welcome to nginx!</h1>\n" \
248
"<p>If you see this page, the nginx web server is successfully installed and\n" \
249
"working. Further configuration is required.</p>\n\n" \
250
"<p>For online documentation and support please refer to\n" \
251
"<a href=\"http://nginx.org/\">nginx.org</a>.<br/>\n" \
252
"Commercial support is available at\n" \
253
"<a href=\"http://nginx.com/\">nginx.com</a>.</p>\n\n" \
254
"<p><em>Thank you for using nginx.</em></p>\n" +
255
("<script src='#{config.get('beef.http.hook_file')}'></script>" if config.get('beef.http.web_server_imitation.hook_root')).to_s +
256
"</body>\n" \
257
"</html>\n"
258
else
259
print_error 'Configuration error in beef.http.web_server_imitation.type!'
260
print_more 'Supported values are: apache, iis, nginx.'
261
''
262
end
263
end
264
265
def error_page_404
266
config = BeEF::Core::Configuration.instance
267
268
return 'Not Found.' unless config.get('beef.http.web_server_imitation.enable')
269
270
case config.get('beef.http.web_server_imitation.type')
271
when 'apache'
272
return <<-EOF
273
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
274
<html><head>
275
<title>404 Not Found</title>
276
</head><body>
277
<h1>Not Found</h1>
278
<p>The requested URL was not found on this server.</p>
279
<hr>
280
<address>Apache/2.2.3 (CentOS)</address>
281
#{("<script src='#{config.get('beef.http.hook_file')}'></script>" if config.get('beef.http.web_server_imitation.hook_404'))}
282
</body></html>
283
EOF
284
when 'iis'
285
return <<-EOF
286
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
287
<HTML><HEAD><TITLE>The page cannot be found</TITLE>
288
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">
289
<STYLE type="text/css">
290
BODY { font: 8pt/12pt verdana }
291
H1 { font: 13pt/15pt verdana }
292
H2 { font: 8pt/12pt verdana }
293
A:link { color: red }
294
A:visited { color: maroon }
295
</STYLE></HEAD><BODY><TABLE width=500 border=0 cellspacing=10><TR><TD>
296
<h1>The page cannot be found</h1>
297
The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.
298
<hr>
299
<p>Please try the following:</p>
300
<ul>
301
<li>Make sure that the Web site address displayed in the address bar of your browser is spelled and formatted correctly.</li>
302
<li>If you reached this page by clicking a link, contact the Web site administrator to alert them that the link is incorrectly formatted.</li>
303
<li>Click the <a href="javascript:history.back(1)">Back</a> button to try another link.</li>
304
</ul>
305
<h2>HTTP Error 404 - File or directory not found.<br>Internet Information Services (IIS)</h2>
306
<hr>
307
<p>Technical Information (for support personnel)</p>
308
<ul>
309
<li>Go to <a href="http://go.microsoft.com/fwlink/?linkid=8180">Microsoft Product Support Services</a> and perform a title search for the words <b>HTTP</b> and <b>404</b>.</li>
310
<li>Open <b>IIS Help</b>, which is accessible in IIS Manager (inetmgr),and search for topics titled <b>Web Site Setup</b>, <b>Common Administrative Tasks</b>, and <b>About Custom Error Messa
311
</ul>
312
</TD></TR></TABLE>
313
#{("<script src='#{config.get('beef.http.hook_file')}'></script>" if config.get('beef.http.web_server_imitation.hook_404'))}
314
</BODY></HTML>
315
EOF
316
when 'nginx'
317
return <<-EOF
318
<html>
319
<head><title>404 Not Found</title></head>
320
<body bgcolor="white">
321
<center><h1>404 Not Found</h1></center>
322
<hr><center>nginx</center>
323
#{("<script src='#{config.get('beef.http.hook_file')}'></script>" if config.get('beef.http.web_server_imitation.hook_404'))}
324
</body>
325
</html>
326
EOF
327
else
328
print_error 'Configuration error in beef.http.web_server_imitation.type!'
329
print_more 'Supported values are: apache, iis, nginx.'
330
'Not Found.'
331
end
332
end
333
end
334
end
335
end
336
end
337
338