Skip to content

Instantly share code, notes, and snippets.

@zeroSteiner
Last active April 6, 2022 19:14
Show Gist options
  • Save zeroSteiner/7b046bdfefc8e50bac55b1a49d639791 to your computer and use it in GitHub Desktop.
Save zeroSteiner/7b046bdfefc8e50bac55b1a49d639791 to your computer and use it in GitHub Desktop.
Python logging.Handler for use in external Metasploit modules.
import logging
import metasploit.module as module
class MetasploitLogHandler(logging.Handler):
def emit(self, record):
log_entry = self.format(record)
level = 'debug'
if record.levelno >= logging.ERROR:
level = 'error'
elif record.levelno >= logging.WARNING:
level = 'warning'
elif record.levelno >= logging.INFO:
level = 'info'
module.log(log_entry, level)
return
@classmethod
def setup(cls, level=logging.DEBUG):
logger = logging.getLogger()
if level is not None:
logger.setLevel(level)
handler = cls()
logger.addHandler(handler)
return handler
MetasploitLogHandler.setup()
@Boslx
Copy link

Boslx commented Apr 6, 2022

Just as a note to all who use this:
Metasploit has some difficulties if there are newlines in the message to be logged. To solve the problem, you can do something like this:

for msplit in message.splitlines():
   logging.info(msplit )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment