diff --git a/lib/msf/core/db_manager.rb b/lib/msf/core/db_manager.rb index e61f37d28e..d5220916a9 100644 --- a/lib/msf/core/db_manager.rb +++ b/lib/msf/core/db_manager.rb @@ -92,6 +92,7 @@ class DBManager autoload :Report, 'msf/core/db_manager/report' autoload :Service, 'msf/core/db_manager/service' autoload :Session, 'msf/core/db_manager/session' + autoload :SessionEvent, 'msf/core/db_manager/session_event' autoload :Sink, 'msf/core/db_manager/sink' autoload :Task, 'msf/core/db_manager/task' autoload :Vuln, 'msf/core/db_manager/vuln' @@ -120,6 +121,7 @@ class DBManager include Msf::DBManager::Report include Msf::DBManager::Service include Msf::DBManager::Session + include Msf::DBManager::SessionEvent include Msf::DBManager::Sink include Msf::DBManager::Task include Msf::DBManager::Vuln @@ -416,54 +418,6 @@ class DBManager } end - # - # Record a session event in the database - # - # opts MUST contain one of: - # +:session+:: the Msf::Session OR the ::Mdm::Session we are reporting - # +:etype+:: event type, enum: command, output, upload, download, filedelete - # - # opts may contain - # +:output+:: the data for an output event - # +:command+:: the data for an command event - # +:remote_path+:: path to the associated file for upload, download, and filedelete events - # +:local_path+:: path to the associated file for upload, and download - # - def report_session_event(opts) - return if not active - raise ArgumentError.new("Missing required option :session") if opts[:session].nil? - raise ArgumentError.new("Expected an :etype") unless opts[:etype] - session = nil - - ::ActiveRecord::Base.connection_pool.with_connection { - if opts[:session].respond_to? :db_record - session = opts[:session].db_record - if session.nil? - # The session doesn't have a db_record which means - # a) the database wasn't connected at session registration time - # or - # b) something awful happened and the report_session call failed - # - # Either way, we can't do anything with this session as is, so - # log a warning and punt. - wlog("Warning: trying to report a session_event for a session with no db_record (#{opts[:session].sid})") - return - end - event_data = { :created_at => Time.now } - else - session = opts[:session] - event_data = { :created_at => opts[:created_at] } - end - - event_data[:session_id] = session.id - [:remote_path, :local_path, :output, :command, :etype].each do |attr| - event_data[attr] = opts[attr] if opts[attr] - end - - s = ::Mdm::SessionEvent.create(event_data) - } - end - def report_session_route(session, route) return if not active if session.respond_to? :db_record diff --git a/lib/msf/core/db_manager/session_event.rb b/lib/msf/core/db_manager/session_event.rb new file mode 100644 index 0000000000..6b53d3348c --- /dev/null +++ b/lib/msf/core/db_manager/session_event.rb @@ -0,0 +1,49 @@ +module Msf::DBManager::SessionEvent + # + # Record a session event in the database + # + # opts MUST contain one of: + # +:session+:: the Msf::Session OR the ::Mdm::Session we are reporting + # +:etype+:: event type, enum: command, output, upload, download, filedelete + # + # opts may contain + # +:output+:: the data for an output event + # +:command+:: the data for an command event + # +:remote_path+:: path to the associated file for upload, download, and filedelete events + # +:local_path+:: path to the associated file for upload, and download + # + def report_session_event(opts) + return if not active + raise ArgumentError.new("Missing required option :session") if opts[:session].nil? + raise ArgumentError.new("Expected an :etype") unless opts[:etype] + session = nil + + ::ActiveRecord::Base.connection_pool.with_connection { + if opts[:session].respond_to? :db_record + session = opts[:session].db_record + if session.nil? + # The session doesn't have a db_record which means + # a) the database wasn't connected at session registration time + # or + # b) something awful happened and the report_session call failed + # + # Either way, we can't do anything with this session as is, so + # log a warning and punt. + wlog("Warning: trying to report a session_event for a session with no db_record (#{opts[:session].sid})") + return + end + event_data = { :created_at => Time.now } + else + session = opts[:session] + event_data = { :created_at => opts[:created_at] } + end + + event_data[:session_id] = session.id + [:remote_path, :local_path, :output, :command, :etype].each do |attr| + event_data[attr] = opts[attr] if opts[attr] + end + + s = ::Mdm::SessionEvent.create(event_data) + } + end +end \ No newline at end of file