Breaking affine ciphers – a matrix approach

An affine cipher is a one the remnants of classical cryptography, easily broken with todays computational power. The cipher defines a symbol mapping from $f :\{A,B,\ldots,\} \mapsto \mathbb{Z}_n$. Each cipher symbol is then computed as $a \cdot x + b \rightarrow y$, where $a \in \mathbb{Z}^*_n$ and $b \in \mathbb{Z}_n$. Decryption is then done by computing $x= (y - b) \cdot a^{-1}$.

In this blog post, I will show how to break this cipher in time faster than trying all keys. Let us first sketch the general idea. Consider an expected distribution $\hat{P}$ of the symbols and a given distribution $P$, the integral $\int (\hat{P}(x) - P(x))^2 dx$ defines a statistical distance between the distributions (this would correspond to the Euclidian distance), which we would like to minimize. Now, clearly

$(\hat{P}(x) - P(x))^2 = \hat{P}(x)^2 - \hat{P}(x)P(x) + P(x)^2$.

Trivially, $\hat{P}(x)^2$ and $P(x)^2$ remains constant over any keypair $(a,b)$, so instead of minimizing the above, we can maximize $\hat{P}(x)P(x)$. Therefore, the minimization problem can be turned into a maximization problem $\max_{a,b} \int \hat{P}(x)P_{a,b}(x) dx$. Cool.

In terms of our cipher, which is discrete, the minimization problem is a sum $\max_{a,b} \sum \hat{P}(x)P_{a,b}(x)$. The observant reader may notice that this looks like a term in a matrix multiplication. There is just one caveat; the indices corresponding appear only in one term. There is an easy way to get around this. Instead of applying transformations on only $P$, we may split them among the two. So by instead computing

$\max_{a,b} \sum \hat{P}_a(x) P_{b}(x)$,

we have achieved what we desired. This means that we shuffle $\hat{P}$ with $a$ and ${P}$ with $b$. Let us interpret this as Python. The expected distribution of an alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZ ,. may be as follows (depending on the observation):

P_hat = {' ': 0.05985783763561542, ',': 0.0037411148522259637, '.': 0.0028058361391694723, 'A': 0.0764122708567153, 'C': 0.02600074822297044, 'B': 0.012065095398428732, 'E': 0.11878039655817432, 'D': 0.03974934530490086, 'G': 0.018892630003741116, 'F': 0.020856715301159744, 'I': 0.0651889263000374, 'H': 0.05695847362514029, 'K': 0.00720164609053498, 'J': 0.0014029180695847362, 'M': 0.02254021698466143, 'L': 0.03769173213617658, 'O': 0.07023943135054246, 'N': 0.06313131313131314, 'Q': 0.0009352787130564909, 'P': 0.01805087916199027, 'S': 0.05920314253647587, 'R': 0.0560231949120838, 'U': 0.025813692480359144, 'T': 0.08473625140291807, 'W': 0.022072577628133184, 'V': 0.00916573138795361, 'Y': 0.01842499064721287, 'X': 0.0014029180695847362, 'Z': 0.0006546950991395436}


The transformations are done by computing the matrices

# compute first matrix for transformed P_hat
for i in range(1, N):
for element in priori_dist:
X[i, (look_up.index(element) * i) % N] = priori_dist[element]

# compute second matrix for transformed P
for j in range(N):
for element in dist:
Y[(look_up.index(element) - j) % N, j] = dist[element]


Here, the $i$th row in $X$ corresponds to $\hat{P}$ transformed by $a = i$. Moreover, the $j$th row in $Y$ corresponds ${P}$ transformed by $b = j$.

For some distribution, they may look like

As we can see, $X$ is only shifted (by the addition), while in $Y$ the indices are reordered by multiplication with row index $i$. Taking advantage of the matrix multiplication property, we may now compute $Z=XY$.

Any entry in $Z$ is $Z_{a,b} = \sum_x X_{a,x} Y_{x,b}$ so finding a maximum element in $Z$ is equivalent to saying

