Arab Security Cyber Wargames qualification round started this
year from 5th till 6th of August and as
always the top 10 teams will be qualified to the finals. In this
write-up, we’re going to discuss and solve the 5 crypto
challenges in the competition . The challenges was easy-medium
ones, but full of tricks for those who aren’t up-todate with the
encryption algorithms and techniques. However, I’ll show you my
thought process and how to divide the challenge into
sub-challenges in order to find a correct solution.
Challenge
Difficulty
Points
RSA in the wild
Warmup
100
OSP
Easy
300
Teaser
Medium
600
FHE
Medium
600
Vault
Hard
900
RSA in the wild
Description:
Our security team found encrypted messages between members of
an active threat actor in the wild.
This is a warmup straightforward challenge, so let’s have a look
at the given files.
The output file contains an array of kinda users messages with
some keys. Let’s look at the script:
The script has one class called
WildRSA
and it first generates a random 256-bit even integer seed. After
that, it instantiate an object from the class with the generated
seed as input, then it iterate over the APT array and encrypt
the messages inside.
Solution
Obviousley, the class generates a strong 512-bit prime in the
constructor and there’re two functions
pub
and
__get_q. The trick here is in the
pub
method. It generates only one random prime from
__get_q
and the other prime is still the same for each message.
To visualiza, we have for each message \(N\) and \(E\):
\[N_i = PQ_i\]
This is a basic algebra problem to get \(P\) we have to find the
GCD between each pair of the \(N_i\).
\[P = gcd(N_1, N_2, ...)\]
Finally, we can get the private key of the RSA.
Flag:
ASCWG{7h3_c0mM0n_9re4t_P0W3r_0f_6r0k3N_R$A}
OSP
Description:
Have you heard of OTP? This is OSP (One Secret Password).
This challenge was an easy one given the
OSP.py
script and
output.txt
file.
Basically, the script does some OTP on the flag with a randomly
generated secret bytes and an odd 256-bit prime using the
following equation \(C_i = F_ip + k\) where \(F_i\) are the flag
bytes.
And the other file contains the cipher bytes:
Solution
Obviously, you can notice that \(k\) is in the byte range \([0,
255]\) and \(F_{0...5}\) all are known bytes
ASCWG{. So, we can rearrange the equation above in order to get the
prime number generated during the encryption.
\[p = \frac{C_i - k}{F_0}\]
We’ll calculate this by bruteforcing the \(k\) and check the
result is it a prime or not, then we can do a bruteforce on the
flag bytes itself.
Flag:
ASCWG{Wh47_1f_17's_N07_@_Pr1M3!-f0ffa3657e}
Teaser
Description:
The APT hasn’t stopped their operations yet and decided to
change the technique they use.
Given 2 files a python script
Teaser.py
and text
output.txt
file. The script contains 2 layers of encryption RSA with some
modular polynomial equations.
And the text files contains some values
After analyzing the script, it computes the modulus \(N=pq\),
then generates three 64-bit random numbers
a,
b,
x.
Aftet that, it calculates \(s = x( ax + b) + 1\) and \(inf =
-x(ax + b)\) and they’re additive inverse of each other except
the 1.
Then here comes to the first layer with the following
Second layer calculates two modular polynomial linear eqautions
\[Q_1 = xaC_1 + bC_2 + ab \pmod{N}\] \[Q_2 = aC_2 - xbC_1 + ab
\pmod{N}\]
Finally, it scrambles the value of x in some sort of another
5th degree polynomial equation \(hint = x^5 + abx^4 +
bx^3 - ab^2x^2 + xba^2 - b^2a^2\)
Solution
We can start from the end, so the goal first is to calculate the
unknown variable \(x\), then try to solve the two modular
equations to find \(C_1\) and \(C_2\) after that we can multiply
each other to get the flag.
We’ll use SageMath in order to recover the encrypted flag.
Then we’ll construct
PolynomialRing
with a field module \(N\), then we can use
Ideals
and
Groebner Basis.
And we know that they’re basis so the result has to equal zero
\(C_1 + 12960... \pmod{N}= 0\) and \(C_2 + 16301... \pmod{N}=
0\).
Note that: There’re multiple ways to solve multivariate
polynomials I’ll leave it for you to discover, but most of them
are limited to for example the degree. However, Ideals and
Groebner Basis are general ways to find roots of multivairate
non-linear modular polynomials under certain conditions. You can
learn more from this
book.
Flag:
ASCWG{8r4in_T3s$s1n9_7h3_Ba51s_0f_9r036n3r}
Fully Heuristic Encryption
Description:
We’re looking for briliant mathematicians to decrypt this
cipher. If you’re not the one, go away!
This one is a bit tricky and it’s all about basic homomorphic
encryption. Basically, you can look at the homomorphic
encryption as an extension of public-key cryptography. Given the
FHE.py
script and a text
output.txt
file.
The script has two functions
gen
that retunrs a public key with a secret from and
encrypt
that does some sort of encryption on the flag bits with the
generated public key.
The first function
gen,
it generates a 256-bit prime number with the last 25 bits are
random \(p = 2^{255} + 2^{127} + rand(2^{25})\). After that, it
generates two random numbers \(q\) and \(r\) 25 times, then
computes the public-key using the following equation \(pub_i =
pq_i + 2r_i\).
The
encrypt
functions takes the public key and the flag bits, then it get 5
random values from the public-key as
ids
with a random 128-bit
r.
After that, it encrypts the each bit of the flag with the
following formula \(C_i = 2r + Flag_{bit} + \sum{ids}\).
Solution
The attack here is pretty simple just you need to focus on what
is given by looking at the equtions used. First you can notice
that the last few bits of prime are low so we can bruteforce
those bits, then after finding \(p\) and looking at the \(ids\)
it’s just some values of the public-key \(\sum{ids} = \sum{pq_i
+ 2r_i}\).
Now, here comes the interesting question:
How can we get rid off the values \(2r + \sum{2r_i + pq_i}\)
to recover the flag bits?
In order to make the above value equals to zero, the property of
module somes into place by first take it module the recovered
prime and this will get rid off all values multiplied by \(p\),
then the remaining value is equal to some even number added to
the flag bits.
After hunting down the APT over the darkweb we’ve finally
found their encrypted vault.
The last challenge was about
ECC
and especially on
SECP256k1
bitcoin curve, but with some tweeks on the curve params. Given
vault.py
script and
vault.enc
an encrypted file.
Looking at the script, it has a class called
ECC
with some functions operates on the curve adding points or
multiplying constants. After that, it creates an instance from
the class and a given generator point \(G\), then encrypts the
vault.json
file with
encrypt
function in the class.
The class
ECC
has the following functions:
add:
Add 2 points on the defined curve.
mul:
Add a point to itself
n
times.
valid: Validate a point on the curve.
inv_mod_p: Find modular inverse of a coordinates with the curve prime.
encrypt: Encrypt a message with a secret point \(C = privG\) using
AES-CBC mode.
The curve is using the following parameters:
Solution
First thing you need to focus while working with elliptic curves
is the curve structure.
Is it secure or not with the defined parameters?
Let’s first look at the order of the curve is it secure? then we
can llok at the order of the given generator point.
Apparently, the order is factored int small primes and one of
the them is exactly the order of the generator point, so we can
assume that the given point is generated by dividing the curve
order by
12708491
then multiplied by the original generator
G = (o//12708491)*E.gens()[0]
that will produce a point with order or
12708491.
Notice that, any generated points from
G will
have the same order of
12708491, so we can brute force the point from the range
\([1,12708491]\), try to decrypt the message, and check if it
contains the flag or not.
In this writeup, I’ve conducted a detailed analysis of all the
crypto challenges I’ve created in the competition as well as, my
thought process of how I can look over and examine the problem.
I hope you’ve enjoyed the competition and learned something from
the challenges that are created by me or even my teammates. For
those who are still stuck or willing to learn more about
cryptography, I’ve made a small roadmap
here
that can guide you through your journey and of course you can
hit me up any time on my socials. Finally, congrats to every
team who made it until the end and wish you all the best in the
finals.