You think you found your soulmate, but you lost the train ticket that your love gave you to meet again. The only thing that you remember is the coordinates of the departing station and your love disappearing into the steam of the train.
Challenge Analysish3
This challenge uses Elliptic Curves to calculate the coordinates of the train stations where we’ll look for our “love”. First of all, the curve and generator are set up with the following parameters, none of which seem to have an odd or vulnerable length:
class TrainRoute: def __init__(self, a=None): self.p = 17101937747109687265202713197737423 self.Gx = 3543321030468950376213178213609418 self.Gy = 14807290861072031659976937040569354 self.ec_order = 17101937747109687496599931614463506 burp self.E = ecc.CurveFp(self.p, 2, 3) self.G = ecc.Point(self.E, self.Gx, self.Gy, self.ec_order)To calculate the private key, a random 32-bit number is used as a base seed, which will go through a series of “rotations” until it meets certain conditions to end up being used as a private key. “a” is that random number.
a, x = divmod(a, 30268) a, y = divmod(a, 30306) a, z = divmod(a, 30322) self.seed = int(x) + 1, int(y) + 1, int(z) + 1
def rotate(self): x, y, z = self.seed x = (171 * x) % 30269 y = (172 * y) % 30307 z = (170 * z) % 30323 self.seed = x, y, z
def goToNextStation(self): while True: self.rotate() x, y, z = self.seed
if(isPrime(x) and isPrime(y) and isPrime(z)): d = x * y * z new_point = d * self.G return int(new_point.x()), int(new_point.y())As you can see, the seed is divided by 30268, the result is divided by 30306 and the next one 30322, and we keep the remainder of each operation (x,y,z). The goToNextStation function is the one that computes the private key and the next point (public key). For it to be considered a valid key, the seed (x,y,z) will be rotated until all three are primes. We can see it’s using a 5-digit PRNG (Wichmann-Hill algorithm). Therefore, these numbers could become predictable and could turn out to be factorable (product of 3 5-digit primes).
We are given the first coordinates and we have to predict the next ones, we have 6 tries.
def main(s): route = TrainRoute(getTicketNumber())
station_coords = route.goToNextStation() sendMessage( s, f'The coordinates of the departing station were: {station_coords}\n')
destination_coords = route.goToNextStation() sendMessage(s, 'Your lover has arrived to the destination. Hurry up!\n') pegions_left = 6 sendMessage( s, f'Luckily you have {pegions_left} mechapegions in your pockets.\n') sendMessage( s, 'Use them to find out if your lover is at the destination you think.\n')Attackh3
As we observed, is not excessively large (factor of 3 5-digit primes), where , so in the worst-case scenario , which leaves us with , quite manageable computationally. We also know the upper bound , which is an important piece of info to solve the discrete algorithm problem. The path to take seems to be the following:
- Calculate by solving the ECDLP (Discrete Logarithm Problem) using the Baby-Step Giant-Step attack.
- Factor into its three prime factors.
- Predict the next PRNG state.
- We’ve got 6 chances because even if we know the 3 factors, we don’t know the order they were used in, giving us 6 possible permutations.
Writeuph3
We’re going to follow the steps we discussed before. First, let’s define the curve.
import mathfrom itertools import permutationsfrom ecdsa import ellipticcurve as eccfrom Crypto.Util.number import isPrime
# Elliptic curvep = 17101937747109687265202713197737423Gx = 3543321030468950376213178213609418Gy = 14807290861072031659976937040569354ec_order = 17101937747109687496599931614463506E = ecc.CurveFp(p, 2, 3)G = ecc.Point(E, Gx, Gy, ec_order)To find the factors of we’ll need the list of primes smaller than 30323 because that’s the largest modulus, so none of the factors will be larger than that.
def primos(n): is_prime = [True] * (n + 1) is_prime[0] = is_prime[1] = False for i in range(2, int(math.sqrt(n)) + 1): if is_prime[i]: for j in range(i * i, n + 1, i): is_prime[j] = False return [i for i in range(2, n + 1) if is_prime[i]]
primes_list = primos(30323)We’ll also need the PRNG code to simulate the state and predict the coordinates, so we’ll just replicate the exact logic from the challenge.
def rotate(seed): x, y, z = seed x = (171 * x) % 30269 y = (172 * y) % 30307 z = (170 * z) % 30323 return (x, y, z)
def next_station(state): while True: state = rotate(state) x, y, z = state if isPrime(x) and isPrime(y) and isPrime(z): d = x * y * z P = d * G return (int(P.x()), int(P.y()))Now, we need the code for the attack we’re going to use, Baby-Step Giant-Step:
- Baby Steps: Precalculate and store for
- Giant Steps: Calculate for
- Find match: When we find that , then
# Baby-step giant-step algorithm to solve the discrete logarithmdef BSGS(P, G, M): # Point, Curve, largest possible d m = int(math.isqrt(M)) + 1 print(f"[+] Using m = {m}")
# Precalculate m*G mG = m * G print("[+] Computed mG")
# Baby steps: store points j*G for j=1..m baby = {} current = G for j in range(1, m + 1): if j % 100000 == 0: print(f"[+] Baby step {j}/{m}") x = int(current.x()) y = int(current.y()) baby[(x, y)] = j current = current + G print("[+] Baby steps completed")
# Giant steps: look for P - i*m*G in baby steps current = P minus_mG = -mG for i in range(0, m + 1): if i % 100000 == 0: print(f"[+] Giant step {i}/{m}") if current is None: d = i * m if 1 <= d <= M: return d else: x = int(current.x()) y = int(current.y()) if (x, y) in baby: j = baby[(x, y)] d = i * m + j if 1 <= d <= M: return d current = current + minus_mG return NoneWe create the point to calculate such that using the attack above, and finally, we factor into the three prime numbers.
# Calculate d1 for (first station)M = 30268 * 30306 * 30322 # largest possible dd1 = solve_dl(P1, G, M)
### Factor d1 into three primesfactors = []temp = d1for p in primes_list: while temp % p == 0: factors.append(p) temp //= p if temp == 1: breakLastly, with the three primes, we’ll be able to predict the next point, which will be the solution, keeping in mind that there are 6 possible combinations (permutation of 3) and we just try which one of the 6 combinations solves the challenge.
# Generate permutations of the factorscandidates = list(permutations(factors))next_points = []for cand in candidates: next_point = next_station(cand) next_points.append(next_point)Outputh3
[+] Enter x1: 3428864647596822132441287270704861[+] Enter y1: 14285532721947366128755890796325284[+] Solving discrete log for d1...[+] Using m = 5273939[+] Computed mG[+] Baby step 100000/5273939[+] Baby step 200000/5273939...[+] Baby step 5100000/5273939[+] Baby step 5200000/5273939[+] Baby steps completed[+] Giant step 0/5273939[+] Found d1 = 5699466221[+] Factors: [239, 2473, 9643][+] Computed candidate points for the next station[(305954268276492469392172413348450, 12919694927511434464549567069038228), (10086275924322886605222214856022999, 15423087555193874630114303275551577), (10855500825943079397230099366895337, 1740259238751209310074858782631799), (9827528538597361274655184367749156, 2144401998434754452686073764402848), (10096051336583244131022850246433104, 14404735154846391684833195615715567), (15495654979591332822833437596261090, 17087645792430133778320442371022223)]