Skip to content

Commit

Permalink
πŸ—“ Sep 1, 2023 11:49:39 PM
Browse files Browse the repository at this point in the history
✨ to/from bacon
✨ to/from pickle
πŸ§ͺ tests added/updated
πŸ”§ misc fixes
  • Loading branch information
securisec committed Sep 2, 2023
1 parent 028e035 commit c4c9005
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 10 deletions.
7 changes: 4 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ New ideas:
☐ ✨ zero-width encode
☐ ✨ hill cipher encode/decode/brute
☐ πŸ’‘ maybe a decorator function to convert all inputs into bytes when possible? this will allow for a consistant bytes approach to all functions
☐ rsa enc/dec with key or from pem directly
☐ ✨ jwt hmac confusion
☐ ✨ amf encode/decode
☐ ✨ rabbit encryption
☐ ✨ aes cmac
☐ ✨ cetacean encode/decode
☐ ✨ Letter Number Code (A1Z26) A=1, B=2, C=3 encoder/decoder T4 l16 _36 510 _27 s26 _11 320 414 {6 }39 C2 T0 m28 317 y35 d31 F1 m22 g19 d38 z34 423 l15 329 c12 ;37 19 h13 _30 F5 t7 C3 325 z33 _21 h8 n18 132 k24 = TFCCTF{th15_ch4ll3ng3_m4k3s_m3_d1zzy_;d}
☐ ✨ whitespace encoding https://www.dcode.fr/whitespace-language
☐ ✨ huffman encode/decode
☐ ✨ bacon encode/decode
☐ ✨ new plugin that can help detect encoding type trained on random data
✨ upside down writing

Bug:

Expand Down Expand Up @@ -65,6 +63,9 @@ Misc:
☐ cyberchef recipe to chepy recipe converter

Archive:
βœ” ✨ bacon encode/decode
βœ” rsa enc/dec with key or from pem directly
βœ” ✨ to/from pickle
βœ” ✨ ls47 enc/dec
βœ” ✨ shuffle
βœ” πŸš€ add crib for xor bruteforce
Expand Down
106 changes: 105 additions & 1 deletion chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import base58
import json
import struct
import pickle
import string
from random import randint

yaml = lazy_import.lazy_module("yaml")
import regex as re
import hexdump
from ast import literal_eval
from typing import TypeVar, Union, List
from typing import TypeVar, Union, List, Literal
from urllib.parse import quote_plus as _urllib_quote_plus
from urllib.parse import unquote_plus as _urllib_unquote_plus

Expand Down Expand Up @@ -1636,3 +1638,105 @@ def to_base36(self, join_by: Union[str, bytes] = b" ") -> DataFormatT:
hold.append(str(int(d.strip(), 36)).encode())
self.state = join_by.join(hold)
return self

@ChepyDecorators.call_stack
def to_pickle(self) -> DataFormatT:
"""Pickle serialize state
Returns:
Chepy: The Chepy object.
"""
self.state = pickle.dumps(self.state)
return self

@ChepyDecorators.call_stack
def from_pickle(self, trust: bool = False) -> DataFormatT:
"""Deserialize pickle data
Args:
trust (bool, optional): As this can lead to code execution, this is a safety net and needs to be set to True. Defaults to False.
Returns:
Chepy: The Chepy object.
"""
if not trust:
return self
self.state = pickle.loads(self._convert_to_bytes())
return self

@ChepyDecorators.call_stack
def to_bacon(
self,
A: Literal["A", "0"] = "A",
B: Literal["B", "1"] = "B",
complete: bool = True,
join_by: Union[str, bytes] = b" ",
invert: bool = False,
) -> DataFormatT:
"""Bacon encode
Args:
A (Literal['A', '0'], optional): The A character. Defaults to 'A'.
B (Literal['B', '1'], optional): The B character. Defaults to 'B'.
complete (bool, optional): Use unique mapping for all characters. Defaults to True.
join_by (Union[str,bytes], optional): Join output by. Defaults to b' '.
invert (bool, optional): Invert encoding. Defaults to False.
Returns:
Chepy: The Chepy object.
"""
join_by = self._str_to_bytes(join_by)
hold = []
for s in self._convert_to_str():
if s in string.ascii_letters:
if complete:
hold.append(Encoding.BACON_26.get(s.upper()))
else: # pragma: no cover
hold.append(Encoding.BACON_24.get(s.upper()))
updated = []
for h in hold:
if invert: # pragma: no cover
updated.append(h.replace("a", B).replace("b", A).encode())
else:
updated.append(h.replace("a", A).replace("b", B).encode())
self.state = join_by.join(updated)
return self

