Path: blob/master/extensions/network/models/network_host.rb
1154 views
#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 Core7module Models8#9# Table stores each host identified on the zombie browser's network(s)10#11class NetworkHost < BeEF::Core::Model12belongs_to :hooked_browser1314#15# Stores a network host in the data store16#17def self.add(host = {})18unless BeEF::Filters.is_valid_hook_session_id?(host[:hooked_browser_id])19print_error 'Invalid hooked browser session'20return21end22unless BeEF::Filters.is_valid_ip?(host[:ip])23print_error 'Invalid IP address'24return25end2627# save network hosts with private IP addresses only?28unless BeEF::Filters.is_valid_private_ip?(host[:ip])29configuration = BeEF::Core::Configuration.instance30if configuration.get('beef.extension.network.ignore_public_ips') == true31print_debug "Ignoring network host with public IP address [ip: #{host[:ip]}]"32return33end34end3536# prepare new host for data store37new_host = {}38new_host[:hooked_browser_id] = host[:hooked_browser_id]39new_host[:ip] = host[:ip]40new_host[:hostname] = host[:hostname] unless host[:hostname].nil?41new_host[:ntype] = host[:ntype] unless host[:ntype].nil?42new_host[:os] = host[:os] unless host[:os].nil?43new_host[:mac] = host[:mac] unless host[:mac].nil?4445# if host already exists in data store with the same details46# then update lastseen and return47existing_host = BeEF::Core::Models::NetworkHost.where(hooked_browser_id: new_host[:hooked_browser_id], ip: new_host[:ip]).limit(1)48unless existing_host.empty?49existing_host = existing_host.first50existing_host.lastseen = Time.new.to_i51existing_host.save!52return53end5455# store the new network host details56new_host[:lastseen] = Time.new.to_i57network_host = BeEF::Core::Models::NetworkHost.new(new_host)58if network_host.save59print_error 'Failed to save network host'60return61end6263network_host64end6566#67# Removes a network host from the data store68#69def self.delete(id)70unless BeEF::Filters.nums_only?(id.to_s)71print_error 'Failed to remove network host. Invalid host ID.'72return73end7475host = BeEF::Core::Models::NetworkHost.find(id.to_i)76if host.nil?77print_error "Failed to remove network host [id: #{id}]. Host does not exist."78return79end80host.destroy81end8283#84# Convert a Network Host object to JSON85#86def to_h87{88id: id,89hooked_browser_id: hooked_browser_id,90ip: ip,91hostname: hostname,92ntype: ntype,93os: os,94mac: mac,95lastseen: lastseen96}97end98end99end100end101end102103104