Skip to content

Fix smbclient rget local path traversal - #2392

Merged
anadrianmanrique merged 2 commits into
masterfrom
fix/smbclient-rget-path-traversal
Dec 1, 2026
Merged

anadrianmanrique merged 2 commits into
masterfrom
fix/smbclient-rget-path-traversal

Conversation

@gabrielg5

Copy link
Copy Markdown
Collaborator

Fixes a client-side path traversal in MiniImpacketShell.do_rget().

rget used directory names returned by the SMB server to reconstruct local output paths. A malicious server could return a directory entry containing parent-directory components or path separators, causing downloaded files to be written outside the intended local download directory.

This can result in attacker-controlled file creation or overwrite at locations writable by the operating-system user running smbclient.py.

Changes

  • Reject server-provided directory names that:
    • are empty, . or ..
    • contain / or \
    • are absolute, drive-qualified, or UNC paths
  • Capture the local download root when rget starts.
  • Resolve and verify every local destination remains inside that root before creating directories or files.
  • Handle incompatible Windows drive paths safely.
  • Preserve normal recursive downloads.
  • Add offline regression tests using the real do_rget() implementation and a fake SMB connection.

@anadrianmanrique anadrianmanrique self-assigned this Nov 25, 2026
@anadrianmanrique anadrianmanrique added bug Unexpected problem or unintended behavior high High priority item labels Nov 25, 2026
@mubix

mubix commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

@gabrielg5 - Nice work, thanks for the careful write-up and the tests! I verified the vuln is real on master: do_rget() reconstructs local output paths from the server's directory long names, and the existing 'if filename in [".", ".."]' filter does not stop a name like '..\\..\\outside', so a malicious server could write files outside the download directory. The two-layer fix (rejecting unsafe directory names up front + resolving every destination against os.getcwd() with realpath/commonpath before mkdir/write) looks correct and defense-in-depth, and the offline tests nicely cover the traversal, the normal nested-download happy path, and a local symlink escape. One small thing to consider: the containment check happens before makedirs()/open(), so a local race (TOCTOU) could in theory plant a symlink between the check and the write, probably not worth blocking on for a local tool, just flagging it. Otherwise this looks good to merge to me.

Nice work catch!

@anadrianmanrique

Copy link
Copy Markdown
Collaborator

Tested in master and current PR:

Test case master (8ad5fcd6) PR #2392 (ff64c2ae)
Normal nested download Downloaded the file Downloaded the file
Server returns a directory named ..\..\outside Created outside\controlled.txt with server-supplied bytes Skipped the directory before downloading
Local directory or final file is a symlink pointing outside the download root Overwrote the outside file in both cases Blocked both writes

@anadrianmanrique

Copy link
Copy Markdown
Collaborator

no changes to be requested, merging now, thanks!

@anadrianmanrique
anadrianmanrique merged commit a5ea22c into master Dec 1, 2026
11 checks passed
@anadrianmanrique
anadrianmanrique deleted the fix/smbclient-rget-path-traversal branch December 1, 2026 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected problem or unintended behavior high High priority item

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants