# Category: Cryptography

## NSA Backdoor (500 points)

I heard someone has been sneakily installing backdoors in open-source implementations of Diffie-Hellman… I wonder who it could be… 😉

This challenge took me quite a bit to solve (as to be expected; it’s a 500 point challenge). However, having solved it, it actually wasn’t that hard of a challenge.

On first glance it…actually looks similar to Very Smooth. Infact, it’s the same script. However, the only difference is that when the ciphertext is computed, it is performed through e^flag rather than flag^e. Although a small alteration, this practically means that RSA will not work in this case. So, we’re going to need to find some way to figure out what exactly is going on here.

The hint listed (the only one) tells us to look up D. Wong, and that he is very friendly to cats, apparently. After some digging I then discovered this (written by a David Wong, so he’s probably our guy). The post mentioned is just a simple overview of the longer post listed here. Essentially, given the aspect of the hint referring to “cats”, it can be ascertained that it’s referring to the socat diffie hellman backdoor. This was a pretty big vulnerability essentially brought on by the fact that all diffie hellman exchanges were being carried out by a nonprime 1024 bit modulus (p).

Essentially, what we can do is, if we know the order of n (now p), we can do one of two things to recover c (now the Diffie-Hellman public key):

• Active Small Subgroup Attack
• Pohlig-Hellman Algorithm

Given how we don’t have access to a live server, we aren’t able to perform option 1. This leaves us with option 2: Pohlig-Hellman.

Pohlig-Hellman is essentially a way to solve the Discrete Logarithm Problem provided that we are able to factor the order of p into smaller prime factors, for which we can then compute the discrete logarithm of each of them. This vastly decreases the amount of time it takes compared to just straight up factoring p.

Afterwards, we can then use these results with the Chinese Remainder Theorem to construct the result of the discrete log of `3^x=public_key`.

…ALTHOUGH…one small mention…

What I said was all good and whatnot, but when it actually came time to running the thing, I noticed that, well, firstly…I wasn’t able to code the algorithm myself. So I just resorted to `sympy.ntheory.residue_ntheory._discrete_log_pohlig_hellman`.

But I also noticed that, once pohlig hellman completed, our order returned a discrete logarithm result that was valid, but was not the flag. It took me a long while to fix this, however, eventually I did. All I needed to do was divide the order by a specific amount and constrain it enough so we know for certain that only the flag will be considered as a possible private key.

After that, I got the flag.

### Script

``````#!/usr/bin/python3
#define the variables
g = 3

#create imports
from tqdm import trange
from Crypto.Util.number import long_to_bytes
from sympy.ntheory import pollard_pm1
from sympy.ntheory.residue_ntheory import _discrete_log_pohlig_hellman, _discrete_log_pollard_rho

#sympy already has an implementation of the Pollard p-1 algorithm
#i didn't need to code it from scratch with my Very_Smooth writeup. lol.
#so we first need to extract p_1 and p_2 from p to get the smooth factors
print("Gathering p_1 using Pollard p-1...")
p_1 = pollard_pm1(p, 0xffff, 2)
if p_1 == None:
print("p_1 was none (Pollard p-1 failure?)")
exit()

p_2 = p//p_1
if p_1*p_2 != p:
print(f"{p_1*p_2} != {p}") #sanity check
exit()

#phi(p) = order
order = (p_1-1)*(p_2-1)

#perform Pohlig-Hellman against the public key to obtain X, using the group order
#I used a for-loop to divide the order by larger amounts in order to decrease the possible private key size
#this was because in my original testing, pohlig hellman DID return a valid result, but it was too large to be the flag
#it was around the third iteration where the flag was returned to me.
#picoCTF{cf58a7b8}
print("Performing Pohlig-Hellman...")
for i in trange(10):
try:
x = _discrete_log_pohlig_hellman(p, public_key, g, order//(i+1))
except KeyboardInterrupt:
continue
if pow(g, x, p) != public_key:
print("Pohlig-Hellman failure") #another sanity check
exit()

print(long_to_bytes(x))``````