Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/extensions/network/models/network_host.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 Core
8
module Models
9
#
10
# Table stores each host identified on the zombie browser's network(s)
11
#
12
class NetworkHost < BeEF::Core::Model
13
belongs_to :hooked_browser
14
15
#
16
# Stores a network host in the data store
17
#
18
def self.add(host = {})
19
unless BeEF::Filters.is_valid_hook_session_id?(host[:hooked_browser_id])
20
print_error 'Invalid hooked browser session'
21
return
22
end
23
unless BeEF::Filters.is_valid_ip?(host[:ip])
24
print_error 'Invalid IP address'
25
return
26
end
27
28
# save network hosts with private IP addresses only?
29
unless BeEF::Filters.is_valid_private_ip?(host[:ip])
30
configuration = BeEF::Core::Configuration.instance
31
if configuration.get('beef.extension.network.ignore_public_ips') == true
32
print_debug "Ignoring network host with public IP address [ip: #{host[:ip]}]"
33
return
34
end
35
end
36
37
# prepare new host for data store
38
new_host = {}
39
new_host[:hooked_browser_id] = host[:hooked_browser_id]
40
new_host[:ip] = host[:ip]
41
new_host[:hostname] = host[:hostname] unless host[:hostname].nil?
42
new_host[:ntype] = host[:ntype] unless host[:ntype].nil?
43
new_host[:os] = host[:os] unless host[:os].nil?
44
new_host[:mac] = host[:mac] unless host[:mac].nil?
45
46
# if host already exists in data store with the same details
47
# then update lastseen and return
48
existing_host = BeEF::Core::Models::NetworkHost.where(hooked_browser_id: new_host[:hooked_browser_id], ip: new_host[:ip]).limit(1)
49
unless existing_host.empty?
50
existing_host = existing_host.first
51
existing_host.lastseen = Time.new.to_i
52
existing_host.save!
53
return
54
end
55
56
# store the new network host details
57
new_host[:lastseen] = Time.new.to_i
58
network_host = BeEF::Core::Models::NetworkHost.new(new_host)
59
if network_host.save
60
print_error 'Failed to save network host'
61
return
62
end
63
64
network_host
65
end
66
67
#
68
# Removes a network host from the data store
69
#
70
def self.delete(id)
71
unless BeEF::Filters.nums_only?(id.to_s)
72
print_error 'Failed to remove network host. Invalid host ID.'
73
return
74
end
75
76
host = BeEF::Core::Models::NetworkHost.find(id.to_i)
77
if host.nil?
78
print_error "Failed to remove network host [id: #{id}]. Host does not exist."
79
return
80
end
81
host.destroy
82
end
83
84
#
85
# Convert a Network Host object to JSON
86
#
87
def to_h
88
{
89
id: id,
90
hooked_browser_id: hooked_browser_id,
91
ip: ip,
92
hostname: hostname,
93
ntype: ntype,
94
os: os,
95
mac: mac,
96
lastseen: lastseen
97
}
98
end
99
end
100
end
101
end
102
end
103
104