OpenSSL - Weak KDF



EKU-ID: 6216 CVE: OSVDB-ID:
Author: anonymous Published: 2017-01-12 Verified: Verified
Download:

Rating

☆☆☆☆☆
Home


I'm glad more people are in here now. It gets much, much worse than this.
 
The post below literally says "if you have the password, you can generate the key and open the file.
 
The real exploit is that you don't need the password or the key to open a file.
 
That is how serious this is. It is really bad. It is not a joke.
 
Some of you are joining now and seeing how bad the weak KDF is that they're using. It gets much worse.
 
The salt is not a random number. It is a side channel that leaks information about the passphrase. This reduces the entropy of the search space, so that it now becomes possible to brute force.
 
The REAL exploit is what's going on with memory management. The ENGINE plugin literally modifies the code during the compile, so what you think it's doing isn't what's being done. It's one of the most insane things I've ever seen in my life. You can see it if you compile and then decompile the code.
 
It's basically the same thing they did with Heartbleed, but on steroids. Same exact design pattern. Memory management is so thoroughly skewered that you might as well think of the entire thing as a secret data structure.
 
If you really want to see what is happening here, just go look in the random number generator. It is not random. They do all sorts of stuff in the entropy pool to make it look like the time, date, process id, etc are all involved. Then they clear the buffer in a call to ENGINE (!!) which throws all of that out, pops stuff off of the secret data structure.
 
There are race conditions in the entropy pool. There are actually even comments telling you this.
 
There is actually a comment in the entropy pool saying something like, "we are not ambitious to provide *information-theoretic* randomness." Or, translated into plain English, "I am a contractor who is trying to warn people the random number generator is not random."
 
There are times when they even have comments in the code telling people not to worry about valgrind complaining that uninitialized memory is being used to seed the bufer. Again, translated into plain English, "I am a contractor, and you should definitely worry about this."
 
There is, I swear to god, a comment right in the decrypt function that basically says "PADDING ORACLE HERE." It is insane. They didn't even take it out.
 
The whole thing is ruined. If you are just tuning in now, read the writeup posted before. Seriously, you are in for a hell of a ride.
-------------------------------------------------------------------------------------------
# One error and one missing step in 5) have been corrected.
# Procedure now 100% verified
 
You can actually get the full key.
 
This is how it works.
 
first half of the key = MD5(password+salt)
second half of the key = first half + MD5(first half+password+salt)
 
Let's prove this step by step.
 
openssl enc -aes-256-cbc -p -in 000svgLA.7z -out test.aes -salt -k p@ssword
salt=596C09F4AFCC2B9D
key=DD73502243215E39A0CDDE52CF5AB975EAA8F8DA936B35650308113E42DF8862
iv =322ACE8546EBA994AF17A1BC5DC999B1
 
We wan't to get the key value. Let's do it.
 
1.) Save the salt:
perl -e 'print pack "H*", "596C09F4AFCC2B9D"' > salt
 
2.) Save the password:
echo -n p@ssword > keyhalf
 
3.) Add them (password+salt):
cat salt >> keyhalf
 
4.) Get the first half of the key:
md5sum keyhalf
dd73502243215e39a0cdde52cf5ab975  keyhalf
 
Compare to the full key:
DD73502243215E39A0CDDE52CF5AB975 EAA8F8DA936B35650308113E42DF8862
 
Checks out. So now we need the rest.
 
We can easily get it with the information we have so far since we now know that it's = first half + MD5(first half+password+salt)
 
5.) Save the part of the key we already have:
perl -e 'print pack "H*", "DD73502243215E39A0CDDE52CF5AB975"' > key
echo -n p@ssword > password
cat key > keysecond
 
6.) Add the password:
cat password >> keysecond
 
7.) Add the salt:
cat salt >> keysecond
 
8.) Get the second half of the key:
md5sum keysecond
eaa8f8da936b35650308113e42df8862  keysecond
 
Compare to second half:
EAA8F8DA936B35650308113E42DF8862
 
In step 4.) we got dd73502243215e39a0cdde52cf5ab975
In step 8.) we got eaa8f8da936b35650308113e42df8862
 
dd73502243215e39a0cdde52cf5ab975 eaa8f8da936b35650308113e42df8862
DD73502243215E39A0CDDE52CF5AB975 EAA8F8DA936B35650308113E42DF8862
 
We have the full key. We only used MD5 and didn't write a single line of code.
 
Note that this the current version of OpenSSL.
There is no patch that can fix the files that have already been encrypted.
 
