Posted onEdited onInCryptoSymbols count in article: 7.2kReading time ≈7 mins.
The problem is mainly based on the backdoor. In RSA library, it has the CVE of CVE-2017-15361.In this case, we can calculate the prime which is likely random with the equation that: $$ Prime = k \times M + (65537^{a} \mod M) $$ It means that produce the list of primes and make them to collide, which satisfy the condition, thus output the target prime.
Also,the PKI system has different standards, we make some distinctions between different standards.
In RSALib, the prime is produced by the equaction: $$ Prime = k \times M + 65537^{a} \mod M $$ which means that the $M$ is the multiplication of the generated primes. $$ M = \prod_{i=1}^{n}{P_{i}}= P_{1} \times P_{2} \times \cdots \times P_{n} $$ $M$ must be large and is around the same size of they generated. The weakness is the $k$ and $a$ are relatively small values. The entropy is also must drop.
The problem is common provided with $c$, $e$ and $n$, and just do the commandRSA_Backdoor(n). If the problems satisfy the conditions, the function will return the $p$ and the $q$.
Openssl
relate to the PKI system.
generate
In kali, you can directly input the command that openssl.
python3 rsatool.py -f DER -o key.der -p [p] -q [q]
Pem in RSA
The pem can be used in different ways in RSA.
One is to extract the $n$ and the $e$—— when you get $p$, $q$ from the $n$, we can change flag.enc to $c$ by using the function bytes_to_long().
The other way is also extract the $n$ and the $e$ ——however, we MUST construct the primes and others into the pri.pem. Using the pri.pem,and we can decrypt the flag.enc to get the message.
-----BEGIN PUBLIC KEY----- MFMwDQYJKoZIhvcNAQEBBQADQgAwPwI4EkoKC0EfUXgDvsQUYRI+j0/XPrqQxHJ+ v3CKL9WVp5SMk0Njud03to6sxuvPt93fSGHkAsW+wHsCAwEAAQ== -----END PUBLIC KEY-----
from Crypto.PublicKey import RSA from base64 import * import binascii from Crypto.Util.number import * import gmpy2 from Crypto.Cipher import PKCS1_OAEP
""" 2. public.key:rb->RSA.import_key->e,n 3. construct to privatekey 1. (n,e,d,(p),(q)) to save 2. RSA.construct() to construct 3. 2.exportKey() import 4. RSA.importKey() export 5. PKCS1_OAEP.new() -> important! 4. d by using invert(), and int(d) to get integer 5. use private key to decrypt """ withopen('public.key', 'rb') as f: pub = RSA.import_key(f.read()) e = pub.e n = pub.n # e = 65537 # n = 79832181757332818552764610761349592984614744432279135328398999801627880283610900361281249973175805069916210179560506497075132524902086881120372213626641879468491936860976686933630869673826972619938321951599146744807653301076026577949579618331502776303983485566046485431039541708467141408260220098592761245010678592347501894176269580510459729633673468068467144199744563731826362102608811033400887813754780282628099443490170016087838606998017490456601315802448567772411623826281747245660954245413781519794295336197555688543537992197142258053220453757666537840276416475602759374950715283890232230741542737319569819793988431443
# yafu-x64/factordb.com p = 3133337 q = 25478326064937419292200172136399497719081842914528228316455906211693118321971399936004729134841162974144246271486439695786036588117424611881955950996219646807378822278285638261582099108339438949573034101215141156156408742843820048066830863814362379885720395082318462850002901605689761876319151147352730090957556940842144299887394678743607766937828094478336401159449035878306853716216548374273462386508307367713112073004011383418967894930554067582453248981022011922883374442736848045920676341361871231787163441467533076890081721882179369168787287724769642665399992556052144845878600126283968890273067575342061776244939 N = (p-1) * (q-1) d = int(gmpy2.invert(e, N)) withopen('flag.enc', 'r') as f: cipher = b64decode(f.read()) # print(c) con = RSA.construct((n, e, d, p, q)) pri = PKCS1_OAEP.new(RSA.importKey(con.exportKey())) m = pri.decrypt(cipher) print(m) # b'afctf{R54_|5_$0_B0rin9}'