SlideShare une entreprise Scribd logo
1  sur  33
1
Much Ado About Randomness.
What Is a Random Number?
Aleksandr Yampolskiy, Ph.D.
Gilt Groupe
Randomness
Random number generation is easy to get wrong.
3
In Theory
• Cryptography is based on random
numbers:
– Secret keys must be random
– Random bits are needed for public-key
encryption, signatures, SSL, etc.
• A common assumption in theory is a
random oracle model, where all parties
have access to a perfect random
source [BR92].
• Under this assumption, many crypto
tools can be proven formally secure.
In Practice
• Kaminsky bug used to poison DNS caches.
• Debian OpenSSL versions <0.9.8g-9
generated weak SSL keys.
• Some Majordomo versions were
susceptible to subscribing victim to
thousands of mailing lists.
• Kerberos 4 secret keys could be guessed in
a few seconds.
• Netscape 1.1 generated SSL keys using
time and process ID as seed; easily
guessable and breakable.
Example #1
- unsigned char magic_cookie[LEN];
- srand(time(NULL));
- for (int i=0; i<LEN; i++) magic_cookie[i] = rand()
& 0xFF;
X Windows “magic cookie” used a weak LCG generator
and was guessable in X11R6.
Example #2
protected void doStart()
{
//…
_random=new java.util.Random();
_random.setSeed(_random.nextLong()^System.currentTimeMillis()
^hashCode()^Runtime.getRuntime().freeMemory());
_sessions = new MultiMap();
//...
}
Jetty 4.2.26 used java.util.Random to generate predictable
session ID which could be brute-forced.
7
Example #3
• global variable seed;
• function RNG_CreateContext()
– (seconds, microseconds) = time of day;
– pid = process ID; ppid = parent process ID;
– a = mklcpr(microseconds);
– b = mklcpr(pid + seconds + (ppid << 12));
– seed = MD5(a, b);
• function mklcpr(x) // simple scrambler
– return ((0xDEECE66D * x + 0x2BBB62DC) >> 1);
• function MD5(x) // secure hash function
In 1996, two UC Berkeley students reverse exploit Netscape 1.1
Lessons Learnt
• Numbers, used to derive session IDs and keys,
weren’t truly random!
• Seeds must be unpredictable
– 128 bit sequences are sufficient
– All possibilities equally likely
– Best seeds are truly random
• PRG (pseudorandom number generator) must be
secure
– No detectable pattern
– Even if attacker guesses some pseudorandom bits, no
correlation to other bits.
9
Two Types of Randomness
• Truly random number generator
– Radioactive decay.
– Disk timing.
– Fair coin flip.
– Randomness inherent in PC disk IO, thread scheduling, etc..
• Pseudo-random number generator (aka
computational)
– Generate a small “truly random” seed.
– Stretch into a larger pseudo-random sequence.
• Fact: In practice, we use pseudo-random number
generators.
11
What is (Pseudo)-Random? (cont.)
PRG
random seed
pseudorandom string random string
look indistinguishable
to any efficient observer
Definition [Blum-Micali-Yao]:
PRG is a polytime function whose output is
indistinguishable from random by any efficient
observer
01010111001… 11010011010…
1001
12
Find programs with weak PRGFind programs with weak PRG
Break-inBreak-in
Attacking Weak PRGs
Guess the initial
seed of a PRG
Guess the initial
seed of a PRG
Guess the state
of a PRG
Guess the state
of a PRG
Finding programs with weak PRG
• If source code is available, grep for weak API
calls.
• If only a binary is available, reverse engineer
the program or grep for weak system calls.
• For client programs, use Stompy or Ent to
analyze output’s randomness quality.
• For web-based programs, use BurpSuite or
WebScarab proxy to analyze session ID
randomness.
• Google Hacking for weak session IDs.
Finding programs with weak PRG
(cont.)
• High-entropy session ID generators use things like:
• java.security.SecureRandom (Java)
• System.Security.Cryptography.RNGCryptoServiceProvider (.NET)
• /dev/urandom, /dev/(s)random (if the latter, look for exhaustion attacks!)
• OpenSSL’s RAND bytes
• hardware security module.
• It’s pretty easy to quickly identify weak, low-entropy session ID
generation in the code. They use
• the time and date
• a random static string in the source code
• the output of C library rand, the output of java.util.Random
• small (32 bits or less) numbers
• a cryptographic hash (like MD5) of anything low in entropy to generate
their session IDs.
Know Weak API
•The weak API generally use insecure constructions such as LCG,
LSFR, Mersenne twister, etc.
•The strong API may use DES, SHA-1 based PRNG, Blum-Blum-
Shub, etc.
Reverse Engineering The Binaries
• Many Java programs mistakenly use
java.util.Random to generate session IDs
instead of java.security.SecureRandom
root# javap -c BadRandom | grep Random
Compiled from "BadRandom.java"
public class BadRandom extends java.lang.Object{
public BadRandom();
0: new #2; //class java/util/Random
4: invokespecial #3; //Method java/util/Random."<init>":()V
24:invokevirtual #9; //Method java/util/Random.nextInt:()I
Reverse Engineering the Binaries
• Similarly, C/C++ programs use rand() or random() instead
of reading from /dev/random.
• Note /dev/random blocks and /dev/urandom doesn’t
but may produce randomness of worse quality.
root# strings bad_random
__gmon_start
libc.so.6
_IO_stdin_used
srand
time
printf
…
root# nm bad_random | grep rand
U rand@@GLIBC_2.0
U srand@@GLIBC_2.0
18
Analyzing the Output Without Binaries
• Compute entropy of the stream
• Count number of characters in each position
• FIPS 140-2 statistical PRNG tests
– Monobit test: Are there as many 1’s as 0’s?
– Runs test: Are the number of runs (sequences of only 0’s
or 1’s) as expected for random numbers?
– Maurer’s test: Can the sequence be compressed?
– Next-bit test: given m bits of the sequence, predict (m+1)st
bit
• Just compress the data using WinRAR
java.util.Random
•The java.util.Random PRG is really a linear congruential
generator (LCG) where x(n+1) = axn + b (mod m) for large
constants a, b and moduli n,m
• synchronized protected int next(int bits) { seed = (seed *
0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); return (int)
(seed >>> (48 - bits)); }
Entropy of java.util.Random
• In practice things aren’t that bad.
• For 20,000 samples, the entropy of java.util.Random and
java.security.SecureRandom streams is almost identical.
• For both, 14.2877123795 bits of entropy.
• They also pass all FIPS 140-2 tests.
• For 200,000 samples, java.security.SecureRandom has
slightly more entropy than java.util.Random, but is it
significant?
• For java.util.Random, we get 17.6095804744 bits of entropy
• For java.security.SecureRandom, we get 17.6096204744 bits of
entropy
Is java.security.SecureRandom that much
worse than java.util.Random?
• Folklore says that it is. But it really depends on
OS:
• OpenSolaris (SunOS 5.11) : 67.9 slower 
• Windows XP, 64.5 times slower 
• Windows 7, 24.5 times slower 
• MAC OS X, Leopard: 25.1 times slower 
Hacking Java bytecode to use SecureRandom
• Java.security.SecureRandom inherits from java.util.Random
and has all its methods
• ASM bytecode manipulation framework: http://asm.ow2.org/
• Replace Random with SecureRandom in the bytecode
public class ChangeMethodCallAdapter extends
MethodAdapter {
@Override
public void visitMethodInsn(int opcode, String
owner, String name, String desc) {
System.out.println("ChangeMethodCallAdapter():
opcode=" + opcode + ",owner=" + owner +
",name=" + name + ",desc=" + desc);
if ("java/util/Random".equals(owner)) {
mv.visitMethodInsn(opcode,
"java/security/SecureRandom", name, desc);
} else {
mv.visitMethodInsn(opcode, owner, name,
desc);
}
}
gilt-ml-ayampolskiy:ClassTransformer
ayampolskiy$ javap -c API | grep Random
8: new #5; //class java/util/Random
12: invokespecial #6; //Method
java/util/Random."<init>":()V
27: invokevirtual #7; //Method
java/util/Random.nextInt:(I)I
gilt-ml-ayampolskiy:new ayampolskiy$ javap -c
API | grep Random
8: new #28; //class java/util/Random
12: invokespecial #31; //Method
java/security/SecureRandom."<init>":()V
27: invokevirtual #35; //Method
java/security/SecureRandom.nextInt:(I)I
Google Hacking
• Know the common session cookie names
(SESSIONID,JSESSIONID,PHPSESSID,PHPSESSIO
NID, etc.)
• Google for the cookie names: inurl:"?
sessionid=”
• Even better, try googling session IDs with non-
random sequences “66”, “128”: inurl:”?
sessionid=128”
• How about “lang:java java.util.Random
session”
Testing Randomness of Client
Programs
• Fourmilab’s entropy tests: http://www.fourmilab.c
• Stompy (session stomper): http://lcamtuf.coredum
“We could not arrest or charge this suspect because technically, no
offence was being committed as there was no legislation in place to say
that the act being committed was criminal. So, we had to let him go,” said
Sergeant Jemesa Lave of the Fiji Police Cyber Crime Unit.
Amazon.com experiment
• Amazon.com uses
a session-id, a 17-digit
random number- is a
persistent cookie that
expires after 7 days. It is set
the first time you reach
Amazon. Its value does not
change after you log in, nor
when you switch users.
Testing Randomness of Web-Based
Programs
• Several nice GUI tools to analyze session IDs
for common problems ( WebScarab,
BurpSuite, SPI Cookie Cruncher,Foundstone
CookieDigger, etc)
• Test alphabet distribution, average bits
changed, FIPS tests, etc.
WebScarab – Predictable Cookies
Entropy is a measure of uncertainty regarding
a discrete random variable. For many
purposes, the Shannon entropy is the only
measure needed. Shannon entropy is defined
byShannon (4.1)
has the unit bits.
Not amazon.com
WebScarab – amazon.com
Burpsuite - amazon.com
BurpSuite – amazon.com
Typical amazon.com
session-id 180-3029497-
6907862
BurpSuite – amazon.com
Conclusion
• Use good seeds and strong PRNGs.
• Know what the strong API for generating secure
random numbers are (SecureRandom,
/dev/random)
• Try out Stompy, Ent, WebScarab, BurpSuite.
• Happy hacking!
35
Questions, Comments?

Contenu connexe

Tendances

Protocol T50: Five months later... So what?
Protocol T50: Five months later... So what?Protocol T50: Five months later... So what?
Protocol T50: Five months later... So what?Nelson Brito
 
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devicessrkedmi
 
1300 david oswald id and ip theft with side-channel attacks
1300 david oswald   id and ip theft with side-channel attacks1300 david oswald   id and ip theft with side-channel attacks
1300 david oswald id and ip theft with side-channel attacksPositive Hack Days
 
[PH-Neutral 0x7db] Exploit Next Generation®
[PH-Neutral 0x7db] Exploit Next Generation®[PH-Neutral 0x7db] Exploit Next Generation®
[PH-Neutral 0x7db] Exploit Next Generation®Nelson Brito
 
The day I ruled the world (RootedCON 2020)
The day I ruled the world (RootedCON 2020)The day I ruled the world (RootedCON 2020)
The day I ruled the world (RootedCON 2020)Javier Junquera
 
remote-method-guesser - BHUSA2021 Arsenal
remote-method-guesser - BHUSA2021 Arsenal remote-method-guesser - BHUSA2021 Arsenal
remote-method-guesser - BHUSA2021 Arsenal Tobias Neitzel
 
Peter Gutmann Presentation - CSO Perspectives Roadshow Auckland 9th Mar 2015
Peter Gutmann Presentation - CSO Perspectives Roadshow Auckland 9th Mar 2015Peter Gutmann Presentation - CSO Perspectives Roadshow Auckland 9th Mar 2015
Peter Gutmann Presentation - CSO Perspectives Roadshow Auckland 9th Mar 2015CSO_Presentations
 
Purpose Driven Hunt (DerbyCon 2017)
Purpose Driven Hunt (DerbyCon 2017)Purpose Driven Hunt (DerbyCon 2017)
Purpose Driven Hunt (DerbyCon 2017)Jared Atkinson
 
The hangover: A "modern" (?) high performance approach to build an offensive ...
The hangover: A "modern" (?) high performance approach to build an offensive ...The hangover: A "modern" (?) high performance approach to build an offensive ...
The hangover: A "modern" (?) high performance approach to build an offensive ...Nelson Brito
 
Malware Analysis 101 - N00b to Ninja in 60 Minutes at CactusCon on April 4, 2014
Malware Analysis 101 - N00b to Ninja in 60 Minutes at CactusCon on April 4, 2014Malware Analysis 101 - N00b to Ninja in 60 Minutes at CactusCon on April 4, 2014
Malware Analysis 101 - N00b to Ninja in 60 Minutes at CactusCon on April 4, 2014grecsl
 
Source Boston 2009 - Anti-Debugging A Developers Viewpoint
Source Boston 2009 - Anti-Debugging A Developers ViewpointSource Boston 2009 - Anti-Debugging A Developers Viewpoint
Source Boston 2009 - Anti-Debugging A Developers ViewpointTyler Shields
 
Ricardo J. Rodríguez & Daniel Uroz - When ROP meets Turing: Automatic Generat...
Ricardo J. Rodríguez & Daniel Uroz - When ROP meets Turing: Automatic Generat...Ricardo J. Rodríguez & Daniel Uroz - When ROP meets Turing: Automatic Generat...
Ricardo J. Rodríguez & Daniel Uroz - When ROP meets Turing: Automatic Generat...RootedCON
 
Introduction to Memory Exploitation (CppEurope 2021)
Introduction to Memory Exploitation (CppEurope 2021)Introduction to Memory Exploitation (CppEurope 2021)
Introduction to Memory Exploitation (CppEurope 2021)Patricia Aas
 
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013midnite_runr
 
Silicon scanners cambridge report
Silicon scanners cambridge reportSilicon scanners cambridge report
Silicon scanners cambridge reportLiberteks
 
Higher Level Malware
Higher Level MalwareHigher Level Malware
Higher Level MalwareCTruncer
 
Chromium Sandbox on Linux (BlackHoodie 2018)
Chromium Sandbox on Linux (BlackHoodie 2018)Chromium Sandbox on Linux (BlackHoodie 2018)
Chromium Sandbox on Linux (BlackHoodie 2018)Patricia Aas
 
Csw2016 economou nissim-getting_physical
Csw2016 economou nissim-getting_physicalCsw2016 economou nissim-getting_physical
Csw2016 economou nissim-getting_physicalCanSecWest
 
Эксплуатируем неэксплуатируемые уязвимости SAP
Эксплуатируем неэксплуатируемые уязвимости SAPЭксплуатируем неэксплуатируемые уязвимости SAP
Эксплуатируем неэксплуатируемые уязвимости SAPPositive Hack Days
 
Do WAFs dream of static analyzers
Do WAFs dream of static analyzersDo WAFs dream of static analyzers
Do WAFs dream of static analyzersVladimir Kochetkov
 

Tendances (20)

Protocol T50: Five months later... So what?
Protocol T50: Five months later... So what?Protocol T50: Five months later... So what?
Protocol T50: Five months later... So what?
 
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
[Blackhat EU'14] Attacking the Linux PRNG on Android and Embedded Devices
 
1300 david oswald id and ip theft with side-channel attacks
1300 david oswald   id and ip theft with side-channel attacks1300 david oswald   id and ip theft with side-channel attacks
1300 david oswald id and ip theft with side-channel attacks
 
[PH-Neutral 0x7db] Exploit Next Generation®
[PH-Neutral 0x7db] Exploit Next Generation®[PH-Neutral 0x7db] Exploit Next Generation®
[PH-Neutral 0x7db] Exploit Next Generation®
 
The day I ruled the world (RootedCON 2020)
The day I ruled the world (RootedCON 2020)The day I ruled the world (RootedCON 2020)
The day I ruled the world (RootedCON 2020)
 
remote-method-guesser - BHUSA2021 Arsenal
remote-method-guesser - BHUSA2021 Arsenal remote-method-guesser - BHUSA2021 Arsenal
remote-method-guesser - BHUSA2021 Arsenal
 
Peter Gutmann Presentation - CSO Perspectives Roadshow Auckland 9th Mar 2015
Peter Gutmann Presentation - CSO Perspectives Roadshow Auckland 9th Mar 2015Peter Gutmann Presentation - CSO Perspectives Roadshow Auckland 9th Mar 2015
Peter Gutmann Presentation - CSO Perspectives Roadshow Auckland 9th Mar 2015
 
Purpose Driven Hunt (DerbyCon 2017)
Purpose Driven Hunt (DerbyCon 2017)Purpose Driven Hunt (DerbyCon 2017)
Purpose Driven Hunt (DerbyCon 2017)
 
The hangover: A "modern" (?) high performance approach to build an offensive ...
The hangover: A "modern" (?) high performance approach to build an offensive ...The hangover: A "modern" (?) high performance approach to build an offensive ...
The hangover: A "modern" (?) high performance approach to build an offensive ...
 
Malware Analysis 101 - N00b to Ninja in 60 Minutes at CactusCon on April 4, 2014
Malware Analysis 101 - N00b to Ninja in 60 Minutes at CactusCon on April 4, 2014Malware Analysis 101 - N00b to Ninja in 60 Minutes at CactusCon on April 4, 2014
Malware Analysis 101 - N00b to Ninja in 60 Minutes at CactusCon on April 4, 2014
 
Source Boston 2009 - Anti-Debugging A Developers Viewpoint
Source Boston 2009 - Anti-Debugging A Developers ViewpointSource Boston 2009 - Anti-Debugging A Developers Viewpoint
Source Boston 2009 - Anti-Debugging A Developers Viewpoint
 
Ricardo J. Rodríguez & Daniel Uroz - When ROP meets Turing: Automatic Generat...
Ricardo J. Rodríguez & Daniel Uroz - When ROP meets Turing: Automatic Generat...Ricardo J. Rodríguez & Daniel Uroz - When ROP meets Turing: Automatic Generat...
Ricardo J. Rodríguez & Daniel Uroz - When ROP meets Turing: Automatic Generat...
 
Introduction to Memory Exploitation (CppEurope 2021)
Introduction to Memory Exploitation (CppEurope 2021)Introduction to Memory Exploitation (CppEurope 2021)
Introduction to Memory Exploitation (CppEurope 2021)
 
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
 
Silicon scanners cambridge report
Silicon scanners cambridge reportSilicon scanners cambridge report
Silicon scanners cambridge report
 
Higher Level Malware
Higher Level MalwareHigher Level Malware
Higher Level Malware
 
Chromium Sandbox on Linux (BlackHoodie 2018)
Chromium Sandbox on Linux (BlackHoodie 2018)Chromium Sandbox on Linux (BlackHoodie 2018)
Chromium Sandbox on Linux (BlackHoodie 2018)
 
Csw2016 economou nissim-getting_physical
Csw2016 economou nissim-getting_physicalCsw2016 economou nissim-getting_physical
Csw2016 economou nissim-getting_physical
 
Эксплуатируем неэксплуатируемые уязвимости SAP
Эксплуатируем неэксплуатируемые уязвимости SAPЭксплуатируем неэксплуатируемые уязвимости SAP
Эксплуатируем неэксплуатируемые уязвимости SAP
 
Do WAFs dream of static analyzers
Do WAFs dream of static analyzersDo WAFs dream of static analyzers
Do WAFs dream of static analyzers
 

Similaire à OWASP Much ado about randomness

Crypto failures every developer should avoid
Crypto failures every developer should avoidCrypto failures every developer should avoid
Crypto failures every developer should avoidOwaspCzech
 
Crypto failures every developer should avoid
Crypto failures every developer should avoidCrypto failures every developer should avoid
Crypto failures every developer should avoidFilip Šebesta
 
How to Use Cryptography Properly: Common Mistakes People Make When Using Cry...
How to Use Cryptography Properly:  Common Mistakes People Make When Using Cry...How to Use Cryptography Properly:  Common Mistakes People Make When Using Cry...
How to Use Cryptography Properly: Common Mistakes People Make When Using Cry...All Things Open
 
Intel Random Number Generator
Intel Random Number GeneratorIntel Random Number Generator
Intel Random Number GeneratorXequeMateShannon
 
Using Cryptography Properly in Applications
Using Cryptography Properly in ApplicationsUsing Cryptography Properly in Applications
Using Cryptography Properly in ApplicationsGreat Wide Open
 
Fantastic Red Team Attacks and How to Find Them
Fantastic Red Team Attacks and How to Find ThemFantastic Red Team Attacks and How to Find Them
Fantastic Red Team Attacks and How to Find ThemRoss Wolf
 
Jvm profiling under the hood
Jvm profiling under the hoodJvm profiling under the hood
Jvm profiling under the hoodRichardWarburton
 
CNIT 126: 13: Data Encoding
CNIT 126: 13: Data EncodingCNIT 126: 13: Data Encoding
CNIT 126: 13: Data EncodingSam Bowne
 
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slapDEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slapFelipe Prado
 
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attacDefcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attacPriyanka Aash
 
Sullivan randomness-infiltrate 2014
Sullivan randomness-infiltrate 2014Sullivan randomness-infiltrate 2014
Sullivan randomness-infiltrate 2014Cloudflare
 
PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...
PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...
PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...CODE BLUE
 
Practical Malware Analysis Ch13
Practical Malware Analysis Ch13Practical Malware Analysis Ch13
Practical Malware Analysis Ch13Sam Bowne
 
Testing curl for security
Testing curl for securityTesting curl for security
Testing curl for securityDaniel Stenberg
 
Search for Vulnerabilities Using Static Code Analysis
Search for Vulnerabilities Using Static Code AnalysisSearch for Vulnerabilities Using Static Code Analysis
Search for Vulnerabilities Using Static Code AnalysisAndrey Karpov
 
How to Use Cryptography Properly: The Common Mistakes People Make When Using ...
How to Use Cryptography Properly: The Common Mistakes People Make When Using ...How to Use Cryptography Properly: The Common Mistakes People Make When Using ...
How to Use Cryptography Properly: The Common Mistakes People Make When Using ...POSSCON
 
CNIT 141: 3. Cryptographic Security
CNIT 141: 3. Cryptographic SecurityCNIT 141: 3. Cryptographic Security
CNIT 141: 3. Cryptographic SecuritySam Bowne
 
EMBA Firmware analysis - TROOPERS22
EMBA Firmware analysis - TROOPERS22EMBA Firmware analysis - TROOPERS22
EMBA Firmware analysis - TROOPERS22MichaelM85042
 

Similaire à OWASP Much ado about randomness (20)

Crypto failures every developer should avoid
Crypto failures every developer should avoidCrypto failures every developer should avoid
Crypto failures every developer should avoid
 
Crypto failures every developer should avoid
Crypto failures every developer should avoidCrypto failures every developer should avoid
Crypto failures every developer should avoid
 
How to Use Cryptography Properly: Common Mistakes People Make When Using Cry...
How to Use Cryptography Properly:  Common Mistakes People Make When Using Cry...How to Use Cryptography Properly:  Common Mistakes People Make When Using Cry...
How to Use Cryptography Properly: Common Mistakes People Make When Using Cry...
 
Intel Random Number Generator
Intel Random Number GeneratorIntel Random Number Generator
Intel Random Number Generator
 
Using Cryptography Properly in Applications
Using Cryptography Properly in ApplicationsUsing Cryptography Properly in Applications
Using Cryptography Properly in Applications
 
Fantastic Red Team Attacks and How to Find Them
Fantastic Red Team Attacks and How to Find ThemFantastic Red Team Attacks and How to Find Them
Fantastic Red Team Attacks and How to Find Them
 
Jvm profiling under the hood
Jvm profiling under the hoodJvm profiling under the hood
Jvm profiling under the hood
 
CNIT 126: 13: Data Encoding
CNIT 126: 13: Data EncodingCNIT 126: 13: Data Encoding
CNIT 126: 13: Data Encoding
 
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slapDEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
DEF CON 27 - CHRISTOPHER ROBERTS - firmware slap
 
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attacDefcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
 
DIY Java Profiling
DIY Java ProfilingDIY Java Profiling
DIY Java Profiling
 
Sullivan randomness-infiltrate 2014
Sullivan randomness-infiltrate 2014Sullivan randomness-infiltrate 2014
Sullivan randomness-infiltrate 2014
 
PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...
PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...
PANDEMONIUM: Automated Identification of Cryptographic Algorithms using Dynam...
 
Practical Malware Analysis Ch13
Practical Malware Analysis Ch13Practical Malware Analysis Ch13
Practical Malware Analysis Ch13
 
Testing curl for security
Testing curl for securityTesting curl for security
Testing curl for security
 
Search for Vulnerabilities Using Static Code Analysis
Search for Vulnerabilities Using Static Code AnalysisSearch for Vulnerabilities Using Static Code Analysis
Search for Vulnerabilities Using Static Code Analysis
 
How to Use Cryptography Properly: The Common Mistakes People Make When Using ...
How to Use Cryptography Properly: The Common Mistakes People Make When Using ...How to Use Cryptography Properly: The Common Mistakes People Make When Using ...
How to Use Cryptography Properly: The Common Mistakes People Make When Using ...
 
CNIT 141: 3. Cryptographic Security
CNIT 141: 3. Cryptographic SecurityCNIT 141: 3. Cryptographic Security
CNIT 141: 3. Cryptographic Security
 
EMBA Firmware analysis - TROOPERS22
EMBA Firmware analysis - TROOPERS22EMBA Firmware analysis - TROOPERS22
EMBA Firmware analysis - TROOPERS22
 
Us 17-krug-hacking-severless-runtimes
Us 17-krug-hacking-severless-runtimesUs 17-krug-hacking-severless-runtimes
Us 17-krug-hacking-severless-runtimes
 

Plus de Aleksandr Yampolskiy

New York REDIS Meetup Welcome Session
New York REDIS Meetup Welcome SessionNew York REDIS Meetup Welcome Session
New York REDIS Meetup Welcome SessionAleksandr Yampolskiy
 
"Managing software development" by Peter Bell
"Managing software development" by Peter Bell"Managing software development" by Peter Bell
"Managing software development" by Peter BellAleksandr Yampolskiy
 
Recruiting Great Engineers in Six Easy Steps
Recruiting Great Engineers in Six Easy StepsRecruiting Great Engineers in Six Easy Steps
Recruiting Great Engineers in Six Easy StepsAleksandr Yampolskiy
 
You Too Can Be a Radio Host Or How We Scaled a .NET Startup And Had Fun Doing It
You Too Can Be a Radio Host Or How We Scaled a .NET Startup And Had Fun Doing ItYou Too Can Be a Radio Host Or How We Scaled a .NET Startup And Had Fun Doing It
You Too Can Be a Radio Host Or How We Scaled a .NET Startup And Had Fun Doing ItAleksandr Yampolskiy
 
Malware Goes to the Movies - Briefing
Malware Goes to the Movies - BriefingMalware Goes to the Movies - Briefing
Malware Goes to the Movies - BriefingAleksandr Yampolskiy
 
Eight simple rules to writing secure PHP programs
Eight simple rules to writing secure PHP programsEight simple rules to writing secure PHP programs
Eight simple rules to writing secure PHP programsAleksandr Yampolskiy
 
Social Engineering and What to do About it
Social Engineering and What to do About itSocial Engineering and What to do About it
Social Engineering and What to do About itAleksandr Yampolskiy
 
Inoculation strategies for victims of viruses
Inoculation strategies for victims of virusesInoculation strategies for victims of viruses
Inoculation strategies for victims of virusesAleksandr Yampolskiy
 
Threshold and Proactive Pseudo-Random Permutations
Threshold and Proactive Pseudo-Random PermutationsThreshold and Proactive Pseudo-Random Permutations
Threshold and Proactive Pseudo-Random PermutationsAleksandr Yampolskiy
 
Secure information aggregation in sensor networks
Secure information aggregation in sensor networksSecure information aggregation in sensor networks
Secure information aggregation in sensor networksAleksandr Yampolskiy
 
A verifiable random function with short proofs and keys
A verifiable random function with short proofs and keysA verifiable random function with short proofs and keys
A verifiable random function with short proofs and keysAleksandr Yampolskiy
 
Towards a theory of data entangelement
Towards a theory of data entangelementTowards a theory of data entangelement
Towards a theory of data entangelementAleksandr Yampolskiy
 
Price of anarchy is independent of network topology
Price of anarchy is independent of network topologyPrice of anarchy is independent of network topology
Price of anarchy is independent of network topologyAleksandr Yampolskiy
 
Spreading Rumors Quietly and the Subgroup Escape Problem
Spreading Rumors Quietly and the Subgroup Escape ProblemSpreading Rumors Quietly and the Subgroup Escape Problem
Spreading Rumors Quietly and the Subgroup Escape ProblemAleksandr Yampolskiy
 

Plus de Aleksandr Yampolskiy (20)

New York REDIS Meetup Welcome Session
New York REDIS Meetup Welcome SessionNew York REDIS Meetup Welcome Session
New York REDIS Meetup Welcome Session
 
"Managing software development" by Peter Bell
"Managing software development" by Peter Bell"Managing software development" by Peter Bell
"Managing software development" by Peter Bell
 
Recruiting Great Engineers in Six Easy Steps
Recruiting Great Engineers in Six Easy StepsRecruiting Great Engineers in Six Easy Steps
Recruiting Great Engineers in Six Easy Steps
 
You Too Can Be a Radio Host Or How We Scaled a .NET Startup And Had Fun Doing It
You Too Can Be a Radio Host Or How We Scaled a .NET Startup And Had Fun Doing ItYou Too Can Be a Radio Host Or How We Scaled a .NET Startup And Had Fun Doing It
You Too Can Be a Radio Host Or How We Scaled a .NET Startup And Had Fun Doing It
 
Malware Goes to the Movies - Briefing
Malware Goes to the Movies - BriefingMalware Goes to the Movies - Briefing
Malware Goes to the Movies - Briefing
 
Privacy and E-Commerce
Privacy and E-CommercePrivacy and E-Commerce
Privacy and E-Commerce
 
Eight simple rules to writing secure PHP programs
Eight simple rules to writing secure PHP programsEight simple rules to writing secure PHP programs
Eight simple rules to writing secure PHP programs
 
Social media security challenges
Social media security challengesSocial media security challenges
Social media security challenges
 
Social Engineering and What to do About it
Social Engineering and What to do About itSocial Engineering and What to do About it
Social Engineering and What to do About it
 
Malware goes to the movies
Malware goes to the moviesMalware goes to the movies
Malware goes to the movies
 
Inoculation strategies for victims of viruses
Inoculation strategies for victims of virusesInoculation strategies for victims of viruses
Inoculation strategies for victims of viruses
 
Number theory lecture (part 1)
Number theory lecture (part 1)Number theory lecture (part 1)
Number theory lecture (part 1)
 
Number theory lecture (part 2)
Number theory lecture (part 2)Number theory lecture (part 2)
Number theory lecture (part 2)
 
Threshold and Proactive Pseudo-Random Permutations
Threshold and Proactive Pseudo-Random PermutationsThreshold and Proactive Pseudo-Random Permutations
Threshold and Proactive Pseudo-Random Permutations
 
Secure information aggregation in sensor networks
Secure information aggregation in sensor networksSecure information aggregation in sensor networks
Secure information aggregation in sensor networks
 
A verifiable random function with short proofs and keys
A verifiable random function with short proofs and keysA verifiable random function with short proofs and keys
A verifiable random function with short proofs and keys
 
Towards a theory of data entangelement
Towards a theory of data entangelementTowards a theory of data entangelement
Towards a theory of data entangelement
 
Price of anarchy is independent of network topology
Price of anarchy is independent of network topologyPrice of anarchy is independent of network topology
Price of anarchy is independent of network topology
 
Business Case Studies
Business Case Studies Business Case Studies
Business Case Studies
 
Spreading Rumors Quietly and the Subgroup Escape Problem
Spreading Rumors Quietly and the Subgroup Escape ProblemSpreading Rumors Quietly and the Subgroup Escape Problem
Spreading Rumors Quietly and the Subgroup Escape Problem
 

OWASP Much ado about randomness

  • 1. 1 Much Ado About Randomness. What Is a Random Number? Aleksandr Yampolskiy, Ph.D. Gilt Groupe
  • 2. Randomness Random number generation is easy to get wrong.
  • 3. 3 In Theory • Cryptography is based on random numbers: – Secret keys must be random – Random bits are needed for public-key encryption, signatures, SSL, etc. • A common assumption in theory is a random oracle model, where all parties have access to a perfect random source [BR92]. • Under this assumption, many crypto tools can be proven formally secure.
  • 4. In Practice • Kaminsky bug used to poison DNS caches. • Debian OpenSSL versions <0.9.8g-9 generated weak SSL keys. • Some Majordomo versions were susceptible to subscribing victim to thousands of mailing lists. • Kerberos 4 secret keys could be guessed in a few seconds. • Netscape 1.1 generated SSL keys using time and process ID as seed; easily guessable and breakable.
  • 5. Example #1 - unsigned char magic_cookie[LEN]; - srand(time(NULL)); - for (int i=0; i<LEN; i++) magic_cookie[i] = rand() & 0xFF; X Windows “magic cookie” used a weak LCG generator and was guessable in X11R6.
  • 6. Example #2 protected void doStart() { //… _random=new java.util.Random(); _random.setSeed(_random.nextLong()^System.currentTimeMillis() ^hashCode()^Runtime.getRuntime().freeMemory()); _sessions = new MultiMap(); //... } Jetty 4.2.26 used java.util.Random to generate predictable session ID which could be brute-forced.
  • 7. 7 Example #3 • global variable seed; • function RNG_CreateContext() – (seconds, microseconds) = time of day; – pid = process ID; ppid = parent process ID; – a = mklcpr(microseconds); – b = mklcpr(pid + seconds + (ppid << 12)); – seed = MD5(a, b); • function mklcpr(x) // simple scrambler – return ((0xDEECE66D * x + 0x2BBB62DC) >> 1); • function MD5(x) // secure hash function In 1996, two UC Berkeley students reverse exploit Netscape 1.1
  • 8. Lessons Learnt • Numbers, used to derive session IDs and keys, weren’t truly random! • Seeds must be unpredictable – 128 bit sequences are sufficient – All possibilities equally likely – Best seeds are truly random • PRG (pseudorandom number generator) must be secure – No detectable pattern – Even if attacker guesses some pseudorandom bits, no correlation to other bits.
  • 9. 9 Two Types of Randomness • Truly random number generator – Radioactive decay. – Disk timing. – Fair coin flip. – Randomness inherent in PC disk IO, thread scheduling, etc.. • Pseudo-random number generator (aka computational) – Generate a small “truly random” seed. – Stretch into a larger pseudo-random sequence. • Fact: In practice, we use pseudo-random number generators.
  • 10. 11 What is (Pseudo)-Random? (cont.) PRG random seed pseudorandom string random string look indistinguishable to any efficient observer Definition [Blum-Micali-Yao]: PRG is a polytime function whose output is indistinguishable from random by any efficient observer 01010111001… 11010011010… 1001
  • 11. 12 Find programs with weak PRGFind programs with weak PRG Break-inBreak-in Attacking Weak PRGs Guess the initial seed of a PRG Guess the initial seed of a PRG Guess the state of a PRG Guess the state of a PRG
  • 12. Finding programs with weak PRG • If source code is available, grep for weak API calls. • If only a binary is available, reverse engineer the program or grep for weak system calls. • For client programs, use Stompy or Ent to analyze output’s randomness quality. • For web-based programs, use BurpSuite or WebScarab proxy to analyze session ID randomness. • Google Hacking for weak session IDs.
  • 13. Finding programs with weak PRG (cont.) • High-entropy session ID generators use things like: • java.security.SecureRandom (Java) • System.Security.Cryptography.RNGCryptoServiceProvider (.NET) • /dev/urandom, /dev/(s)random (if the latter, look for exhaustion attacks!) • OpenSSL’s RAND bytes • hardware security module. • It’s pretty easy to quickly identify weak, low-entropy session ID generation in the code. They use • the time and date • a random static string in the source code • the output of C library rand, the output of java.util.Random • small (32 bits or less) numbers • a cryptographic hash (like MD5) of anything low in entropy to generate their session IDs.
  • 14. Know Weak API •The weak API generally use insecure constructions such as LCG, LSFR, Mersenne twister, etc. •The strong API may use DES, SHA-1 based PRNG, Blum-Blum- Shub, etc.
  • 15. Reverse Engineering The Binaries • Many Java programs mistakenly use java.util.Random to generate session IDs instead of java.security.SecureRandom root# javap -c BadRandom | grep Random Compiled from "BadRandom.java" public class BadRandom extends java.lang.Object{ public BadRandom(); 0: new #2; //class java/util/Random 4: invokespecial #3; //Method java/util/Random."<init>":()V 24:invokevirtual #9; //Method java/util/Random.nextInt:()I
  • 16. Reverse Engineering the Binaries • Similarly, C/C++ programs use rand() or random() instead of reading from /dev/random. • Note /dev/random blocks and /dev/urandom doesn’t but may produce randomness of worse quality. root# strings bad_random __gmon_start libc.so.6 _IO_stdin_used srand time printf … root# nm bad_random | grep rand U rand@@GLIBC_2.0 U srand@@GLIBC_2.0
  • 17. 18 Analyzing the Output Without Binaries • Compute entropy of the stream • Count number of characters in each position • FIPS 140-2 statistical PRNG tests – Monobit test: Are there as many 1’s as 0’s? – Runs test: Are the number of runs (sequences of only 0’s or 1’s) as expected for random numbers? – Maurer’s test: Can the sequence be compressed? – Next-bit test: given m bits of the sequence, predict (m+1)st bit • Just compress the data using WinRAR
  • 18. java.util.Random •The java.util.Random PRG is really a linear congruential generator (LCG) where x(n+1) = axn + b (mod m) for large constants a, b and moduli n,m • synchronized protected int next(int bits) { seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); return (int) (seed >>> (48 - bits)); }
  • 19. Entropy of java.util.Random • In practice things aren’t that bad. • For 20,000 samples, the entropy of java.util.Random and java.security.SecureRandom streams is almost identical. • For both, 14.2877123795 bits of entropy. • They also pass all FIPS 140-2 tests. • For 200,000 samples, java.security.SecureRandom has slightly more entropy than java.util.Random, but is it significant? • For java.util.Random, we get 17.6095804744 bits of entropy • For java.security.SecureRandom, we get 17.6096204744 bits of entropy
  • 20. Is java.security.SecureRandom that much worse than java.util.Random? • Folklore says that it is. But it really depends on OS: • OpenSolaris (SunOS 5.11) : 67.9 slower  • Windows XP, 64.5 times slower  • Windows 7, 24.5 times slower  • MAC OS X, Leopard: 25.1 times slower 
  • 21. Hacking Java bytecode to use SecureRandom • Java.security.SecureRandom inherits from java.util.Random and has all its methods • ASM bytecode manipulation framework: http://asm.ow2.org/ • Replace Random with SecureRandom in the bytecode public class ChangeMethodCallAdapter extends MethodAdapter { @Override public void visitMethodInsn(int opcode, String owner, String name, String desc) { System.out.println("ChangeMethodCallAdapter(): opcode=" + opcode + ",owner=" + owner + ",name=" + name + ",desc=" + desc); if ("java/util/Random".equals(owner)) { mv.visitMethodInsn(opcode, "java/security/SecureRandom", name, desc); } else { mv.visitMethodInsn(opcode, owner, name, desc); } } gilt-ml-ayampolskiy:ClassTransformer ayampolskiy$ javap -c API | grep Random 8: new #5; //class java/util/Random 12: invokespecial #6; //Method java/util/Random."<init>":()V 27: invokevirtual #7; //Method java/util/Random.nextInt:(I)I gilt-ml-ayampolskiy:new ayampolskiy$ javap -c API | grep Random 8: new #28; //class java/util/Random 12: invokespecial #31; //Method java/security/SecureRandom."<init>":()V 27: invokevirtual #35; //Method java/security/SecureRandom.nextInt:(I)I
  • 22. Google Hacking • Know the common session cookie names (SESSIONID,JSESSIONID,PHPSESSID,PHPSESSIO NID, etc.) • Google for the cookie names: inurl:"? sessionid=” • Even better, try googling session IDs with non- random sequences “66”, “128”: inurl:”? sessionid=128” • How about “lang:java java.util.Random session”
  • 23. Testing Randomness of Client Programs • Fourmilab’s entropy tests: http://www.fourmilab.c • Stompy (session stomper): http://lcamtuf.coredum
  • 24. “We could not arrest or charge this suspect because technically, no offence was being committed as there was no legislation in place to say that the act being committed was criminal. So, we had to let him go,” said Sergeant Jemesa Lave of the Fiji Police Cyber Crime Unit.
  • 25. Amazon.com experiment • Amazon.com uses a session-id, a 17-digit random number- is a persistent cookie that expires after 7 days. It is set the first time you reach Amazon. Its value does not change after you log in, nor when you switch users.
  • 26. Testing Randomness of Web-Based Programs • Several nice GUI tools to analyze session IDs for common problems ( WebScarab, BurpSuite, SPI Cookie Cruncher,Foundstone CookieDigger, etc) • Test alphabet distribution, average bits changed, FIPS tests, etc.
  • 27. WebScarab – Predictable Cookies Entropy is a measure of uncertainty regarding a discrete random variable. For many purposes, the Shannon entropy is the only measure needed. Shannon entropy is defined byShannon (4.1) has the unit bits. Not amazon.com
  • 30. BurpSuite – amazon.com Typical amazon.com session-id 180-3029497- 6907862
  • 32. Conclusion • Use good seeds and strong PRNGs. • Know what the strong API for generating secure random numbers are (SecureRandom, /dev/random) • Try out Stompy, Ent, WebScarab, BurpSuite. • Happy hacking!

Notes de l'éditeur

  1. http://www.javamex.com/tutorials/random_numbers/lcg_planes.shtml
  2. http://www.flickr.com/whitehouse?phpsessid=6ec6733ca8594df4268ef8708a5438c2.