Skip to content

fix: WMI IEnumWbemClassObject.Next() drops final batch; method dispatch swallows errors - #2296

Open
nullsection wants to merge 3 commits into
fortra:masterfrom
nullsection:fix/wmi-enumeration-and-execmethod-errors
Open

nullsection wants to merge 3 commits into
fortra:masterfrom
nullsection:fix/wmi-enumeration-and-execmethod-errors

Conversation

@nullsection

@nullsection nullsection commented Sep 15, 2026 •

Copy link
Copy Markdown

Summary

Fixes two correctness bugs in impacket/dcerpc/v5/dcom/wmi.py:

  • IEnumWbemClassObject.Next() now returns the final partial batch when the server sends objects together with WBEM_S_FALSE. It still raises WBEM_S_FALSE when no objects remain, preserving the existing end-of-enumeration behavior.
  • Dynamic IWbemClassObject method calls now propagate ExecMethod failures and raise TypeError for argument-count mismatches instead of silently returning None.

No changelog changes are included.

Tests

tests/SMB_RPC/test_wmi_next.py and tests/SMB_RPC/test_wmi_methods.py use real remote WMI/DCOM connections and Impacket's parse_target helper for the standard command-line credential format. They contain no mocks and can run directly without a configuration file. They also retain the existing RemoteTestCase configuration and remote marker for pytest.

The enumeration tests query the singleton Win32_OperatingSystem class to cover full-batch success, final partial-batch recovery, exhaustion after consuming a result, and an empty query. Requesting two objects when only one exists exercises the server's actual WBEM_S_FALSE response with a trailing object. A semisynchronous query for a nonexistent class verifies that Next() propagates WBEM_E_INVALID_CLASS.

The method tests fetch the real Win32_Process class and verify that too few or too many arguments raise TypeError, and that calling the instance method GetOwner() on the class propagates the server's WBEM_E_INVALID_METHOD_PARAMETERS error. These tests do not create processes or modify the target. Connections and remote interfaces are cleaned up even on failure.

Run directly against a Windows target:

python tests/SMB_RPC/test_wmi_next.py 'domain/user:password@target'
python tests/SMB_RPC/test_wmi_methods.py 'domain/user:password@target'

Omit :password to be prompted. Both scripts also accept the usual -hashes LMHASH:NTHASH, -no-pass, -k, -aesKey, and -dc-ip options, and exit nonzero if a test fails.

Validation:

  • All seven remote tests passed against a Windows lab target.
  • The partial-batch and both method-dispatch regression tests failed against the PR base, confirming they catch the original bugs.
  • Coverage recorded execution of all nine added executable lines in wmi.py; this is line coverage, not a claim of exhaustive branch coverage.
  • Both existing offline WMI tests passed.
  • The changed files pass the critical flake8 gate, and both test scripts pass flake8. The repository-wide critical gate reports five existing F824 findings in unchanged files.

Two related correctness bugs in impacket.dcerpc.v5.dcom.wmi:

IEnumWbemClassObject.Next() dropped the final partial batch. Per
[MS-WMI] 3.1.4.4.2, when fewer than uCount objects remain the server
returns the trailing objects together with a WBEM_S_FALSE status in the
same response. The generic DCERPC layer raises on that non-zero status
before Next() can read apObjects, so fetching more than one row per call
silently discarded the last rows (this also affects the bundled
wmiquery.py and dpapidump.py). Next() now recovers those objects from the
decoded packet on WBEM_S_FALSE and re-raises only when the enumeration is
truly exhausted, preserving the historical end-of-iteration signal for
existing callers.

IWbemClassObject dynamic method dispatch (createMethods/innerMethod)
swallowed ExecMethod exceptions and argument-count mismatches, logging
them and returning None. A failed remote method call (e.g.
Win32_Process.Create used by wmiexec.py) was therefore indistinguishable
from success and only surfaced later as an opaque AttributeError on the
None result. Both now propagate.

Adds host-free unit tests for Next()'s partial-batch handling.
@anadrianmanrique anadrianmanrique self-assigned this Sep 16, 2026
@anadrianmanrique anadrianmanrique added the in review This issue or pull request is being analyzed label Sep 16, 2026
@nullsection

Copy link
Copy Markdown
Author

Anything I can do to help move things along?

@anadrianmanrique

Copy link
Copy Markdown
Collaborator

@nullsection Hi! for the time being, can you remove changelog changes? that's something we want to have exclusive control for now.
Also, testcase can be modified to be a real remote case and not rely on mocked instances? thanks

@nullsection

Copy link
Copy Markdown
Author

thanks @anadrianmanrique

I've reverted the change log and added some new tests. Unless I've missed something it should cover all code paths.

@anadrianmanrique

Copy link
Copy Markdown
Collaborator

@nullsection Thanks! I'll continue with the review process next week. Will let you know if more changes are necessary.

@anadrianmanrique

Copy link
Copy Markdown
Collaborator

@nullsection hey! sorry for the delayed response. Review was ready with just one little item to adjust in the code. Feel free to reopen the PR in order to have it merged. Thanks!

@nullsection nullsection reopened this Nov 3, 2026
@nullsection

Copy link
Copy Markdown
Author

@nullsection hey! sorry for the delayed response. Review was ready with just one little item to adjust in the code. Feel free to reopen the PR in order to have it merged. Thanks!

Hey @anadrianmanrique, sorry it's early AM for me and I misread the merge 😅

No stress, what would you like changed?

@anadrianmanrique anadrianmanrique added medium Medium priority item and removed in review This issue or pull request is being analyzed labels Nov 3, 2026
if len(args) != len(methodDefinition['InParams']):
LOG.error("Function called with %d parameters instead of %d!" % (len(args), len(methodDefinition['InParams'])))
return None
# Previously this only logged and returned None, silently

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.

argument validation is conditional on methodDefinition['InParams'] is not None. Methods without input parameters are represented by None, so a call such as process.GetOwner('unexpected') bypasses validation. At wmi.py:3001 (impacket/dcerpc/v5/dcom/wmi.py:3001), the wrapper sends pInParams=NULL, silently discarding the supplied argument and executing the no-argument method instead.
Please validate against zero arguments when InParams is None. Add regression coverage beside tests/SMB_RPC/test_wmi_methods.py:34 (tests/SMB_RPC/test_wmi_methods.py:34), asserting that process.GetOwner('unexpected') raises TypeError.

@anadrianmanrique anadrianmanrique added the waiting for response Further information is needed from people who opened the issue or pull request label Nov 3, 2026
godylockz pushed a commit to godylockz/impacket that referenced this pull request Nov 11, 2026
godylockz pushed a commit to godylockz/impacket that referenced this pull request Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

medium Medium priority item waiting for response Further information is needed from people who opened the issue or pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants