Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/extensions/evasion/evasion.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 Evasion
9
class Evasion
10
include Singleton
11
12
@@config = BeEF::Core::Configuration.instance
13
@@enabled = @@config.get('beef.extension.evasion.enable')
14
15
def initialize
16
return unless @@enabled
17
18
@techniques ||= load_techniques
19
20
if @techniques.empty?
21
print_error '[Evasion] Initialization failed. No obfuscation techniques specified.'
22
@@config.set('beef.extension.evasion.enable', false)
23
return
24
end
25
26
print_debug "[Evasion] Loaded obfuscation chain: #{@techniques.join(', ')}"
27
end
28
29
# load obfuscation technique chain
30
def load_techniques
31
techniques = @@config.get('beef.extension.evasion.chain') || []
32
return [] if techniques.empty?
33
34
chain = []
35
techniques.each do |technique|
36
unless File.exist?("#{$root_dir}/extensions/evasion/obfuscation/#{technique}.rb")
37
print_error "[Evasion] Failed to load obfuscation technique '#{technique}' - file does not exist"
38
next
39
end
40
chain << technique
41
end
42
43
chain
44
rescue StandardError => e
45
print_error "[Evasion] Failed to load obfuscation technique chain: #{e.message}"
46
[]
47
end
48
49
# Obfuscate the input JS applying the chain of techniques defined in the main config file.
50
def obfuscate(input)
51
@input = apply_chain(input)
52
end
53
54
def add_bootstrapper
55
bootstrap = ''
56
# add stuff at the end, only once (when serving the initial init javascript)
57
@techniques.each do |technique|
58
# Call the "execute" method of the technique module, passing the input and update
59
# the input in preperation for the next technique in the chain
60
klass = BeEF::Extension::Evasion.const_get(technique.capitalize).instance
61
if klass.need_bootstrap?
62
print_debug "[Evasion] Adding bootstrapper for technique: #{technique}"
63
bootstrap << klass.get_bootstrap
64
end
65
end
66
67
bootstrap
68
rescue StandardError => e
69
print_error "[Evasion] Failed to bootstrap obfuscation technique: #{e.message}"
70
print_error e.backtrace
71
end
72
73
def apply_chain(input)
74
output = input
75
@techniques.each do |technique|
76
# Call the "execute" method of the technique module, passing the input and update
77
# the input in preperation for the next technique in the chain
78
print_debug "[Evasion] Applying technique: #{technique}"
79
klass = BeEF::Extension::Evasion.const_get(technique.capitalize).instance
80
output = klass.execute(output, @@config)
81
end
82
83
print_debug "[Evasion] Obfuscation completed (#{output.length} bytes)"
84
output
85
rescue StandardError => e
86
print_error "[Evasion] Failed to apply obfuscation technique: #{e.message}"
87
print_error e.backtrace
88
end
89
end
90
end
91
end
92
end
93
94