testing global log registration, spoonm's going to hate it!

git-svn-id: file:///home/svn/incoming/trunk@2500 4d416f70-5f16-0410-b530-b9f4589650da
unstable
Matt Miller 2005-05-22 02:50:35 +00:00
parent e78604b603
commit 4e124436e1
5 changed files with 110 additions and 78 deletions

View File

@ -13,8 +13,6 @@ module Msf
### ###
class Framework class Framework
include Msf::Logging::LogDispatcher
def initialize() def initialize()
self.events = EventDispatcher.new self.events = EventDispatcher.new
# self.encoders = EncoderManager.new # self.encoders = EncoderManager.new

View File

@ -8,61 +8,119 @@ module Logging
# LogDispatcher # LogDispatcher
# ------------- # -------------
# #
# This interface is included in the Framework class and is used to provide a # The log dispatcher associates log sources with log sinks. A log source
# common interface for dispatching logs to zero or more registered log sinks. # is a unique identity that is associated with one and only one log sink.
# Log sinks are typically backed against arbitrary storage mediums, such as a # For instance, the framework-core registers the 'core'
# flatfile, a database, or the console. The log dispatcher itself is really
# just a log sink that backs itself against zero or more log sinks rather than
# a file, database, or other persistent storage.
# #
### ###
module LogDispatcher class LogDispatcher
include Msf::Logging::LogSink
def initialize() def initialize()
initialize_log_dispatcher self.log_sinks = {}
end
#
# Log sink registration
#
def add_log_sink(sink)
log_sinks_rwlock.synchronize_write {
log_sinks << sink
}
end
def remove_log_sink(sink)
log_sinks_rwlock.synchronize_write {
sink.cleanup
log_sinks.delete(sink)
}
end
#
# Log dispatching
#
protected
def initialize_log_dispatcher
self.log_sinks = []
self.log_sinks_rwlock = ReadWriteLock.new self.log_sinks_rwlock = ReadWriteLock.new
end end
def log(sev, level, msg, from) # Returns the sink that is associated with the supplied source
def [](src)
sink = nil
log_sinks_rwlock.synchronize_read { log_sinks_rwlock.synchronize_read {
log_sinks.each { |sink| sink = log_sinks[src]
sink.dispatch_log(sev, level, msg, from) }
}
return sink
end
# Calls the source association routnie
def []=(src, sink)
store(src, sink)
end
# Associates the supplied source with the supplied sink
def store(src, sink)
log_sinks_rwlock.synchronize_write {
if (log_sinks[src] == nil)
log_sinks[src] = sink
else
raise(
RuntimeError,
"The supplied log source #{src} is already registered.",
caller)
end
} }
end end
attr_accessor :log_sinks # Removes a source association if one exists
attr_accessor :log_sinks_rwlock def delete(src)
sink = nil
log_sinks_rwlock.synchronize_write {
sink = log_sinks[src]
log_sinks.delete(src)
}
if (sink)
sink.cleanup
return true
end
return false
end
# Performs the actual log operation against the supplied source
def log(sev, src, level, msg, from)
log_sinks_rwlock.synchronize_read {
if ((sink = log_sinks[src]))
sink.log(sev, src, level, msg, from)
end
}
end
attr_accessor :log_sinks, :log_sinks_rwlock
end end
end; end end
end
###
#
# An instance of the log dispatcher exists in the global namespace, along
# with stubs for many of the common logging methods. Various sources can
# register themselves as a log sink such that logs can be directed at
# various targets depending on where they're sourced from. By doing it
# this way, things like sessions can use the global logging stubs and
# still be directed at the correct log file.
#
###
def dlog(msg, src = 'core', level = 0, from = caller)
$dispatcher.log(Msf::LOG_DEBUG, src, level, msg, from)
end
def elog(msg, src = 'core', level = 0, from = caller)
$dispatcher.log(Msf::LOG_ERROR, src, level, msg, from)
end
def wlog(msg, src = 'core', level = 0, from = caller)
$dispatcher.log(Msf::LOG_WARN, src, level, msg, from)
end
def ilog(msg, src = 'core', level = 0, from = caller)
$dispatcher.log(Msf::LOG_INFO, src, level, msg, from)
end
def rlog(msg, src = 'core', level = 0, from = caller)
$dispatcher.log(Msf::LOG_RAW, src, level, msg, from)
end
def register_log_source(src, sink)
$dispatcher[src] = sink
end
def deregister_log_source(src, sink)
$dispatcher.delete(src)
end
# Creates the global log dispatcher
$dispatcher = Msf::Logging::LogDispatcher.new

View File

@ -18,32 +18,12 @@ module LogSink
def cleanup def cleanup
end end
def dlog(level, msg, from = caller) def log(sev, src, level, msg, from)
log(LOG_DEBUG, level, msg, from) raise NotImplementedError
end
def elog(level, msg, from = caller)
log(LOG_ERROR, level, msg, from)
end
def wlog(level, msg, from = caller)
log(LOG_WARN, level, msg, from)
end
def ilog(level, msg, from = caller)
log(LOG_INFO, level, msg, from)
end
def rlog(msg)
log(LOG_RAW, 0, msg, nil)
end end
protected protected
def log(sev, level, msg, from)
raise NotImplementedError
end
def get_current_timestamp def get_current_timestamp
return Time.now.strftime("%m/%d/%Y %H:%M:%S") return Time.now.strftime("%m/%d/%Y %H:%M:%S")
end end

View File

@ -17,26 +17,22 @@ class Flatfile
def initialize(file) def initialize(file)
self.fd = File.new(file, "a") self.fd = File.new(file, "a")
ilog(0, "Logging initialized.")
end end
def cleanup def cleanup
ilog(0, "Logging finished.")
fd.close fd.close
end end
protected def log(sev, src, level, msg, from)
def log(sev, level, msg, from)
if (sev == LOG_RAW) if (sev == LOG_RAW)
fd.write(msg) fd.write(msg)
else else
fd.write("[#{get_current_timestamp}] (sev=#{sev},level=#{level}): #{msg}\n") fd.write("[#{get_current_timestamp}] (src=#{src},sev=#{sev},level=#{level}): #{msg}\n")
end end
end end
protected
attr_accessor :fd attr_accessor :fd
end end

View File

@ -4,9 +4,9 @@ require 'Msf/Core'
require 'Encoders/IA32/JmpCallAdditive' require 'Encoders/IA32/JmpCallAdditive'
require 'Nops/IA32/SingleByte' require 'Nops/IA32/SingleByte'
framework = Msf::Framework.new register_log_source('core', Msf::Logging::Sinks::Flatfile.new('/tmp/msfcli.log'))
framework.add_log_sink(Msf::Logging::Sinks::Flatfile.new('/tmp/msfcli.log')) dlog('yo yo yo')
#encoder = framework.encoders.instantiate('gen_ia32_jmp_call_additive') #encoder = framework.encoders.instantiate('gen_ia32_jmp_call_additive')
encoder = Msf::Encoders::Generic::IA32::JmpCallAdditive.new encoder = Msf::Encoders::Generic::IA32::JmpCallAdditive.new