1. 程式人生 > 實用技巧 >攻防世界-crypto-Normal_RSA(openssl和rsatool工具解密RSA)

攻防世界-crypto-Normal_RSA(openssl和rsatool工具解密RSA)

進入題目後下載附件,發現是2個檔案,flag.enc和pubkey.pem。猜測分別為加密後的flag和RSA公鑰。

其中,pubkey.pem檔案內容如下:

-----BEGIN PUBLIC KEY-----
MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhAMJjauXD2OQ/+5erCQKPGqxsC/bNPXDr
yigb/+l/vjDdAgMBAAE=
-----END PUBLIC KEY-----

要想解密flag,必須使用私鑰。

第1步,使用openssl從pubkey.pem中提取出e和modulus(即大素數)的值。

rsa指令說明:

root@kali:~# openssl rsa -help
Usage: rsa [options]
Valid options are:
 
-help Display this summary -inform format Input format, one of DER PEM -outform format Output format, one of DER PEM PVK -in val Input file -out outfile Output file -pubin Expect a public key in input file //指定輸入檔案是公鑰 -pubout Output a public key
-passout val Output file pass phrase source -passin val Input file pass phrase source -RSAPublicKey_in Input is an RSAPublicKey -RSAPublicKey_out Output is an RSAPublicKey -noout Don't print key out -text Print the key in text //以明文形式輸出各個引數值 -modulus Print the RSA key modulus //
輸出模數值 -check Verify key consistency -* Any supported cipher -pvk-strong Enable 'Strong' PVK encoding level (default) -pvk-weak Enable 'Weak' PVK encoding level -pvk-none Don't enforce PVK encoding -engine val Use engine, possibly a hardware device

命令:openssl rsa -pubin -text -modulus -in pubkey.pem

第2步,進位制轉換,將modulus從16進位制轉換為十進位制。

線上進位制轉換工具:https://tool.lu/hexconvert/

Modulus(十六進位制)=C2636AE5C3D8E43FFB97AB09028F1AAC6C0BF6CD3D70EBCA281BFFE97FBE30DD

Modulus(十進位制)=87924348264132406875276140514499937145050893665602592992418171647042491658461

第3步,大數分解找pq。 線上大數分解工具:http://www.factordb.com/

所以現在知道了

p=275127860351348928173285174381581152299
q=319576316814478949870590164193048041239
e=65537

第4步,使用rsatool工具生成私鑰。

安裝:

git clone https://github.com/ius/rsatool.git
cd rsatool-master/  //進入這個目錄
python setup.py install

如果安裝出現問題,請參考:https://blog.csdn.net/jcbx_/article/details/97250664

命令:python rsatool.py -o prikey.pem -e 65537 -p 275127860351348928173285174381581152299 -q 319576316814478949870590164193048041239

第5步,使用私鑰解密flag。

命令:openssl rsautl -decrypt -in flag.enc -inkey prikey.pem

參考:

https://www.jianshu.com/p/830776f8a286

https://www.cnblogs.com/gordon0918/p/5363466.html