Module: PWN::Plugins::Log

Defined in:
lib/pwn/plugins/log.rb

Overview

This plugin is used to instantiate a PWN logger with a custom message format

Class Method Summary collapse

Class Method Details

.append(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Log.create( )



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pwn/plugins/log.rb', line 14

public_class_method def self.append(opts = {})
  level = opts[:level].to_s.downcase.to_sym
  msg = opts[:msg]
  which_self = opts[:which_self].to_s

  driver_name = File.basename($PROGRAM_NAME)

  # Only attempt to exit gracefully if level == :error
  exit_gracefully = false

  # Define Date / Time Format
  datetime_str = '%Y-%m-%d %H:%M:%S.%N%z'

  # Always append to log file
  if level == :learning
    session = SecureRandom.hex
    log_file_path = "/tmp/pwn-ai-#{session}.json" if level == :learning
    log_file = File.open(log_file_path, 'w')
  else
    log_file_path = '/tmp/pwn.log'
    log_file = File.open(log_file_path, 'a')
  end

  # Leave 10 "old" log files where
  # each file is ~ 1,024,000 bytes
  logger = Logger.new(
    log_file,
    10,
    1_024_000
  )
  logger.datetime_format = datetime_str

  case level
  when :debug
    logger.level = Logger::DEBUG
  when :error
    logger.level = Logger::ERROR
    exit_gracefully = true unless driver_name == 'pwn'
    puts "\nERROR: See #{log_file_path} for more details." if driver_name == 'pwn'
  when :fatal
    logger.level = Logger::FATAL
    puts "\n FATAL ERROR: See #{log_file_path} for more details." if driver_name == 'pwn'
  when :info, :learning
    logger.level = Logger::INFO
  when :unknown
    logger.level = Logger::UNKNOWN
  when :warn
    logger.level = Logger::WARN
  else
    level_error = "ERROR: Invalid log level. Valid options are:\n"
    level_error += ":debug\n:error\n:fatal\n:info\n:learning\n:unknown\n:warn\n"
    raise level_error
  end

  if level == :learning
    log_event = msg
    logger.formatter = proc do |_severity, _datetime, _progname, learning_arr|
      JSON.pretty_generate(
        learning_data: learning_arr
      )
    end
  else
    log_event = "driver: #{driver_name}"

    if msg.instance_of?(Interrupt)
      logger.level = Logger::WARN
      if driver_name == 'pwn'
        log_event += ' => CTRL+C Detected.'
      else
        log_event += ' => CTRL+C Detected...Exiting Session.'
        exit_gracefully = true unless driver_name == 'pwn'
      end
    else
      log_event += " => #{msg}"
      if msg.respond_to?('backtrace') && !msg.instance_of?(Errno::ECONNRESET)
        log_event += " => \n\t#{msg.backtrace.join("\n\t")}"
        log_event += "\n\n\n"
      end
    end
  end

  logger.add(logger.level, log_event, which_self)
rescue Interrupt
  puts "\n#{self}.#{__method__} => Goodbye."
rescue StandardError => e
  raise e
end

.authorsObject

Author(s)

0day Inc. <[email protected]>



104
105
106
107
108
# File 'lib/pwn/plugins/log.rb', line 104

public_class_method def self.authors
  "AUTHOR(S):
    0day Inc. <[email protected]>
  "
end

.helpObject

Display Usage for this Module



112
113
114
115
116
117
118
119
120
# File 'lib/pwn/plugins/log.rb', line 112

public_class_method def self.help
  puts "USAGE:
    logger = #{self}.append(
      level: 'required - log verbosity :debug|:error|:fatal|:info|:learning|:unknown|:warn',
      msg: 'required - message to log',
      which_self: 'required - pass in self object from module calling #{self}'
    )
  "
end