`sshpk` is vulnerable to ReDoS when parsing crafted invalid public keys
A regular expression in the sshpk library used to parse SSH public keys can be made to backtrack heavily by a crafted key string, so parsing takes a very long time. An attacker who can supply a key for parsing could cause denial of service.
- Triaged in 12 hours
- Fixed in 17 days
- Disclosed after 38 days
I would like to report a ReDoS in sshpk
It allows to cause Denial of Service by trying to parse a crafted public key.
Module
module name: sshpk
version: 1.13.1
npm page: https://www.npmjs.com/package/sshpk
Module Description
Parse, convert, fingerprint and use SSH keys (both public and private) in pure node -- no ssh-keygen or other external dependencies.
Module Stats
320 485 downloads in the last day 4 709 033 downloads in the last week 19 365 516 downloads in the last month
~232 386 192 estimated downloads per year
Vulnerability
Vulnerability Description
ReDoS.
- regex: /^([a-z0-9-]+)[ \t]+([a-zA-Z0-9+/]+[=]*)([\n \t]+([^\n]+))?$/
- evil string:
ssh-rsa a${Array(200000).join(' ')}x\nx(~200 KB, unwrap js template string) - file: https://github.com/joyent/node-sshpk/blob/v1.13.1/lib/formats/ssh.js#L17
The testcase uses ~200 KB string to demonstrate long unavailability period, but parsing is also considerably slow on shorter strings.
Steps To Reproduce:
var keyPub = `ssh-rsa a${Array(200000).join(' ')}x\nx`;
var key = require('sshpk').parseKey(keyPub, 'ssh');
Supporting Material/References:
- Arch Linux Current
- Node.js 9.5.0
- npm 5.6.0
Wrap up
- I contacted the maintainer to let him know: N
- I opened an issue in the related repository: N
Impact
Cause denial of service by parsing a crafted public key file.
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 regular expression used to split the SSH key line has nested optional groups and greedy quantifiers that allow catastrophic backtracking on crafted input.
Impact
Parsing a crafted public key causes a long unavailability period, allowing denial of service; the report notes parsing is also considerably slow on shorter strings.
What the attacker needed
Attacker can supply a public key for parsing
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 check or split text. Some patterns can be made to try an enormous number of combinations when the input is crafted in a particular way, which keeps the program busy for a long time. Here a very long key string with many spaces makes the key parser hang, so the service cannot respond to other requests.
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.