Skip to content

Commit

Permalink
🗓 Dec 15, 2024 8:18:53 PM
Browse files Browse the repository at this point in the history
✨ strip_non_printable method
🐙 improve CHEPY_PLUGINS env handling
  • Loading branch information
securisec committed Dec 16, 2024
1 parent 8880ebf commit 3b40fb2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions chepy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
13 changes: 13 additions & 0 deletions chepy/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions chepy/modules/utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 3b40fb2

Please sign in to comment.