Giter Club home page Giter Club logo

ctf-rsa-tool's Introduction

Description

CTF-RSA-tool 是一款基于python以及sage的小工具,助不熟悉RSA的CTFer在CTF比赛中快速解决RSA相关的 基本题型

Requirements

  • requests
  • gmpy2
  • pycrypto
  • libnum
  • sagemath(optional)

Installation

  • 安装libnum
git clone https://github.com/hellman/libnum.git
cd libnum
python setup.py install
  • 安装gmpy2,参考:

easy_install gmpy2

如果不行,可以尝试我的安装过程:https://3summer.github.io/2018/01/24/CTF-RSA-tool-install/

  • 克隆仓库,安装依赖
git clone https://github.com/3summer/CTF-RSA-tool.git
cd CTF-RSA-tool
pip install -r "requirements.txt"
  • 安装sagemath(非必须)

安装sagemath的以支持更多的算法,提高解题成功率,嫌麻烦也可以不安装

官网:http://www.sagemath.org

我的安装过程:https://3summer.github.io/2017/12/06/sage/

Usage

查看全部参数及帮助

python solve.py -h

列举几个实用的小功能(解题的例子见下面)

  • 输入N与e创建公钥

python solve.py -g --createpub -N your_modulus -e your_public_exponent -o public.pem

  • 查看密钥文件

python solve.py -g --dumpkey --key examples/smallfraction.pub

  • 将加密文件转为十进制(方便写入文本,配合-i需要)

python solve.py -g --enc2dec examples/jarvis_oj_hardRSA/flag.enc

Examples

非 --input(文本文档自动识别攻击) 的情况下,请至少选择 --private(打印得到的私钥) 或 --decrypt(解密一个加密的文件) 或 --decrypt_int(解密一个十进制数) 中的一个,不然程序不会干什么事,具体参考example.txt

大多数情况下,只用使用 python solve.py -i rsa.txt 指定一个txt文本,txt的内容为你从题目获取的变量,如

n = **********
e = **********
c = **********

-i指定这个文本文档就行了,这样就不用用命令行去一个个指定参数,弄的终端看着很乱。 这个txt的编写规范参看examples/input_example.txt

Tips

每次使用都要找到项目目录很麻烦,可以做个符号链接,链接solve.py到bin目录下,如在我的MACos中

ln -s /Users/3summer/Documents/code/CTF-RSA-tool/solve.py /usr/local/bin/rsa_solve

之后,就能直接在终端输入rsa_solve -i rsa.txt去快速秒简单题了

How does it work

根据题目给的参数类型,自动判断应该采用哪种攻击方法,并尝试得到私钥或者明文,从而帮助CTFer快速拿到flag或解决其中的RSA考点

大体思路

  • 判断输入

首先,识别用户的输入,可以是证书 pem 文件,也可以通过命令行参数指定ne等变量的值,甚至可以通过命令行指定题目所给的txt文件并自动识别里面的变量(见examples)

  • 判断攻击方法

根据取到的参数类型及数量,选取可能成功的方法并采用一定的优先级逐个尝试。

如常见的题型:给了一个公钥和一个加密的密文,我们需要先分解大整数N,然后得到私钥再去解密。考点在于大整数分解,脚本会挨个尝试下面 已实现的攻击方法 中列举出的关于分解大整数的方法,直到分解成功。

  • 选择输出

CTFer可以通过命令行选择是输出私钥还是输出解密后的密文,还是一起输出

已实现的攻击方法

主要参考ctf-wikiRsaCtfTool及自己平时遇见的一些题型

  • 大整数分解

    • 检查过去的ctf比赛中出现的素数
    • Gimmicky Primes method
    • Wiener's attack
    • factordb在线分解N
    • Small q (q < 100,000)
    • 费马分解(p&q相近时)
    • Boneh Durfee Method (d < n^0.292)
    • Small fractions method when p/q is close to a small fraction
  • Basic Broadcast Attack

  • Known High Bits Factor Attack

  • Common factor between ciphertext and modulus attack

  • 小公钥指数攻击

  • Rabin 算法

  • 模不互素

  • 共模攻击

  • d泄露攻击

