Skip to content

Reply with STATUS_DATA_ERROR when an SMB2 request fails to parse - #307

Open
Pushpenderrathore wants to merge 1 commit into
rapid7:masterfrom
Pushpenderrathore:fix/smb2-dispatch-wrong-class-on-parse-error
Open

Pushpenderrathore wants to merge 1 commit into
rapid7:masterfrom
Pushpenderrathore:fix/smb2-dispatch-wrong-class-on-parse-error

Conversation

@Pushpenderrathore

@Pushpenderrathore Pushpenderrathore commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

GenericPacket.read (lib/ruby_smb/generic_packet.rb:51-63) falls back to SMB1::Packet::EmptyPacket when the raw bytes can be parsed as neither the request class nor SMB2::Packet::ErrorPacket. A well-formed SMB2 header followed by a truncated or malformed body therefore returned an SMB1 EmptyPacket from an SMB2 request class's .read. handle_smb2 only checked is_a?(SMB2::Packet::ErrorPacket), missed the fallback, and dispatched the SMB1 object to the SMB2 do_* method, which raised NoMethodError on request.smb2_header and dropped the connection without a reply.

The rescue branch was also missing an early return and wrote response.smb_header.nt_status, which does not exist on an SMB2 ErrorPacket.

Return STATUS_DATA_ERROR from the rescue, add SMB1::Packet::EmptyPacket to the parse-failure check, and use smb2_header when stamping the status. This matches handle_smb1's shape so an unparsable body always reaches the client as an SMB2 error.

Lab PoC

Reproducer builds an SMB2 header with command = SESSION_SETUP and appends a two-byte body, then hands it to RubySMB::Server::ServerClient#handle_smb2.

Before (upstream/master at b303067):

[CRASH] NoMethodError: undefined method 'smb2_header' for #<RubySMB::SMB1::Packet::EmptyPacket>
  lib/ruby_smb/server/server_client/session_setup.rb:58:in `do_session_setup_smb2'
  lib/ruby_smb/server/server_client.rb:405:in `handle_smb2'

After (this branch):

[OK] response: RubySMB::SMB2::Packet::ErrorPacket, status=0xc000003e  # STATUS_DATA_ERROR

Verification Steps

  • bundle exec rspec spec/lib/ruby_smb/server/server_client_spec.rb passes locally (27 examples, 0 failures)
  • bundle exec rspec spec/lib/ruby_smb/server/ passes locally (365 examples, 0 failures)
  • Reproducer against the live ServerClient#handle_smb2 returns an SMB2::Packet::ErrorPacket with STATUS_DATA_ERROR for a truncated SESSION_SETUP body (upstream crashed with NoMethodError on request.smb2_header)

Test Evidence

365 examples in spec/lib/ruby_smb/server/, 0 failures. New describe #handle_smb2 with a malformed request body asserts both the SMB2 ErrorPacket reply and that do_session_setup_smb2 is not invoked.

GenericPacket.read falls back to SMB1::Packet::EmptyPacket when the
raw bytes can be parsed as neither the request class nor
SMB2::Packet::ErrorPacket, so a well-formed SMB2 header followed by a
truncated or malformed body produced an SMB1 EmptyPacket here. The
handler only checked for SMB2::Packet::ErrorPacket and so dispatched
the SMB1 object to the SMB2 do_* method, which raised NoMethodError
on request.smb2_header and dropped the connection without a reply.
The rescue branch was also missing an early return and read
response.smb_header, which does not exist on an SMB2 ErrorPacket.

Return STATUS_DATA_ERROR from the rescue, add SMB1::Packet::EmptyPacket
to the parse-failure check, and use smb2_header when stamping the
status. This matches handle_smb1's shape so an unparsable body always
reaches the client as an SMB2 error.
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.

1 participant