Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/plugins/socket_logger.rb
Views: 11704
module Msf1###2#3# This class hooks all sockets created by a running exploit4#5###67class Plugin::SocketLogger < Msf::Plugin89###10#11# This class implements a socket communication logger12#13###14class MySocketEventHandler15include Rex::Socket::Comm::Events1617def initialize(path, prefix)18@path = path19@prefix = prefix20end2122def on_before_socket_create(comm, param); end2324def on_socket_created(_comm, sock, param)25# Sockets created by the exploit have MsfExploit set and MsfPayload not set26if param.context && param.context['MsfExploit'] && !param.context['MsfPayload']27sock.extend(SocketLogger::SocketTracer)28sock.context = param.context29sock.params = param30sock.initlog(@path, @prefix)31end32end33end3435def initialize(framework, opts)36log_path = opts['path'] || Msf::Config.log_directory37log_prefix = opts['prefix'] || 'socket_'3839super40@eh = MySocketEventHandler.new(log_path, log_prefix)41Rex::Socket::Comm::Local.register_event_handler(@eh)42end4344def cleanup45Rex::Socket::Comm::Local.deregister_event_handler(@eh)46end4748def name49'socket_logger'50end5152def desc53'Log socket operations to a directory as individual files'54end5556end57end5859# This module extends the captured socket instance60module SocketLogger61module SocketTracer62@@last_id = 06364attr_accessor :context, :params6566# Hook the write method67def write(buf, opts = {})68@fd.puts "WRITE\t#{buf.length}\t#{Rex::Text.encode_base64(buf)}"69@fd.flush70super(buf, opts)71end7273# Hook the read method74def read(length = nil, opts = {})75r = super(length, opts)76@fd.puts "READ\t#{r ? r.length : 0}\t#{Rex::Text.encode_base64(r.to_s)}"77@fd.flush78return r79end8081def close(*args)82super(*args)83@fd.close84end8586def format_socket_conn87"#{params.proto.upcase} #{params.localhost}:#{params.localport} > #{params.peerhost}:#{params.peerport}"88end8990def format_module_info91return '' unless params.context && params.context['MsfExploit']92if params.context['MsfExploit'].respond_to? :fullname93return 'via ' + params.context['MsfExploit'].fullname94end9596'via ' + params.context['MsfExploit'].to_s97end9899def initlog(path, prefix)100@log_path = path101@log_prefix = prefix102@log_id = @@last_id103@@last_id += 1104@fd = File.open(File.join(@log_path, "#{@log_prefix}#{@log_id}.log"), 'w')105@fd.puts "Socket created at #{Time.now} (#{Time.now.to_i})"106@fd.puts "Info: #{format_socket_conn} #{format_module_info}"107@fd.puts ''108@fd.flush109end110end111end112113114