Reference

TODO

ctf-rsa-tool's People

Contributors

6u661e avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ctf-rsa-tool's Issues

invalid literal for long() with base 10: '\r\n'

python solve.py --verbose --private -N 460657813884289609896372056585544172485318117026246263899744329237492701820627219556007788200590119136173895989001382151536006853823326382892363143604314518686388786002989248800814861248595075326277099645338694977097459168530898776007293695728101976069423971696524237755227187061418202849911479124793990722597 -e 1
DEBUG: factor N: try past ctf primes
Traceback (most recent call last):
File "solve.py", line 149, in
attackobj.attack()
File "$mypath$/CTF-RSA-tool/lib/RSAutils.py", line 128, in attack
factors = factor_N.solve(self.n, self.e, self.c, self.sageworks)
File "$mypath$/CTF-RSA-tool/lib/factor_N.py", line 19, in solve
return pastctfprimes(N) or noveltyprimes(N) or wiener_attack(N, e) or factordb(N) or comfact_cn(N, c) or smallq(N) or p_q_2_close(N) or None
File "$mypath$/CTF-RSA-tool/lib/factor_N.py", line 96, in pastctfprimes
) if not x.startswith('#') and not x.startswith('\n')]
ValueError: invalid literal for long() with base 10: '\r\n'

python版本?

您好,这个是python2还是python3,能再梳理一下安装过程吗,libnum、gmpy2、sagemath这些pip install不是可以安装吗

UnicodeDecodeError: 'ascii' codec can't decode byte 0xde in position 1: ordinal not in range(128)

root@kali:~/tools/CTF-RSA-tool# python solve.py --verbose -k examples/jarvis_oj_hardRSA/pubkey.pem --decrypt examples/jarvis_oj_hardRSA/flag.enc
Traceback (most recent call last):
File "solve.py", line 148, in
attackobj = lib.RSAutils.RSAAttack(args)
File "/root/tools/CTF-RSA-tool/lib/RSAutils.py", line 34, in init
self.c = libnum.s2n(f.read().strip())
File "/usr/local/lib/python2.7/dist-packages/libnum/strings.py", line 10, in s2n
return int(s.encode("utf-8").hex(), 16)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xde in position 1: ordinal not in range(128)

运行的时候存在报错

python solve.py -e 17 -p 473398607161 -q 4511491 --private

返回的结果是
Traceback (most recent call last):
File "solve.py", line 148, in
attackobj.attack()
File "/soft/ctf/ctf-rsatools/lib/RSAutils.py", line 126, in attack
factors = factor_N.solve(self.n, self.e, self.c, self.sageworks)
File "/soft/ctf/ctf-rsatools/lib/factor_N.py", line 18, in solve
return pastctfprimes(N) or noveltyprimes(N) or wiener_attack(N, e) or factordb(N) or comfact_cn(N, c) or smallq(N) or p_q_2_close(N) or None
File "/soft/ctf/ctf-rsatools/lib/factor_N.py", line 97, in pastctfprimes
if N % prime == 0:
TypeError: unsupported operand type(s) for %: 'NoneType' and 'long'
TypeError: unsupported operand type(s) for %: 'NoneType' and 'long'

error: argument -i/--input: not allowed with argument --private

python solve.py --verbose --private -i examples/closed_p_q.txt

usage: solve.py [-h]
(--decrypt DECRYPT | -c DECRYPT_INT | --private | -i INPUT | -g)
[--createpub] [-o OUTPUT] [--dumpkey] [--enc2dec ENC2DEC]
[-k KEY] [-N N] [-e E] [-d D] [-p P] [-q Q] [--KHBFA KHBFA]
[--pbits PBITS] [-v]
solve.py: error: argument -i/--input: not allowed with argument --private

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.