We are given a server that acts as an oracle doing elliptic curve scalar point multiplication (ECSPM) with a given point. Using this oracle we must solve the elliptic curve discrete logarithm problem (ECDLP) for a constant , which is our flag.
Finding the Vulnerability
Checking out the source code we are given the coefficients and , as well as the finite field the elliptic curve is defined over.
If this curve looks a little suspicious it’s because it happens to be BrainpoolP256t1, a somewhat-rigid twisted curve, which has some overlap in its coefficients:
A = 0x7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9
B = 0x26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6
Unfortunately while certainly suspicious it doesn’t seem to make the curve cryptographically insecure.
(Technically this curve has a cryptographically low cost () for a combined attack using low order points and could also probably be exploited, but there is an easier and less expensive method.)
With no inherent weakness in the curve itself we need to check for insecurities elsewhere.
Reading through the code we can find that it does the ECSPM with custom functions
add(), and that they were used in the function
Reading through we can see that the script takes a point and using the curve where computes and returns . Checking the server’s math functions we can see that mathematically they work, but they don’t use the point in their calculations! This is the standard for short Weierstrass curves like the one the server is using, however since the server also neglets to verify or the server is vulnerable to an invalid-curve attack which we can use to recover .
Since the server doesn’t use in its calculations we can define a new curve using a coefficient instead of where in our short Weierstrass equation from earlier. Let’s call this new curve .
Choosing such that this new curve’s order has a small prime divisor will ensure there exists a subgroup of of small order . We can then send the server a point of this subgroup, and since neither nor is used by the server the ECSPM will actually occur over our new curve . As the discrete logarithm is now in the subgroup of generated by , the result will only have possible values! Since there is such a small keyspace and the multiplicative group’s order is a smooth integer, we can then easily solve the ECDLP using the Pohlig-Hellman algorithm. This will leak . Repeating this for new curves and new values will create a system of linear congruences which can be solved with the Chinese Remainder Theorem, thus yielding the final value of .
Implementing and Executing the Vulnerability
Now comes the fun part, writing all this up in a Sage Script. Sage is crucial here as it will do all of the ECC work for us, as well as letting us communicate with the server and do logic in Python. Technically a script isn’t necessary as we could do everything by hand in the Sage terminal, however a script saves time and lowers the likelihood of error. This is especially useful since we will need enough linear congruences to find the correct value of .
##The Code: Note: The server seems to not like points that are generated from primes that are too small or result in points that are too large. As far as I can tell this is a bug on their end.
Running this with a few values I arrived at:
We can now solve for in sage:
Giving our final value for :
Encoding this integer as hex and decoding that hex as ascii reverses the encoding shown in the redacted sever’s code:
We arrive at our final flag: