MCPcopy
hub / github.com/RsaCtfTool/RsaCtfTool

github.com/RsaCtfTool/RsaCtfTool @main sqlite

repository ↗ · DeepWiki ↗
791 symbols 2,193 edges 98 files 225 documented · 28%
README

RsaCtfTool

Test Ruff CodeQL

GitHub issues GitHub forks GitHub stars GitHub license GitHub contributors

RSA multi-attack tool that decrypts data from weak public keys and recovers private keys.

Overview

This tool is a utility designed to decrypt data from weak public keys and attempt to recover the corresponding private key. It offers a comprehensive range of attack options for cracking RSA encryption.

RSA security relies on the complexity of integer factorization. This project combines multiple factorization algorithms to enhance decryption capabilities.

Note: This tool is primarily intended for educational purposes. Not every key can be broken in a reasonable timeframe. The tool only supports RSA textbook semiprime composite modulus (not multiprimes).

For advanced factorization, consider msieve, yafu, or cado-nfs.

Usage

RsaCtfTool --publickey key.pub --private                   # Recover private key
RsaCtfTool --publickey key.pub --decryptfile ciphertext    # Decrypt file
RsaCtfTool --publickey key.pub --attack wiener             # Use specific attack

For complete usage, run: RsaCtfTool --help

Installation

Prerequisites

  • Python 3.9+
  • SageMath (optional but recommended)

Virtual Environment (Recommended)

python3 -m venv venv
source venv/bin/activate
pip install -e .

Docker

docker build -t rsactftool/rsactftool .
docker run -it --rm -v $PWD:/data rsactftool/rsactftool <arguments>

Attacks

Non-Factorization Attacks

Integer Factorization Methods

Method Description
Fermat Close p and q
Pollard Rho General factorization
Elliptic Curve (ECM) Smooth numbers
Pollard p-1 Smooth numbers
Williams p+1 Smooth numbers
ROCA Vulnerable key generation
SQUFOF Square forms
Quadratic Sieve General factorization
Dixon Random squares
Factordb Online factorization database
Common factor attacks Keys sharing factors
GCD attacks Mersenne, Primorial, Fibonacci, etc.

CTF-Specific Methods

Examples

Recover Private Key

RsaCtfTool --publickey key.pub --private

Decrypt a File

RsaCtfTool --publickey key.pub --decryptfile ciphertext

Attack Multiple Keys

RsaCtfTool --publickey "*.pub" --private

Create Public Key from n and e

RsaCtfTool --createpub -n 7828374823761928712873... -e 65537

Dump Key Parameters

RsaCtfTool --dumpkey --key key.pub
RsaCtfTool --dumpkey --ext --key key.pub  # Include CRT parameters

Factor with ECM

RsaCtfTool --publickey key.pub --ecmdigits 25 --private

Use Specific Attack

RsaCtfTool --publickey key.pub --attack wiener --private
RsaCtfTool --publickey key.pub --attack factordb --private

Send Results to Factordb

RsaCtfTool --publickey "*.pub" --private --sendtofdb

Check for ROCA Vulnerability

RsaCtfTool --isroca --publickey "examples/*.pub"

Convert SSH Key to PEM

RsaCtfTool --convert_idrsa_pub --publickey ~/.ssh/id_rsa.pub

For more examples, run pytest tests/ --collect-only to see available tests.

Testing

Tests use pytest and are located in tests/.

Running Tests

pytest tests/                 # Run all tests
pytest tests/ -m "not slow"   # Skip slow tests
pytest tests/ -v              # Verbose mode
pytest tests/ -k "fermat"     # Run tests matching "fermat"

Test Markers

  • @pytest.mark.slow - Slow factorization tests
  • @pytest.mark.network - Tests requiring network (Factordb)
  • @pytest.mark.attack - Attack integration tests

Test Files

File Description
test_number_theory.py Number theory functions (gcd, is_prime, phi, etc.)
test_algos.py Factorization algorithms (fermat, brent, pollard_rho, etc.)
test_keys_wrapper.py PublicKey/PrivateKey classes
test_utils.py Utility functions
test_pubkey_decode.py RSA public key decoding
test_attacks.py Attack integration tests
test_regression.py Bug fix regression tests
test_exceptions.py Custom exceptions
conftest.py pytest configuration and fixtures

Contributing

Please read CONTRIBUTING.md for guidelines and CODE_OF_CONDUCT.md before contributing.

License

The original project was released under GPLv3. This code has been relicensed under the MIT License.

Thanks

Core symbols most depended-on inside this repo

create_private_key_from_pqe
called by 17
src/RsaCtfTool/attacks/abstract_attack.py
get_numeric_value
called by 11
src/RsaCtfTool/lib/utils.py
binary_search
called by 9
src/RsaCtfTool/lib/utils.py
create_private_key
called by 9
src/RsaCtfTool/attacks/abstract_attack.py
legendre
called by 8
src/RsaCtfTool/lib/number_theory.py
generate_keys_from_p_q_e_n
called by 8
src/RsaCtfTool/lib/keys_wrapper.py
_build_qs_factor_base
called by 8
src/RsaCtfTool/lib/algos.py
decrypt
called by 7
src/RsaCtfTool/lib/keys_wrapper.py

Shape

Method 442
Class 177
Function 172

Languages

Python100%

Modules by API surface

tests/test_number_theory.py121 symbols
tests/test_algos.py86 symbols
src/RsaCtfTool/lib/number_theory.py64 symbols
tests/test_attacks.py49 symbols
src/RsaCtfTool/lib/algos.py37 symbols
src/RsaCtfTool/main.py23 symbols
tests/test_keys_wrapper.py21 symbols
src/RsaCtfTool/lib/utils.py21 symbols
tests/test_utils.py20 symbols
src/RsaCtfTool/lib/rsa_attack.py16 symbols
src/RsaCtfTool/lib/keys_wrapper.py16 symbols
tests/conftest.py11 symbols

Dependencies from manifests, versioned

bitarray
cryptography39.0.1 · 1×
factordb-pycli
gmpy22.2.1 · 1×
psutil5.9.4 · 1×
pycryptodome3.10.4 · 1×
requests2.25.1 · 1×
six
tqdm
urllib31.26.5 · 1×
z3-solver

For agents

$ claude mcp add RsaCtfTool \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact