Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/core/main/geoip.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
7
module BeEF
8
module Core
9
class GeoIp
10
include Singleton
11
12
def initialize
13
@config = BeEF::Core::Configuration.instance
14
@enabled = @config.get('beef.geoip.enable') ? true : false
15
16
return unless @enabled
17
18
geoip_file = @config.get('beef.geoip.database')
19
20
unless File.exist? geoip_file
21
BeEF::Core::Logger.instance.register('System', "[GeoIP] Could not find MaxMind GeoIP database: '#{geoip_file}'")
22
@enabled = false
23
return
24
end
25
26
require 'maxmind/db'
27
@geoip_reader = MaxMind::DB.new(geoip_file, mode: MaxMind::DB::MODE_MEMORY)
28
@geoip_reader.freeze
29
rescue StandardError => e
30
print_error "[GeoIP] Failed to load GeoIP database: #{e.message}"
31
@enabled = false
32
end
33
34
#
35
# Check if GeoIP functionality is enabled and functional
36
#
37
# @return [Boolean] GeoIP functionality enabled?
38
#
39
def enabled?
40
@enabled
41
end
42
43
#
44
# Search the MaxMind GeoLite2 database for the specified IP address
45
#
46
# @param [String] The IP address to lookup
47
#
48
# @return [Hash] IP address lookup results
49
#
50
def lookup(ip)
51
raise TypeError, '"ip" needs to be a string' unless ip.is_a?(String)
52
53
return unless @enabled
54
55
@geoip_reader.get(ip)
56
end
57
end
58
end
59
end
60
61