Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/extensions/admin_ui/classes/session.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
module BeEF
7
module Extension
8
module AdminUI
9
#
10
# The session for BeEF UI.
11
#
12
class Session
13
include Singleton
14
15
attr_reader :ip, :id, :nonce, :auth_timestamp
16
17
def initialize
18
set_logged_out
19
@auth_timestamp = Time.new
20
end
21
22
#
23
# set the session logged in
24
#
25
def set_logged_in(ip)
26
@id = BeEF::Core::Crypto.secure_token
27
@nonce = BeEF::Core::Crypto.secure_token
28
@ip = ip
29
end
30
31
#
32
# set the session logged out
33
#
34
def set_logged_out
35
@id = nil
36
@nonce = nil
37
@ip = nil
38
end
39
40
#
41
# set teh auth_timestamp
42
#
43
def set_auth_timestamp(time)
44
@auth_timestamp = time
45
end
46
47
#
48
# return the session id
49
#
50
def get_id
51
@id
52
end
53
54
#
55
# return the nonce
56
#
57
def get_nonce
58
@nonce
59
end
60
61
#
62
# return the auth_timestamp
63
#
64
def get_auth_timestamp
65
@auth_timestamp
66
end
67
68
#
69
# Check if nonce valid
70
#
71
def valid_nonce?(request)
72
# check if a valid session
73
return false unless valid_session?(request)
74
return false if @nonce.nil?
75
return false unless request.post?
76
77
# get nonce from request
78
request_nonce = request['nonce']
79
return false if request_nonce.nil?
80
81
# verify nonce
82
request_nonce.eql? @nonce
83
end
84
85
#
86
# Check if a session valid
87
#
88
def valid_session?(request)
89
# check if a valid session exists
90
return false if @id.nil?
91
return false if @ip.nil?
92
93
# check ip address matches
94
return false unless @ip.to_s.eql? request.ip
95
96
# get session cookie name from config
97
session_cookie_name = BeEF::Core::Configuration.instance.get('beef.extension.admin_ui.session_cookie_name')
98
99
# check session id matches
100
request.cookies.each do |cookie|
101
return true if (cookie[0].to_s.eql? session_cookie_name) and (cookie[1].eql? @id)
102
end
103
request
104
105
# not a valid session
106
false
107
end
108
end
109
end
110
end
111
end
112
113