$\max_{a,b} \sum_x X_{a,x} Y_{x,b}$.

Looks familiar? It should. This is our maximization problem, which we stated earlier.

Therefore, we may solve the problem using

Z = numpy.dot(X, Y)
a, b = numpy.unravel_index(Z.argmax(), Z.shape)


This breaks the affine cipher.

Some notes on complexity

So, what is the complexity of the matrix approach? Computing the matrices takes $O(N^2)$ modular operations. The matrix multiplication takes naively $O(N^3)$ operations, but for large $N$ this can be achieved faster. For instance Strassen takes $O(N^{2.807})$ but faster algorithms exist. Also, taking advantage of symmetry and structure could probably decrease the complexity further. This is the total complexity of this approach.

Compare this with brute-force guessing of the key (taking $O(N^2)$ guesses) and for each guess, compute the distance taking $O(N)$ operations, which in total yields $O(N^3)$. It should be noted that complexity of this approach may be reduced by picking $a,b$ in an order which minimizes the number of tries.

Example implementation for the id0-rsa.pub: github

Custom terminal for Vagrant/SSH

Short story: I wanted to distinguish my terminal windows between local sessions, ssh sessions and vagrant sessions.

SSH_THEME="SSH"
VAGRANT_THEME="Vagrant"

set_th () {
osascript -e "tell app \"Terminal\" to set current settings of first window to settings set \"$1\"" } set_id () { osascript -e "tell app \"Terminal\" to set current settings of first window to$1 $2$3 $4" #$@ does not work!
}

get_id () {
cur_id=$(osascript -e "tell app \"Terminal\" to get current settings of first window") } ssh(){ #!/bin/sh get_id set_th$SSH_THEME
/usr/bin/ssh "$@" set_id$cur_id
}

