Coppersmith attack, RSA

Quick Mafs (Crypto) HTB
3 mins
609 words
Loading views

Why does maths have to be so hard? Because some dudes with “degrees” decided there have to be 4529837459872034759 different equations and 347293475923709458 different fields instead of just pure numbers.

Challenge Analysish3

First off, we’re presented with a seemingly secure RSA implementation using a 2048-bit modulus, but we need to pay attention to how the parts of our flag were encrypted.

Tip before starting: This is an advanced challenge, I would recommend doing the baby quick mafs challenge first. It’ll give us a much easier foundation to understand this one.

The provided code is the following:

from Crypto.Util.number import *
from sage.all import *
from secret import pts,p,q
e = random_prime(2^10) # this will also be a secret , don't complain
N = p*q
pts = [bytes_to_long(i) for i in pts]
cts = [pow(i,e,N) for i in pts]
hint = sum(pts) # bcuz i don't want make chall unsolvable
print(f"{N},{cts},{hint}")

We have three key points:

  1. Small exponent: The public exponent is a random 10-bit number, e<=1024e <= 1024.
  2. Flag fragmentation: It’s divided into 3 parts, m1,m2,m3m_1, m_2, m_3, which are encrypted independently.
  3. Hint: We are given the sum of the three messages, m1+m2+m3=hintm_1 + m_2 + m_3 = hint.

Theoryh3

Long story short, the Coppersmith attack allows us to find small roots of polynomial equations modulo NN. We can decrypt a message without the private key if certain conditions are met:

  • Exponent ee must be small.
  • We need partial information about the plaintext.

You can learn more about this kind of attack and some others in the following paper

In this case, the polynomial relationship is the hinthint we’re given. We can transform the encryption equations into a system of polynomial equations modulo NN.

  • m1e−c1=0m1^e - c1 = 0
  • m2e−c2=0m2^e - c2 = 0
  • (hint−m1−m2)e−c3=0(hint-m1-m2)^e - c3 = 0

This way we get a system of 3 equations with 2 unknowns. To solve it, we’ll use Gröbner Bases with SageMath.

Exploith3

Since we don’t know the exponent, we have to iterate through the prime numbers smaller than 1024 and for each candidate, we will build the polynomial and calculate its Gröbner Basis.

output.txt
from sage.all import *
from Crypto.Util.number import long_to_bytes
N = 5981664384988507891478572449251897296717727847212579781448791472718547112403550208352320926002397616312181279859738938646168022481824206589739320298482728968548378237391009138243024910596491172979923991673446034011260330224409794208875199561844435663744993504673450898288161482849187018770655419007178851937895764901674192425054643548670616348302447202491340266057221307744866082461604674766259695903766772980842036324667567850124019171425634526227426965833985082234968255176231124754301435374519312001547854794352023852342682220352109083558778402466358598254431167382653831478713628185748237886560605604945010671417
cts = [4064195644006411160585797813860027634920635349984344191047587061586620848352019080467087592184982883284356841385019453458842500930190512793665886381102812026066865666098391973664302897278510995945377153937248437062600080527317980210967973971371047319247120004523147629534186514628527555180736833194525516718549330721987873868571634294877416190209288629499265010822332662061001208360467692613959936438519512705706688327846470352610192922218603268096313278741647626899523312431823527174576009143724850631439559205050395629961996905961682800070679793831568617438035643749072976096500278297683944583609092132808342160168, 3972397619896893471633226994966440180689669532336298201562465946694941720775869427764056001983618377003841446300122954561092878433908258359050016399257266833626893700179430172867058140215023211349613449750819959868861260714924524414967854467488908710563470522800186889553825417008118394349306170727982570843758792622898850338954039322560740348595654863475541846505121081201633770673996898756298398831948133434844321091554344145679504115839940880338238034227536355386474785852916335583794757849746186832609785626770517073108801492522816245458992502698143396049695921044554959802743742110180934416272358039695942552488, 956566266150449406104687131427865505474798294715598448065695308619216559681163085440476088324404921175885831054464222377255942505087330963629877648302727892001779224319839877897857215091085980519442914974498275528112936281916338633178398286676523416008365096599844169979821513770606168325175652094633129536643417367820830724397070621662683223203491074814734747601002376621653739871373924630026694962642922871008486127796621355314581093953946913681152270251669050414866366693593651789709229310574005739535880988490183275291507128529820194381392682870291338920077175831052974790596134745552552808640002791037755434586]
hint = 2674558878275613295915981392537201653631411909654166620884912623530781
# Candidates
possible_e = list(primes(2^10))
found = False
for e in possible_e:
print(f"Testing e = {e}...")
# Polynomial ring modulo N
R = Zmod(N)['m1, m2']
m1, m2 = R.gens()
m3 = hint - m1 - m2
# Setting up the equations
c1, c2, c3 = cts
f1 = m1**e - c1
f2 = m2**e - c2
f3 = m3**e - c3
# Generate the ideal and compute its Gröbner Basis
I = R.ideal([f1, f2, f3])
try:
GB = I.groebner_basis()
except Exception as err:
continue
# 4. Look for the degree 1 collapse (the exact solution)
if len(GB) == 2 and GB[0].degree() == 1 and GB[1].degree() == 1:
print(f"\nGröbner Basis found for e={e}")
# Solving for m1 and m2
sol_m1 = -GB[0].coefficient({m1:0}) / GB[0].coefficient(m1)
sol_m2 = -GB[1].coefficient({m2:0}) / GB[1].coefficient(m2)
sol_m3 = hint - sol_m1 - sol_m2
# Formatting
pt1, pt2, pt3 = long_to_bytes(int(sol_m1)), long_to_bytes(int(sol_m2)), long_to_bytes(int(sol_m3))
# Check printable chars
if all(32 <= b <= 126 for b in pt1 + pt2 + pt3):
print(f"Flag: {(pt1 + pt2 + pt3).decode()}")
found = True
break
if not found:
print("Exploit failed.")
  • The polynomial ring Zmod(N)[m1,m2]Zmod(N)[m1, m2] is the space where we’ll be working, all operations are modulo NN and the variables are m1m1 and m2m2.
  • The Ideal R.ideal([f1,f2,f3])R.ideal([f1, f2, f3]) is the set of all linear combinations of the polynomials f1,f2,f3f1, f2, f3.
  • The Collapse GB=I.groebnerbasis()GB = I.groebner_basis() is the Gröbner basis of the ideal II. If the collapse is successful, it’ll give us the variables m1m1 and m2m2.
  • Finally We solve it using some basic algebra to get the values of m1,m2m1, m2 and m3m3.