smb.lua: fix SMB Extended Security parsing by resetting the offset index - #1476
smb.lua: fix SMB Extended Security parsing by resetting the offset index#1476cnotin wants to merge 1 commit into
Conversation
|
PR effectiveness confirmed by: |
|
A fix for this issue has been committed as r37733. Thank you for contributing to nmap by researching the code discrepancy and proposing a patch. P.S. I agree with your assessment of what roughly needs to be changed but your rationale is somewhat off. The root cause is a spec non-compliance inside Samba. For compliant Samba versions and for Windows the current code seems to be working just fine. In particular,
Variable Line 1025 in e864f4a Under compliant circumstances, smb['key_length'] should be zero, which results in pos being set to 1.
You can see the Samba non-compliance in your Wireshark screenshot. Here The true fix is to completely skip extracting |
|
Thanks for your thorough review and for fixing it :) |
The SMB library (smb.lua) fails to properly parse "Negotiate Protocol Response" messages when SMB Extended Security is enabled. This cause many SMB scripts to fail, in particular against Samba servers based on my observations in real networks. Below are some observations with smb-os-discovery, but I've also noticed that with smb-vuln-ms17-010 which cause it to not run since it generates an exception in the early phase.
Example before the patch and with the latest public Nmap 7.70 release:
Example before the patch and with the latest Nmap version from Git as of now. The error is more visible:
This difference seems to be caused by the change from
bin.unpacktostring.unpackbut this is just a symptom, not the problem itself.nmap/nselib/smb.lua
Line 1070 in cadb662
And after the patch, no error. The results aren't very good, but it is an unrelated issue:
My detailed thoughts are the following.
This section of the function
negotiate_v1parses the "Negotiate Protocol Response" message, where SMB Extended Security is true.In this case, as explained in [MS-SMB], the response has two parts:
parametersanddata.These are returned by
smb_read():nmap/nselib/smb.lua
Line 952 in 1650469
I understand that the
posvariable is used as an offset index. It is first used againstparameters, like:nmap/nselib/smb.lua
Line 982 in 1650469
But then it is reused against
datawithout being reset to 1. Which creates a read problem.nmap/nselib/smb.lua
Line 1030 in 1650469
The patch creates a new index
data_posdedicated to parse thisdataarray.I've confirmed this by adding
printstatements to view the variables values along the way.With the patch, the
smb.server_guidis properly filled which wasn't the case before:Wireshark parsing, if that helps:

I've tested against hosts that didn't show this problem (no Extended Security in this Negotiate Protocol Response) and I didn't observe any regression.