From 3b40fb287b7ee72259ff5fa1c3d82a308731f28e Mon Sep 17 00:00:00 2001 From: securisec Date: Sun, 15 Dec 2024 20:19:59 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=97=93=20Dec=2015,=202024=208:18:53?= =?UTF-8?q?=E2=80=AFPM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✨ strip_non_printable method 🐙 improve CHEPY_PLUGINS env handling --- TODO | 4 ++-- chepy/config.py | 8 +++++--- chepy/modules/utils.py | 13 +++++++++++++ chepy/modules/utils.pyi | 1 + tests/test_utils.py | 4 ++++ 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/TODO b/TODO index 1fbc91d..0ac3893 100644 --- a/TODO +++ b/TODO @@ -9,11 +9,10 @@ New ideas: ☐ save registers https://t.co/pBNRufibY8?amp=1 ☐ cbor encode/decode https://github.com/agronholm/cbor2 (plugin) ☐ fuzzy search - ☐ pgp, generate, encrypt, decrypt, verify + ☐ 🚧 pgp, generate, encrypt, decrypt, verify ☐ swap little and big endian ☐ ignore error method ☐ swap bytes - ☐ @high disable plugins from code ☐ homophonic decoder ☐ append method for core to add data to the state ☐ qr create @@ -48,6 +47,7 @@ Misc: ☐ cyberchef recipe to chepy recipe converter Archive: + ✔ @high disable plugins from code ✔ 🐙 update config to use envars for plugins ✔ register support in callstack ✔ ecb no padding in aes/des diff --git a/chepy/config.py b/chepy/config.py index 59597e3..3e683c8 100644 --- a/chepy/config.py +++ b/chepy/config.py @@ -60,9 +60,11 @@ def __init__(self): self.__get_conf_value("false", "EnablePlugins", "Plugins") ) - self.enable_plugins = ( - os.environ.get("CHEPY_PLUGINS", "false") == "true" - ) or self.enable_plugins + plugin_envar = os.environ.get("CHEPY_PLUGINS", "") + if plugin_envar == "true": + self.enable_plugins = True + elif plugin_envar != "": + self.enable_plugins = False if self.enable_plugins: if plugin_path != "None": diff --git a/chepy/modules/utils.py b/chepy/modules/utils.py index 1d98344..5bf02f0 100644 --- a/chepy/modules/utils.py +++ b/chepy/modules/utils.py @@ -495,6 +495,19 @@ def strip(self, pattern: str, ignore_case=True) -> UtilsT: self.state = re.sub(pattern, "", self._convert_to_str(), flags=flags) return self + @ChepyDecorators.call_stack + def strip_non_printable(self) -> UtilsT: + """String non printable characters + + Returns: + Chepy: The Chepy object. + """ + data = self._convert_to_bytes() + printable_set = set(range(32, 127)) | {9, 10, 13, 12} + # Filter out non-printable characters + self.state = bytes(b for b in data if b in printable_set) + return self + @ChepyDecorators.call_stack def find_replace(self, pattern: str, repl: str, ignore_case=True) -> UtilsT: """Replace matched pattern with repln diff --git a/chepy/modules/utils.pyi b/chepy/modules/utils.pyi index b55ecae..fde3092 100644 --- a/chepy/modules/utils.pyi +++ b/chepy/modules/utils.pyi @@ -28,6 +28,7 @@ class Utils(ChepyCore): def slice(self: UtilsT, start: int=..., end: int=...) -> UtilsT: ... def strip_ansi(self: UtilsT) -> UtilsT: ... def strip(self: UtilsT, pattern: str, ignore_case: bool=...) -> UtilsT: ... + def strip_non_printable(self: UtilsT) -> UtilsT: ... def find_replace(self: UtilsT, pattern: str, repl: str, ignore_case: bool=...) -> UtilsT: ... def escape_string(self: UtilsT) -> UtilsT: ... def unescape_string(self: UtilsT) -> UtilsT: ... diff --git a/tests/test_utils.py b/tests/test_utils.py index 64f030c..ebcbdc5 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -181,6 +181,10 @@ def test_strip(): assert Chepy("some some data").strip(r"some\s").o == b"data" +def test_string_non_printable(): + assert Chepy("Hello\x00W\xc1orld\x1b!").strip_non_printable().o == b"HelloWorld!" + + def test_find_replace(): assert Chepy("some some data").find_replace(r"some\s", "data").o == b"datadatadata"