Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a log handler for external Python modules #9739

Merged
merged 1 commit into from
Mar 21, 2018

Conversation

zeroSteiner
Copy link
Contributor

@zeroSteiner zeroSteiner commented Mar 21, 2018

This adds a LogHandler and LogFormatter class to the metasploit.module Python module. This allows external modules written in Python to keep existing logging statements from their code or imported libraries and have the information properly relayed to the Metasploit Framework.

Setup is simple, one of these two options should fit most of the use cases:

# Configure the Metasploit log handler for the root logger at the DEBUG level
module.LogHandler.setup()
# Do the same, but this time include the rhost:rport prefix (as commonly seen in scanner modules)
module.LogHandler.setup(msg_prefix="{0} - ".format(args['rhost']))

The LogFormatter class takes a prefix argument which is not a format string like typical logging.Formatter arguments are. This means we don't have to worry about escaping it or anything like that.

Verification

  • Create a module for testing at ~/.msf4/modules/auxiliary/test/scanner/python_logging.py
    • Use the "Testing Module" contents below and ensure that the file is executable
  • Start msfconsole
  • use auxiliary/test/scanner/python_logging
  • set RHOSTS 1.2.3.4
  • Run the module and see "Hello World!" from Python's builtin logging module

Testing Module

#!/usr/bin/env python
import logging
import metasploit.module as module

metadata = {
    'name': 'Test Python Logging',
    'description': '''
        Test Python's ability to utilize the builtin logging module from the
        metasploit framework.
     ''',
    'authors': ['Spencer McIntyre'],
    'date': '2018-03-20',
    'references': [],
    'type': 'single_scanner',
    'options': {},
}

def run(args):
    module.LogHandler.setup(msg_prefix="{0} - ".format(args['rhost']))
    logging.info('Hello World!')

if __name__ == '__main__':
    module.run(metadata, run)

Example Output

metasploit-framework (S:0 J:1) > use auxiliary/test/scanner/python_logging
metasploit-framework (S:0 J:1) auxiliary(test/scanner/python_logging) > set RHOSTS 1.2.3.4
RHOSTS => 1.2.3.4
metasploit-framework (S:0 J:1) auxiliary(test/scanner/python_logging) > run

[*] [2018.03.21-10:37:32] Running for 1.2.3.4...
[*] [2018.03.21-10:37:32] 1.2.3.4 - Hello World!
[*] [2018.03.21-10:37:32] Scanned 1 of 1 hosts (100% complete)
[*] Auxiliary module execution completed
metasploit-framework (S:0 J:1) auxiliary(test/scanner/python_logging) > 

@zeroSteiner zeroSteiner requested a review from acammack-r7 March 21, 2018 14:37
@acammack-r7 acammack-r7 self-assigned this Mar 21, 2018
@acammack-r7 acammack-r7 merged commit 3aa37eb into rapid7:master Mar 21, 2018
acammack-r7 added a commit that referenced this pull request Mar 21, 2018
@acammack-r7
Copy link
Contributor

acammack-r7 commented Mar 21, 2018

Release Notes

The new LogHandler and LogFormatter classes in the metasploit.module Python module allow external modules written in Python to keep existing logging statements from their code or imported libraries and have the information properly relayed to the Metasploit Framework.

Copy link
Contributor

@acammack-r7 acammack-r7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, cheers!

@allrosenthal-r7 allrosenthal-r7 added the rn-enhancement release notes enhancement label Apr 10, 2018
@acammack-r7 acammack-r7 added the external modules PRs dealing with modules run as their own process label Apr 12, 2018
@zeroSteiner zeroSteiner deleted the feat/ex-py-log branch February 23, 2021 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external modules PRs dealing with modules run as their own process rn-enhancement release notes enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants