From . in regex to SSRF — part 3
The author investigated a blind SSRF vulnerability in a service that used Node.js and the node-fetch library, attempting to escalate it via CRLF injection to talk to a Redis service. Despite trying various payloads and examining node-fetch code, the escalation was unsuccessful, but the report was still awarded $400 and marked medium.
Published on xvnpw.github.io
This is last part of my stories about exploiting service with SSRF bug. Part 1 is available here, and part 2 here.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
This is the final part of a three-part series about exploiting a service that had a blind SSRF bug. Parts one and two are linked from the original post. This part concentrates on abusing Node.js together with the node-fetch library, with the goal of talking to a Redis service through CRLF injection in the HTTP parser.
The target here is not a specific web application: it is the behaviour of the client-side HTTP stack that performs the outbound request in an SSRF scenario. If the application passes attacker-controlled input to node-fetch, the question is whether that input can be shaped so that the bytes sent on the wire contain a CRLF sequence, letting the attacker start a new line in the protocol stream and inject commands for the service on the other end.
Setup
The researcher used a Kali 2020.1b machine with the following versions:
| Component | Version |
|---|---|
| Node.js | 10.19.0 |
| node-fetch | 2.6.0 |
For convenience, the Redis service was simulated with netcat listening on the Redis port:
nc -vvlp 6379
This gives a raw TCP listener that prints whatever arrives, which is enough to see whether injected lines reach the service. Debugging of the Node.js code was done in Visual Studio Code.
Finding it
The starting hypothesis was that a CRLF injection in the URL handling of node-fetch could be used to smuggle commands into the protocol spoken by the backend service. Redis speaks a simple line-based protocol, so if the HTTP client can be made to emit a line such as SET foo 0 60 5 followed by a data line, the request would be interpreted by Redis as a command rather than as part of an HTTP request.
Two classes of possible errors were in mind:
- URL parsing: the URL parser might accept characters that later end up unencoded in the request line or headers.
- Handling the URL input as an object rather than as a string: in many dynamic languages, a query string such as
url[href]=...produces an object, and libraries that accept either a string or an options object may then read properties the developer never intended to expose.
Exploitation
Step 1: PayloadsAllTheThings CRLF payloads
The first attempt was to loop over the CRLF injection payloads collected in PayloadsAllTheThings and watch the netcat console for any injected line. The result was that none of them succeeded: every payload failed.
Step 2: Orange Tsai payloads
The next step was to try the payloads from two articles by Orange Tsai, the first from his Red Hat 2017 talk and the second from his blog. These give a few more options to test:
-*Set-Cookie:injection-* (Unicode U+FF0D U+FF0A)
http://0\r\n SET foo 0 60 5\r\n :6379/
https://0\r\nSET foo 0 60 5\r\n:6379/
These also failed. The conclusion drawn was that this version of Node.js is not vulnerable to CRLF attacks of this kind.
Step 3: Reading the node-fetch code
With the payload approach exhausted, the next move was to dig into the node-fetch source. The first of the two hypotheses, URL parsing, turned out not to exist as a problem: node-fetch uses the standard Node.js Url.parse for its input and does not do anything fancy with it.
The second hypothesis needed more investigation. The reason for being interested in an object instead of a string is that in many dynamic languages a request like this produces an object rather than a string:
http://localhost:3000/c?url[href]=localhost&url[method]=POST
If the application passes the parsed query value straight through, the library receives an object with href and method properties. That can be handy in some scenarios, especially when developers did not predict it. The researcher demonstrated this parsing behaviour in the Node.js Express framework.
In node-fetch one possible attack vector was found: it looked possible to use an object instead of a string for the input parameter, and input.method could change the method type under some specific conditions. After spending some time in the debugger, this turned out to be a dead end.
Root cause
The report does not identify a vulnerability in node-fetch 2.6.0 or in Node.js 10.19.0. The CRLF payloads from PayloadsAllTheThings and from Orange Tsai's work did not produce injected lines, and the conclusion drawn was that this Node.js version is not vulnerable to CRLF attacks of this kind. The object-input idea was investigated in the debugger and found not to be exploitable in the way hoped. The report does not state which code path blocked the payloads.
Impact
The researcher did not manage to escalate the blind SSRF to anything more. Several days were spent trying different approaches without success. After submitting the report, the researcher was awarded 400 USD and the bug was marked as medium. The report does not describe the affected application or what the blind SSRF itself allowed.
Fix
Variants to test
The report does not list further variants beyond the payloads above. The general directions it explores, which can be repeated against other targets, are:
- Run the CRLF payload collections from PayloadsAllTheThings and from Orange Tsai's research against the target's HTTP client, watching a listener such as
nc -vvlp 6379for an injected line. A positive result is a line such asSET foo 0 60 5arriving at the listener outside the HTTP request. - Check whether the application passes query parameters as objects (for example
url[href]=...&url[method]=...) into a library that accepts either a string or an options object. A positive result is the library honouring a property such asmethodthat the developer did not intend to expose. - Confirm which URL parser the client library uses before spending time on parser-specific bypasses, since a standard parser may normalise or reject the characters the payloads rely on.
What happened, in plain words
This is the third and final part of a series about a bug called SSRF. The author spent a few days trying to turn a blind SSRF into something bigger, did not manage it, and still received a 400 USD reward with the bug marked as medium. This part is about one specific idea: making a program that fetches web pages (a library called node-fetch, running on Node.js) send extra hidden lines to another service, so that the other service treats those lines as commands.
To follow this you only need to know that when a program fetches a URL, it builds a short text message (an HTTP request) and sends it over the network. If an attacker can slip a line break into that message, they may be able to add a line of their own. That trick is called CRLF injection, and it is the heart of this story.
Background: what SSRF is
SSRF stands for Server-Side Request Forgery. Imagine a hotel concierge who will fetch anything you ask for from anywhere in the building. If you can trick the concierge into fetching something from a staff-only room, you get information you were never meant to see. In web terms, the server makes a request on your behalf, and if you control the address it fetches, you can point it at internal services that are not reachable from the internet.
A blind SSRF is one where you do not see the response. You can make the server send a request, but you cannot read what comes back. That makes it much harder to turn into a real attack, because you are working without feedback.
Background: what Redis is
Redis is a service that stores data in memory, a bit like a very fast filing cabinet. Programs talk to it over the network using a simple text protocol: you send a line such as SET foo 0 60 5 and it stores something under the name foo. Because the protocol is line-based, if you can inject a line break and then a command, Redis may treat your command as a real instruction. That is why the author wanted to reach Redis through the SSRF.
Background: what CRLF means
CRLF stands for Carriage Return and Line Feed. In text protocols these two characters mark the end of a line. In HTTP, lines separate the request line, the headers and the body. If a program lets a user's input contain a CRLF, the user can end the current line early and start a new one, which is how header injection and request smuggling work. The whole experiment here is about whether node-fetch lets that happen.
The setup
The author used a Kali 2020.1b machine with these versions:
| Component | Version |
|---|---|
| Node.js | 10.19.0 |
| node-fetch | 2.6.0 |
Node.js is a program that runs JavaScript outside the browser. node-fetch is a small library that lets Node.js programs download web pages, the same way a browser does. The author also needed something to play the role of Redis, so instead of installing Redis they used a tool called netcat, which simply listens on a network port and prints whatever arrives:
nc -vvlp 6379
nc is netcat, listening on port 6379, the port Redis normally uses. Anything sent to that port appears on screen, so the author could see whether an injected line made it through. Debugging of the Node.js code was done in Visual Studio Code, an editor that can pause a program and show what it is doing.
The idea being tested
The author had two possible mistakes in mind:
- URL parsing: the part of the library that reads a URL might accept characters that later end up unencoded in the request.
- Handling the URL input as an object rather than as a string: in many dynamic languages, a query string such as
url[href]=...produces an object, and libraries that accept either a string or an options object may then read properties the developer never intended to expose.
A string is just text, like http://example.com. An object is a bundle of named values, like a form with fields. If a library accepts either, an attacker may be able to fill in fields the developer never expected.
Step 1: PayloadsAllTheThings CRLF payloads
The first attempt was to loop over the CRLF injection payloads collected in PayloadsAllTheThings, a well-known public list of attack strings, and watch the netcat console for any injected line. The result was that none of them succeeded: every payload failed.
Step 2: Orange Tsai payloads
The next step was to try the payloads from two articles by Orange Tsai, the first from his Red Hat 2017 talk and the second from his blog. These give a few more options to test:
-*Set-Cookie:injection-* (Unicode U+FF0D U+FF0A)
http://0\r\n SET foo 0 60 5\r\n :6379/
https://0\r\nSET foo 0 60 5\r\n:6379/
These also failed. The conclusion drawn was that this version of Node.js is not vulnerable to CRLF attacks of this kind.
Step 3: Reading the node-fetch code
With the payload approach exhausted, the next move was to dig into the node-fetch source. The first of the two hypotheses, URL parsing, turned out not to exist as a problem: node-fetch uses the standard Node.js Url.parse for its input and does not do anything fancy with it.
The second hypothesis needed more investigation. The reason for being interested in an object instead of a string is that in many dynamic languages a request like this produces an object rather than a string:
http://localhost:3000/c?url[href]=localhost&url[method]=POST
If the application passes the parsed query value straight through, the library receives an object with href and method properties. That can be handy in some scenarios, especially when developers did not predict it. The researcher demonstrated this parsing behaviour in the Node.js Express framework.
In node-fetch one possible attack vector was found: it looked possible to use an object instead of a string for the input parameter, and input.method could change the method type under some specific conditions. After spending some time in the debugger, this turned out to be a dead end.
Why it did not work
The report does not identify a vulnerability in node-fetch 2.6.0 or in Node.js 10.19.0. The CRLF payloads from PayloadsAllTheThings and from Orange Tsai's work did not produce injected lines, and the conclusion drawn was that this Node.js version is not vulnerable to CRLF attacks of this kind. The object-input idea was investigated in the debugger and found not to be exploitable in the way hoped. The report does not state which code path blocked the payloads.
What it was worth
The researcher did not manage to escalate the blind SSRF to anything more. Several days were spent trying different approaches without success. After submitting the report, the researcher was awarded 400 USD and the bug was marked as medium. The report does not describe the affected application or what the blind SSRF itself allowed.
Was there a fix?
Ideas you can reuse on other targets
The report does not list further variants beyond the payloads above. The general directions it explores, which can be repeated against other targets, are:
- Run the CRLF payload collections from PayloadsAllTheThings and from Orange Tsai's research against the target's HTTP client, watching a listener such as
nc -vvlp 6379for an injected line. A positive result is a line such asSET foo 0 60 5arriving at the listener outside the HTTP request. - Check whether the application passes query parameters as objects (for example
url[href]=...&url[method]=...) into a library that accepts either a string or an options object. A positive result is the library honouring a property such asmethodthat the developer did not intend to expose. - Confirm which URL parser the client library uses before spending time on parser-specific bypasses, since a standard parser may normalise or reject the characters the payloads rely on.
Glossary
- SSRF: Server-Side Request Forgery, a bug where an attacker makes a server send a request to an address of the attacker's choosing.
- Blind SSRF: an SSRF where the attacker cannot see the response, only cause the request.
- CRLF: Carriage Return and Line Feed, the two characters that end a line in text protocols such as HTTP.
- CRLF injection: slipping a line break into a message so that extra lines can be added.
- HTTP request: the text message a client sends to a server to ask for something.
- Header: a named value at the top of an HTTP request or response, such as
Set-Cookie. - Node.js: a program that runs JavaScript outside a browser.
- node-fetch: a library that lets Node.js programs download web pages.
- Redis: an in-memory data store that speaks a simple line-based protocol.
- netcat: a tool that listens on a network port and prints whatever arrives.
- Port: a numbered channel on a computer that a service listens on; Redis normally uses 6379.
- Payload: the crafted input an attacker sends to try to trigger a bug.
- URL parser: the part of a program that reads a URL and breaks it into pieces.
- String: plain text, such as
http://example.com. - Object: a bundle of named values, like a form with fields.
- Express: a popular framework for building web applications with Node.js.
- Debugger: a tool that pauses a running program so you can inspect what it is doing.
- Visual Studio Code: an editor with a built-in debugger.
- Kali: a Linux distribution used for security testing.
- Medium: a severity label meaning the bug is real but not the most serious kind.