skyf0l/RsaCracker
GitHub: skyf0l/RsaCracker
专为 CTF 与密码研究设计的 RSA 破解工具,支持多格式密钥与多种数学攻击以恢复私钥和解密密文。
Stars: 150 | Forks: 7
# RSA Cracker
[](https://github.com/skyf0l/RsaCracker/actions/workflows/ci.yml)
[](https://crates.io/crates/rsacracker)
[](https://hub.docker.com/r/skyf0l/rsacracker)
[](https://codecov.io/gh/skyf0l/rsacracker)
强大的 RSA 破解工具,适用于 CTF。支持 RSA、X509、OPENSSH、PKCS#12、PKCS#7 以及 PEM 和 DER 格式的 CSR。
RsaCracker 提供一个简洁、可扩展的接口,用于分析和恢复 RSA 私钥,并使用大量针对性攻击和启发式算法来解密消息。
## TLDR - 快速开始
- 安装:`cargo install rsacracker`
- 破解公钥:`rsacracker --key public.pem --dump`
- 解密密文:`rsacracker --key public.pem -c 0xdeadbeef`
- 从文件解密:`rsacracker --key public.pem -f 0xdeadbeef -o result.bin`
注意:在 Windows 上构建需要使用 [MSYS2](https://www.msys2.org/)。这是由于 [rug](https://crates.io/crates/rug) 依赖项所致。更多信息请参见 [building-on-windows](https://gitlab.com/tspiteri/gmp-mpfr-sys#building-on-windows)。
## 用法
```
Powerful RSA cracker for CTFs. Supports RSA, X509, OPENSSH, PKCS#12, PKCS#7, and CSR in PEM and DER formats.
Usage: rsacracker [OPTIONS]
Options:
-r, --raw
Retrieve values from raw file
-c, --cipher Cipher: the message to uncipher. Can be specified multiple times for multi-key attacks
-f, --cipherfile Cipher file: the file to uncipher. Can be specified multiple times for multi-key attacks
-o, --outfile Write unciphered data to a file. If many unciphered data are found, they will be written to files suffixed with _1, _2, ...
-n Modulus. Can be specified multiple times for multi-key attacks
-e Public exponent. Default: 65537. Can be specified multiple times for multi-key attacks
-p Prime number p (supports wildcards: 0xDEADBEEF????, 10737418??, 0x...C0FFEE, 0xDEADBEEF..., etc.)
-q Prime number q (supports wildcards: 0x????C0FFEE, ??741827, 0x...C0FFEE, 0xDEADBEEF..., etc.)
-d Private exponent
--phi Phi or Euler's totient function of n. (p-1)(q-1)
--dp dP or dmp1 CRT exponent. (d mod p-1)
--dq dQ or dmq1 CRT exponent. (d mod q-1)
--qinv qInv or iqmp CRT coefficient. (q^-1 mod p)
--pinv pInv or ipmq CRT coefficient. (p^-1 mod q)
--sum-pq The sum of the two primes p and q
--diff-pq The difference of the two primes p and q
--dlog Discrete logarithm attack. When c and e are swapped in the RSA encryption formula. (e^c mod n)
-k, --key Public or private key file(s). (RSA, X509, OPENSSH, PKCS#12, PKCS#7, CSR in PEM and DER formats.) Can be specified multiple times for multi-key attacks
--password Private key password/passphrase if encrypted
--public Print the public key in PEM format
--private Print the private key in PEM format
--addpassword Add a password/passphrase to the private key
--showinputs Print all the input parameters
--dump Print the private RSA key variables n, e, p, q and d
--dumpext Print the extended RSA key variables n, e, p, q, d, dP, dQ, pInv and qInv
--factors Print all factors of n
-t, --threads Number of threads to use. Default: number of CPUs
-a, --attack Specify attacks to run. Default: all. (e.g. --attacks ecm,wiener,sparse)
--exclude Specify attacks to exclude. Default: none. (e.g. --exclude ecm,wiener,sparse)
--list List all available attacks
-h, --help Print help
-V, --version Print version
```
你也可以使用转储内容作为输入:
```
$ rsacracker [OPTIONS] < challenge.txt
[RESULTS]
$ cat challenge.txt | rsacracker [OPTIONS]
[RESULTS]
$ cat challenge.txt
c: 7839407718[...]0577093673
n = 9359619564[...]3745124619
e= 1595235523[...]6275096193
```
## 示例
### 从公钥解密密条并写入文件
```
rsacracker --key public.pem -c 0xdeadbeef -o result.txt
```
### 从 n 和 e 解密密条
```
rsacracker -c 0xdeadbeef -n 123...789 -e 65537
```
### 使用其他已知值从 n、e 解密密条
```
rsacracker -c 0xdeadbeef -n 123...789 -e 65537 --phi 123 --dp 123 --dq 123 --qinv 123 --pinv 123
```
### 从公钥解密密文件
```
rsacracker --key public.pem -f secret.txt.enc
```
### 从部分素数信息恢复私钥
当你已知某些位/数字(MSB 或 LSB)时,可以在二进制、八进制、十进制或十六进制表示中使用通配符(`?`):
```
# 二进制:MSB 已知(尾部通配符)
rsacracker -n 123...789 -p 0b1101010???
# 八进制:LSB 已知(首部通配符)
rsacracker -n 123...789 -p 0o???777
# 十进制:MSB 已知(尾部通配符)
rsacracker -n 2305843027467304993 -p 10737418??
# 十进制:LSB 已知(首部通配符)
rsacracker -n 123...789 -p ??741827
# 十六进制:MSB 已知(尾部通配符)
rsacracker -n 123...789 -p 0xDEADBEEF????
# 十六进制:LSB 已知(首部通配符)
rsacracker -n 123...789 -p 0x????C0FFEE
```
每个 `?` 代表指定进制中的一位数字。
你也可以使用省略号(`...`)在未知长度时根据模数大小自动推断:
```
# 十六进制:LSB 已知(MSB 长度推断)
rsacracker -n 123...789 -p 0x...C0FFEE
# 十六进制:MSB 已知(LSB 长度推断)
rsacracker -n 123...789 -p 0xDEADBEEF...
```
### 使用参数运行特定攻击
```
rsacracker --attack known_phi -n 123...789 -e 65537 --phi 0xdeadbeef
```
### 从公钥生成私钥
```
rsacracker --key public.pem --private
```
### 从 e 和 n 生成公钥
```
rsacracker -e 65537 -n 0xdeadbeef --public
```
### 转储私钥信息
```
rsacracker --key private.pem --dump
$ rsacracker --key private.pem --dumpext
```
### 移除私钥密码
```
rsacracker --key private.pem --password R54Cr4ck3R --private
```
### 为私钥添加密码
```
rsacracker --key private.pem --addpassword R54Cr4ck3R --private
```
### 显示 n 的所有因子
```
rsacracker -n 123...789 --factors
```
### 运行离散对数攻击:当加密公式中 c 和 e 互换时(e^c mod n)
```
rsacracker --key public.pem -c 0xdeadbeef --dlog
```
### 多密钥攻击
RsaCracker 支持需要多个 RSA 密钥的攻击。你可以通过多种方式提供多个密钥:
**方法 1:通过带索引符号的原始文件**
```
# 创建包含多个密钥的文件(multikeys.txt)
# 公因子攻击 - 当两个密钥共享一个质因数
n1 = 166162630914502531310583922419891282066165820974633135604215723500594369488785155668770814942798477925368262423257419073645831352835527789101770856835355683177962166057699839663569971312562086050531058716298108813024798653596850452010850976880829077654912494652271256054564920903881745267063001869548202922099
e1 = 65537
c1 = 123
n2 = 148455898656074447797752378503069279028991863906908832057033693077681993859745690328279867444062926638337203683279627319119630089306918893030699950731547426066997479055479829293964341682216330844958953722765260947532634616964944677851975839768164255655099799121904635086103339949975609477039895462111764318783
e2 = 65537
# 运行攻击
rsacracker --raw multikeys.txt
```
**方法 2:通过多个 --key 参数**
```
# 通过 CLI 直接提供多个密钥文件
rsacracker --key key1.pem --key key2.pem --dump
# 适用于任意组合密钥文件
rsacracker --key public1.pem --key public2.pem --key public3.pem
```
**方法 3:通过多个 -n 参数**
```
# 直接为公因子攻击提供多个模数
rsacracker -n 166209509310787961... -n 137801924148643799... --dump
# 可与其他参数结合
rsacracker -n 123456... -n 789012... -e 65537
```
**方法 4:通过多个 -c 和 -e 参数**
```
# 多个密文和指数的公模数攻击
rsacracker -n 166270918338126577... -e 65537 -e 65539 -c 136917880321258914... -c 46689866063983112...
# Hastad 广播攻击(多个 n、e 和 c)
rsacracker -n 123... -n 456... -n 789... -e 3 -c 100... -c 200... -c 300...
```
## Docker
从 Docker Hub 获取:
```
docker pull skyf0l/rsacracker
docker run -it --rm -v $PWD:/data skyf0l/rsacracker [args]
```
或自行构建:
```
DOCKER_BUILDKIT=1 docker build . --file Dockerfile -t rsacracker
docker run -it --rm -v $PWD:/data rsacracker [args]
```
## 许可证
根据以下之一授权:
- Apache License, Version 2.0
([LICENSE-APACHE](LICENSE-APACHE) 或 )
- MIT 许可证
([LICENSE-MIT](LICENSE-MIT) 或 )
任选其一。
## 贡献
除非你明确声明否则,任何有意提交
至本工作的贡献,在 Apache-2.0 许可定义下,将被
双重授权,不附加额外条款或条件。
标签:CSR, CTF工具, DER, DOS头擦除, OpenSSH, PEM, PKCS#12, PKCS#7, RSA, Rust, X.509, 公钥密码, 加密分析, 加密协议, 可视化界面, 密文解密, 密码学工具, 密码破解, 密钥分析, 密钥恢复, 密钥提取, 漏洞搜索, 私钥恢复, 网络流量审计, 证书解析, 请求拦截, 通知系统