Module: NewRelic::Security::Instrumentation::IO

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

Defined Under Namespace

Modules: Chain, Prepend

Instance Method Summary collapse

Instance Method Details

#binread_on_enter(*var) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 128

def binread_on_enter(*var)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  fname = ::File.path(var[0]) #some times it is 'String' or 'Path' class
  abs_path = ::File.expand_path(fname)
  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:: #{var}"
  else
    event = NewRelic::Security::Agent::Control::Collector.collect(FILE_OPERATION, [fname], READ)
  end
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
  return event
end

#binread_on_exit(event, retval) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 145

def binread_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?(String)
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
end

#binwrite_on_enter(*var) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 237

def binwrite_on_enter(*var)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  fname = ::File.path(var[0]) #some times it is 'String' or 'Path' class
  abs_path = ::File.expand_path(fname)
  fmode = BINWRITE
  if NewRelic::Security::Instrumentation::InstrumentationUtils.notify_app_integrity_open?(fname, abs_path, fmode)
    event = NewRelic::Security::Agent::Control::Collector.collect(FILE_INTEGRITY, [fname], WRITE)
  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, [fname], WRITE)
    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

#binwrite_on_exit(event, retval) ⇒ Object



259
260
261
262
263
264
265
266
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 259

def binwrite_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

#foreach_on_enter(*var) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 180

def foreach_on_enter(*var)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  fname = ::File.path(var[0]) #some times it is 'String' or 'Path' class
  abs_path = ::File.expand_path(fname)
  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:: #{var}"
  else
    event = NewRelic::Security::Agent::Control::Collector.collect(FILE_OPERATION, [fname], READ)
  end
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
  return event
end

#foreach_on_exit(event, retval) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 197

def foreach_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?(NilClass) || retval.is_a?(Enumerator)
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
end

#new_on_enter(*var) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 45

def new_on_enter(*var)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  fname = var[0].to_s
  if var[0].is_a? Integer
    fname = NewRelic::Security::Agent::Control::HTTPContext.get_context.cache[var[0].object_id.to_s].to_s if NewRelic::Security::Agent::Control::HTTPContext.get_context && NewRelic::Security::Agent::Control::HTTPContext.get_context.cache.key?(var[0].object_id.to_s)
  else 
    fname = ::File.path(var[0]) #some times it is 'String' or 'Path' class
  end
  abs_path = ::File.expand_path(fname)
  fmode = var[1] if var[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, [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, [fname], event_category)
    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

#new_on_exit(event) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 73

def new_on_exit(event)
  NewRelic::Security::Agent.logger.debug "OnExit :  #{self.class}.#{__method__}"
  NewRelic::Security::Agent::Utils.create_exit_event(event)
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) ⇒ Object



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
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 8

def open_on_enter(*args)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  fname = args[0].to_s
  if args[0].is_a? Integer
    fname = NewRelic::Security::Agent::Control::HTTPContext.get_context.cache[args[0].object_id.to_s].to_s if NewRelic::Security::Agent::Control::HTTPContext.get_context && NewRelic::Security::Agent::Control::HTTPContext.get_context.cache.key?(args[0].object_id.to_s)
  else 
    fname = ::File.path(args[0]) if args[0] #some times it is 'String' or 'Path' class
  end
  abs_path = ::File.expand_path(fname)
  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, [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, [fname], event_category)
    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) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 36

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

#popen_on_enter(*var) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 268

def popen_on_enter(*var)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  ic_args = []
  var.each { |arg| 
    if arg.is_a? String
      ic_args << arg
    elsif arg.is_a? Array
      ic_args << arg.join(" ")
    end 
  }
  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

#popen_on_exit(event) ⇒ Object



287
288
289
290
291
292
293
294
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 287

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

#read_on_enter(*var) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 102

def read_on_enter(*var)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  fname = ::File.path(var[0]) #some times it is 'String' or 'Path' class
  abs_path = ::File.expand_path(fname)
  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:: #{var}"
  else
    event = NewRelic::Security::Agent::Control::Collector.collect(FILE_OPERATION, [fname], READ)
  end
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
  return event
end

#read_on_exit(event, retval) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 119

def read_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?(String)
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
end

#readlines_on_enter(*var) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 154

def readlines_on_enter(*var)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  fname = ::File.path(var[0]) #some times it is 'String' or 'Path' class
  abs_path = ::File.expand_path(fname)
  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:: #{var}"
  else
    event = NewRelic::Security::Agent::Control::Collector.collect(FILE_OPERATION, [fname], READ)
  end
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
  return event
end

#readlines_on_exit(event, retval) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 171

def readlines_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?(Array)
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
end

#sysopen_on_enter(*var) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 82

def sysopen_on_enter(*var)
  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

#sysopen_on_exit(event, retval, *var) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 92

def sysopen_on_exit(event, retval, *var)
  NewRelic::Security::Agent.logger.debug "OnExit :  #{self.class}.#{__method__}"
  fname = ::File.path(var[0]) #some times it is 'String' or 'Path' class
  NewRelic::Security::Agent::Control::HTTPContext.get_context.cache[retval.object_id.to_s] = fname if NewRelic::Security::Agent::Control::HTTPContext.get_context
rescue => exception
  NewRelic::Security::Agent.logger.error "Exception in hook in #{self.class}.#{__method__}, #{exception.inspect}, #{exception.backtrace}"
ensure
  yield
end

#write_on_enter(*var, **kwargs) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 206

def write_on_enter(*var, **kwargs)
  event = nil
  NewRelic::Security::Agent.logger.debug "OnEnter : #{self.class}.#{__method__}"
  fname = ::File.path(var[0]) #some times it is 'String' or 'Path' class
  abs_path = ::File.expand_path(fname)
  fmode = kwargs.has_key?(:mode) ? kwargs[:mode] : WRITE
  if NewRelic::Security::Instrumentation::InstrumentationUtils.notify_app_integrity_open?(fname, abs_path, fmode)
    event = NewRelic::Security::Agent::Control::Collector.collect(FILE_INTEGRITY, [fname], WRITE)
  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, [fname], WRITE)
    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

#write_on_exit(event, retval) ⇒ Object



228
229
230
231
232
233
234
235
# File 'lib/newrelic_security/instrumentation-security/io/instrumentation.rb', line 228

def write_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