Blog | G5 Cyber Security

TOTP Code Prediction Risk

TL;DR

Yes, an attacker *could* potentially predict future Time-based One-Time Password (TOTP) codes if they have enough past codes and their exact timestamps. This is because TOTP relies on a predictable algorithm. However, it’s difficult in practice without compromising the device or server generating the codes.

Understanding the Risk

TOTP generates codes based on a shared secret key and the current time. The algorithm typically uses a hash function (like SHA-1) with the secret key and a time step. The time step is usually 30 seconds, but can be configured differently.

How an Attack Might Work

  1. Code Collection: An attacker needs to collect a series of TOTP codes generated by the victim’s device. The more codes collected, the higher the chance of success.
  2. Timestamp Capture: Crucially, the attacker *must* know the exact timestamps when each code was generated. This is often the hardest part. If the timestamps are inaccurate, the attack will fail.
  3. Secret Key Recovery: Using the collected codes and timestamps, an attacker can attempt to reverse-engineer the shared secret key used by the TOTP algorithm. There are several techniques for this (see ‘Tools & Techniques’ below).
  4. Future Code Prediction: Once the secret key is recovered, the attacker can calculate future TOTP codes using the current time and the same algorithm as the victim’s device.

Steps to Mitigate the Risk

  1. Protect Timestamp Accuracy: The most important step! Ensure that the server or system receiving the TOTP codes has a highly accurate clock synchronized with a reliable time source (like NTP). Clock drift is the attacker’s biggest advantage.
  2. Rate Limiting: Implement rate limiting on TOTP code submissions. This makes it harder for an attacker to collect enough codes quickly. For example, only allow a limited number of attempts per minute.
  3. Code Expiration: TOTP codes are already time-limited (typically 30 seconds), but ensure this is strictly enforced on the server side. Do not accept codes that have expired.
  4. Monitor for Anomalous Activity: Look for patterns of repeated failed code submissions from the same IP address or user account. This could indicate an attack in progress.
  5. Use Strong Secrets: Ensure that the shared secrets used for TOTP are generated using a cryptographically secure random number generator (CSPRNG). Longer, more complex secrets are harder to crack.
  6. Consider HOTP: Hash-based One-Time Password (HOTP) doesn’t rely on time and is less vulnerable to this type of attack. However, it requires a counter that must be synchronized between the server and client.

Tools & Techniques

Example Command (oathtool)

This example shows how to verify a TOTP code against a shared secret:

oathtool --totp -b  -t 

(Replace <base32_secret> with the victim’s TOTP secret and <current_time_stamp> with the current Unix timestamp.)

cyber security Best Practices

Exit mobile version