CVE-2017-15361 n大e为65537 weak-RSA-key not-random-primes superprime

漏洞来源:p,q的随机生成使用aimodM,a是大素数,M是大合数,这时p的空间因为是由大a生成而显著减小,

a在题目中一般是65537,同时又因为M是大合数,导致展开后的式子中的kM+ai=0中的k很小,这直接导致了格攻击的可行性

k+aiM-1=0 modp


k (1 1 0
    1  x 0 1  (x是r*M
    -1
)
-t p 0 0)=(0 k 1)

还原了k,之后尝试直接分解出M,ai

一般而言,先看题目提示,后自己使用工具判断是否为ROCA

示例题目:


from Crypto.Util.number import *
import random
from sympy import prime
FLAG=b'hgame{xxxxxxxxxxxxxxxxxx}'
e=0x10001
def primorial(num):
    result = 1
    for i in range(1, num + 1):
        result *= prime(i)
    return result
M=primorial(random.choice([39,71,126]))
def gen_key():
    while True:
        k = getPrime(random.randint(20,40))
        a = getPrime(random.randint(20,60))
        p = k * M + pow(e, a, M)
        if isPrime(p):
            return p
p,q=gen_key(),gen_key()
n=p*q
m=bytes_to_long(FLAG)
enc=pow(m,e,n)
print(f'{n=}')
print(f'{enc=}')
"""
n=787190064146025392337631797277972559696758830083248285626115725258876808514690830730702705056550628756290183000265129340257928314614351263713241
enc=365164788284364079752299551355267634718233656769290285760796137651769990253028664857272749598268110892426683253579840758552222893644373690398408
"""

解题示例:

在git上找的


'''
An implementation of [1].
[1]: Matus Nemec, Marek Sys, Patr Svenda, Dusan Klinec, and Vashek Matyas. 2017. "The Return of Coppersmith's Attack: Practical Factorization of Widely Used RSA Moduli"
[2]: Alexander May. 2003. "New RSA Vulnerabilities UsingLattice Reduction Methods"
'''
import functools
import binascii
import math
# display matrix picture with 0 and X
# references: https://github.com/mimoo/RSA-and-LLL-attacks/blob/master/boneh_durfee.sage
def matrix_overview(BB):
    for ii in range(BB.dimensions()[0]):
        a = ('%02d ' % ii)
        for jj in range(BB.dimensions()[1]):
            a += ' ' if BB[ii,jj] == 0 else 'X'
        if BB.dimensions()[0] < 60:
            a += ' '
        print(a)
    print("")
def factor_expand(n):
    res = []
    for p, e in factor(n):
        for i in range(0, e):
            res += [p**(i + 1)]
    return res
def reward_at_cost(old_M, new_M, old_order, new_order):
    '''
Cost function
cf. [1] 2.7.2 Greedy heuristic
'''
    return (log(old_order, 2) - log(new_order, 2)) / (log(old_M, 2) - log(new_M, 2))
def compute_new_M_from_order(M, order_new_M):
    '''
Compute M' from M, such that ord_{M'}(65537) = order_new_M.
cf. [1] Algorithm 2.
'''
    M_ = M
    for pi in factor_expand(M):
        order_pi = Mod(65537, pi).multiplicative_order()
        if order_new_M % order_pi != 0:
            M_ = M_ // pi
    return M_
def find_new_M(M, log2N):
    '''
Find M' from M
'''
    M_ = M
    order = Mod(65537, M_).multiplicative_order()
    while True:
        L = []
        for fi in factor_expand(order):
            new_order = order // fi
            new_M = compute_new_M_from_order(M_, new_order)
            L += [(reward_at_cost(M_, new_M, order, new_order), (new_order, new_M))]
        _, (new_order, new_M) = list(reversed(sorted(L)))[0]
        if log(new_M, 2) < log2N / 4:
            break
        M_ = new_M
        order = new_order
    return M_
def coppersmith_univariate(pol, NN, XX, mm, tt, beta=1.0):
    '''
An implementation of Coppersmith's method for univariate polynomial
Note: I didn't use [1]'s definition. This implementation referenced [2].
'''
    PR.<x> = PolynomialRing(ZZ)
    polZZ = PR(pol)
    delta = polZZ.degree()
    pols = []
    for i in range(mm):
        for j in range(delta):
            pols += [(x * XX)^j * NN^(mm - i) * polZZ(x * XX)^i]
    for i in range(tt):
        pols += [(x * XX)^i * polZZ(x * XX)^mm]
    deg = delta * mm + tt
    M = Matrix(ZZ, deg)
    for i in range(deg):
        for j in range(deg):
            M[i, j] = pols[i].monomial_coefficient(x^j)
    B = M.LLL()
    f = 0
    for i in range(deg):
        f += x^i * B[0, i] / XX^i
    roots = []
    for x0, _ in f.roots():
        if x0.is_integer():
            if gcd(polZZ(ZZ(x0)), NN) >= NN^beta:
                roots += [ZZ(x0)]
    return list(set(roots))
def roca_attack(N, M_, mm, tt):
    '''
ROCA Attack
* mm and tt are tweakable parameter
cf. [1] Algorithm 1.
'''
    c_ = discrete_log(N, Mod(65537, M_))
    order_ = Mod(65537, M_).multiplicative_order()
    a_ = c_ // 2
    upper_bounds = (c_ + order_) // 2
    ZmodN = Zmod(N)
    PR.<x> = PolynomialRing(ZmodN, implementation='NTL')
    print("[+] c' = {}".format(c_))
    print("[+] Iteration range: [{}, {}]".format(a_, upper_bounds))
    while a_ <= upper_bounds:
        const = (Mod(65537, M_)^a_).lift()
        f = x + Mod(M_, N)^-1 * const
        beta = 0.5
        XX = floor(2 * N^beta / M_)
        # small_roots is useless (It can't solve this polynomial).
        # res = f.small_roots(beta=beta, X=XX)
        # Pari/GP's zncoppersmith function can solve
        res = coppersmith_univariate(f, N, XX, mm, tt, beta)
        for k_ in res:
            p = k_ * M_ + const
            if N % p == 0:
                return (p, N // p)
        a_ += 1
def main():
    '''
Test
'''
    n=787190064146025392337631797277972559696758830083248285626115725258876808514690830730702705056550628756290183000265129340257928314614351263713241
    M = 962947420735983927056946215901134429196419130606213075415963491270
    enc=365164788284364079752299551355267634718233656769290285760796137651769990253028664857272749598268110892426683253579840758552222893644373690398408
    e = 0x10001
    M_ = find_new_M(M, 512)
    print("[+] M' = {}".format(M_))
    p, q = roca_attack(n, M_, 5, 6)
    assert p * q == n
    print("[+] p, q = {}, {}".format(p, q))
if __name__ == "__main__":
    main()
'''
[+] M' = 2373273553037774377596381010280540868262890
[+] c' = 680400
[+] Iteration range: [340200, 940800]
[+] p, q = 954455861490902893457047257515590051179337979243488068132318878264162627, 824752716083066619280674937934149242011126804999047155998788143116757683
time 30m58.4s
#注意耗时久是正常的
'''