/
service.rb
executable file
·56 lines (45 loc) · 1.52 KB
/
service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Mdm::Service < ActiveRecord::Base
#
# Callbacks
#
after_save :normalize_host_os
#
# CONSTANTS
#
STATES = ['open', 'closed', 'filtered', 'unknown']
#
# Relations
#
has_many :creds, :dependent => :destroy, :class_name => 'Mdm::Cred'
has_many :exploited_hosts, :dependent => :destroy, :class_name => 'Mdm::ExploitedHost'
belongs_to :host, :class_name => 'Mdm::Host', :counter_cache => :service_count
has_many :notes, :dependent => :destroy, :class_name => 'Mdm::Note'
has_many :vulns, :dependent => :destroy, :class_name => 'Mdm::Vuln'
has_many :web_sites, :dependent => :destroy, :class_name => 'Mdm::WebSite'
#
# Through :web_sites
#
has_many :web_pages, :through => :web_sites, :class_name => 'Mdm::WebPage'
has_many :web_forms, :through => :web_sites, :class_name => 'Mdm::WebForm'
has_many :web_vulns, :through => :web_sites, :class_name => 'Mdm::WebVuln'
#
# Scopes
#
scope :inactive, where("services.state != 'open'")
scope :with_state, lambda { |a_state| where("services.state = ?", a_state)}
scope :search, lambda { |*args|
where([
"services.name ILIKE ? OR " +
"services.info ILIKE ? OR " +
"services.proto ILIKE ? OR " +
"services.port = ? ",
"%#{args[0]}%", "%#{args[0]}%", "%#{args[0]}%", (args[0].to_i > 0) ? args[0].to_i : 99999
])
}
def normalize_host_os
if info_changed?
host.normalize_os
end
end
ActiveSupport.run_load_hooks(:mdm_service, self)
end