BUG-2099: Fix Unhandled ValueError during authentication#2151
Open
herbenderbler wants to merge 1 commit intofortra:masterfrom
Open
BUG-2099: Fix Unhandled ValueError during authentication#2151herbenderbler wants to merge 1 commit intofortra:masterfrom
herbenderbler wants to merge 1 commit intofortra:masterfrom
Conversation
… log) - structure: asciiz without NUL raises clear ValueError with field name - smb: catch ValueError at session/negotiate parse sites; log at debug, raise SessionError - smbconnection: docstring notes invalid/truncated server response - ci: fix flake8 F824 (goldenPac, ldapattack) - tests: regression for asciiz NUL and session setup parsing; align with TESTING.md - docs: ChangeLog
67ced8a to
d27ee6c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reported problem
When SMB authentication runs against a host that drops the connection, times out, or sends a truncated session setup response, the client can hit:
The traceback points at
structure.py(data.index(b'\x00')for asciiz) and then throughsmb.login_extended→sessionData.fromString(sessionResponse['Data']). The message is unclear and the error is never turned into an authentication/connection failure, so callers see a rawValueErrorinstead of a session error. (Issue #2099; see also PR #2118 and maintainer feedback there.)How we fixed it
Structure layer (
impacket/structure.py)For asciiz (
'z') parsing, we catch theValueErrorfromdata.index(b'\x00')and re-raise with a clear message that includes the field name, e.g. "Can't find NUL terminator in field 'NativeOS'". We also pass the field name through the code specifier so formats like'z=""'get the correct name in the message.SMB layer (
impacket/smb.py)We handle parsing failures at every place that parses session/negotiate responses:
sessionData.fromString(sessionResponse['Data'])is wrapped intry/except ValueError. On failure we log the exception at debug and raiseSessionErrorwithSTATUS_LOGON_FAILUREso the failure is reported as an authentication error._dialects_data.fromString(...)and the extended-security parse; we log at debug and raiseSessionErrorwithSTATUS_INVALID_PARAMETERso the failure is reported as a connection/response error.Callers (including
smbconnection.login()) now receive a normal SessionError (with a clear code and message) instead of a raw ValueError.Documentation
The
smbconnection.login()docstring is updated to mention that a session error can be raised for invalid or truncated server response. An implementation plan (IMPLEMENTATION_PLAN_2099.md) and a ChangeLog entry document the change.Regression testing
Structure layer
In
tests/misc/test_structure.py,Test_asciiz_no_nul_raises_clear_error:ValueErrorwhose message includes the field name and “NUL terminator” (so we never again surface the generic “subsection not found” for this case).NativeOS).SMB layer
In
tests/SMB_RPC/test_smb.py,Test_Issue2099_SessionError_On_Truncated_Response::test_login_raises_session_error_when_session_response_parsing_fails:SMBSessionSetupAndX_Extended_Response_Data.fromStringto raiseValueError(simulating a truncated/malformed response).SMBConnection(...)orconn.login(...)raises a SessionError (or the underlyingsmb.SessionError) with an error code set, so we never again let a ValueError from this parsing path escape to the caller.Together, these tests ensure that (1) the structure layer gives a clear ValueError when a NUL is missing, and (2) the SMB layer turns that into a SessionError so the bug does not reappear.
Checklist
ValueErrorfor asciiz without NUL.ValueErrorat all four parse sites, logs at debug, and raisesSessionError.Fixes #2099.