This post deals with a short excerpt of a security and hacking related exercise at the University of Applied Sciences Upper Austria, Campus Hagenberg: we obtain the plaintext from an intercepted RC4 encoded message, from which we know that it uses an easy to crack re-initialization of the RC4 cipher for each message part.
The problem
Imagine we have intercepted a secret message split into 11 message parts, each (but the last) consisting of 40 1-byte characters:
We know all these message parts have been encrypted using RC4 with a known to be easy to crack reinitialization of the RC4 stream for each message part. We also know the message plaintext only contains letters and whitespaces. The task is: obtain the plaintext. How to do so?
The solution
We know all 11 message parts have been encrypted using the same key stream (as the RC4 cipher has been re-initialized for each message using the same secret key). To avoid confusion here: as stream cipher, RC4 gets initialized from a secret key (e.g. the user’s password) and generates a continuous key stream from it. Each time a RC4 cipher is initialized this way, the generated key stream is exactly the same. Therefore, the RC4 reinitialization for each package causes the key stream to be exactly the same for all messages. RC4 does an XOR on the key stream and the plaintext to obtain the ciphertext. Hence, an XOR with the key stream used for all 11 message ciphertexts causes these to become correct plaintext. We further know that valid plaintext only allows certain characters. Therefore it’s possible to try through all 255 possibilities per keystream character: XOR each one with all corresponding characters of all ciphertexts and keep only those, where all resulting plaintext characters are actually allowed ones. By doing so we generate a list of valid keystreams in an effective way (which by doing an XOR on the ciphertext all result in valid plaintext). Trying through these keys (XOR with plaintext) until we see a message that “makes sense” is now very easy – and could even be done by hand (not posting the actual solution here).
# Cracking RC4 messages that use weak RC4 reinitialization for each message part # # Rainhard Findling # University of Applied Sciences Upper Austria, Campus Hagenberg # 2015/03 # # all ciphertext message parts c1 = [0xd9, 0xef, 0x7b, 0x6e, 0xca, 0xb5, 0x12, 0xa0, 0x4f, 0x4b, 0x56, 0xb4, 0x94, 0xdf, 0x27, 0xed, 0xf8, 0xcc, 0x64, 0x94, 0xeb, 0x5a, 0x77, 0xea, 0x9b, 0x76, 0xdf, 0xe9, 0x18, 0x02, 0xc6, 0x36, 0x19, 0x1f, 0xc8, 0xf2, 0x5a, 0xa8, 0xda, 0xe7] c2 = [0xb7, 0xd3, 0x5f, 0x4e, 0x89, 0x9f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2, 0x01, 0xb3, 0x47, 0x5b] # concatenate all ciphertext parts (except last) ciphertext = [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10] # our brute forcing method def try_keys(c): # each key is one byte, so 255 possibilities here k_ok = [] for k in range(255): plaintext = [x^k for x in c] # all chars of one position have to translate to chars allowed in plaintext: 41-5a, 61-7a ok = [p_chr == 0x20 or p_chr >= 0x41 and p_chr <= 0x5a or p_chr >= 0x61 and p_chr <= 0x7a for p_chr in plaintext] if(all(ok)): k_ok += [k] return k_ok # brute force all keystream positions xor = [] for i in range(40): c = [x[i] for x in ciphertext] xor += [try_keys(c)] # see amount of possibilities left per keystream char position print filter(lambda x:x[1] > 1, [(i, len(xor[i])) for i in range(len(xor))]) # use "first" key key = [x[0] for x in xor] # adapt specific keystream positions per hand so that all plaintext is correct (could be automated) key[3] = xor[3][7] key[14] = xor[14][1] key[21] = xor[21][1] key[24] = xor[24][3] key[25] = xor[25][5] key[34] = xor[34][14] # decode (now include last message part) plaintext = [] for c in (ciphertext + [c11]): t = [] for i in range(len(c)): t += [chr(c[i]^key[i])] plaintext += t print ''.join(plaintext)
Note: the more plaintext messages encoded by the same, re-initialized RC4 cipher we have, the less possible keystreams will remain, and the faster we obtain the plaintext. Btw: this is pretty much the problem WiFi WEP had – this is the core problem of why it can be cracked so easily, and why it is considered very insecure by now.