vagrant(){
#!/bin/sh
if [ $1 = "ssh" ]; then get_id set_th$VAGRANT_THEME
/opt/vagrant/bin/vagrant "$@" set_id$cur_id
else
/opt/vagrant/bin/vagrant "$@" fi }  The code creates a temporary variable of the current theme before switching. So, when ending the session, the original theme changes back instead of a fixed one. Putting the above code in your .bash_profile: gives the following nice behavior: Color coding your sessions is a great way to visualize things and make sure you do not take any unwanted action by mistake 🙂 Of course, the code can be used to wrap any application. For instance, one could use it to make the interactive interpreter of Python/Sage or terminal sessions using torsocks appear in different colors or even fonts. Re-mapping KBT Pure Pro in OS X For my everyday-use computer, I use a modded KBT Pure Pro; this is a small mechanical keyboard with aluminium base and background lightning, perfect for programming and typing. The size of the keyboard is 60 % of a normal one, making it suitable for spatially constrained workspaces. To my experience, it is also more ergonomic. Below is a comparison of the Pure Pro and a wireless Apple keyboard. For those being the in the process of buying a keyboard, I recommend this one 🙂 For quite a while, I have used Linux on this computer. But after installing OS X, the keyboard map went wack, so to speak. Many keys were mapped incorrectly. Using Ukulele, I created a customized layout with correct mapping (don’t mind the duplicate keys): The layout covers all keys and can be found here. NOTE: this is a layout for KBT Pure Pro with British ISO layout and not ANSI. BackdoorCTF16 – Collision Course With 350 points and a description as follows: In today’s world, hash collisions are becoming more and more popular. That is why, one must rely on standardized hashing techniques, such as bcrypt. However, n00bster shall never learn, and he has implemented his own hash function which he proudly calls foobar. Attached is an implementation of the hash function and a file with which you are supposed to find a collision. He believes that you will not be able to find a collision for the file, especially since he hasn’t even given you the hashing algorithm, but has packaged it as a black box application. Prove to him that he is wrong. Note: Multiple collisions are possible, but only one of them is a valid flag. You will realize you’ve gotten it once you do. The hash is given as follows: So, we start off by looking at the binary. Using Hopper, we obtain the following pseudo code by decompilation: int hash(int input) { eax = _rotr(input ^ 0x24f50094, (input ^ 0x24f50094) & 0xf); eax = _rotl(eax + 0x2219ab34, eax + 0x2219ab34 & 0xf); eax = eax * 0x69a2c4fe; return eax; } int main() { esp = (esp & 0xfffffff0) - 0x20; puts(0x80486d0); gets(0x804a060); stack[2039] = "\nBar:"; puts(stack[2039]); while (stack[2039] < *(esp + 0x18)) { stack[2039] = *(stack[2039] + stack[2039] * 0x4); *(esp + 0x14) = *(esp + 0x14) ^ hash(stack[2039]); eax = _rotr(stack[2039], 0x7); printf("%08lx", stack[2039]); *(esp + 0x10) = *(esp + 0x10) + 0x1; } eax = putchar(0xa); return eax; }  We sketch the above code as block scheme below: The first thing to note is that we can find an infinite number of collisions just by appending arbitrary data after 10 blocks. However, this is not interesting to us, but completely defeats the conditions for a safe cryptographic hash function. This Merkle-Damgård-like structure allows us to solve blocks iteratively, starting from the first. Here is how. Starting from the first block, we can find an input to the function $H$ such that when rotated 7 steps is equal to block 0 (here, denoted $B_0$). Hence, the problem we solve is to find an $x$ such that $H(x) \ll 7 = B_0$. This is a simple thing for Z3. Then, we take the next block and solve for $(H(x) \oplus B_0) \ll 7 = B_1$ and so forth. Implemented in Python/Z3, it may look like the following: from z3 import * import binascii, string, itertools bits = 32 mask = 2**bits - 1 allowed_chars = string.printable def convert_to_hex(s): return ''.join([hex(ord(x))[2:].zfill(2) for x in s[::-1]]) def convert_to_string(h): return ''.join([chr(int(x, 16)) for x in list(map(''.join, zip(*[iter(hex(h)[2:])]*2)))[::-1]]) def rot(val, steps): return (val << (bits-steps)) | LShR(val, steps) def hash_foobar(input): eax = rot(input ^ 0x24f50094, (input ^ 0x24f50094) & 0xf) eax = rot(eax + 0x2219ab34, bits - (eax + 0x2219ab34 & 0xf)) eax = eax * 0x69a2c4fe return eax & mask def break_iteratively(hashdata, i): if i == 0: prev_block = 0 else: prev_block = hashdata[i-1] s = Solver() j = BitVec('current_block', bits) eax = rot(prev_block ^ hash_foobar(j), 7) s.add(eax == hashdata[i]) block_preimages = [] while s.check() == sat: sol = s.model() s.add(j != sol[j].as_long()) block_string = convert_to_string(sol[j].as_long()) if all(c in allowed_chars for c in block_string): block_preimages.append(block_string) return block_preimages known = '9513aaa552e32e2cad6233c4f13a728a5c5b8fc879febfa9cb39d71cf48815e10ef77664050388a3' # this the hash of the file data = list(map(''.join, zip(*[iter(known)]*8))) hashdata = [int(x, 16) for x in data] print '[+] Hash:', ''.join(data) print '[+] Found potential hashes:\n' for x in itertools.product(*[break_iteratively(hashdata, i) for i in range(10)]): print ' * ' + ''.join(x)  This code is surprisingly fast, thanks to Z3, and runs in 0.3 seconds. Taking all possible collisions into consideration… [+] Hash: 9513aaa552e32e2cad6233c4f13a728a5c5b8fc879febfa9cb39d71cf48815e10ef77664050388a3 [+] Found potential hashes: * CTFEC0nstra1nts_m4keth_fl4g} * CTFEC0nstra1nts_m4keth_nl4g} * CTFEC0nstra1nws_m4keth_fl4g} * CTFEC0nstra1nws_m4keth_nl4g} * CTFEC0nstra9nts_m4keth_fl4g} * CTFEC0nstra9nts_m4keth_nl4g} * CTFEC0nstra9nws_m4keth_fl4g} * CTFEC0nstra9nws_m4keth_nl4g} * CTF{C0nstra1nts_m4keth_fl4g} * CTF{C0nstra1nts_m4keth_nl4g} * CTF{C0nstra1nws_m4keth_fl4g} * CTF{C0nstra1nws_m4keth_nl4g} * CTF{C0nstra9nts_m4keth_fl4g} * CTF{C0nstra9nts_m4keth_nl4g} * CTF{C0nstra9nws_m4keth_fl4g} * CTF{C0nstra9nws_m4keth_nl4g}  …we finally conclude that the flag is the SHA-256 of C0nstra1nts_m4keth_fl4g. BackdoorCTF16 – Baby Worth 200 points, this challenge was presented with the following: z3r0c00l has a safe repository of files. The filename is signed using z3r0c00l’s private key (using the PKCS-1 standard). Anyone willing to read a file, has to ask for a signature from z3r0c00l. But z3r0c00l is currently unavailable. Can you still access a file named “flag” on z3rc00l’s repository? nc hack.bckdr.in 9001 Let us take a look at the public key… 3072 bits and public exponent $e = 3$. Hmm… having a small exponent is usually not a good practice. First, I tried computing the roots to $x^3 - s \bmod n$, where $s$ is the signature and $n$ is the modulus, but then I realized that this was not the way to go. What if we use non-modular squareroot, plain old Babylonian style? After looking around, I also realized that this is Bleicherbacher’s $e = 3$ attack, which I probably should have known about. There is a lot of information about this attack (therefore, I will not describe it here) and, of course, lots of people have already written code for this. Being lazy/efficient, I rewrote a functional code into the the following: from libnum import * from gmpy2 import mpz, iroot, powmod, mul, t_mod import hashlib, binascii, rsa, os def get_bit(n, b): """ Returns the b-th rightmost bit of n """ return ((1 << b) & n) >> b def set_bit(n, b, x): """ Returns n with the b-th rightmost bit set to x """ if x == 0: return ~(1 << b) & n if x == 1: return (1 << b) | n def cube_root(n): return int(iroot(mpz(n), 3)[0]) snelhest = hashlib.sha256('flag') ASN1_blob = rsa.pkcs1.HASH_ASN1['SHA-256'] suffix = b'\x00' + ASN1_blob + snelhest.digest() sig_suffix = 1 for b in range(len(suffix)*8): if get_bit(sig_suffix ** 3, b) != get_bit(s2n(suffix), b): sig_suffix = set_bit(sig_suffix, b, 1) while True: prefix = b'\x00\x01' + os.urandom(3072//8 - 2) sig_prefix = n2s(cube_root(s2n(prefix)))[:-len(suffix)] + b'\x00' * len(suffix) sig = sig_prefix[:-len(suffix)] + n2s(sig_suffix) if b'\x00' not in n2s(s2n(sig) ** 3)[:-len(suffix)]: break print hex(s2n(sig))[2:-1]  Ok, so lets try it: Great! Defcon CTF – b3s23 (partial?) The server runs a program (game of life) which has a $110 \times 110$ board with cells (bits). After a fixed number $n$ of iterations, the simulation stops and the program jumps to the first bit of the memory containing the board. We want to create an input which contains shellcode in this area after $n$ iterations. Obviously, we could choose any shellcode, and run game of life backwards. Cool, let us do that then! Uh-oh, inverting game of life is in fact a very hard problem… so it is not really feasible 😦 What to do, then? Game of life Game of life a cellular automata, found by Jon Conway, and is based on the following rules: 1. A cell is born if it has exactly 3 neighbours. Neighbors are defined as adjacent cells in vertical, horistontal and diagonal. 2. A cell dies if it has less than two or more than three neighbors. Stable code (still life) Still life consists of cell structures with repeating cycles having period 1. Here are the building blocks I used to craft the shellcode. Of course, the still life is invariant of rotation and mirroring. Shellcode So, I tried to find the shortest shellcode that would fit one line (110 bits). This one is 8 bytes. Great. 08048334 <main>: 8048334: 99 cltd 8048335: 6a 0b push$0xb
8048337:   58                pop    %eax
8048338:   60                pusha
8048339:   59                pop    %ecx
804833a:   cd 80             int    \$0x80


