Module: NewRelic::Security::Instrumentation::Kernel

Included in:
Prepend
Defined in:
lib/newrelic_security/instrumentation-security/kernel/chain.rb,
lib/newrelic_security/instrumentation-security/kernel/prepend.rb,
lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb

Defined Under Namespace

Modules: Chain, Prepend

Instance Method Summary collapse

Instance Method Details

#backtick_on_enter(cmd) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 54

def backtick_on_enter(cmd)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  event = NewRelic::Security::Agent::Control::Collector.collect(SYSTEM_COMMAND, Array(cmd))
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
  return event
end

#backtick_on_exit(event) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 65

def backtick_on_exit(event)
  NewRelic::Security::Agent.logger.debug "OnExit :  #{self.class}.#{__method__}"
  NewRelic::Security::Agent::Utils.create_exit_event(event) if $? && $?.exitstatus == 0
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
end

#exec_on_enter(*var) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 98

def exec_on_enter(*var)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  event = NewRelic::Security::Agent::Control::Collector.collect(SYSTEM_COMMAND, var)
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
  return event
end

#exec_on_exit(event) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 109

def exec_on_exit(event)
  NewRelic::Security::Agent.logger.debug "OnExit :  #{self.class}.#{__method__}"
  NewRelic::Security::Agent.logger.debug "Exit event : #{event}"
  # TODO: Add exit event if required
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
end

#open_on_enter(*args, **kwargs) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 119

def open_on_enter(*args, **kwargs)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  fname = ::File.path(args[0]) #some times it is 'String' or 'Path' class
  if fname.start_with?(PIPE)
    event = NewRelic::Security::Agent::Control::Collector.collect(SYSTEM_COMMAND, args)
  else
    abs_path = ::File.expand_path(fname)
    if args.length == 2
      fmode = args[1]
      event_category = NewRelic::Security::Instrumentation::InstrumentationUtils::OPEN_MODES.include?(fmode) ? READ : WRITE
      if NewRelic::Security::Instrumentation::InstrumentationUtils.notify_app_integrity_open?(fname, abs_path, fmode)
        event = NewRelic::Security::Agent::Control::Collector.collect(FILE_INTEGRITY, Array(fname), event_category)
      else
        if NewRelic::Security::Instrumentation::InstrumentationUtils.read_filter?(fname, abs_path)
          NewRelic::Security::Agent.logger.debug "Filtered because File name exist in filtered list #{self.class}.#{__method__} Args:: #{fname} #{fmode}"
        else
          event = NewRelic::Security::Agent::Control::Collector.collect(FILE_OPERATION, Array(fname), event_category)
        end
      end
    else
      if NewRelic::Security::Instrumentation::InstrumentationUtils.read_filter?(fname, abs_path)
        NewRelic::Security::Agent.logger.debug "Filtered because File name exist in filtered list #{self.class}.#{__method__} Args:: #{args}"
      else
        event = NewRelic::Security::Agent::Control::Collector.collect(FILE_OPERATION, Array(fname), READ)
      end
    end
  end
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
  return event
end

#open_on_exit(event, retval) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 154

def open_on_exit(event, retval)
  NewRelic::Security::Agent.logger.debug "OnExit :  #{self.class}.#{__method__}"
  NewRelic::Security::Agent::Utils.create_exit_event(event) if retval.is_a?(Integer)
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
end

#require_on_enter(name) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 8

def require_on_enter(name)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
  return event
end

#require_on_exit(event, retval, name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 18

def require_on_exit(event, retval, name)
  NewRelic::Security::Agent.logger.debug "OnExit :  #{self.class}.#{__method__}"
  if retval
    NewRelic::Security::Agent.logger.info "Dynamic loading of #{name} module observed, TODO: Call Instrumentation API"
     # TODO: Call Instrumentation API here (Dynamic loading of module observed)
  end
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
end

#spawn_on_enter(*var) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 74

def spawn_on_enter(*var)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  ic_args = []
  var.each { |arg| 
    ic_args << arg if arg.is_a? String
  }
  event = NewRelic::Security::Agent::Control::Collector.collect(SYSTEM_COMMAND, ic_args)
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
  return event
end

#spawn_on_exit(event, retval) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 89

def spawn_on_exit(event, retval)
  NewRelic::Security::Agent.logger.debug "OnExit :  #{self.class}.#{__method__}"
  NewRelic::Security::Agent::Utils.create_exit_event(event) if retval.is_a?(Integer)
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
end

#system_on_enter(*var) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 30

def system_on_enter(*var)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  ic_args = []
  var.each { |arg| 
    ic_args << arg if arg.is_a? String
  }
  event = NewRelic::Security::Agent::Control::Collector.collect(SYSTEM_COMMAND, ic_args)
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
  return event
end

#system_on_exit(event, retval) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/newrelic_security/instrumentation-security/kernel/instrumentation.rb', line 45

def system_on_exit(event, retval)
  NewRelic::Security::Agent.logger.debug "OnExit :  #{self.class}.#{__method__}"
  NewRelic::Security::Agent::Utils.create_exit_event(event) if retval
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
end