[工具使用] Metasploit进阶笔记之使用 Metasploit JSON RPC

494 0
Honkers 2026-5-26 09:19:33 来自手机 | 显示全部楼层 |阅读模式

RPC API 允许通过基于 HTTP 的远程过程调用 (RPC) 服务以编程方式驱动 Metasploit Framework 和商业产品。RPC 服务是一组消息类型和远程方法,提供了一种结构化的方式,使外部应用程序能够与 Web 应用程序交互。可以使用 RPC 接口在本地或远程执行 Metasploit 命令,以执行基本任务,如运行模块、与数据库通信、管理会话、导出数据和生成报告。

Metasploit 产品主要用 Ruby 编写,这是使用远程 API 最简单的方式。然而,除 Ruby 外,任何支持 HTTPS 和 MessagePack 的语言(如 Python、Java 和 C)都可以利用 RPC API。

当前有两种 Metasploit RPC 实现:

  • HTTP 和 MessagePack - 另有单独的指南介绍
  • HTTP 和 JSON - 本指南涵盖

请注意,MessagePack 和 JSON RPC 服务提供的操作非常相似,建议查看两个文档。

一、启动 JSON API 服务器

运行 JSON API 服务器的前提是启动 Metasploit 数据库。可以使用 msfdb 初始化数据库。注意,msfdb 会询问是否希望运行 JSON RPC Web 服务,但对于本指南直接通过 thin 或 Puma 启动 JSON 服务并非必须:

首先运行 Metasploit 数据库:

  1. msfdb init
复制代码

配置数据库后,可以使用 thin Ruby Web 服务器初始化 JSON RPC 服务:

  1. bundle exec thin --rackup msf-json-rpc.ru --address 0.0.0.0 --port 8081 --environment production --tag msf-json-rpc start
复制代码

或者使用 Puma:

  1. bundle exec puma msf-json-rpc.ru --port 8081 --environment production --tag msf-json-rpc start
复制代码

二、开发

如果你想要开发或调试 JSON RPC 服务的 Ruby 实现,将 Metasploit API 同步运行在前台会很有帮助。这可以让控制台日志直接显示在终端中,还可以通过 require 'pry-byebug'; binding.pry 与断点进行交互:

也可以调试 Msfconsole 的 web 服务组件:

  1. bundle exec ruby ./msfdb reinit
  2. bundle exec ruby ./msfdb --component webservice stop
  3. bundle exec ruby ./msfdb --component webservice --no-daemon start
复制代码

RPC 日志记录

你可以使用 MSF_WS_DATA_SERVICE_LOGGER 环境变量配置 RPC 服务日志记录。

支持的日志记录器列表可以通过 msfconsole --help 查看。目前支持的列表如下:

  • Stdout / Stderr / StdoutWithoutTimestamps - 将日志写入 stdout/stderr
  • Flatfile / TimestampColorlessFlatfile - 将日志写入 ~/.msf4/logs

示例用法:

  1. $ MSF_WS_DATA_SERVICE_LOGGER=Stdout bundle exec thin --rackup msf-json-rpc.ru --address localhost --port 8081 --environment production --tag msf-json-rpc start
  2. [11/25/2020 17:34:53] [e(0)] core: Dependency for windows/encrypted_shell_reverse_tcp is not supported
  3. [11/25/2020 17:34:53] [e(0)] core: Dependency for windows/x64/encrypted_shell_reverse_tcp is not supported
  4. [11/25/2020 17:34:53] [e(0)] core: Dependency for windows/encrypted_reverse_tcp is not supported
  5. [11/25/2020 17:34:53] [e(0)] core: Dependency for windows/x64/encrypted_reverse_tcp is not supported
  6. [11/25/2020 17:34:54] [e(0)] core: Unable to load module /Users/adfoster/Documents/code/metasploit-framework/modules/auxiliary/gather/office365userenum.py - LoadError Try running file manually to check for errors or dependency issues.
  7. Thin web server (v1.7.2 codename Bachmanity)
  8. Maximum connections set to 1024
  9. Listening on localhost:8081, CTRL+C to stop
  10. [11/25/2020 17:35:17] [d(0)] core: Already established connection to postgresql, so reusing active connection.
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行