Last active
April 6, 2022 19:14
-
-
Save zeroSteiner/7b046bdfefc8e50bac55b1a49d639791 to your computer and use it in GitHub Desktop.
Python logging.Handler for use in external Metasploit modules.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: