Skip to content

Commit

Permalink
πŸ“… Commit Sat, 18 Sep 2021 19:01:41
Browse files Browse the repository at this point in the history
✨ otp_crib_drag in cypto extras
✨ ChepyLite class
πŸ”οΈ more lazy imports
βœ… tests added/updated
  • Loading branch information
securisec committed Sep 18, 2021
1 parent bf12c29 commit b234c18
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Misc:
☐ cyberchef recipe to chepy recipe converter

Archive:
βœ” crib dragging @project(New ideas)
βœ” xor data with key @project(Utility)
βœ” strip ansi color codes @project(New ideas)
βœ” build pyinstaller artifact for osx, linux, windows @project(Github Actions)
Expand Down
38 changes: 34 additions & 4 deletions chepy/extras/crypto_extras.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from typing import Iterator, Dict
from binascii import hexlify
from typing import Iterator, Dict, List, Union
from binascii import hexlify, unhexlify
from itertools import cycle
from urllib.request import urlopen
from Crypto.PublicKey import RSA
Expand Down Expand Up @@ -77,12 +77,12 @@ def xor_bruteforce_multi(
}


def xor_two_files(file1: str, file2: str) -> bytes: # pragma: no cover
def xor_two_files(file1: str, file2: str) -> bytes: # pragma: no cover
# TODO
pass


def xor(data: bytes, key: bytes) -> bytes: # pragma: no cover
def xor(data: bytes, key: bytes) -> bytes: # pragma: no cover
"""XOR data with a hex key
Args:
Expand All @@ -97,3 +97,33 @@ def xor(data: bytes, key: bytes) -> bytes: # pragma: no cover
b'c2dea0b1c5'
"""
return hexlify(bytes(a ^ b for a, b in zip(data, cycle(key))))


def one_time_pad_crib(
cipherText1: Union[bytes, str], cipherText2: Union[bytes, str], crib: bytes
) -> List[str]:
"""One time pad crib attack.
Args:
cipherText1 (Union[bytes, str]): Cipher text 1 as hex
cipherText2 (Union[bytes, str]): Cipher text 2 as hex
crib (bytes): Crib (known text) as bytes
Returns:
List[str]: List of possible plaintexts
"""
cipherText1 = unhexlify(cipherText1)
cipherText2 = unhexlify(cipherText2)
xored = bytearray(a ^ b for a, b in zip(cipherText1, cipherText2))
hold = []
for offset in range(0, len(xored) - len(crib) + 1):
piece = xored[offset : offset + len(crib)]
piece = bytearray(a ^ b for a, b in zip(crib, piece))
if all(32 <= c <= 126 for c in piece):
piece = (
("." * offset)
+ piece.decode()
+ ("." * (len(xored) - len(crib) - offset))
)
hold.append(piece)
return hold
34 changes: 31 additions & 3 deletions chepy/lite.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
from .modules.aritmeticlogic import AritmeticLogic
from .modules.compression import Compression
from .modules.dataformat import DataFormat
from .modules.datetimemodule import DateTime
from .modules.encryptionencoding import EncryptionEncoding
from .modules.extractors import Extractors
from .modules.hashing import Hashing
from .modules.language import Language
from .modules.links import Links
from .modules.multimedia import Multimedia
from .modules.networking import Networking
from .modules.other import Other
from .modules.publickey import Publickey
from .modules.search import Search
from .modules.utils import Utils

class ChepyLite(
AritmeticLogic
AritmeticLogic,
Compression,
DataFormat,
DateTime,
EncryptionEncoding,
Extractors,
Hashing,
Language,
Links,
Networking,
Other,
Publickey,
Search,
Utils,
):
"""Chepy Lite class. Chepy lite is a lightweight version of Chepy which uses
very little outside dependencies making it much faster to load than Chepy core.
"""Chepy Lite class. Chepy lite is a lightweight version of Chepy which offers
most of the core functionalities of Chepy, but without any extensions or extra
modules. This is so that Chepy load time can be faster.
"""
pass
3 changes: 2 additions & 1 deletion chepy/modules/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import lazy_import
import difflib
from collections import OrderedDict
from typing import TypeVar, Union

import chepy.modules.internal.colors as _int_colors
import exrex
exrex = lazy_import.lazy_module("exrex")
import pydash
import regex as re

Expand Down
12 changes: 11 additions & 1 deletion tests/test_extras/test_crypto_extras.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
from chepy.extras.crypto_extras import *
from chepy.extras.crypto_extras import one_time_pad_crib


def test_one_time_pad_crib():
assert (
one_time_pad_crib(
"51060328ac104b70881b267fb254d7914948e697aff2ce1c07c91c51b4ff2a172b6477c7e006",
"560b032eb6481826df19237ce403d7c34c4db194fff59a4f559a4d09b6fa72157a642797e31a",
b"a" * 38,
)[0]
== "flag{9276cdb76a3dd6b1f523209cd9c0a11b}"
)

0 comments on commit b234c18

Please sign in to comment.