Class: RCS::Backdoor::Application

Inherits:
Object
  • Object
show all
Includes:
Tracer
Defined in:
lib/rcs-backdoor/backdoor.rb

Overview

this module is used only form bin/rcs-backdoor as a wrapper to execute the backdoor from command line

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run!(*argv) ⇒ Object

since we cannot use trace from a class method we instantiate here an object and run it



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/rcs-backdoor/backdoor.rb', line 263

def self.run!(*argv)
  # This hash will hold all of the options parsed from the command-line by OptionParser.
  options = {}

  srand(Time.now.to_i)
  
  types = [:RANDOM] + RCS::EVIDENCE_TYPES.values
  
  optparse = OptionParser.new do |opts|
    # Set a banner, displayed at the top of the help screen.
    opts.banner = "Usage: rcs-backdoor [options] arg1 arg2 ..."

    # Define the options, and what they do
    opts.on( '-r', '--randomize NUM', Integer, 'Randomize NUM instances') do |num|
      options[:randomize] = num
    end
    opts.on( '-g', '--generate NUM', Integer, 'Generate NUM evidences' ) do |num|
      options[:generate] = true
      options[:gen_num] = num
    end
    opts.on( '-t', '--type TYPE', types, 'Generate evidences of type TYPE' ) do |type|
      options[:gen_type] = type
    end
    opts.on( '-s', '--sync HOST', 'Synchronize with remote HOST' ) do |host|
      options[:sync] = true
      options[:sync_host] = host
    end
    opts.on('--tag STRING', 'Append to instance string' ) do |tag|
      options[:tag] = tag
    end
    opts.on( '-l', '--loop DELAY', Integer, 'Loop synchronization every DELAY seconds') do |seconds|
      options[:loop] = true
      options[:loop_delay] = seconds
    end
    opts.on( '-c', '--config CONFIGURATION', String, 'Configuration/environment name') do |value|
      options[:config_name] = value
    end
    opts.on( '--scout', 'Auth like a scout' ) do
      options[:scout] = true
    end
    opts.on( '--soldier', 'Auth like a soldier' ) do
      options[:soldiers] = true
    end

    # This displays the help screen, all programs are assumed to have this option.
    opts.on( '-h', '--help', 'Display this screen' ) do
      puts opts
      exit
    end
  end

  optparse.parse(argv)

  return Application.new.run(options)
end

Instance Method Details

#run(options) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/rcs-backdoor/backdoor.rb', line 205

def run(options)

  # if we can't find the trace config file, default to the system one
  if File.exist?('trace.yaml') then
    load_path = Dir.pwd
    trace_yaml = 'trace.yaml'
  else
    load_path = File.dirname(File.dirname(File.dirname(__FILE__))) + "/bin"
    trace_yaml = load_path + "/trace.yaml"
    puts "Cannot find 'trace.yaml' using the default one (#{trace_yaml})"
  end
  
  # initialize the tracing facility
  begin
    trace_init load_path, trace_yaml
  rescue Exception => e
    puts e
    exit
  end

  unless options[:randomize].nil?
    binary = begin
      File.open(load_path + '/ident.yaml', "r") do |f|
        binary = YAML.load(f.read)
      end
    rescue
      trace :fatal, "Cannot open binary patched file"
      exit
    end

    threads = []
    options[:randomize].times do |n|
      binary["INSTANCE_ID"] = Digest::SHA1.hexdigest(n.to_s).upcase
      path_to_yaml = load_path + "/ident#{n}.yaml"
      File.open(path_to_yaml, "w") {|f| f.write(binary.to_yaml)}
      trace :info, "Spawned backdoor #{path_to_yaml}"
      threads << Thread.new { run_backdoor(load_path + '/binary.yaml', path_to_yaml, options)}
    end

    begin
      threads.each do |th|
        th.join
      end
    rescue Interrupt
      trace :info, "User asked to exit. Bye bye!"
      return 0
    end

  else
    run_backdoor(load_path + '/binary.yaml', load_path + '/ident.yaml', options)
  end

  # concluded successfully
  return 0
end

#run_backdoor(path_to_binary, path_to_ident, options) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rcs-backdoor/backdoor.rb', line 172

def run_backdoor(path_to_binary, path_to_ident, options)
  b = RCS::Backdoor::Backdoor.new(path_to_binary, path_to_ident, options)

  # set the scout flag if specified
  b.scout = true if options[:scout]
  b.soldier = true if options[:soldier]

  if options[:generate] then
    trace :info, "Creating #{options[:gen_num]} fake evidences..."
    b.create_evidences(options[:gen_num], options[:gen_type])
  end
  
  while true
    if options[:sync] then
      b.sync options[:sync_host], !(options[:randomize] or options[:loop]) # delete evidences if not randomizing
    end
    
    break unless options[:loop]
    
    options[:loop_delay].times do |n|
      sleep 1
      trace :info, "#{options[:loop_delay] - n} seconds to next synchronization." if n % 5 == 0
    end 
  end
rescue Interrupt
  trace :info, "User asked to exit. Bye bye!"
  return 0
rescue Exception => e
  trace :fatal, "FAILURE: " << e.to_s
  trace :fatal, "EXCEPTION: " + e.backtrace.join("\n")
  return 1
end