This page contains various unstructured notes about crypto related things.
General notes
- If you find somewhere that something should be random, but it isn’t, this is already an important finding.
Stream Ciphers
- A stream cipher encrypts a cleartext bit for bit or character for character (normally via XOR) with a secret key. It produces a key stream which it XORs with the cleartext stream to create the cipher stream.
- The secret lies in the secret key (= called IV initialization vector) and the algorithm.
- The IV must not used twice or an attacker can retrieve the cleartext via XORing both captured ciphertexts.
- The algorithm must have a large period and must never repeat the same encryption pattern.
- Before an algorithm produces the same key stream again, the key/IV must be changed (= rotated).
- A longer IV provides more information for the algorithm so that the period can be longer before key rotation is needed.
- A shorter IV provides less information for the algorithm so that the period must be shorter before key rotation is needed.
Block Ciphers
- A block cipher encrypts a fixed-length cleartext block with a key of the same length as the block into a ciphertext block.
- If a cleartext is shorter as the blocksize, padding has to been used.
- If a cleartext is longer as the blocksize, the cleartext has to been split into multiple blocks.
- A block cipher mode determines how a cleartext block is transformed into a ciphertext block. The main ones are:
- ECB Electronic Codebook Mode: Each block is encrypted with the same key.
- It often produces patterns. A cleartext block is always encrypted to the same ciphertext if their lenghts match.
- Look for repeatable parts in the ciphertext.
- CBC Cipher Block Chaining Mode: Like ECB, but before each round, the key block is XOR’d with a changing value. Therefore the key block looks different at each round and thus the same cleartext won’t produce the same ciphertext.
- For the first round, another initial value is needed.
- From the second round on, usually a part of the previously generated ciphertext block is used.
- This means also that if one block is corrupted, the cleartext from this block on is lost.
- This method works only sequentially.
- CTR Counter Mode: Like ECB, but the changing value before each round is a value from a serialized sequence which can be predicted. Normally increasing numbers.
- ECB Electronic Codebook Mode: Each block is encrypted with the same key.
Heuristics to determine the crypto algorithm
Look to the size of multiple encrypted files.
- Is the size always evenly divisible by 8?
- Maybe a stream cipher, RC4
- Is the size always evenly divisible by 16?
- Maybe AES
- Is the size sometimes divisible by 16, sometimes by 8?
- Maybe DES/3DES
Checking entropy
Check the entropy of the data: (CyberChef, pcaphistogram.py)
- Higher entropy: Probably encrypted.
- Lower entropy: Probably (partly) compressed
Use tcpick -r t.pcap -wR to extract the raw data without TCP/IP headers. Then use ent to calculate the entropy of the TCP streams.
Attacking CBC Cipher Block Chaining Mode
Scenario:
- You found out that a CBC-based ID is used somewhere and stores come content you want to change. For example, a web application stores the user id in a cookie.
- The cookie has the following value:
U2FsdGVkX19TNGlhPemU/Ob/PhaDjIBTnsIkwsNfeGg=. This represents your UID 1042. - You want to change this value to UID 1000 to be recognized as administrator by the web application.
Preperation: Let’s create a ciphertext and encrypt and decrypt it. Note that we use a random password here which doesn’t matter because we are going to change only the encoded UID value and the server will decrypt out provided values without the need for us to know the server’s password.
// Create the file with the value and encrypt and decrypt. $ echo "1042" > uid.cleartext $ openssl enc -aes-128-cbc -in uid.cleartext -out uid.ciphertext // Typed "password" at the password prompt. $ openssl enc -aes-128-cbc -d -in uid.ciphertext -pass pass:password 1042 // Convert binary file into hex string. $ cat uid.ciphertext | xxd --plain -c 100 > uid.hexstring // Convert from hex string into a binary file again. $ cat uid.hexstring | xxd -r -p > uid_new.ciphertext // Check that both files are identical. $ md5sum uid_new.ciphertext uid.ciphertext c95fbe7ded09bab641141296ddb6125d uid_new.ciphertext c95fbe7ded09bab641141296ddb6125d uid.ciphertext // This leads us to the following one liner which we can play: $ echo 53616c7465645f5f533469613de994fce6ff3e16838c80539ec224c2c35f7868 | xxd -r -p | openssl enc -aes-128-cbc -d -in - -pass pass:password 1042
Hash length extension attack
Scenario:
- A webserver offers a file to download but also requires a hash to “prove” that you are allowed to download this file. You know one file and hash:
/download/?file=public.pdf&h=905d6439e669681be03727687de68d47 - You want to download arbitrary files now: For example:
/download/?file=/etc/passwd&h=???
To do this, we are using hash_extender. Its readme contains a good explanation how it works. Note that it only works if the server prepends the secret.
- Create a new signature and URL string with something like:
./hash_extender -d public.pdf -s 676dc17ec36bc6f200595e4a5ad0a9b2cee0c8b3 -a ../secret.pdf --format sha1 -l 9 --out-data-format html
Leave a Reply
You must be logged in to post a comment.