hackedteam/rcs-collector

View on GitHub
bin/rcs-collector-diagnostic

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')

require 'bundler/setup'
require 'zip'
require 'pp'
require 'json'
require 'optparse'
require 'rcs-common/diagnosticable'
require 'rcs-common/path_utils'
require 'rcs-common/winfirewall'

$options = {}

OptionParser.new do |parser|
  parser.banner << "\n\nZip generation options:"

  parser.on("-o", "--output PATH", "Specify the output file path") { |value|
    $options[:output] = value
  }

  parser.on("--hide-addresses", "Mask ip addresses and domain names that may appear") { |value|
    $options[:hide_addresses] = true
  }

  parser.separator "\nDiagnostic options:"

  parser.on("-h", "--help", "Show this message") {
    puts parser
    exit
  }

  parser.on("--log-level INFO|DEBUG", "Change the log devel") { |value|
    value.upcase!
    raise "Ivalid log level #{value}" unless %w[INFO DEBUG].include?(value)
    $options[:log_level] = value
  }

  parser.on("-v", "--version", "Print the RCS version") do
    $options[:version] = true
  end
end.parse!

include RCS::Diagnosticable

if level = $options[:log_level]
  puts "Changing log level to #{level}"
  change_trace_level(level)
  exit
end

if $options[:version]
  version, build = get_version_info
  puts "RCS version #{version} build #{build}"
  exit
end

zipname = $options[:output] || File.basename(__FILE__)
zipname << ".zip" unless zipname =~ /zip$/i

puts "Execution directory is #{execution_directory}"
puts "Generating file #{zipname}. It may take a while."

buffer = Zip::OutputStream.write_buffer do |out|
  relevant_logs.each do |path|
    if huge_log?(path)
      puts "Warning: #{path} is too big"
      next
    end

    entry = path[path.index('log/')..-1]
    out.put_next_entry(entry)
    out.write(hide_addresses(File.read(path)))
  end

  config_files.each do |path|
    entry = path[path.index('config/')..-1]
    out.put_next_entry(entry)
    content = File.read(path)
    content = hide_addresses(content) unless %w[.pem .crt].include?(File.extname(path))
    out.write(content)
  end

  ['rcs-collector-stats', 'rcs-collector-queue', 'rcs-carrier-stats'].each do |command|
    out.put_next_entry(command.gsub(/(\-|\s)/, "_"))
    out.puts(hide_addresses(command_output(command)))
  end

  if windows?
    out.put_next_entry("environment/machine")
    pretty_print(machine_info, out)

    out.put_next_entry('environment/hosts')
    out.write(hide_addresses(File.read('C:\Windows\system32\drivers\etc\hosts')))

    out.put_next_entry('environment/ipconfig')
    out.write(hide_addresses(command_output("ipconfig")))

    out.put_next_entry("environment/systeminfo")
    out.write(hide_addresses(command_output("systeminfo")))

    out.put_next_entry("environment/tree")
    out.write(hide_addresses(command_output("tree")))

    out.put_next_entry("environment/services_state")
    out.puts(services_state)

    out.put_next_entry("environment/firewall_rules")
    out.puts(WinFirewall.raw_rules)
  end
end

File.open(zipname, "wb") {|f| f.write(buffer.string) }
puts "#{buffer.size} byte(s) written"