Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/proto/ldap/auth_adapter/rex_ntlm.rb
Views: 11766
# frozen_string_literal: true12require 'net/ldap/auth_adapter'3require 'net/ldap/auth_adapter/sasl'4require 'rubyntlm'56module Rex::Proto::LDAP::AuthAdapter7class RexNTLM < Net::LDAP::AuthAdapter8def bind(auth)9flags = 0 |10RubySMB::NTLM::NEGOTIATE_FLAGS[:UNICODE] |11RubySMB::NTLM::NEGOTIATE_FLAGS[:REQUEST_TARGET] |12RubySMB::NTLM::NEGOTIATE_FLAGS[:NTLM] |13RubySMB::NTLM::NEGOTIATE_FLAGS[:ALWAYS_SIGN] |14RubySMB::NTLM::NEGOTIATE_FLAGS[:EXTENDED_SECURITY] |15RubySMB::NTLM::NEGOTIATE_FLAGS[:KEY_EXCHANGE] |16RubySMB::NTLM::NEGOTIATE_FLAGS[:TARGET_INFO] |17RubySMB::NTLM::NEGOTIATE_FLAGS[:VERSION_INFO]1819if auth[:sign_and_seal]20flags = flags |21RubySMB::NTLM::NEGOTIATE_FLAGS[:SIGN] |22RubySMB::NTLM::NEGOTIATE_FLAGS[:SEAL] |23RubySMB::NTLM::NEGOTIATE_FLAGS[:KEY128] |24RubySMB::NTLM::NEGOTIATE_FLAGS[:KEY56]25end2627ntlm_client = RubySMB::NTLM::Client.new(28(auth[:username].nil? ? '' : auth[:username]),29(auth[:password].nil? ? '' : auth[:password]),30workstation: 'WORKSTATION',31domain: auth[:domain].blank? ? '.' : auth[:domain],32flags: flags33)3435challenge_response = proc do |challenge|36challenge.force_encoding(Encoding::BINARY)37type2_message = Net::NTLM::Message.parse(challenge)38channel_binding = nil39if @connection.socket.respond_to?(:peer_cert)40channel_binding = Rex::Proto::Gss::ChannelBinding.from_tls_cert(@connection.socket.peer_cert)41end4243type3_message = ntlm_client.init_context(type2_message.encode64, channel_binding)44type3_message.serialize45end4647result = Net::LDAP::AuthAdapter::Sasl.new(@connection).bind(48method: :sasl,49mechanism: 'GSS-SPNEGO',50initial_credential: ntlm_client.init_context.serialize,51challenge_response: challenge_response52)5354if auth[:sign_and_seal]55encryptor = Encryptor.new(ntlm_client)56encryptor.setup(@connection)57end5859result60end61end62end6364Net::LDAP::AuthAdapter.register(:rex_ntlm, Rex::Proto::LDAP::AuthAdapter::RexNTLM)656667