RIP OpenSSL.
 
------------------------------------------------------
this is crazy...
 
------------------------------------------------------
>the first 16 bytes of the key will be equal to MD5(password||salt)
 
Let's test this.
 
openssl enc -aes-256-cbc -pass pass:p@ssword -p -in 000svgLA.7z -out testfile.aes
salt=C532A7E7BFFBAD69
key=9104D17FB6C06D9B0F8368D52678FD4B88DF2E244029BF068EED22DD816A5DBC
iv =B08DB48DCF6CAC52C6CF040FB06A0809
 
python pwdsalt2key.py 'p@ssword' 'C532A7E7BFFBAD69'
9104D17FB6C06D9B0F8368D52678FD4B
9104D17FB6C06D9B0F8368D52678FD4B88DF2E244029BF068EED22DD816A5DBC
 
pwdsalt2key.py
http://gateway.glop.me/ipfs/QmbYCbZYsViLSAy7ht6iNpecyCeoTWBonsLPtHDa6bX6Ku/pwdsalt2key.py
https://zerobin.net/?7ff571d39efdcd1c#uBSr6l6vCFq1EA95h3SQSmK4KVN9rAlMx/58uGRgN0o=
 
 
import sys
from passlib.utils.pbkdf2 import pbkdf1
from binascii import unhexlify, hexlify
 
pwd = str(sys.argv[1])
salt = str(sys.argv[2])
 
out = pbkdf1(pwd, unhexlify(b''+salt), 1, 16, 'md5')
print hexlify(out).decode('ascii').upper()
 
 
 
 
------------------------------------------------------
*********************************************************************
ORACLE:
https://blog.cloudflare.com/yet-another-padding-oracle-in-openssl-cbc-ciphersuites/
https://github.com/FiloSottile/CVE-2016-2107
 
Explanation: https://blog.cloudflare.com/yet-another-padding-oracle-in-openssl-cbc-ciphersuites/
Code: https://github.com/FiloSottile/CVE-2016-2107
Internal discussion: https://www.openssl.org/news/secadv/20160503.txt
Exploit: https://www.exploit-db.com/exploits/39768/
Damage control: http://web-in-security.blogspot.ca/2016/05/curious-padding-oracle-in-openssl-cve.html
'Fix': https://git.openssl.org/?p=openssl.git;a=commitdiff;h=68595c0c2886e7942a14f98c17a55a88afb6c292;hp=643e8e972e54fa358935e5f8b7f5a8be9616d56b
Compiled information: https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2016-2107
 
*********************************************************************
BLOCKS AND SIZE:
(ls -l, head -c 256)
 
There is also this weird comment in enc.c, line 431:
/*
* zero the complete buffer or the string passed from the command
* line bug picked up by Larry J. Hughes Jr. <hughes@indiana.edu>
*/
if (str == strbuf)
OPENSSL_cleanse(str, SIZE);
else
OPENSSL_cleanse(str, str_len);
 
The memcpy() routine is explicitly not safe if the source and destination buffers overlap. This can happen when the EVP_enc_null() cipher is used to decrypt longer text than BUF_OFFSET bytes because of this code in bio_enc.c:
EVP_CipherUpdate(&(ctx->cipher),
(unsigned char *)ctx->buf, &ctx->buf_len,
(unsigned char *)&(ctx->buf[BUF_OFFSET]), i);
It's the overwrite of the ctx->buf memory that is the problem.
Saw this on a SUSE 12 SP2 machine. It is not necessarily repeatable on other machines. Valgrind also complained.
https://github.com/openssl/openssl/issues/1935
 
