SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
How to exploit rand()?
Dharma Ganesan
Using basic rand() to generate keys
● Many programming languages support the basic rand() function
● rand() functions have a predictable behavior
○ Given the ith
byte of a random number, it is possible to predict the (i+1)th
byte
● Crypto keys shall be generated using secure random number generators
● For example, in Linux, urandom file contains good quality random numbers
● This demo shows how an attacker can identify the key when rand() is used
● Assume that the attacker knows the date/time of key generation
○ This is not a hard assumption - it is possible to perform this attack without this knowledge
2
Using rand to generate keys
unsigned char* generateKey_bad(unsigned int keySize) {
int i = 0;
unsigned char* key = (unsigned char *)malloc(sizeof(unsigned char)*keySize);
/* Error handling code left out for demo purposes */
srand(time(NULL)); /* Predictable behavior due to this seed */
for (i = 0; i < keySize; i++) {
key[i] = rand() % 256;
}
return key;
}
3
Using rand to generate keys ...
● After running the above program, we got 16 bytes of AES key
db76b5c16f7a4956ffb01de3433b05b8
● The attacker can reconstruct all these 16 bytes very easily as follows
● Let’s say the key was generated in the interval [Thu Nov 7 10:57:12 EST
2019, Thu Nov 7 11:01:29 EST 2019]
● We now show how to predict the possible list of keys
● We just have to initialize the seed for all time t in the above interval only
○ $ date -d "2019-11-07 10:57:12" +%s
○ Output: 1573142232
○ date -d "2019-11-07 11:01:29" +%s
○ Output: 1573142489
4
Attack: generating a list of potential keys
5
● For demo purposes, the
starting and ending seed
values are hardcoded (see
the loop index)
● Note that srand is called for
all possible seed values
● This attack is not specific to
CBC but worth stressing it
Attack: generating a list of potential keys ...
6
● There were only 258 keys in that time interval
○ wc -l keys.txt
○ 258 keys.txt
● The highlighted row indeed shows our key
● The attacker has to just try only 258 possible
keys to decrypt the data
Fix is simple - use /dev/urandom
unsigned char* generateKey_good(unsigned int keySize) {
unsigned char* key = (unsigned char *)malloc(sizeof(unsigned char)*keySize);
/* Error handling code is left out for demo purposes */
FILE* random = fopen("/dev/urandom", "r");
int i = 0;
for (i = 0; i < keySize; i++) {
fread(key, sizeof(unsigned char)*keySize, 1, random);
}
fclose(random);
return key;
}
7

More Related Content

PPTX
Secure coding for developers
PPT
14 key management & exchange
PDF
Sullivan randomness-infiltrate 2014
PDF
3. Cryptographic Security
PDF
CNIT 141: 3. Cryptographic Security
PDF
Linux randomnumbergenerator
PDF
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
PDF
[Usenix's WOOT'14] Attacking the Linux PRNG and Android - Weaknesses in Seedi...
Secure coding for developers
14 key management & exchange
Sullivan randomness-infiltrate 2014
3. Cryptographic Security
CNIT 141: 3. Cryptographic Security
Linux randomnumbergenerator
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
[Usenix's WOOT'14] Attacking the Linux PRNG and Android - Weaknesses in Seedi...

Similar to How to exploit rand()? (20)

PDF
The entropic principle: /dev/u?random and NetBSD by Taylor R Campbell
PDF
Working with cryptographic key information
PPT
Software security
PPTX
«Applied cryptanalysis stream ciphers» by Vladimir Garbuz
PDF
Applied cryptanalysis - stream ciphers
ODP
Network Security Topic 3 cryptography
PDF
6.Temp & Rand
PPTX
antoanthongtin_Lesson 3- Software Security (1).pptx
PPTX
Basic Cryptography & Security
PPSX
Linux for Cybersecurity CYB110 - Unit 7.ppsx
PPTX
Ethical hacking Chapter 12 - Encryption - Eric Vanderburg
PPTX
Public Key Cryptosystems and RSA
PDF
hashdays 2011: Jean-Philippe Aumasson - Cryptanalysis vs. Reality
PDF
Cryptography
PDF
Biting into the forbidden fruit. Lessons from trusting Javascript crypto.
PPT
Computer security
PPT
Computer security
PPT
Computer security
PPT
Computer security
PPT
Computer security
The entropic principle: /dev/u?random and NetBSD by Taylor R Campbell
Working with cryptographic key information
Software security
«Applied cryptanalysis stream ciphers» by Vladimir Garbuz
Applied cryptanalysis - stream ciphers
Network Security Topic 3 cryptography
6.Temp & Rand
antoanthongtin_Lesson 3- Software Security (1).pptx
Basic Cryptography & Security
Linux for Cybersecurity CYB110 - Unit 7.ppsx
Ethical hacking Chapter 12 - Encryption - Eric Vanderburg
Public Key Cryptosystems and RSA
hashdays 2011: Jean-Philippe Aumasson - Cryptanalysis vs. Reality
Cryptography
Biting into the forbidden fruit. Lessons from trusting Javascript crypto.
Computer security
Computer security
Computer security
Computer security
Computer security
Ad

