|
| 1 | +# Security Through Obscurity - 150 points |
| 2 | + |
| 3 | +I've never seen such a cryptosystem]before! It looks like a public key cryptosystem, though... Could you help me crack it? |
| 4 | + |
| 5 | +[encrypt.sage](../files/sage.py) |
| 6 | +[publickey_and_ciphertext.txt](../files/key.txt) |
| 7 | + |
| 8 | +### Solution |
| 9 | +###### Writeup by Valar Dragon |
| 10 | + |
| 11 | + |
| 12 | +I think this problem achieves the true definition of failed Security Through Obscurity! These files, except for my [solver](https://github.com/HackThisCode/CTF-Writeups/blob/master/2017/EasyCTF/Security%20Through%20Obscurity/sageSolver.py), are all sage files. |
| 13 | +Sage is an open source python-based alternative to Mathematica and Matlab. |
| 14 | + |
| 15 | +Lets analyze the code in sage.py |
| 16 | + |
| 17 | +``` python |
| 18 | +def calc_root(num, mod, n): |
| 19 | + f = GF(mod) |
| 20 | + temp = f(num) |
| 21 | + return temp.nth_root(n) |
| 22 | +``` |
| 23 | +Looking through the Sage documentation, we can see that |
| 24 | +f becomes a finite field of order mod. Mod is actually a prime, being passed from `gen_v_list(primelist, p, secret)`. |
| 25 | + |
| 26 | +temp.nth_root(n) is finding the nth root of temp in this finite field. This means that |
| 27 | +``` python |
| 28 | +pow(temp.nth_root(n),n) ≡ temp mod num |
| 29 | +``` |
| 30 | + |
| 31 | +This means that we can quite easily brute force for SECRET, see [sageSecret](https://github.com/HackThisCode/CTF-Writeups/blob/master/2017/EasyCTF/Security%20Through%20Obscurity/sageSecret.py) file. |
| 32 | +We don't actually need to solve for SECRET however. |
| 33 | + |
| 34 | +So now this tells us what |
| 35 | +``` python |
| 36 | +def gen_v_list(primelist, p, secret): |
| 37 | + a = [] |
| 38 | + for prime in primelist: |
| 39 | + a.append(calc_root(prime, p, secret)) |
| 40 | + return a |
| 41 | +``` |
| 42 | +means. gen_v_list is the nth root of each prime in the finite field of size p. |
| 43 | + |
| 44 | +``` python |
| 45 | +primelist = [2,3,5,7,11,13,17,19,23,29,31,37,43,47,53,59] |
| 46 | +message = REDACTED |
| 47 | +chunks = [] |
| 48 | +for i in range(0,len(message),2): |
| 49 | + chunks += [message[i:i+2]] |
| 50 | +``` |
| 51 | +This splits the message into 2 byte chunks |
| 52 | + |
| 53 | +``` python |
| 54 | +for chunk in chunks: |
| 55 | + binarized = bin(int(chunk.encode('hex'),16)).replace('0b','').zfill(16)[::-1] #lsb first |
| 56 | + enc = 1 |
| 57 | + for bit in range(len(binarized)): |
| 58 | + enc *= vlist[bit]**int(binarized[bit]) |
| 59 | + enc = enc%p |
| 60 | + print(enc) |
| 61 | +``` |
| 62 | +This converts every chunk to hex, removes any `0b` in the hex, converts it to binary, pads the left with 0's until its 16 bits longs, and then reverses the string. The for loop is multiplying enc by that |
| 63 | +index in vlist if binarized[i] is 1. If its 0, then do nothing to enc. Finally take enc modulo p. |
| 64 | + |
| 65 | +Wait!!! Only 16 bits in a chunk?? That means theres only `2**16 = 65536` options, well within the brute force range! |
| 66 | + We can just do a reverse lookup on everything in the ciphertext! |
| 67 | + |
| 68 | + |
| 69 | +Brute forcing all 16 bits of options, and doing reverse lookups on Ciphertext, gives us the flag: |
| 70 | +``` bash |
| 71 | +$ python3 sageSolver.py |
| 72 | +flag{i_actu4lly_d0nt_know_th3_name_of_th15_crypt0sy5tem} |
| 73 | +``` |
| 74 | + |
| 75 | +In discussion with Neptunia, the challenge creator, I found out this was actually an unintended solution, |
| 76 | + and this solution reduced the point value from 500 points (iirc), |
| 77 | + to this current 150. |
| 78 | + |
| 79 | +The intended solution involved learning what cryptosystem this is through google, and from there figuring out its decrypt function. |
| 80 | + |
| 81 | +A link for the cryptosystem given post-CTF on this cryptosystem is: |
| 82 | + https://www.di.ens.fr/~stern/data/St63.pdf |
| 83 | + ### External Writeups |
| 84 | + |
| 85 | + * [https://github.com/HackThisCode/CTF-Writeups/blob/master/2017/EasyCTF/Security%20Through%20Obscurity/README.md](https://github.com/HackThisCode/CTF-Writeups/blob/master/2017/EasyCTF/Security%20Through%20Obscurity/README.md) |
0 commit comments