Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/extensions/dns/api.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 Dns
9
module API
10
module NameserverHandler
11
BeEF::API::Registrar.instance.register(
12
BeEF::Extension::Dns::API::NameserverHandler,
13
BeEF::API::Server,
14
'pre_http_start'
15
)
16
17
BeEF::API::Registrar.instance.register(
18
BeEF::Extension::Dns::API::NameserverHandler,
19
BeEF::API::Server,
20
'mount_handler'
21
)
22
23
# Starts the DNS nameserver at BeEF startup.
24
#
25
# @param http_hook_server [BeEF::Core::Server] HTTP server instance
26
def self.pre_http_start(_http_hook_server)
27
servers, interfaces, address, port, protocol, upstream_servers = get_dns_config # get the DNS configuration
28
29
# Start the DNS server
30
dns = BeEF::Extension::Dns::Server.instance
31
dns.run(upstream: servers, listen: interfaces)
32
end
33
34
def self.print_dns_info
35
servers, interfaces, address, port, protocol, upstream_servers = get_dns_config # get the DNS configuration
36
37
# Print the DNS server information
38
print_info "DNS Server: #{address}:#{port} (#{protocol})"
39
print_more upstream_servers unless upstream_servers.empty?
40
end
41
42
def self.get_dns_config
43
dns_config = BeEF::Core::Configuration.instance.get('beef.extension.dns')
44
45
protocol = begin
46
dns_config['protocol'].to_sym
47
rescue StandardError
48
:udp
49
end
50
address = dns_config['address'] || '127.0.0.1'
51
port = dns_config['port'] || 5300
52
interfaces = [[protocol, address, port]]
53
54
servers = []
55
upstream_servers = ''
56
57
unless dns_config['upstream'].nil? || dns_config['upstream'].empty?
58
dns_config['upstream'].each do |server|
59
up_protocol = server[0].downcase
60
up_address = server[1]
61
up_port = server[2]
62
63
next if [up_protocol, up_address, up_port].include?(nil)
64
65
servers << [up_protocol.to_sym, up_address, up_port] if up_protocol =~ /^(tcp|udp)$/
66
upstream_servers << "Upstream Server: #{up_address}:#{up_port} (#{up_protocol})\n"
67
end
68
end
69
70
return servers, interfaces, address, port, protocol, upstream_servers
71
end
72
73
# Mounts the handler for processing DNS RESTful API requests.
74
#
75
# @param beef_server [BeEF::Core::Server] HTTP server instance
76
def self.mount_handler(beef_server)
77
beef_server.mount('/api/dns', BeEF::Extension::Dns::DnsRest.new)
78
end
79
end
80
end
81
end
82
end
83
end
84
85