Repository navigation
Conversation
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>
gabrielg5
left a comment
There was a problem hiding this comment.
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!
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>
|
@gabrielg5 I have made the suggested changes. Please let me if if this look good to you. |
Summary
The ntlmrelayx
attacks/,clients/andservers/socksplugins/packages discover their plugins at import time by listing the package directory on disk withimportlib.resources.files(...).iterdir(). That only works when the plugins exist as individual.pyfiles 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 withimportlib.import_module().Changes
impacket/examples/ntlmrelayx/attacks/__init__.pyimpacket/examples/ntlmrelayx/clients/__init__.pyimpacket/examples/ntlmrelayx/servers/socksplugins/__init__.pyAll three had the same copy-pasted discovery loop; this applies the same fix to each.
Notes
__spec__/__package__fallback, sinceiter_modulesyields fully-qualified module names directly.iter_modulesenumerates subpackages as well as modules, whereas the oldis_file()check only picked up plain.pyfiles. 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
ntlmrelayxstill discovers and loads the same attack, client, and SOCKS relay plugins as before, from a normal source checkout.🤖 Generated with Claude Code