-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Fix encoding issue with non-ASCII characters #238
Fix encoding issue with non-ASCII characters #238
Conversation
- add `safe_encode` to handle specific Metasploit cases when non-ASCII character strings are set to `ASCII-8BIT` - changes exposed `String#encode` calls to `safe_encode` - Monkey patch `rubyntlm`'s `ntlmv2_hash` methods to include a fix for an issue with NTLMv2 hashes when usernames contain non-ASCII characters
Hi @cdelafuente-r7 , I was working on some ASCII/UTF-8 issues and hooked up your changes to see if they would fix an issue we were running into on a call. I'll add a before and after comparing BeforeEncoding exception is raised:
Note: the username AfterThe exception is now different:
Note: the username StacktraceOriginal stacktrace from ruby_smb:
and this is from framework logs:
Was this PR in isolation able to fix the original metasploit-framework issue, or are there additional changes required in metasploit? |
Thanks @cgranleese-r7 for reporting this issue. I tracked down to a |
- Use prepend to monkey patch `Net::NTLM` - Move the patch to a separate file (custom/ntlm.rb) - Change `safe_encode` to a module class to avoid having to include the module everywhere
@@ -0,0 +1,19 @@ | |||
require 'net/ntlm' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure this is loaded whenever ruby_smb
is it might be worth adding a require 'custom/ntlm'
here.
I am not sold on using Custom
as the top level namespace in a gem
, but I don't have a better option in mind so just noting this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Addressed in 10997a3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry names are hard, I like the new path. Would it be reasonable to have custom ntlm adjustment follow naming conventions encase we zeitwerk this code later?
module Custom | ||
module NTLM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filename ruby_smb/ntlm/custom/string_encoder
module Custom | |
module NTLM | |
module RubySMB::NTLM::Custom | |
module StringEncoder |
end | ||
end | ||
|
||
Net::NTLM::EncodeUtil.send(:prepend, Custom::NTLM) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Net::NTLM::EncodeUtil.send(:prepend, Custom::NTLM) | |
Net::NTLM::EncodeUtil.send(:prepend, RubySMB::NTLM::Custom::StringEncoder) |
@@ -1,3 +1,5 @@ | |||
require 'ruby_smb/ntlm/custom/ntlm' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
require 'ruby_smb/ntlm/custom/ntlm' | |
require 'ruby_smb/ntlm/custom/string_encoder' |
@@ -6,11 +6,13 @@ | |||
require 'openssl/cmac' | |||
require 'windows_error' | |||
require 'windows_error/nt_status' | |||
require 'ruby_smb/ntlm/custom/ntlm' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
require 'ruby_smb/ntlm/custom/ntlm' | |
require 'ruby_smb/ntlm/custom/string_encoder' |
I was able to reproduce the original issue and validate that the proposed changes fix it. Fixed / After Patch
Broken / Before Patch
|
Fix encoding issue with non-ASCII characters
I see we still have some outstanding comments here. Should we open another PR to address those? Jeffery's points seem valid here. |
Yes that can be a follow up PR, this at least gets the issue addressed as landed. |
This is related to this Metasploit issue.
This PR addresses an authentication error when usernames with non-ASCII characters are used. This PR fixes two things:
ASCII-8BIT
encoding.Calling
String#encode
on those strings were throwing aEncoding::UndefinedConversionError
exception. To address this, asafe_encode
wrapper method has been added. This method handles this specific Metasploit case and prevents it from breaking. This wrapper simply catchesEncoding
exceptions and#force_encode
instead of#encode
the string. This might not be the best solution, but since it seems to only happen in this specific scenario, I'm confident it is safe to do.Every calls to
String#encode
on a string that could come from a user input directly have been replace by#safe_encode
.rubyntlm
librarySee WinRb/rubyntlm#55 for details. A PR has been submitted, but since we want this to be fixed ASAP, the fix has been added to RubySMB directly. This PR adds the patched
rubyntlm
'sntlmv2_hash
method and forces theRubySMB::Client
to useRubySMB::NTLM
instead ofrubyntlm
library.Before the fix
After the fix