In binary, this translates to:

000001101001100101101010000010110101100001100000010110011100110110000000


Ok, so we note that

110101 ... 01110

cannot be constructed by our building blocks (there most certainly exist such blocks, but I didn’t consider them). So, I use a padding trick. By inserting an operation which does nothing specific

10110011 00000000    mov    bl,0x0

we are able to use the blocks given in previous section. This Python code gives the binary (still-life-solvable) sequence:

from pwn import *
binary_data = ''.join([bin(ord(opcode))[2:].zfill(8) for opcode in shellcode])
context(arch = 'i386', os = 'linux')
print disasm(shellcode)
print binary_data[0:110]


which is

0000011010011001101100110000000001101010000010110101100001100000010110011100110110000000


The following cellular automata is stable, and the first line contains our exploit:

As can be seen in the animation below, we have found a still-life shellcode.

When feeding it to the program, we find that it remains in memory after any number of iterations:

Nice! Unfortunately, the code did not give me a shell, but at least the intended code was executed. I had a lot of fun progressing this far 🙂

TU CTF – Secure Auth

This was a 150 point challenge with the description:

We have set up this fancy automatic signing server!
We also uses RSA authentication, so it’s super secure!

nc 104.196.116.248 54321

Connecting to the service, we get the following

Obviously, we cannot feed the message get_your_hands_off_my_RSA! to the oracle. So, we will only receive signatures, but no way to verify them; this means we don’t know either the public modulus, nor the public exponent. But, of course, we could guess the public exponent… there are a few standard ones: $3, 17, 65537...$

