A mutitool for cryptography CTF problems
Install sage
(>9) from other means, then install package via git URL.
Some modules may still work without sage
installed.
# seutp conda env?
conda install sage
pip install git+https://github.com/4yn/sage_army_knife.git@main
from sage_army_knife import *
Many of these functions aren't my own, credits to the original authors where due.
- Data manipulation:
ChefKnife
- RSA:
RSAKnife
,boneh_durfee
by @mimoo - DH:
b_smooth_prime
by @mimoo - PRNG:
Untwister
by @icemonster,get_seed_for_random_state
- ECC:
two_points_to_a4_a6
,is_curve_singular
,solve_singular_node
,solve_singular_cusp
,check_ecc_curve
by @pqlx - Lattices:
closest_vector
by @rbtree,inequality_closest_vector
by @rkm0959 - Polynomials:
small_roots
by @defund
Other CTF crypto code libraries worth looking at:
- @p4-team/crypto-commons
- @ValarDragon/CTF-Crypto
- @rkm0959/Cryptography_Writeups
- @josephsurin/lattice-based-cryptanalysis
from sage_army_knife import ChefKnife
from sage_army_knife import Chef
from sage_army_knife import CK
Polyglot data container for casting between data encodings, inspired by CyberChef and @securisec/chepy.
Has functions for casting to and from str
, bytes
, b64
, json
, hex
,
int
, io.BytesIO
and io.StringIO
. Can also calculate hashes, xor and
basic AES calculations.
The aliases CK
or Chef
can also be used.
The conversion functions try to follow this naming convention:
to_*
encodes aChefKnife(data)
toChefKnife(encoded data)
from_*
parses aChefKnife(encoded data)
toChefKnife(data)
init_*
takes in a non-ChefKnife
object, decodes the data and returns aChefKnife(data)
into_*
turns aChefKnife(data)
into a non-ChefKnife
object
The conversion functions can also be chained together.
Example usage:
>>> from sage_army_knife import ChefKnife
>>> ChefKnife("abcd").to_b64()
ChefKnife(b'YWJjZA==')
>>> ChefKnife("abcd").to_b64().to_hex()
ChefKnife('59574a6a5a413d3d')
>>> ChefKnife("abcd").to_b64().to_hex().to_hexdigest("md5")
ChefKnife('58a15dd16f7d263689469ea66b4e57d9')
>>> ChefKnife("abcd").to_b64().to_hex().to_hexdigest("md5").into_str()
'58a15dd16f7d263689469ea66b4e57d9'
>>> ChefKnife("abcd") ^ "bcde"
ChefKnife(b'\x03\x01\x07\x01')
>>> ck = ChefKnife("abcd")
>>> ck
ChefKnife('abcd')
>>> ck += "efg"
>>> ck
ChefKnife('abcdefg')
>>> ck += b"hij"
>>> ck
ChefKnife(b'abcdefghij')
>>> ck += ChefKnife("bG1u").from_b64()
>>> ck
ChefKnife(b'abcdefghijlmn')
>>> ck[1::2]
Chef(b'bdfhjm')
>>> ck.pad_pcks7()
ChefKnife(b'abcdefghijlmn\x03\x03\x03')
Docs WIP
- https://github.com/mimoo/RSA-and-LLL-attacks
- https://github.com/mimoo/Diffie-Hellman_Backdoor
- https://github.com/defund/coppersmith
- https://gist.github.com/pqlx/d0bdf2d0c4a2aa400b2b52d9bd9b7b65
- https://github.com/hyunsikjeong/LLL
- https://github.com/rkm0959/Inequality_Solving_with_CVP
- https://github.com/icemonster/symbolic_mersenne_cracker
- https://ctftime.org/writeup/12563
- https://crypto.stackexchange.com/questions/61302/how-to-solve-this-ecdlp
- https://jgeralnik.github.io/writeups/2021/08/12/Lattices/