You should use EVP_ENCODE_LENGTH to determine the required output buffer size:
https://github.com/openssl/openssl/blob/master/include/openssl/evp.h#L458
*********************************************************************
MEMORY:
(https://www.hex-rays.com/products/decompiler/)
 
>Mem allocator isn't actually an allocator, it's LIFO system that allows the devs to be sloppy and access "already free()d memory", I notice that they alloc in EVP_BytesToKey, whereas LibreSSL does not.
 
>double free
https://github.com/guidovranken/openssl-x509-vulnerabilities
http://seclists.org/fulldisclosure/2016/Oct/62
There are many other instances of double frees in the code.
 
*********************************************************************
3SAT:
(https://github.com/msoos/cryptominisat)
(http://baldur.iti.kit.edu/sat-competition-2016/solvers/main/)
 
>You need to do linear correleation attack on the bit vectors and other annoying things. You can reduce entropy down to 64+32 bits and then its within brute force range with ASIC.
 
>For 3-sat Solver you just download it off of github. There are dozens.
 
>The problem is getting the md5 and aes circuit into 3-SAT, CNF form. You may need to take the C code for the decryption functions and md5 hashing functions, then compile it to verilog. See if there is a way.
 
>You have to represent the hash function as a circuit in CNF.
 
>128 Bit is breakable in 40 seconds with 3-SAT attack. Its not breakable with brute force.
*********************************************************************
MD5 PBKDF1:
(http://blog.thireus.com/cracking-story-how-i-cracked-over-122-million-sha1-and-md5-hashed-passwords/)
(https://github.com/qsantos/rainbow)
 
>"The first 16 bytes are actually derived using PBKDF1 as defined in PKCS#5 v1.5.
The next 16 bytes would be MD5(PBKDF1(PASSWORD, SALT) || PASSWORD || SALT)
and the IV would be MD5(MD5(PBKDF1(PASSWORD, SALT) || PASSWORD || SALT) || PASSWORD || SALT)"
 
>PBKDF1(PASSWORD, SALT) = MD5(PASSWORD || SALT), where || is concatenation
 
^^There is an idea that this decimates the entropy to 2^128, assuming each of the first 128 passphrases hashes to a unique MD5. However, it's tricky, because you have to find the right preimage to compute the full key, as well as the IV. However, if you have some way to identify that you have the first 128 bits right, then finding collisions for it is easy, since a collision attack on MD5 exists. Either way, this is pretty weak and it's crazy that it's there.
 
> EVP_BytesToKey uses an outrageously weak KDF (basically MD5 salted and iterated a couple times), and drops the entropy down to 128 bits at least.
 
>The process by which the password and salt are turned into the key and IV is not documented, but a look at the source code shows that it calls the OpenSSL-specific EVP_BytesToKey() function, which uses a custom key derivation function with some repeated hashing. This is a non-standard and not-well vetted construct (!) which relies on the MD5 hash function of dubious reputation (!!); that function can be changed on the command-line with the undocumented -md flag (!!!); the "iteration count" is set by the enc command to 1 and cannot be changed (!!!!). This means that the first 16 bytes of the key will be equal to MD5(password||salt), and that's it.
 
>I will tell you a secret.
>To decrypt the insurance file. Make sure to archive this before it disappears.
>The EVP_BytesToKey key derivative algorithm uses md5. It is trivial to break the key for the first block.
*********************************************************************
CONTENT:
(binwalk, diff, comm, head/tail -c)
>See if the filename, file contents, or choice of key or IV changes the salt. (I believe I saw the key choice did not change the salt)
 
Download: http://gateway.glop.me/ipfs/QmZudb4s2nF5JgdeFA1nKzs6MtvaPr58rR4LzddZqYir3s/evp_test.py
It recreates EVP_BytesToKey completely outside of OpenSSL.
Example:
python evp_test.py md5 'p@ssword' '64 97 22 63 0B 61 9D 74'
 
salt=649722630B619D74
key=F6EEA040C6BDD0EF1429C4CF4FE09FD3EA1C9BDE96B6B41DBFF838E408628BBE
iv=576F54891CADC222492E038F8ECE557A
*********************************************************************
AES:
(http://www.lifl.fr/~bouillag/implementation.html)
 
http://gateway.glop.me/ipfs/QmUUm47AkBuv2atQVLBPjTrVhaXQRDj9bNTqErkaP2TNwB/papers.zip
http://gateway.glop.me/ipfs/QmZHXz3g6LBNGYknFMLZbtdTawTNDt8dQByauk5fmFLb2k/aestools.zip
https://mega.nz/#!cI9jUAoQ!VGJnhIlTU5YBhIXTNLBfhasER6qxfsD_ho3PO_U5oSs
https://mega.nz/#!0BN3lRYT!G172BViFAInD2gTOsyZOZ56zHC4nNA1DHHwP7RliT6U
 
If people are saying the first block of the insurance files contains "runs of zeros," then here's what that looks like.
Suppose that AES(block, key) encrypts a block with a key. Then the first block looks like the following equation:
AES(IV,key) ^ ciphertext = plaintext
Where ^ is XOR.
Now: if the plaintext were ALL zeros, that would mean
AES(IV,key) ^ ciphertext = 0000000…
which means
AES(IV,key) = ciphertext
 
But, the IV and key are generated in completely deterministic fashion from the key and the salt. And we have the salt. So that first function really becomes
F(passphrase) = ciphertext
for a certain F.
*********************************************************************
 
 
 
------------------------------------------------------
openssl enc -aes-256-cbc -pass pass:p@ssword -p -d -in testfile.aes -out test1
 
salt=2F140A2A667109B6
key=460EBADE3CCC9AD2E6D223EB119435F947CC802044295EA90793AEC981AE3183
iv =20E75E1E60ADF1C345E420EB9CD935BA
 
openssl enc -aes-256-cbc -pass pass:WRONG_PASSWORD -p -d -in testfile.aes -out test2
 
salt=2F140A2A667109B6
key=C44D5867FFE37E08397DE9A4CC8058EAED59C20EB66759D7F78C960C2B91A200
iv =5F5A6DFC5CCF0A50AD7502BD047076CE
bad decrypt
140687120832160:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:539:
 
openssl enc -aes-256-cbc -pass pass:WRONG_PASSWORDxxxxxxx -p -d -in testfile.aes -out test2b
salt=2F140A2A667109B6
key=4D44D8C33DD754B64F460A69DA4E4F722678CBF04C44A03C7DB2B5AE4499C9E4
iv =4EABEE050D7D4C29F83B7134BE9FDF21
bad decrypt
140683261372064:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:539:
 
head -c 2560 testfile.aes > snippet
 
openssl enc -aes-256-cbc -pass pass:p@ssword -p -d -in snippet -out test3
 
salt=2F140A2A667109B6
key=460EBADE3CCC9AD2E6D223EB119435F947CC802044295EA90793AEC981AE3183
iv =20E75E1E60ADF1C345E420EB9CD935BA
bad decrypt
140591540508320:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:539:
 
openssl enc -aes-256-cbc -pass pass:WRONG_PASSWORD -p -d -in snippet -out test4
 
salt=2F140A2A667109B6
key=C44D5867FFE37E08397DE9A4CC8058EAED59C20EB66759D7F78C960C2B91A200
iv =5F5A6DFC5CCF0A50AD7502BD047076CE
bad decrypt
140213217490592:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:539:
 
ls -l
 
5083502  test1
5083488  test2
5083488  test2b
2528         test3
2528         test4
 
strings test1 > s1
strings test2 > s2
strings test2b > s2b
strings test3 > s3
strings test4 > s4
strings testfile.aes > as
strings snippet > ss
 
ls -l
 
s1 352345
s2 351306
s2b 352104
s3 153
s4 156
ss 186
as 352191
 
binwalk test1
 
DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             7-zip archive data, version 0.3
 
binwalk test2
 
DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
 
binwalk test2b
 
DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
 
binwalk test3
 
DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             7-zip archive data, version 0.3
 
binwalk test4
 
DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
 
binwalk snippet
 
DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             OpenSSL encryption, salted, salt: 0x2F140A2A667109B6
 
binwalk testfile.aes
 
DECIMAL       HEXADECIMAL     DESCRIPTION
--------------------------------------------------------------------------------
0             0x0             OpenSSL encryption, salted, salt: 0x2F140A2A667109B6
 
 
head -c 8 test1
7z��'
head -c 8 test2
��`@R��
head -c 8 test2b
��nZ�
 
head -c 8 test3
7z��'
head -c 8 test4
��`@R��
tail -c 8 test1
W[��
tail -c 8 test2
a  ��ܹ�
tail -c 8 test3
�!��H\�
 
tail -c 8 test4
�O��$�
 
tail -c 8 snippet
�(���
tail -c 8 testfile.aes
�s��Q�
 
 
hachoir-subfile test1
[+] Start search on 5083502 bytes (4.8 MB)
[+] File at 0 size=5083502 (4.8 MB): Compressed archive in 7z format
[+] File at 4139258 size=27418666 (26.1 MB): MS-DOS executable
[+] End of search -- offset=5083502 (4.8 MB)
 
hachoir-subfile test2
[+] Start search on 5083488 bytes (4.8 MB)
[+] File at 1605181 size=18645165 (17.8 MB): MS-DOS executable
[+] File at 3773184 size=15678389 (15.0 MB): MS-DOS executable
[+] File at 4786234 size=2034059 (1.9 MB): MS-DOS executable
[+] End of search -- offset=5083488 (4.8 MB)
 
hachoir-subfile test2b
[+] Start search on 5083488 bytes (4.8 MB)
[+] End of search -- offset=5083488 (4.8 MB)
 
hachoir-subfile test3
[+] Start search on 2528 bytes (2528 bytes)
[+] File at 0 size=5083502 (4.8 MB): Compressed archive in 7z format
[+] End of search -- offset=2528 (2528 bytes)
 
hachoir-subfile test4
[+] Start search on 2528 bytes (2528 bytes)
[+] End of search -- offset=2528 (2528 bytes)
 
hachoir-subfile snippet
[+] Start search on 2560 bytes (2560 bytes)
[+] End of search -- offset=2560 (2560 bytes)
 
hachoir-subfile testfile.aes
[+] Start search on 5083520 bytes (4.8 MB)
[+] File at 1292169 size=18091181 (17.3 MB): MS-DOS executable
[+] File at 3813667 size=11944255 (11.4 MB): MS-DOS executable
[+] End of search -- offset=5083520 (4.8 MB)
 
 
------------------------------------------------------
Alright - the holidays are over. We laughed, we cried, we did lots of math. Time to finish the job and bring it all home.
 
In this thread, we will start to make this concrete with OpenSSL.
 
Applying some of the ideas here, what we really want to do with OpenSSL is decrypt a file with a random key (or passphrase), and then let it fail. But, unfortunately, OpenSSL leaks "information" about why it fails (padding oracle, etc). So, if you test a bunch of random passwords, you can collect this leaked information, and use it to narrow down the search space (!!!!) for the next round of tests.
 
THUS, YOU NEVER HAVE TO BRUTE FORCE THE ENTIRE SEARCH SPACE. That is the big idea.
 
You instead brute force a small random "representative sample" of the search space, gather the leaked information as a set of experimental "results," and adjust your probability distribution (and as a result, the entropy) accordingly, use that to rule out HUGE sections of the search space in the next round of tests, then repeat and iterate towards a solution.
 
Rather than try to re-derive the math here from scratch, let's try a concrete example. The following 7z on archive.org is about 5 MB, completely public domain under CC0, and contains SVGs of all the Tic-Tac-Toe games (as used on Wikipedia):
 
Details: https://archive.org/details/000svgLA.7z
Direct Download: https://archive.org/download/000svgLA.7z/000svgLA.7z
 
 
Now, so that we can ALL have the same file, simply do the following commands:
 
$ wget https://archive.org/download/000svgLA.7z/000svgLA.7z
...
 
$ openssl enc -aes-256-cbc -pass pass:p@ssword -S 2F140A2A667109B6 -p -in 000svgLA.7z -out testfile.aes
salt=2F140A2A667109B6
key=460EBADE3CCC9AD2E6D223EB119435F947CC802044295EA90793AEC981AE3183
iv =20E75E1E60ADF1C345E420EB9CD935BA
 
$ sha256sum -b testfile.aes
b5210324b21f88e83601d8aa2b940622258c3aab459d9476570f9ec427157574 *testfile.aes
 
And now we're all on the same page here. The last line is just to confirm you have the right file.
 
 
 
So, now we want to take all of this stuff and do some tests. Here's three sample "Bad Decrypts" with the password set to "WRONG_PASSWORD":
 
$ openssl enc -aes-256-cbc -pass pass:WRONG_PASSWORD -p -d -in testfile.aes -out testdecrypt.7z
salt=2F140A2A667109B6
key=C44D5867FFE37E08397DE9A4CC8058EAED59C20EB66759D7F78C960C2B91A200
iv =5F5A6DFC5CCF0A50AD7502BD047076CE
bad decrypt
140341122025112:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:529:
 
$ openssl enc -aes-256-cbc -pass pass:WRONG_PASSWORD -p -d -in testfile.aes -out testdecrypt.7z
salt=2F140A2A667109B6
key=C44D5867FFE37E08397DE9A4CC8058EAED59C20EB66759D7F78C960C2B91A200
iv =5F5A6DFC5CCF0A50AD7502BD047076CE
bad decrypt
140164202464920:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:529:
 
$ openssl enc -aes-256-cbc -pass pass:WRONG_PASSWORD -p -d -in testfile.aes -out testdecrypt.7z
salt=2F140A2A667109B6
key=C44D5867FFE37E08397DE9A4CC8058EAED59C20EB66759D7F78C960C2B91A200
iv =5F5A6DFC5CCF0A50AD7502BD047076CE
bad decrypt
140193671071384:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:529:
 
 
Note the three numbers we get:
 
1. 140341122025112
2. 140164202464920
3. 140193671071384
 
 
You can see it's different every time, so some randomness is involved here. This number is, also, what the padding oracle is mysteriously spitting out.
 
The question is, what the hell does this number mean, and how do we interpret these results?