Skip to content

Commit

Permalink
πŸ—“ Apr 11, 2024 5:26:39β€―PM
Browse files Browse the repository at this point in the history
πŸ§ͺ tests added/updated
βž• deps added/updated
πŸ™ automatically remove whitespace for base32/64
  • Loading branch information
securisec committed Apr 11, 2024
1 parent 625be78 commit 0960163
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 24 deletions.
12 changes: 1 addition & 11 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ New ideas:
☐ ✨ aes cmac
☐ ✨ whitespace encoding https://www.dcode.fr/whitespace-language
☐ beaufort
☐ register support in callstack
☐ πŸ”₯ update python_requires in setup.py on python version change
☐ πŸ™ update config to use envars also

Expand All @@ -46,19 +45,10 @@ Enhance:

Plugins:

Methods:
☐ magic method from cyberchef
☐ base 92

Github Actions:

Distribution:
☐ request to add to kali
☐ request to add to brew

Misc:
☐ cyberchef recipe to chepy recipe converter

Archive:
βœ” register support in callstack
βœ” ecb no padding in aes/des
βœ” update ml model with different spacing for hex, binary etc
3 changes: 2 additions & 1 deletion chepy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ChepyCore(object):
**buffers**, etc and is required to use and extend Chepy.
Args:
\*data (tuple): The core class takes arbitrary number of arguments as \*args.
*data (tuple): The core class takes arbitrary number of arguments as *args.
Attributes:
states (dict): Contains all the current states. Each arg passed to
Expand Down Expand Up @@ -1469,6 +1469,7 @@ def cb(data):
self.state = callback_function(self.state)
return self

