/sage_army_knife

A mutitool for cryptography CTF problems

Primary LanguagePython

Sage Army Knife

A mutitool for cryptography CTF problems

Install

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

Script Bag

from sage_army_knife import *

Many of these functions aren't my own, credits to the original authors where due.

Other CTF crypto code libraries worth looking at:

ChefKnife

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 a ChefKnife(data) to ChefKnife(encoded data)
  • from_* parses a ChefKnife(encoded data) to ChefKnife(data)
  • init_* takes in a non-ChefKnife object, decodes the data and returns a ChefKnife(data)
  • into_* turns a ChefKnife(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')

RSAKnife

Docs WIP

References

  1. https://github.com/mimoo/RSA-and-LLL-attacks
  2. https://github.com/mimoo/Diffie-Hellman_Backdoor
  3. https://github.com/defund/coppersmith
  4. https://gist.github.com/pqlx/d0bdf2d0c4a2aa400b2b52d9bd9b7b65
  5. https://github.com/hyunsikjeong/LLL
  6. https://github.com/rkm0959/Inequality_Solving_with_CVP
  7. https://github.com/icemonster/symbolic_mersenne_cracker
  8. https://ctftime.org/writeup/12563
  9. https://crypto.stackexchange.com/questions/61302/how-to-solve-this-ecdlp
  10. https://jgeralnik.github.io/writeups/2021/08/12/Lattices/