CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/base/logging.rb
Views: 11780
1
# -*- coding: binary -*-
2
require 'rex'
3
module Msf
4
5
# This module provides an initialization interface for logging.
6
class Logging
7
8
#Is logging initialized
9
#@private
10
@@initialized = false
11
#Is session logging enabled
12
#@private
13
@@session_logging = false
14
15
# Initialize logging.
16
#
17
# @param log_sink_name [string] Log sink name.
18
# @return [void]
19
def self.init(log_sink_name = nil)
20
if (! @@initialized)
21
@@initialized = true
22
23
log_sink ||= Rex::Logging::LogSinkFactory.new(
24
log_sink_name,
25
Msf::Config.log_directory + File::SEPARATOR + "framework.log"
26
)
27
28
# Register each known log source
29
[
30
Rex::LogSource,
31
Rex::Socket::LogSource,
32
Msf::LogSource,
33
'base',
34
].each { |src|
35
register_log_source(src, log_sink)
36
}
37
end
38
end
39
40
# Enables a log source of name src. Creates the .log file in the
41
# configured directory if logging is not already enabled for this
42
# source.
43
#
44
# @param src [String] log source name.
45
# @param level [Integer] logging level.
46
# @return [void]
47
def self.enable_log_source(src, level = 0)
48
if (log_source_registered?(src) == false)
49
f = Rex::Logging::Sinks::Flatfile.new(
50
Msf::Config.log_directory + File::SEPARATOR + "#{src}.log")
51
52
register_log_source(src, f, level)
53
end
54
end
55
56
# Stops logging for a given log source.
57
#
58
# @param src [String] the log source to disable.
59
# @return [Boolean] true if successful. false if not.
60
def self.disable_log_source(src)
61
deregister_log_source(src)
62
end
63
64
# Sets whether or not session logging is to be enabled.
65
#
66
# @param tf [Boolean] true if enabling. false if disabling.
67
# @return [void]
68
def self.enable_session_logging(tf)
69
@@session_logging = tf
70
end
71
72
# Returns whether or not session logging is enabled.
73
#
74
# @return [Boolean] true if enabled. false if disabled.
75
def self.session_logging_enabled?
76
@@session_logging || false
77
end
78
79
# Starts logging for a given session.
80
#
81
# @param session [Msf::Session] the session to start logging on.
82
# @return [void]
83
def self.start_session_log(session)
84
if (log_source_registered?(session.log_source) == false)
85
f = Rex::Logging::Sinks::TimestampColorlessFlatfile.new(
86
Msf::Config.session_log_directory + File::SEPARATOR + "#{session.log_file_name}.log")
87
88
register_log_source(session.log_source, f)
89
90
rlog("\n[*] Logging started: #{Time.now}\n\n", session.log_source)
91
end
92
end
93
94
# Stops logging for a given session.
95
#
96
# @param session [Msf::Session] the session to stop logging.
97
# @return [Boolean] true if successful. false if not.
98
def self.stop_session_log(session)
99
rlog("\n[*] Logging stopped: #{Time.now}\n\n", session.log_source)
100
101
deregister_log_source(session.log_source)
102
end
103
104
end
105
106
end
107
108