@ChepyDecorators.call_stack
def register(
self,
pattern: Union[str, bytes],
Expand Down
12 changes: 10 additions & 2 deletions chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,22 @@ def to_base32(self) -> DataFormatT:
return self

@ChepyDecorators.call_stack
def from_base32(self) -> DataFormatT:
def from_base32(self, remove_whitespace: bool = True) -> DataFormatT:
"""Decode as Base32
Base32 is a notation for encoding arbitrary byte data using a
restricted set of symbols that can be conveniently used by humans
and processed by computers. It uses a smaller set of characters than
Base64, usually the uppercase alphabet and the numbers 2 to 7.
Args:
remove_whitespace(bool, optional): If true, all whitespaces are removed
Returns:
Chepy: The Chepy object.
"""
if remove_whitespace:
self.state = self.remove_whitespace().o
self.state = base64.b32decode(self.state)
return self

Expand Down Expand Up @@ -485,7 +490,7 @@ def to_base64(self, custom: str = None, url_safe: bool = False) -> DataFormatT:
return self

@ChepyDecorators.call_stack
def from_base64(self, custom: str = None, url_safe: bool = False) -> DataFormatT:
def from_base64(self, custom: str = None, url_safe: bool = False, remove_whitespace: bool = True) -> DataFormatT:
"""Decode as Base64
Base64 is a notation for encoding arbitrary byte data using a
Expand All @@ -496,6 +501,7 @@ def from_base64(self, custom: str = None, url_safe: bool = False) -> DataFormatT
Args:
custom (str, optional): Provide a custom charset to base64 with
url_safe (bool, optional): If true, decode url safe. Defaults to False
remove_whitespace(bool, optional): If true, all whitespaces are removed
Returns:
Chepy: The Chepy object.
Expand All @@ -507,6 +513,8 @@ def from_base64(self, custom: str = None, url_safe: bool = False) -> DataFormatT
>>> c.out
b"some random? data"
"""
if remove_whitespace:
data = self.remove_whitespace().o
data = self._convert_to_str()
if custom is not None:
std_base64chars = (
Expand Down
4 changes: 2 additions & 2 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class DataFormat(ChepyCore):
def to_base16(self: DataFormatT) -> DataFormatT: ...
def from_base16(self: DataFormatT) -> DataFormatT: ...
def to_base32(self: DataFormatT) -> DataFormatT: ...
def from_base32(self: DataFormatT) -> DataFormatT: ...
def from_base32(self: DataFormatT, remove_whitespace: bool=True) -> DataFormatT: ...
def to_int(self: DataFormatT) -> DataFormatT: ...
def to_bytes(self: DataFormatT) -> DataFormatT: ...
def from_bytes(self: DataFormatT) -> DataFormatT: ...
def to_base64(self: DataFormatT, custom: str=...) -> DataFormatT: ...
def from_base64(self: DataFormatT, custom: str=..., url_safe: bool=...) -> DataFormatT: ...
def from_base64(self: DataFormatT, custom: str=..., url_safe: bool=..., remove_whitespace: bool=True) -> DataFormatT: ...
def decode_bytes(self: DataFormatT, errors: Literal['ignore', 'backslashreplace', 'replace']=...) -> DataFormatT: ...
def to_hex(self: DataFormatT, delimiter: str=..., join_by: str=...) -> DataFormatT: ...
def from_hex(self: DataFormatT, delimiter: Union[str, None]=None, join_by: str='') -> DataFormatT: ...
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ decorator
docstring-parser
emoji==2.0.0
exrex
fire==0.4.0
fire==0.6.0
lazy-import
hexdump
jsonpickle
Expand Down
8 changes: 3 additions & 5 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,9 @@ def test_run_recipe():

def test_recipe():
temp = str(Path(tempfile.gettempdir()) / os.urandom(24).hex())
Chepy(
"tests/files/encoding"
).load_file().reverse().rot_13().from_base64().from_base32().str_from_hexdump().save_recipe(
temp
)
Chepy("tests/files/encoding").load_file().reverse().rot_13().from_base64(
remove_whitespace=False
).from_base32(remove_whitespace=False).str_from_hexdump().save_recipe(temp)

assert (
Chepy("tests/files/encoding").load_recipe(temp).o
Expand Down
18 changes: 16 additions & 2 deletions tests/test_dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,15 @@ def test_from_base85():
assert Chepy("F)Po,+Cno&@/").from_base85().out.decode() == "some data"


def test_to_base32():
def test_base32():
assert Chepy("some data").to_base32().out.decode() == "ONXW2ZJAMRQXIYI="
assert (
Chepy("""MFWWC5DFOVZHGQ2UIZ5XA2LDGBPXONBVNY3V6ZZQGBSF63RQOVTWQXZVGBPWSXZXGAYGWX3TN5WT
GX3DOIZTI5BROYZV63BRMIZXE5BRGM2V6YLEMU4DQMRQMV6Q====""")
.from_base32()
.o
== b"amateursCTF{pic0_w45n7_g00d_n0ugh_50_i_700k_som3_cr34t1v3_l1b3rt135_ade8820e}"
)


def test_to_base64():
Expand All @@ -132,6 +139,13 @@ def test_from_base64():
== b"some random? data"
)
assert Chepy("dGVzdA").from_base64(url_safe=True).o == b"test"
assert (
Chepy("""YW1hdGV1cnNDVEZ7cGljMF93NDVuN19nMDBkX24wdWdoXzUwX2lfNzAwa19zb20zX2NyMzR0MXYz
X2wxYjNydDEzNV9hZGU4ODIwZX0=""")
.from_base64()
.o
== b"amateursCTF{pic0_w45n7_g00d_n0ugh_50_i_700k_som3_cr34t1v3_l1b3rt135_ade8820e}"
)


def test_decode_bytes():
Expand Down Expand Up @@ -227,7 +241,7 @@ def test_bytearray_to_str():


def test_get_by_index():
assert Chepy([1, "a", True]).get_by_index(2).state == True
assert Chepy([1, "a", True]).get_by_index(2).state


def test_get_by_key():
Expand Down

0 comments on commit 0960163

Please sign in to comment.