Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/extensions/qrcode/qrcode.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 Qrcode
9
module QrcodeGenerator
10
BeEF::API::Registrar.instance.register(BeEF::Extension::Qrcode::QrcodeGenerator, BeEF::API::Server, 'pre_http_start')
11
12
def self.pre_http_start(_http_hook_server)
13
require 'uri'
14
require 'qr4r'
15
16
fullurls = []
17
18
# get server config
19
configuration = BeEF::Core::Configuration.instance
20
beef_proto = configuration.beef_proto
21
beef_host = configuration.beef_host
22
beef_port = configuration.beef_port
23
24
# get URLs from QR config
25
configuration.get('beef.extension.qrcode.targets').each do |target|
26
# absolute URLs
27
if target.lines.grep(%r{^https?://}i).size.positive?
28
fullurls << target
29
# relative URLs
30
else
31
32
# Retrieve the list of network interfaces from BeEF::Core::Console::Banners
33
interfaces = BeEF::Core::Console::Banners.interfaces
34
35
if not interfaces.nil? and not interfaces.empty? # If interfaces are available, iterate over each network interface
36
# If interfaces are available, iterate over each network interface
37
interfaces.each do |int|
38
# Skip the loop iteration if the interface address is '0.0.0.0' (which generally represents all IPv4 addresses on the local machine)
39
next if int == '0.0.0.0'
40
# Construct full URLs using the network interface address, and add them to the fullurls array
41
# The URL is composed of the BeEF protocol, interface address, BeEF port, and the target path
42
fullurls << "#{beef_proto}://#{int}:#{beef_port}#{target}"
43
end
44
end
45
46
end
47
end
48
49
return unless fullurls.empty?
50
51
img_dir = 'extensions/qrcode/images'
52
begin
53
Dir.mkdir(img_dir) unless File.directory?(img_dir)
54
rescue StandardError
55
print_error "[QR] Could not create directory '#{img_dir}'"
56
end
57
58
data = ''
59
fullurls.uniq.each do |target|
60
fname = ('a'..'z').to_a.sample(8).join
61
qr_path = "#{img_dir}/#{fname}.png"
62
begin
63
Qr4r.encode(
64
target, qr_path, {
65
pixel_size: configuration.get('beef.extension.qrcode.qrsize'),
66
border: configuration.get('beef.extension.qrcode.qrborder')
67
}
68
)
69
rescue StandardError
70
print_error "[QR] Could not write file '#{qr_path}'"
71
next
72
end
73
74
print_debug "[QR] Wrote file '#{qr_path}'"
75
BeEF::Core::NetworkStack::Handlers::AssetHandler.instance.bind(
76
"/#{qr_path}", "/qrcode/#{fname}", 'png'
77
)
78
79
data += "#{beef_proto}://#{beef_host}:#{beef_port}/qrcode/#{fname}.png\n"
80
data += "- URL: #{target}\n"
81
# Google API
82
# url = URI::Parser.new.escape(target,Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
83
# w = configuration.get("beef.extension.qrcode.qrsize").to_i * 100
84
# h = configuration.get("beef.extension.qrcode.qrsize").to_i * 100
85
# data += "- Google API: https://chart.googleapis.com/chart?cht=qr&chs=#{w}x#{h}&chl=#{url}\n"
86
# QRServer.com
87
# url = URI::Parser.new.escape(target,Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
88
# w = configuration.get("beef.extension.qrcode.qrsize").to_i * 100
89
# h = configuration.get("beef.extension.qrcode.qrsize").to_i * 100
90
# data += "- QRServer API: https://api.qrserver.com/v1/create-qr-code/?size=#{w}x#{h}&data=#{url}\n"
91
end
92
93
print_info 'QR code images available:'
94
print_more data
95
end
96
end
97
end
98
end
99
end
100
101