Skip to content

Fix analyze_db size bypass for dynamic payloads - #21743

Open
keyanskv wants to merge 5 commits into
rapid7:masterfrom
keyanskv:fix-analyze-db-dynamic-payload-size
Open

Fix analyze_db size bypass for dynamic payloads#21743
keyanskv wants to merge 5 commits into
rapid7:masterfrom
keyanskv:fix-analyze-db-dynamic-payload-size

Conversation

@keyanskv

@keyanskv keyanskv commented Aug 2, 2026

Copy link
Copy Markdown

Adds payload_cached_size to module metadata cache to allow is_payload_compatible? to correctly reject large payloads with dynamic sizes.

Description

This change adds payload_cached_size to the module metadata cache so that is_payload_compatible? can make payload size decisions without loading the payload module.

Previously, payloads with dynamic sizes could not always be evaluated correctly from cached metadata alone. As a result, oversized payloads could pass compatibility checks because the cached metadata did not include the information needed to determine their effective size.

With this change:

  • payload_cached_size is stored in the module metadata cache.

  • is_payload_compatible? uses the cached size information when performing compatibility checks.

  • Dynamic payloads with cached size metadata are correctly rejected when they exceed the exploit's available payload space.

  • Existing behavior for static payloads remains unchanged.

Related Issue: None


Breaking Changes

None.


Reviewer Notes

The primary implementation is the addition of payload_cached_size to the module metadata cache and updating payload compatibility checks to consume the cached value.

The accompanying RSpec tests verify:

  • Msf::Payload.cached_size behavior.

  • Payload cached size metadata generation and validation.

  • Handling of static and dynamic payloads.

  • Cached size override generation for staged payloads.


Verification Steps

  1. Build the project and install dependencies.

  2. Apply this patch.

  3. Run:

bundle exec rspec spec/lib/msf/core/payload_spec.rb

Expected result:

  • 12 examples

  • 0 failures

  1. Run:

bundle exec rspec spec/lib/msf/util/payload_cached_size_spec.rb

Expected result:

  • 16 examples

  • 0 failures

  1. Verify that payload metadata now includes payload_cached_size.

  2. Verify that is_payload_compatible? correctly rejects payloads whose cached size exceeds the available payload space, including dynamically-sized payloads.


Test Evidence

Test 1

bundle exec rspec spec/lib/msf/core/payload_spec.rb

Finished in 0.64117 seconds (files took 6.53 seconds to load)
12 examples, 0 failures

Coverage report generated for RSpec to coverage.
Line Coverage: 18.54% (2683 / 14471)

Test 2

bundle exec rspec spec/lib/msf/util/payload_cached_size_spec.rb

Finished in 0.468 seconds (files took 5.94 seconds to load)
16 examples, 0 failures

Coverage report generated for RSpec to coverage.
Line Coverage: 16.86% (2238 / 13275)


Environment

Field Details
Operating System Ubuntu 24.04.4 LTS
Ruby Version Ruby 3.2.x
PostgreSQL PostgreSQL 16
Target Software/Hardware Metasploit Framework (master branch)
Docker Image / Vagrant Setup Not used

AI Usage Disclosure

ChatGPT was used to assist with drafting the pull request description and improving documentation. The implementation, testing, debugging, and validation of the code were completed by the contributor.


Pre-Submission Checklist

  • No sensitive information (credentials, API keys, IP addresses, or secrets) included

  • Tested on the target environment specified above

  • Included RSpec tests for library changes

  • Read the CONTRIBUTING.md and module acceptance guidelines

  • Included a corresponding documentation markdown file (Not applicable — library change only)

Screenshot from 2026-08-02 19-05-55 Screenshot from 2026-08-02 19-06-15

Adds payload_cached_size to module metadata cache to allow is_payload_compatible? to correctly reject large payloads with dynamic sizes.

@keyanskv keyanskv left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hello Maintainers,

I have reviewed the proposed changes in this pull request and believe they are ready for GitHub Actions to run. Could a maintainer please review the changes and approve the pending workflow execution?

Thank you for your time and assistance.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds cached payload-size metadata so compatibility checks can reject oversized dynamic payloads.

Changes:

  • Stores and serializes payload_cached_size.
  • Uses cached sizes during payload compatibility checks.
  • Adds metadata, exploit, and JSON-RPC test coverage.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Findings
spec/lib/msf/core/modules/metadata/obj_spec.rb Critical (3 votes): Strict doubles do not stub shortname and module_info, causing the serialization example to fail.
spec/lib/msf/core/exploit_spec.rb Critical (1 vote): Stubs nonexistent module_reference instead of get_module_reference(type:, reference_name:); also affects line 70.
spec/api/json_rpc_spec.rb No final comments.
lib/msf/core/modules/metadata/obj.rb Moderate (4 votes): Existing metadata caches are not invalidated when the new field is absent. Critical (1 vote): Shared doubles do not support the new respond_to?(:cached_size) call.
lib/msf/core/exploit.rb Critical (4 votes): Calls nonexistent module_reference, causing dynamic payload checks with finite payload space to raise NoMethodError.
Suppressed comments (2)

lib/msf/core/modules/metadata/obj.rb:216

  • Important: Problem: a dynamic_size? payload can produce different byte sizes, but this stores only one generation as a definitive cached size; Impact: a sample below payload_space followed by a larger generation makes is_payload_compatible? return true for an oversized payload; Fix: store a conservative bound or an explicit unknown/dynamic state and handle that state instead of serializing one sample.
          require 'msf/util/payload_cached_size'
          opts = Msf::Util::PayloadCachedSize.module_options(module_instance)
          @payload_cached_size = module_instance.replicant.generate_simple(opts).bytesize

spec/lib/msf/core/exploit_spec.rb:70

  • Important: Problem: this cache-miss context repeats the nonexistent module_reference stub on a verifying double; Impact: this example also raises before it runs; Fix: use get_module_reference(type: Msf::MODULE_PAYLOAD, reference_name: payload_name) here.
          allow(cache_instance).to receive(:module_reference).with(payload_name).and_return(nil)

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread lib/msf/core/exploit.rb Outdated
Comment thread lib/msf/core/modules/metadata/obj.rb
Comment thread lib/msf/core/modules/metadata/obj.rb
Comment thread spec/lib/msf/core/exploit_spec.rb Outdated
Comment thread spec/lib/msf/core/modules/metadata/obj_spec.rb

@keyanskv keyanskv left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi maintainers,

I've addressed the review feedback on this PR and pushed the latest changes. I also ran the relevant test suites and verified the changes locally.

Could someone with write access please review the latest changes and, if everything looks good, approve the PR and the pending workflows?

Thanks for your time and review!

@bwatters-r7

Copy link
Copy Markdown
Contributor

@keyanskv
keyanskv requested a review from bwatters-r7 August 26, 2026 13:41
@bwatters-r7 bwatters-r7 self-assigned this Sep 2, 2026
- Use host_ip variable instead of hard-coded IP addresses in expected responses
- Ensures test passes consistently regardless of randomly generated IPs from Faker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants