首页 > 解决方案 > 使用自定义 shell 功能构建模块 python Metasploit

问题描述

我正在使用 python Metasploit-framework 构建一个模块。

exploit我希望我的模块在运行命令后具有自定义外壳的功能。就如:

msf6 exploit(***/***/test_module) > exploit

[*] Running for 192.168.255.162...
[*] ...
[*] ...
[*] ...

custom_shell >

和:

msf6 exploit(***/***/test_module) > exploit

[*] Running for 192.168.255.162...
[*] ...
[*] ...
[*] ...

custom_shell > help

Custom Commands
==================
     Command        Description
     -------        -----------
     download       Download....
     showinfo       Show info....
     project        List project...
     help           Help...
     exit           Exit...

或:类似的东西。但是可以使用自定义命令创建 shell

我的模拟代码。根据文档,我可以module.log()用来打印数据。但没有任何输入。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# standard modules
import logging

# extra modules
dependencies_missing = False
try:
    import requests
except ImportError:
    dependencies_missing = True

from metasploit import module


metadata = {
    'name': 'Example module',
    'description': '''
        Example
    ''',
    'authors': [
        'Jacob Robles'
    ],
    'date': '2021-08-12',
    'license': 'MSF_LICENSE',
    'references': [
        {'type': 'url', 'ref': 'https://blog.rapid7.com/2017/12/28/regifting-python-in-metasploit/'},
        {'type': 'aka', 'ref': 'Coldstone'}
    ],
    'type': 'single_scanner',
    'options': {
        'rhost': {'type': 'address', 'description': 'Target address', 'required': True, 'default': None},
        'rport': {'type': 'integer', 'description': 'Target port', 'required': True, 'default': 80},
    }
}

OPTIONS_LIST = {
    'download' : 'Download....',
    'showinfo' : 'Show info....',
    'project' : 'List project...',
    'help' : 'Help...',
    'exit' : 'Exit...'
}

OPTION_COMMAND = []
for key,_ in OPTIONS_LIST.items():
    OPTION_COMMAND.append(key)

def getInfo():
    print('Service version 1.0')

def download():
    pass
    
def getProject():
    print('Project 1')
    print('Project 2')
    print('...')

def options(ops):
    if ops == 'download':
        download()
    if ops == 'project':
        getProject()
    if ops == 'showinfo':
        getInfo()
    if ops == 'help':
        show_options()
    if ops == 'exit':
        exit()
    if ops not in OPTION_COMMAND:
        print('Invalid Command')

def show_options():
    print("\nCustom Commands\n==================")
    print("{:<5}{:<15}{:<15}".format('', 'Command', 'Description'))
    print("{:<5}{:<15}{:<15}".format('', '-------', '-----------'))
    for command, description in OPTIONS_LIST.items():
        print("{:<5}{:<15}{:<15}".format('', command, description))
    print()
    
def exploit(args):
    module.LogHandler.setup(msg_prefix='{} - '.format(args['rhost']))
    if dependencies_missing:
        logging.error('Module dependency (requests) is missing, cannot continue')
        return
    
    # ................
    # ................
    
    while True:
        cmd = input('custom_shell > ')
        options(cmd)


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

下面是完全按照我想要的方式工作的程序,但这是一个单一的可执行文件,而不是它作为 Metasploit 模块的编写方式。


OPTIONS_LIST = {
    'download' : 'Download....',
    'showinfo' : 'Show info....',
    'project' : 'List project...',
    'help' : 'Help...',
    'exit' : 'Exit...'
}

OPTION_COMMAND = []
for key,_ in OPTIONS_LIST.items():
    OPTION_COMMAND.append(key)

def getInfo():
    # code here
    print('Service version 1.0')

def download():
    # code here
    pass

def getProject():
    # code here
    print('Project 1')
    print('Project 2')
    print('...')

def options(ops):
    if ops == 'download':
        download()
    if ops == 'project':
        getProject()
    if ops == 'showinfo':
        getInfo()
    if ops == 'help':
        show_options()
    if ops == 'exit':
        exit()
    if ops not in OPTION_COMMAND:
        print('Invalid Command')

def show_options():
    print("\nCustom Commands\n==================")
    print("{:<5}{:<15}{:<15}".format('', 'Command', 'Description'))
    print("{:<5}{:<15}{:<15}".format('', '-------', '-----------'))
    for command, description in OPTIONS_LIST.items():
        print("{:<5}{:<15}{:<15}".format('', command, description))
    print()

while True:
    cmd = input('custom_shell > ')
    options(cmd)

标签: pythonmodulemetasploit

解决方案


推荐阅读