TOTP Key is shorter than RFC 4226 recommended minimum
Phabricator presents a 16-character base32 TOTP secret, below the RFC 4226 minimum of 128 bits; the report rates impact as none.
Originally disclosed on HackerOne
- Fixed in 3 hours
- Paid in 3 hours
- Disclosed after 30 days
mongoose
Observed Behavior: When creating a TOTP secret a 16 character base32 encoded string is presented to the user.
Expected Behavior: I would expect a 32 character base32 secret to be presented. The RFC recommends 160 bits of entropy, which is 32 character x 5 bits per character in a base32 string. RFC 4226 Section 4 Requirement 6 - 128 bit minimum, 160 recommended.
Phabricator Version: phabricator
24a061f844958cc22d6f4874535b57574a6c13c3 (Fri, Nov 2)
arcanist
83661809e532c3fe444a8bf7c7d6936e6377691b (Fri, Oct 26)
phutil
cf96fd681e7d35ad21c7aaaa0bd1869124e377ef (Thu, Nov 1)
diff
3.6 at /usr/bin/diff
git
2.19.1 at /usr/bin/git
hg
Not Available
pygmentize
2.2.0 at /usr/local/bin/pygmentize
svn
Not Available
Reproduction Steps: Request a second factor for a user authenticated with username and password, count the characters presented. It should be 32 not 16.
Problematic code is
src/applications/auth/factor/PhabricatorTOTPAuthFactor.php:
return strtoupper(Filesystem::readRandomCharacters(16));
readRandomCharacters from phutils returns a random string using base32 characters.
Impact
I have rated this a no impact, however I am not a cryptographer, and it is a clear violation of a cryptographic minimum key length requirement in a published RFC. Quantum supremacy may also be relevant here.
Context
Phabricator is a suite of web applications for software development, and it supports two-factor authentication. One of the supported second factors is TOTP, the time-based one-time password scheme defined in RFC 6238, which builds on the HOTP algorithm in RFC 4226. When a user enrols a TOTP factor, the server generates a shared secret, presents it to the user (typically as a base32 string to type into an authenticator app), and later derives six-digit codes from it.
The security of the whole scheme rests on the entropy of that shared secret. RFC 4226 Section 4, Requirement 6 sets a minimum of 128 bits and recommends 160 bits. In base32 each character carries 5 bits, so 160 bits corresponds to 32 characters and 128 bits to roughly 26.
Setup
The report records the exact revisions in use:
phabricator
[redacted] (Fri, Nov 2)
arcanist
[redacted] (Fri, Oct 26)
phutil
[redacted] (Thu, Nov 1)
diff
3.6 at /usr/bin/diff
git
2.19.1 at /usr/bin/git
hg
Not Available
pygmentize
2.2.0 at /usr/local/bin/pygmentize
svn
Not Available
Reproduction requires a user authenticated with username and password who requests a second factor. No special role or privilege is described.
Finding it
The researcher requested a second factor and counted the characters presented. The observed behaviour was a 16 character base32 encoded string. The expected behaviour, per the RFC, was 32 characters.
The reasoning is arithmetic: 16 base32 characters at 5 bits each give 80 bits of entropy, half of the 160 bits the RFC recommends and well below its 128 bit minimum. The report cites RFC 4226 Section 4 Requirement 6, with a 128 bit minimum and 160 recommended.
Exploitation
Step 1: Enrol a TOTP factor
Request a second factor for a user authenticated with username and password, then count the characters presented in the secret. The researcher's instruction is simply that the count should be 32, not 16.
Step 2: Confirm the source of the length
The problematic code is in src/applications/auth/factor/PhabricatorTOTPAuthFactor.php:
return strtoupper(Filesystem::readRandomCharacters(16));
readRandomCharacters comes from phutil and returns a random string drawn from the base32 alphabet. The literal 16 passed to it is therefore the number of base32 characters in the secret, which fixes the entropy at 80 bits. Uppercasing the result does not change the entropy; it only matches the conventional presentation of base32 secrets.
Root cause
The secret length is hard-coded to 16 base32 characters at the point where the TOTP factor is created. The random source itself is not in question: the report attributes the character set to Filesystem::readRandomCharacters and says nothing about the quality of the underlying randomness. The defect is the parameter, not the generator. Nothing in the report indicates a check that would compare the generated length against the RFC minimum, so the short secret is produced and shown to the user without objection.
Impact
The researcher rated the issue as no impact, while noting that it is a clear violation of a cryptographic minimum key length requirement in a published RFC, and that they are not a cryptographer. The report does not demonstrate an attack against a live account, and it does not quantify the work needed to recover an 80 bit secret. The practical consequence is that TOTP factors issued by this version are weaker than the standard requires.
Fix
The report does not describe a fix or a patch. What it identifies is the line that would need to change: the argument to Filesystem::readRandomCharacters in PhabricatorTOTPAuthFactor.php should produce a secret of at least the RFC minimum length, that is 32 base32 characters for the recommended 160 bits.
Variants to test
- Other second-factor and recovery-code generators in the same product. Enrol each factor and count the characters or bytes in the issued secret; a positive result is a secret shorter than the RFC minimum for its encoding.
- Other TOTP implementations in different products. Generate a secret and decode it from base32; a positive result is fewer than 26 characters, or fewer than 32 where the vendor claims RFC-recommended strength.
- API tokens and password reset tokens issued by the same application. Compare the token length and alphabet against the documented or expected entropy; a positive result is a token whose alphabet and length multiply out to well under 128 bits.
- Enrolment flows that let the client choose parameters. Check whether the secret length or algorithm can be influenced by the request; a positive result is a server that accepts a shorter secret than its own default.
Hunting notes
This class of bug hides in the code that mints secrets, not in the endpoints that consume them: factor enrolment, token generation, recovery codes and anything else that calls a random-string helper with a length argument. During recon, the signal is a product that documents RFC compliance for its second factor, which makes the actual secret length worth measuring. The report ties the observation to a specific RFC requirement, quotes the offending line and rates the impact as none. The report was disclosed on 7 December 2018 and carries a bounty of 300.00 USD.
What happened, in plain words
Phabricator is a collection of web applications that software teams use to manage their work: code review, bug tracking, project planning and so on. Like many login systems, it offers two-factor authentication, usually shortened to 2FA. That is the extra step after your password, such as a six-digit code from a phone app, so that stealing your password alone is not enough to get into your account.
One of the 2FA methods Phabricator supports is TOTP, which stands for time-based one-time password. When you switch TOTP on, the server makes up a long random secret and shows it to you, usually as a string of letters and digits. You type that string into an authenticator app on your phone. From then on, both your phone and the server use the same secret to calculate a fresh six-digit code every 30 seconds or so, and the codes match because the secret matches.
Everything about the safety of that scheme depends on how hard the secret is to guess. There is a published technical standard, RFC 4226, that describes how these codes should be generated, and it sets a minimum length for the secret and a recommended length. The researcher noticed that Phabricator was handing out secrets shorter than the standard recommends. That is the whole finding: the secret is too short. No account was broken into, and the researcher rated the impact as none, but the product was not following a rule written into a public standard.
Context
Phabricator supports two-factor authentication, and one of the supported second factors is TOTP. TOTP is defined in a document called RFC 6238. An RFC is a numbered public document published by the Internet Engineering Task Force, the body that writes down how much of the internet works; when people say a product "violates an RFC", they mean it does not follow one of those written rules. RFC 6238 builds on an older algorithm called HOTP, which is defined in RFC 4226.
When a user enrols a TOTP factor, meaning they set it up for the first time, the server generates a shared secret. It shows that secret to the user, typically as a base32 string to type into an authenticator app, and later uses the same secret to derive six-digit codes. Base32 is an encoding, a way of writing raw random bytes as text using only 32 possible characters, here the letters A to Z and the digits 2 to 7. It is used because it is easy to read aloud and type without confusion between similar-looking characters. Because the alphabet has exactly 32 symbols, each character carries 5 bits of information: 2 multiplied by itself 5 times is 32. A bit is the smallest unit of information, a single yes or no, and the number of bits in a secret is a measure of how many possibilities an attacker would have to try.
The security of the whole scheme rests on the entropy of that shared secret. Entropy here simply means unpredictability: how many different secrets an attacker would have to consider. RFC 4226 Section 4, Requirement 6 sets a minimum of 128 bits and recommends 160 bits. In base32 each character carries 5 bits, so 160 bits corresponds to 32 characters and 128 bits to roughly 26.
Setup
The report records the exact revisions in use:
phabricator
[redacted] (Fri, Nov 2)
arcanist
[redacted] (Fri, Oct 26)
phutil
[redacted] (Thu, Nov 1)
diff
3.6 at /usr/bin/diff
git
2.19.1 at /usr/bin/git
hg
Not Available
pygmentize
2.2.0 at /usr/local/bin/pygmentize
svn
Not Available
This block is a list of the software the researcher had installed, with version details. Phabricator is made of several parts that are developed separately, and each part has its own revision identifier, a long string of letters and digits that pins down exactly which snapshot of the code was used. phabricator, arcanist and phutil are those parts; the dates in brackets say when each snapshot was made. The remaining lines list helper programs on the machine: diff at version 3.6, git at 2.19.1, pygmentize at 2.2.0, and hg and svn marked as not available. Recording all of this matters because a bug report is only useful if someone else can rebuild the same environment and see the same thing; if the code changes, the bug may already be gone or may look different.
Reproduction requires a user authenticated with username and password who requests a second factor. No special role or privilege is described.
Finding it
The researcher requested a second factor and counted the characters presented. The observed behaviour was a 16 character base32 encoded string. The expected behaviour, per the RFC, was 32 characters.
The reasoning is arithmetic: 16 base32 characters at 5 bits each give 80 bits of entropy, half of the 160 bits the RFC recommends and well below its 128 bit minimum. The report cites RFC 4226 Section 4 Requirement 6, with a 128 bit minimum and 160 recommended. To put 80 bits in perspective without overstating it: it is not a trivially guessable secret, but it is half the strength the standard asks for, and the point of a standard is that everyone can rely on the same floor.
Exploitation
Step 1: Enrol a TOTP factor
Request a second factor for a user authenticated with username and password, then count the characters presented in the secret. The researcher's instruction is simply that the count should be 32, not 16.
Why this step: the fastest way to confirm a length problem is to look at the thing being handed out. You log in normally, go through the setup screen for the authenticator app, and instead of scanning or typing the secret straight away, you count how many characters it contains. No tools beyond a browser and your eyes are needed.
Step 2: Confirm the source of the length
The problematic code is in src/applications/auth/factor/PhabricatorTOTPAuthFactor.php:
return strtoupper(Filesystem::readRandomCharacters(16));
This is one line of PHP, the programming language Phabricator is written in. Reading it from the inside out:
Filesystem::readRandomCharacters(16)calls a helper function that produces a random string, and the number in brackets is how many characters to produce. Here it is 16, which is exactly the length the researcher counted on screen. TheFilesystem::part says where the helper lives: it is a function belonging to a class calledFilesystem.strtoupper(...)takes that string and converts every letter to uppercase. This is presentation only: changing the case of a letter does not change how random the secret is, it just makes it look the way base32 secrets are conventionally written.returnhands the finished string back to whatever asked for it, which is the enrolment screen that shows the secret to the user.
readRandomCharacters comes from phutil, a library of shared code used by Phabricator, and returns a random string drawn from the base32 alphabet. The literal 16 passed to it is therefore the number of base32 characters in the secret, which fixes the entropy at 80 bits. Uppercasing the result does not change the entropy; it only matches the conventional presentation of base32 secrets.
Why this step: seeing the number 16 in the code turns a suspicion into a fact. The screen showed 16 characters, and the code asks for 16 characters, so the two agree and the cause is located.
Root cause
The secret length is hard-coded to 16 base32 characters at the point where the TOTP factor is created. "Hard-coded" means the number is written directly into the program rather than being calculated or read from a setting, so it is the same for every user and every installation. The random source itself is not in question: the report attributes the character set to Filesystem::readRandomCharacters and says nothing about the quality of the underlying randomness. The defect is the parameter, not the generator. Nothing in the report indicates a check that would compare the generated length against the RFC minimum, so the short secret is produced and shown to the user without objection.
A useful comparison: a good lock is useless if the key is cut too short to have enough different possible shapes. The lock mechanism here is fine; the key is simply shorter than the standard says it should be.
Impact
The researcher rated the issue as no impact, while noting that it is a clear violation of a cryptographic minimum key length requirement in a published RFC, and that they are not a cryptographer. The report does not demonstrate an attack against a live account, and it does not quantify the work needed to recover an 80 bit secret. The practical consequence is that TOTP factors issued by this version are weaker than the standard requires.
In plain terms: nobody showed a break-in. What was shown is that a safety rule written down in a public standard was not being followed, and that every user of this feature got a weaker secret than they should have. For a beginner reading bug reports, this is a good example of a finding that is real and worth fixing even though its severity is low.
Fix
The report does not describe a fix or a patch. What it identifies is the line that would need to change: the argument to Filesystem::readRandomCharacters in PhabricatorTOTPAuthFactor.php should produce a secret of at least the RFC minimum length, that is 32 base32 characters for the recommended 160 bits. "Argument" here means the value in the brackets, the 16.
Variants to test
These are other places where the same kind of mistake could appear, written as things a beginner could check:
- Other second-factor and recovery-code generators in the same product. Enrol each factor and count the characters or bytes in the issued secret; a positive result is a secret shorter than the RFC minimum for its encoding. Recovery codes are the backup codes you print out in case you lose your phone; they are secrets too and deserve the same scrutiny.
- Other TOTP implementations in different products. Generate a secret and decode it from base32; a positive result is fewer than 26 characters, or fewer than 32 where the vendor claims RFC-recommended strength. Decoding from base32 means converting the text back into raw bytes so you can count the actual bits.
- API tokens and password reset tokens issued by the same application. Compare the token length and alphabet against the documented or expected entropy; a positive result is a token whose alphabet and length multiply out to well under 128 bits. An API token is a long password-like string that a program uses to prove its identity to a service, and a password reset token is the secret in the link emailed to you when you forget your password. If either is short enough to guess, accounts can be taken over.
- Enrolment flows that let the client choose parameters. Check whether the secret length or algorithm can be influenced by the request; a positive result is a server that accepts a shorter secret than its own default. "The client" means the browser or app making the request; if the server lets it ask for a weaker setting, an attacker can simply ask for one.
Hunting notes
This class of bug hides in the code that mints secrets, not in the endpoints that consume them: factor enrolment, token generation, recovery codes and anything else that calls a random-string helper with a length argument. An endpoint is a specific address on a server that does a specific job, such as the one that starts 2FA setup; the point is that the weakness is in the moment a secret is created, not in the moment it is later checked. During recon, meaning the early exploration of a target to decide what to look at, the signal is a product that documents RFC compliance for its second factor, which makes the actual secret length worth measuring: if a vendor claims to follow a standard, checking whether it really does is fair game.
The report ties the observation to a specific RFC requirement, quotes the offending line and rates the impact as none. That combination, a reproducible observation, a named rule and a modest impact claim, is what makes a low-severity report credible. The report was disclosed on 7 December 2018 and carries a bounty of 300.00 USD.
Glossary
- 2FA (two-factor authentication): a login step that requires something in addition to a password, such as a code from a phone app.
- API token: a long secret string that a program presents to a service to prove who it is, instead of a password.
- Argument: the value written inside the brackets of a function call, such as the 16 in
readRandomCharacters(16). - Base32: an encoding that writes data as text using 32 possible characters, here A to Z and 2 to 7, so each character carries 5 bits.
- Bit: the smallest unit of information, a single yes or no; more bits in a secret means more possible secrets.
- Bounty: a payment a company makes to someone who reports a valid security problem to it.
- Class (in code): a named grouping of related functions in a program, such as
Filesystem. - Client: the browser or app that sends requests to a server.
- Cryptographer: a person who studies the mathematics of keeping information secret.
- Disclosure: making a bug report public, usually after the vendor has had time to fix it.
- Encoding: a way of representing data, such as writing raw bytes as base32 text; it changes the form, not the underlying information.
- Endpoint: a specific address on a server that performs a specific job.
- Entropy: a measure of unpredictability, effectively how many different values an attacker would have to try.
- Enrolment: the one-time setup where a user registers a second factor, such as a TOTP secret, with the server.
- Function: a named piece of code that does a job and can be called from elsewhere.
- Hard-coded: a value written directly into the program rather than calculated or configured.
- HOTP: an earlier one-time password algorithm, defined in RFC 4226, on which TOTP is built.
- IETF (Internet Engineering Task Force): the organisation that publishes the RFC documents describing internet standards.
- Instance: one running installation of a piece of software.
- Literal: a value written out directly in code, such as the number 16.
- Parameter: a value passed into a function that changes what it does, such as the length of a random string.
- Password reset token: the secret contained in the link emailed to you when you ask to reset a forgotten password.
- PHP: the programming language Phabricator is written in.
- Phutil: a library of shared code used by Phabricator.
- Recovery code: a backup code issued at 2FA setup, used if you lose your authenticator device.
- Recon: the early phase of exploring a target to decide what is worth testing.
- Revision: an identifier for one exact snapshot of a piece of software's code.
- RFC: a numbered public document published by the IETF that describes how something on the internet should work.
- RFC 4226: the standard defining the HOTP algorithm, including a requirement that secrets be at least 128 bits and preferably 160 bits.
- RFC 6238: the standard defining TOTP, built on HOTP.
- Root cause: the underlying reason a bug exists, as opposed to its visible symptom.
- Secret (shared secret): the random value that both the server and your authenticator app hold and use to calculate matching codes.
- Severity: a rating of how serious a security issue is.
- TOTP (time-based one-time password): a 2FA method where a fresh six-digit code is derived from a shared secret and the current time.
- Vendor: the company or project that makes the software.
- Vulnerability: a weakness in software that could be used to cause harm.