First, I obtained the signatures for $3$ and $4$ from the provided service. Denote these $s_3, s_4$, respectively. We note that given a correct public exponent $e$, we may compute $s_3^e = 3 + k \cdot N$ and $s_4^e = 4 + l \cdot N$. Inevitably, $\textnormal{gcd}(s_3^e-3,s_4^e-4) = \textnormal{gcd}(k,l)\cdot N$. Hoping for $\textnormal{gcd}(k,l)$ to be small, we can use serveral pairs until we find one that works.

Trying all the listed (guessed) public exponents, we find that $e = 65537$ (this was performed surprisingly fast in Sage with my Intel hexacore). Hence, we have now determined the modulus

$\begin{array}{rl} N = & 24690625680063774371747714092931245796723840632401231916590850908498671935961736 \\ &33219586206053668802164006738610883420227518982289859959446363584099676102569045 \\ &62633701460161141560106197695689059405723178428951147009495321340395974754631827 \\ &95837468991755433866386124620786221838783092089725622611582198259472856998222335 \\ &23640841676931602657793593386155635808207524548748082853989358074360679350816769 \\ &05321318936256004057148201070503597448648411260389296384266138763684110173009876\\ &82339192115588614533886473808385041303878518137898225847735216970008990188644891 \\ &634667174415391598670430735870182014445537116749235017327.\end{array}$

Now, note that

libnum.strings.s2n('get_your_hands_off_my_RSA!') % 3 == 0


OK, so we may split this message $m$ into a product of two message factors: $m_1 = 3$ and $m_2 = 166151459290300546021127823915547539196280244544484032717734177$ and sign them. Then, we compute the final signature $s = m^d = (m_1 \cdot m_2)^d = m_1^d \cdot m_2^d = s_1 \cdot s_2 \bmod N$. Mhm, so what now?

Phew 🙂