python - 使用自定义 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)
解决方案
推荐阅读
- php - 如何在flutter中设置用户时间提示通知
- angular - Angular 9 - TestBed RouterTestingModule 中的 router.url 只读属性
- xcode - “swift package dump-package”命令在构建前操作和构建阶段不起作用
- excel - Excel,我如何计算具有相同名称的单元格旁边的值
- excel - excel错误测试单元格没有错误给出类型不匹配(CVErr(xlErrNA))
- go - 按列连接二维数组
- r - 将数据框转换为 R 中的二进制矩阵
- javascript - 使用像树这样的指针时打字稿记录错误
- python - 读入熊猫数据框的特殊字符
- python - 当python中的正则表达式需要多个组时,如何仅获得所有完整匹配?