Previous Level Guide: Krypton Level 5 → 6
Access
SSH: ssh krypton6@krypton.labs.overthewire.org -p 2231
Password: RANDOM
Info
Hopefully by now its obvious that encryption using repeating keys is a bad idea. Frequency analysis can destroy repeating/fixed key substitution crypto. A feature of good crypto is random ciphertext. A good cipher must not reveal any clues about the plaintext. Since natural language plaintext (in this case, English) contains patterns, it is left up to the encryption key or the encryption algorithm to add the ‘randomness’. Modern ciphers are similar to older plain substitution ciphers, but improve the ‘random’ nature of the key. An example of an older cipher using a complex, random, large key is a vigniere using a key of the same size of the plaintext. For example, imagine you and your confident have agreed on a key using the book ‘A Tale of Two Cities’ as your key, in 256 byte blocks. The cipher works as such: Each plaintext message is broken into 256 byte blocks. For each block of plaintext, a corresponding 256 byte block from the book is used as the key, starting from the first chapter, and progressing. No part of the book is ever re-used as key. The use of a key of the same length as the plaintext, and only using it once is called a “One Time Pad”. Look in the krypton6 directory. You will find a file called ‘plain1’, a 256 byte block. You will also see a file ‘key1’, the first 256 bytes of ‘A Tale of Two Cities’. The file ‘cipher1’ is the cipher text of plain1. As you can see (and try) it is very difficult to break the cipher without the key knowledge. If the encryption is truly random letters, and only used once, then it is impossible to break. A truly random “One Time Pad” key cannot be broken. Consider intercepting a ciphertext message of 1000 bytes. One could brute force for the key, but due to the random key nature, you would produce every single valid 1000 letter plaintext as well. Who is to know which is the real plaintext?!? Choosing keys that are the same size as the plaintext is impractical. Therefore, other methods must be used to obscure ciphertext against frequency analysis in a simple substitution cipher. The impracticality of an ‘infinite’ key means that the randomness, or entropy, of the encryption is introduced via the method. We have seen the method of ‘substitution’. Even in modern crypto, substitution is a valid technique. Another technique is ‘transposition’, or swapping of bytes. Modern ciphers break into two types; symmetric and asymmetric. Symmetric ciphers come in two flavours: block and stream. Until now, we have been playing with classical ciphers, approximating ‘block’ ciphers. A block cipher is done in fixed size blocks (suprise!). For example, in the previous paragraphs we discussed breaking text and keys into 256 byte blocks, and working on those blocks. Block ciphers use a fixed key to perform substituion and transposition ciphers on each block discretely. Its time to employ a stream cipher. A stream cipher attempts to create an on-the-fly ‘random’ keystream to encrypt the incoming plaintext one byte at a time. Typically, the ‘random’ key byte is xor’d with the plaintext to produce the ciphertext. If the random keystream can be replicated at the recieving end, then a further xor will produce the plaintext once again. From this example forward, we will be working with bytes, not ASCII text, so a hex editor/dumper like hexdump is a necessity. Now is the right time to start to learn to use tools like cryptool. In this example, the keyfile is in your directory, however it is not readable by you. The binary ‘encrypt6’ is also available. It will read the keyfile and encrypt any message you desire, using the key AND a ‘random’ number. You get to perform a ‘known ciphertext’ attack by introducing plaintext of your choice. The challenge here is not simple, but the ‘random’ number generator is weak. As stated, it is now that we suggest you begin to use public tools, like cryptool, to help in your analysis. You will most likely need a hint to get going. See ‘HINT1’ if you need a kicktstart. If you have further difficulty, there is a hint in ‘HINT2’. The password for level 7 (krypton7) is encrypted with ‘encrypt6’. Good Luck!
Theory
latr:
cd /krypton/krypton6
Solution
Now that we are logged in the SSH, we can use cd to go to the level folder and see what's there:
~$ cd /krypton/krypton6 /krypton/krypton6$ ls -la total 56 drwxr-xr-x 3 root root 4096 Sep 19 07:10 . drwxr-xr-x 9 root root 4096 Sep 19 07:10 .. -rwsr-x--- 1 krypton7 krypton6 16520 Sep 19 07:10 encrypt6 -rw-r----- 1 krypton6 krypton6 164 Sep 19 07:10 HINT1 -rw-r----- 1 krypton6 krypton6 11 Sep 19 07:10 HINT2 -rw-r----- 1 krypton7 krypton7 11 Sep 19 07:10 keyfile.dat -rw-r----- 1 krypton6 krypton6 15 Sep 19 07:10 krypton7 drwxr-xr-x 2 root root 4096 Sep 19 07:10 onetime -rw-r----- 1 krypton6 krypton6 4342 Sep 19 07:10 README /krypton/krypton6$ cat README Hopefully by now its obvious that encryption using repeating keys is a bad idea. Frequency analysis can destroy repeating/fixed key substitution crypto. A feature of good crypto is random ciphertext. A good cipher must not reveal any clues about the plaintext. Since natural language plaintext (in this case, English) contains patterns, it is left up to the encryption key or the encryption algorithm to add the 'randomness'. Modern ciphers are similar to older plain substitution ciphers, but improve the 'random' nature of the key. An example of an older cipher using a complex, random, large key is a vigniere using a key of the same size of the plaintext. For example, imagine you and your confident have agreed on a key using the book 'A Tale of Two Cities' as your key, in 256 byte blocks. The cipher works as such: Each plaintext message is broken into 256 byte blocks. For each block of plaintext, a corresponding 256 byte block from the book is used as the key, starting from the first chapter, and progressing. No part of the book is ever re-used as key. The use of a key of the same length as the plaintext, and only using it once is called a "One Time Pad". Look in the krypton6/onetime directory. You will find a file called 'plain1', a 256 byte block. You will also see a file 'key1', the first 256 bytes of 'A Tale of Two Cities'. The file 'cipher1' is the cipher text of plain1. As you can see (and try) it is very difficult to break the cipher without the key knowledge. (NOTE - it is possible though. Using plain language as a one time pad key has a weakness. As a secondary challenge, open README in that directory) If the encryption is truly random letters, and only used once, then it is impossible to break. A truly random "One Time Pad" key cannot be broken. Consider intercepting a ciphertext message of 1000 bytes. One could brute force for the key, but due to the random key nature, you would produce every single valid 1000 letter plaintext as well. Who is to know which is the real plaintext?!? Choosing keys that are the same size as the plaintext is impractical. Therefore, other methods must be used to obscure ciphertext against frequency analysis in a simple substitution cipher. The impracticality of an 'infinite' key means that the randomness, or entropy, of the encryption is introduced via the method. We have seen the method of 'substitution'. Even in modern crypto, substitution is a valid technique. Another technique is 'transposition', or swapping of bytes. Modern ciphers break into two types; symmetric and asymmetric. Symmetric ciphers come in two flavours: block and stream. Until now, we have been playing with classical ciphers, approximating 'block' ciphers. A block cipher is done in fixed size blocks (suprise!). For example, in the previous paragraphs we discussed breaking text and keys into 256 byte blocks, and working on those blocks. Block ciphers use a fixed key to perform substituion and transposition ciphers on each block discretely. Its time to employ a stream cipher. A stream cipher attempts to create an on-the-fly 'random' keystream to encrypt the incoming plaintext one byte at a time. Typically, the 'random' key byte is xor'd with the plaintext to produce the ciphertext. If the random keystream can be replicated at the recieving end, then a further xor will produce the plaintext once again. From this example forward, we will be working with bytes, not ASCII text, so a hex editor/dumper like hexdump is a necessity. Now is the right time to start to learn to use tools like cryptool. In this example, the keyfile is in your directory, however it is not readable by you. The binary 'encrypt6' is also available. It will read the keyfile and encrypt any message you desire, using the key AND a 'random' number. You get to perform a 'known ciphertext' attack by introducing plaintext of your choice. The challenge here is not simple, but the 'random' number generator is weak. As stated, it is now that we suggest you begin to use public tools, like cryptool, to help in your analysis. You will most likely need a hint to get going. See 'HINT1' if you need a kicktstart. If you have further difficulty, there is a hint in 'HINT2'. The password for level 7 (krypton7) is encrypted with 'encrypt6'. Good Luck! /krypton/krypton6$ cat krypton7 PNUKLYLWRQKGKBE /krypton/krypton6$ ./encrypt6 usage: encrypt6 foo bar Where: foo is the file containing the plaintext and bar is the destination ciphertext file.
So looks like we have an executable file that encrypts a file the same way the password is encrypted, and because it's encrypted in XOR like specified in the description and README, it is going to be pretty easy because XOR encryption is simple as this. If we try to create a file containing "IAMSOMEONE" for example in uppercase as plaintext and enter it to the encrypt6 file, we can compare the plaintext and ciphertext in binary, using the command xxd -b (before that link the keyfile file):
/krypton/krypton5$ mktemp -d /tmp/tmp.OmYM3UizOG /tmp/tmp.OmYM3UizOG$ ln -s /krypton/krypton6/keyfile.dat /tmp/tmp.OmYM3UizOG$ chmod 777 . /tmp/tmp.OmYM3UizOG$ ls keyfile.dat /tmp/tmp.OmYM3UizOG$ nano plaintext /tmp/tmp.OmYM3UizOG$ cat plaintext EXAMPLE /tmp/tmp.OmYM3UizOG$ /krypton/krypton6/encrypt6 plaintext ciphertext /tmp/tmp.OmYM3UizOG$ ls ciphertext keyfile.dat plaintext /tmp/tmp.OmYM3UizOG$ cat ciphertext IFCFSRC /tmp/tmp.OmYM3UizOG$ xxd -b plaintext 00000000: 01001001 01000001 01001101 01010011 01001111 01001101 IAMSOM 00000006: 01000101 01001111 01001110 01000101 00001010 00001010 EONE.. /tmp/tmp.OmYM3UizOG$ xxd -b ciphertext 00000000: 01001101 01001001 01001111 01001100 01010010 01010011 MIOLRS 00000006: 01000011 01010111 01001100 01000100 CWLD
This looks interesting, if we look into it and take the first letter for example. In the plaintext it's 01000101 and ciphertext has 01001001, and because XOR is reversible by using the same changing we just need to do the XOR thing, which is just 0 when both are equal (0 and 0, or 1 and 1 give 0), and 1 when they're different (0 and 1, or 1 and 0 give 1). And we get 00000100, which is 4, which is the letter E in the alphabet (if A starts in number 0 how it should be). So now we know that the key starts with the letter "E", we can get the rest of the key by just spamming the same letter until the ciphertext repeats. For example, if we make a file with a bunch of letter A's, we can put it into the encryption program and get back the key, because as mentioned before, XOR encryption is reversible with its same algorithm of encryption, or something like that. So, let's try the spamming A's thing:
/tmp/tmp.7iQoUrUXM0$ nano plaintext /tmp/tmp.7iQoUrUXM0$ cat plaintext AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
I don't even know how many A's is that, but with like 35 it should be fine:
/tmp/tmp.7iQoUrUXM0$ /krypton/krypton6/encrypt6 plaintext ciphertext /tmp/tmp.7iQoUrUXM0$ cat ciphertext EICTDGYIYZKTHNSIRFXYCPFUEOCKRNEICTDGYIYZKTHNSIRFXYCPFUEOCKRNEICTDGYIYZKTHNSIRFXYCPFUEOCKRNEICTDGYIYZKTHNSI
We can see that at some point the key starts repeating, so yeah I put in too many A's. But it's not so clear where it repeats, so let me put some new lines in between, and look at that:
EICTDGYIYZKTHNSIRFXYCPFUEOCKRN EICTDGYIYZKTHNSIRFXYCPFUEOCKRN EICTDGYIYZKTHNSIRFXYCPFUEOCKRN EICTDGYIYZKTHNSI
So cool! That's the key that starts with the letter E. Now we can input this key into dCode's vignere cipher and get the decrypted password:
CIPHERTEXT: PNUKLYLWRQKGKBE KEY/PASSWORD: EICTDGYIYZKTHNSIRFXYCPFUEOCKRN RESULT: LFSRISNOTRANDOM
And that's the password! Now we should be good to go to the next level.
https://overthewire.org/wargames/krypton/krypton6.htmlNext Level Guide: Krypton Level 7 → Level 8