@ChepyDecorators.call_stack
def from_bacon(
self,
A: Literal["A", "0"] = "A",
B: Literal["B", "1"] = "B",
complete: bool = True,
split_by: Union[str, bytes] = b" ",
invert: bool = False,
) -> DataFormatT:
"""From Bacon
Args:
A (Literal['A','0'], optional): A character. Defaults to 'A'.
B (str, optional): B character. Defaults to 'B'.
complete (bool, optional): Use unique mapping for all characters. Defaults to True.
split_by (Union[str,bytes], optional): Split by. Defaults to b' '.
invert (bool, optional): Invert decoding. Defaults to False.
Returns:
Chepy: The Chepy object.
"""
split_by = self._bytes_to_str(split_by)
if complete:
mapping = {v: k for k, v in Encoding.BACON_26.items()}
else:
mapping = {v: k for k, v in Encoding.BACON_26.items()} # pragma: no cover
data = self.state
if not isinstance(self.state, list): # pragma: no cover
data = self._convert_to_str().split(split_by)
out = ""
for d in data:
if invert: # pragma: no cover
d = d.replace(A, "b").replace(B, "a")
else:
d = d.replace(A, "a").replace(B, "b")
out += mapping.get(d, "")
self.state = out.encode()
return self
4 changes: 4 additions & 0 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ class DataFormat(ChepyCore):
def from_twin_hex(self: DataFormatT) -> DataFormatT: ...
def to_base36(self: DataFormatT, join_by: Union[str, bytes]=...) -> DataFormatT: ...
def from_base36(self: DataFormatT, delimiter: Union[str, bytes]=..., join_by: Union[str, bytes]=...) -> DataFormatT: ...
def to_pickle(self: DataFormatT) -> DataFormatT: ...
def from_pickle(self: DataFormatT, trust: bool=...) -> DataFormatT: ...
def to_bacon(self: DataFormatT, A: Literal['A', '0']=..., B: Literal['B', '1']=..., complete: bool=..., join_by: bytes=..., invert: bool=...) -> DataFormatT: ...
def from_bacon(self: DataFormatT, A: Literal['A', '0']=..., B: Literal['B', '1']=..., complete: bool=..., split_by: bytes=..., invert: bool=...) -> DataFormatT: ...
2 changes: 1 addition & 1 deletion chepy/modules/encryptionencoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ def to_morse_code(
morse_code_dict[k] = v.replace(".", dot).replace("-", dash)
for word in self._convert_to_str().split():
for w in word:
encode += morse_code_dict.get(w.upper()) + letter_delim
encode += morse_code_dict.get(w.upper(), '') + letter_delim
encode += word_delim
self.state = encode
return self
Expand Down
58 changes: 58 additions & 0 deletions chepy/modules/internal/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,62 @@
class Encoding(object):
BACON_26 = {
"A": "aaaaa",
"B": "aaaab",
"C": "aaaba",
"D": "aaabb",
"E": "aabaa",
"F": "aabab",
"G": "aabba",
"H": "aabbb",
"I": "abaaa",
"J": "abaab",
"K": "ababa",
"L": "ababb",
"M": "abbaa",
"N": "abbab",
"O": "abbba",
"P": "abbbb",
"Q": "baaaa",
"R": "baaab",
"S": "baaba",
"T": "baabb",
"U": "babaa",
"V": "babab",
"W": "babba",
"X": "babbb",
"Y": "bbaaa",
"Z": "bbaab",
}

BACON_24 = {
"A": "aaaaa",
"B": "aaaab",
"C": "aaaba",
"D": "aaabb",
"E": "aabaa",
"F": "aabab",
"G": "aabba",
"H": "aabbb",
"I": "abaaa",
"J": "abaaa",
"K": "ababa",
"L": "ababb",
"M": "abbaa",
"N": "abbab",
"O": "abbba",
"P": "abbbb",
"Q": "baaaa",
"R": "baaab",
"S": "baaba",
"T": "baabb",
"U": "babaa",
"V": "babaa",
"W": "babba",
"X": "babbb",
"Y": "bbaaa",
"Z": "bbaab",
}

BASE91_ALPHABET = [
"A",
"B",
Expand Down
6 changes: 3 additions & 3 deletions chepy/modules/links.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ LinksT = TypeVar('LinksT', bound='Links')
class Links(ChepyCore):
def __init__(self, *data: Any) -> None: ...
state: Any = ...
def pastebin_to_raw(self) -> LinksT: ...
def github_to_raw(self) -> LinksT: ...
def google_search_ei_to_epoch(self) -> LinksT: ...
def pastebin_to_raw(self: LinksT) -> LinksT: ...
def github_to_raw(self: LinksT) -> LinksT: ...
def google_search_ei_to_epoch(self: LinksT) -> LinksT: ...
18 changes: 17 additions & 1 deletion tests/test_dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ def test_nato_convert():
assert (
Chepy(
"Whiskey Hotel Four Tango Dash Alpha Romeo Three Dash Yankee Oscar Uniform Dash Sierra One November Kilo India November Golf Dash Four Bravo Zero Uniform Seven"
).from_nato().o
)
.from_nato()
.o
== b"WH4T-AR3-YOU-S1NKING-4B0U7"
)

Expand Down Expand Up @@ -548,3 +550,17 @@ def test_from_twin_hex():
.o
== b"BDSEC{_tW1n_H3X_c1Ph3r_}"
)


def test_pickle():
out = Chepy("hello").to_pickle().o
assert out == b"\x80\x04\x95\t\x00\x00\x00\x00\x00\x00\x00\x8c\x05hello\x94."
assert Chepy(out).from_pickle(True).o == b"hello"
assert Chepy(out).from_pickle().o == out


def test_bacon():
out = Chepy("hello").to_bacon().o
assert out == b"AABBB AABAA ABABB ABABB ABBBA"
assert Chepy(out).from_bacon().o == b"HELLO"
assert Chepy("hello").to_bacon(A="0", B="1").from_bacon(A="0", B="1").o == b"HELLO"
2 changes: 1 addition & 1 deletion tests/test_encryptionencoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ def test_vigenere_decode():
assert Chepy("kieiim").vigenere_decode("secret").o == b"secret"


def test_affin_encode():
def test_affine_encode():
assert Chepy("secret").affine_encode().o == b"TFDSFU"


Expand Down

0 comments on commit c4c9005

Please sign in to comment.