#1# Copyright (c) 2006-2025 Wade Alcorn - [email protected]2# Browser Exploitation Framework (BeEF) - https://beefproject.com3# See the file 'doc/COPYING' for copying permission4#5module BeEF6module Extension7module Qrcode8module QrcodeGenerator9BeEF::API::Registrar.instance.register(BeEF::Extension::Qrcode::QrcodeGenerator, BeEF::API::Server, 'pre_http_start')1011def self.pre_http_start(_http_hook_server)12require 'uri'13require 'qr4r'1415fullurls = []1617# get server config18configuration = BeEF::Core::Configuration.instance19beef_proto = configuration.beef_proto20beef_host = configuration.beef_host21beef_port = configuration.beef_port2223# get URLs from QR config24configuration.get('beef.extension.qrcode.targets').each do |target|25# absolute URLs26if target.lines.grep(%r{^https?://}i).size.positive?27fullurls << target28# relative URLs29else3031# Retrieve the list of network interfaces from BeEF::Core::Console::Banners32interfaces = BeEF::Core::Console::Banners.interfaces3334if not interfaces.nil? and not interfaces.empty? # If interfaces are available, iterate over each network interface35# If interfaces are available, iterate over each network interface36interfaces.each do |int|37# Skip the loop iteration if the interface address is '0.0.0.0' (which generally represents all IPv4 addresses on the local machine)38next if int == '0.0.0.0'39# Construct full URLs using the network interface address, and add them to the fullurls array40# The URL is composed of the BeEF protocol, interface address, BeEF port, and the target path41fullurls << "#{beef_proto}://#{int}:#{beef_port}#{target}"42end43end4445end46end4748return unless fullurls.empty?4950img_dir = 'extensions/qrcode/images'51begin52Dir.mkdir(img_dir) unless File.directory?(img_dir)53rescue StandardError54print_error "[QR] Could not create directory '#{img_dir}'"55end5657data = ''58fullurls.uniq.each do |target|59fname = ('a'..'z').to_a.sample(8).join60qr_path = "#{img_dir}/#{fname}.png"61begin62Qr4r.encode(63target, qr_path, {64pixel_size: configuration.get('beef.extension.qrcode.qrsize'),65border: configuration.get('beef.extension.qrcode.qrborder')66}67)68rescue StandardError69print_error "[QR] Could not write file '#{qr_path}'"70next71end7273print_debug "[QR] Wrote file '#{qr_path}'"74BeEF::Core::NetworkStack::Handlers::AssetHandler.instance.bind(75"/#{qr_path}", "/qrcode/#{fname}", 'png'76)7778data += "#{beef_proto}://#{beef_host}:#{beef_port}/qrcode/#{fname}.png\n"79data += "- URL: #{target}\n"80# Google API81# url = URI::Parser.new.escape(target,Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))82# w = configuration.get("beef.extension.qrcode.qrsize").to_i * 10083# h = configuration.get("beef.extension.qrcode.qrsize").to_i * 10084# data += "- Google API: https://chart.googleapis.com/chart?cht=qr&chs=#{w}x#{h}&chl=#{url}\n"85# QRServer.com86# url = URI::Parser.new.escape(target,Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))87# w = configuration.get("beef.extension.qrcode.qrsize").to_i * 10088# h = configuration.get("beef.extension.qrcode.qrsize").to_i * 10089# data += "- QRServer API: https://api.qrserver.com/v1/create-qr-code/?size=#{w}x#{h}&data=#{url}\n"90end9192print_info 'QR code images available:'93print_more data94end95end96end97end98end99100101