Skip to content

Discover ntlmrelayx plugins via pkgutil.iter_modules - #2337

Open
wjacobf0 wants to merge 4 commits into
fortra:masterfrom
wjacobf0:pkgutil-plugin-loading
Open

wjacobf0 wants to merge 4 commits into
fortra:masterfrom
wjacobf0:pkgutil-plugin-loading

Conversation

@wjacobf0

Copy link
Copy Markdown

Summary

The ntlmrelayx attacks/, clients/ and servers/socksplugins/ packages discover their plugins at import time by listing the package directory on disk with importlib.resources.files(...).iterdir(). That only works when the plugins exist as individual .py files on the filesystem, so discovery silently finds nothing when impacket is imported from a zipimport/zipapp context or any build where the submodules live inside an archive rather than as files on disk.

This replaces the on-disk directory scan with pkgutil.iter_modules(__path__, ...), which resolves submodules through the package's import machinery and therefore works both on disk and inside an archive. Each discovered module is then imported with importlib.import_module().

Changes

  • impacket/examples/ntlmrelayx/attacks/__init__.py
  • impacket/examples/ntlmrelayx/clients/__init__.py
  • impacket/examples/ntlmrelayx/servers/socksplugins/__init__.py

All three had the same copy-pasted discovery loop; this applies the same fix to each.

Notes

  • Also drops the stale Python 2 __spec__/__package__ fallback, since iter_modules yields fully-qualified module names directly.
  • Behavior change worth flagging for review: iter_modules enumerates subpackages as well as modules, whereas the old is_file() check only picked up plain .py files. None of these three directories contain plugin subpackages today, so the set of loaded plugins is unchanged; the dunder-name filter is preserved.

Testing

Confirmed ntlmrelayx still discovers and loads the same attack, client, and SOCKS relay plugins as before, from a normal source checkout.

🤖 Generated with Claude Code

The attacks/, clients/ and servers/socksplugins/ packages discover their
plugins at import time by listing the package directory on disk with
importlib.resources.files(...).iterdir(). That only works when the plugins
exist as individual .py files on the filesystem, so discovery silently finds
nothing when impacket is imported from a zipimport/zipapp context or a frozen
build where the submodules live inside an archive rather than on disk.

Enumerate the submodules with pkgutil.iter_modules(__path__, ...) instead,
which resolves modules through the package's import machinery and therefore
works both on disk and inside an archive. Each discovered module is imported
with importlib.import_module().

This also lets us drop the stale Python 2 __spec__/__package__ fallback, since
iter_modules yields fully-qualified names directly.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@anadrianmanrique anadrianmanrique added the in review This issue or pull request is being analyzed label Oct 21, 2026

@gabrielg5 gabrielg5 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @wjacobf0, this is better than the current implementation!

I tested all three plugin registries from a normal checkout and from a directory-preserving ZIP outside the source tree; both discovered the same 11 attack, 12 client, and 9 SOCKS plugins. However, I found one blocking integration issue for the advertised archive use case in the current project layout.

A file-only ZIP without explicit directory entries currently fails on the existing impacket.mssql namespace-style directory, while a directory-preserving archive works. I confirmed this with the published pypi wheel too: running its ntlmrelayx entry point directly from the wheel fails even for -h, through servers -> mssqlrelayserver -> tds -> impacket.mssql.version, because the wheel has impacket/mssql/version.py but neither an impacket/mssql/ directory entry nor impacket/mssql/__init__.py. This happens during parent server-package initialization, before SOCKS plugin enumeration.

The targeted solution is to add impacket/mssql/__init__.py (empty apart from the normal license header), making impacket.mssql a regular package that zipimport can recognize from the __init__.py member without relying on a ZIP directory entry.

Could you include that file with this PR so we are good to merge it?

Thanks!

@gabrielg5 gabrielg5 added enhancement Implemented features can be improved or revised waiting for response Further information is needed from people who opened the issue or pull request medium Medium priority item and removed in review This issue or pull request is being analyzed labels Oct 27, 2026
wjacobf0 and others added 3 commits October 28, 2026 09:00
impacket.mssql is already declared as a package in setup.py, but the directory
has no __init__.py, so it only resolves as an implicit (PEP 420) namespace
package. That works on a regular filesystem but not under zipimport: loading
impacket from a file-only archive (no explicit directory entries) fails to find
the package, breaking the ntlmrelayx server import chain
(servers -> mssqlrelayserver -> tds -> impacket.mssql.version) before plugin
discovery even runs.

Add an empty __init__.py so impacket.mssql is a regular package that zipimport
recognizes from the __init__.py member, matching its setup.py declaration.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@wjacobf0

Copy link
Copy Markdown
Author

@gabrielg5 I have made the suggested changes. Please let me if if this look good to you.

@gabrielg5 gabrielg5 removed the waiting for response Further information is needed from people who opened the issue or pull request label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Implemented features can be improved or revised medium Medium priority item

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants