repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
index.mdMarkdown219 lines10.1 KiBsource raw download

title: "Insecure Randomness" topic: "Insecure Randomness" topicSlug: "insecure-randomness" sourcePath: "Insecure Randomness/README.md" sourceUrl: "https://github.com/swisskyrepo/PayloadsAllTheThings/blob/3ac27901c711/Insecure%20Randomness/README.md" sha: "3ac27901c711" isReadme: true#

Insecure Randomness#

Insecure randomness refers to the weaknesses associated with random number generation in computing, particularly when such randomness is used for security-critical purposes. Vulnerabilities in random number generators (RNGs) can lead to predictable outputs that can be exploited by attackers, resulting in potential data breaches or unauthorized access.

Summary#

Methodology#

Insecure randomness arises when the source of randomness or the method of generating random values is not sufficiently unpredictable. This can lead to predictable outputs, which can be exploited by attackers. Below, we examine common methods that are prone to insecure randomness, including time-based seeds, GUIDs, UUIDs, MongoDB ObjectIds, and the uniqid() function.

Time-Based Seeds#

Many random number generators (RNGs) use the current system time (e.g., milliseconds since epoch) as a seed. This approach can be insecure because the seed value can be easily predicted, especially in automated or scripted environments.

import random
import time

seed = int(time.time())
random.seed(seed)
print(random.randint(1, 100))

The RNG is seeded with the current time, making it predictable for anyone who knows or can estimate the seed value. By knowing the exact time, an attacker can regenerate the correct random value, here is an example for the date 2024-11-10 13:37.

import random
import time

# Seed based on the provided timestamp
seed = int(time.mktime(time.strptime('2024-11-10 13:37', '%Y-%m-%d %H:%M')))
random.seed(seed)

# Generate the random number
print(random.randint(1, 100))

GUID / UUID#

A GUID (Globally Unique Identifier) or UUID (Universally Unique Identifier) is a 128-bit number used to uniquely identify information in computer systems. They are typically represented as a string of hexadecimal digits, divided into five groups separated by hyphens, such as 550e8400-e29b-41d4-a716-446655440000. GUIDs/UUIDs are designed to be unique across both space and time, reducing the likelihood of duplication even when generated by different systems or at different times.

GUID Versions#

Version identification: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx The four-bit M and the 1- to 3-bit N fields code the format of the UUID itself.

Version Notes
0 Only 00000000-0000-0000-0000-000000000000
1 based on time, or clock sequence
2 reserved in the RFC 4122, but omitted in many implementations
3 based on a MD5 hash
4 randomly generated
5 based on a SHA1 hash

Tools#

Mongo ObjectId#

Mongo ObjectIds are generated in a predictable manner, the 12-byte ObjectId value consists of:

Token example

Tools#

Uniqid#

Token derived using uniqid are based on timestamp and they can be reversed.

Token examples

Tools#

import math
import datetime

def uniqid(timestamp: float) -> str:
    sec = math.floor(timestamp)
    usec = round(1000000 * (timestamp - sec))
    return "%8x%05x" % (sec, usec)

def reverse_uniqid(value: str) -> float:
    sec = int(value[:8], 16)
    usec = int(value[8:], 16)
    return float(f"{sec}.{usec}")

tokens = ["6659cea087cd6" , "6659cea087cea"]
for token in tokens:
    t = float(reverse_uniqid(token))
    d = datetime.datetime.fromtimestamp(t)
    print(f"{token} - {t} => {d}")

mt_rand#

Breaking mt_rand() with two output values and no bruteforce.

./display_mt_rand.php 12345678 123
712530069 674417379

./reverse_mt_rand.py 712530069 674417379 123 1

Custom Algorithms#

Creating your own randomness algorithm is generally not recommended. Below are some examples found on GitHub or StackOverflow that are sometimes used in production, but may not be reliable or secure.

Tools#

Generic identification and sandwich attack:

References#