More from Dharmalingam Ganesan (20)

PDF
.NET Deserialization Attacks
PDF
Reverse Architecting using Relation Algebra.pdf
PDF
Cyclic Attacks on the RSA Trapdoor Function
PDF
An Analysis of RSA Public Exponent e
PDF
An Analysis of Secure Remote Password (SRP)
PDF
Thank-a-Gram
PDF
Active Attacks on DH Key Exchange
PDF
Can I write to a read only file ?
PPTX
How do computers exchange secrets using Math?
PDF
On the Secrecy of RSA Private Keys
PDF
Computing the Square Roots of Unity to break RSA using Quantum Algorithms
PDF
Analysis of Short RSA Secret Exponent d
PDF
Dependency Analysis of RSA Private Variables
PDF
Analysis of Shared RSA Modulus
PDF
RSA Game using an Oracle
PDF
RSA Two Person Game
PDF
RSA without Integrity Checks
PPTX
RSA without Padding
PDF
Solutions to online rsa factoring challenges
PDF
Security of RSA and Integer Factorization
.NET Deserialization Attacks
Reverse Architecting using Relation Algebra.pdf
Cyclic Attacks on the RSA Trapdoor Function
An Analysis of RSA Public Exponent e
An Analysis of Secure Remote Password (SRP)
Thank-a-Gram
Active Attacks on DH Key Exchange
Can I write to a read only file ?
How do computers exchange secrets using Math?
On the Secrecy of RSA Private Keys
Computing the Square Roots of Unity to break RSA using Quantum Algorithms
Analysis of Short RSA Secret Exponent d
Dependency Analysis of RSA Private Variables
Analysis of Shared RSA Modulus
RSA Game using an Oracle
RSA Two Person Game
RSA without Integrity Checks
RSA without Padding
Solutions to online rsa factoring challenges
Security of RSA and Integer Factorization
Ad

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
master seminar digital applications in india
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
GDM (1) (1).pptx small presentation for students
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Types and Its function , kingdom of life
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
O5-L3 Freight Transport Ops (International) V1.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
master seminar digital applications in india
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pre independence Education in Inndia.pdf
PPH.pptx obstetrics and gynecology in nursing
GDM (1) (1).pptx small presentation for students
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
O7-L3 Supply Chain Operations - ICLT Program
Sports Quiz easy sports quiz sports quiz
Microbial disease of the cardiovascular and lymphatic systems
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf

How to exploit rand()?

  • 1. How to exploit rand()? Dharma Ganesan
  • 2. Using basic rand() to generate keys ● Many programming languages support the basic rand() function ● rand() functions have a predictable behavior ○ Given the ith byte of a random number, it is possible to predict the (i+1)th byte ● Crypto keys shall be generated using secure random number generators ● For example, in Linux, urandom file contains good quality random numbers ● This demo shows how an attacker can identify the key when rand() is used ● Assume that the attacker knows the date/time of key generation ○ This is not a hard assumption - it is possible to perform this attack without this knowledge 2
  • 3. Using rand to generate keys unsigned char* generateKey_bad(unsigned int keySize) { int i = 0; unsigned char* key = (unsigned char *)malloc(sizeof(unsigned char)*keySize); /* Error handling code left out for demo purposes */ srand(time(NULL)); /* Predictable behavior due to this seed */ for (i = 0; i < keySize; i++) { key[i] = rand() % 256; } return key; } 3
  • 4. Using rand to generate keys ... ● After running the above program, we got 16 bytes of AES key db76b5c16f7a4956ffb01de3433b05b8 ● The attacker can reconstruct all these 16 bytes very easily as follows ● Let’s say the key was generated in the interval [Thu Nov 7 10:57:12 EST 2019, Thu Nov 7 11:01:29 EST 2019] ● We now show how to predict the possible list of keys ● We just have to initialize the seed for all time t in the above interval only ○ $ date -d "2019-11-07 10:57:12" +%s ○ Output: 1573142232 ○ date -d "2019-11-07 11:01:29" +%s ○ Output: 1573142489 4
  • 5. Attack: generating a list of potential keys 5 ● For demo purposes, the starting and ending seed values are hardcoded (see the loop index) ● Note that srand is called for all possible seed values ● This attack is not specific to CBC but worth stressing it
  • 6. Attack: generating a list of potential keys ... 6 ● There were only 258 keys in that time interval ○ wc -l keys.txt ○ 258 keys.txt ● The highlighted row indeed shows our key ● The attacker has to just try only 258 possible keys to decrypt the data
  • 7. Fix is simple - use /dev/urandom unsigned char* generateKey_good(unsigned int keySize) { unsigned char* key = (unsigned char *)malloc(sizeof(unsigned char)*keySize); /* Error handling code is left out for demo purposes */ FILE* random = fopen("/dev/urandom", "r"); int i = 0; for (i = 0; i < keySize; i++) { fread(key, sizeof(unsigned char)*keySize, 1, random); } fclose(random); return key; } 7