Skip to content

Commit

Permalink
πŸ—“ Jun 4, 2023 8:28:27 PM
Browse files Browse the repository at this point in the history
✨ find_continuous_patterns
✨ find_longest_continious_pattern
πŸ§ͺ tests added/updated
🚧 5.2.0
πŸ’š build steps added/updated
  • Loading branch information
securisec committed Jun 5, 2023
1 parent 71de3c8 commit cc603e7
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/tests_multi_os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ jobs:
steps:
- uses: actions/checkout@v2.3.4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2.2.1
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
# - uses: actions/cache@v1
# id: devcache
# with:
Expand Down
2 changes: 1 addition & 1 deletion chepy/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "5.1.0" # pragma: no cover
__version__ = "5.2.0" # pragma: no cover
__author__ = "Hapsida @securisec" # pragma: no cover
72 changes: 66 additions & 6 deletions chepy/modules/extractors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TypeVar
from typing import TypeVar, Union
from urllib.parse import urlparse as _pyurlparse

import regex as re
Expand Down Expand Up @@ -44,7 +44,7 @@ def extract_hashes(self) -> ExtractorsT:
return self

@ChepyDecorators.call_stack
def extract_strings(self, length: int = 4, join_by: str='\n') -> ExtractorsT:
def extract_strings(self, length: int = 4, join_by: str = "\n") -> ExtractorsT:
"""Extract strings from state
Args:
Expand All @@ -70,9 +70,7 @@ def extract_strings(self, length: int = 4, join_by: str='\n') -> ExtractorsT:
return self

@ChepyDecorators.call_stack
def extract_ips(
self, is_binary: bool = False
) -> ExtractorsT:
def extract_ips(self, is_binary: bool = False) -> ExtractorsT:
"""Extract ipv4 and ipv6 addresses
Args:
Expand Down Expand Up @@ -116,7 +114,10 @@ def extract_email(self, is_binary: bool = False) -> ExtractorsT:
pattern = b"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"
if is_binary:
matched = list(
filter(lambda x: re.search(pattern, x), self.extract_strings().o.encode().splitlines())
filter(
lambda x: re.search(pattern, x),
self.extract_strings().o.encode().splitlines(),
)
)
else: # pragma: no cover
matched = list(
Expand Down Expand Up @@ -438,3 +439,62 @@ def extract_base64(self, min: int = 20) -> ExtractorsT:
else:
self.state = found[0]
return self

@ChepyDecorators.call_stack
def find_continuous_patterns(self, str2: Union[str, bytes], min_value: int = 10):
"""Find continius patterns between the state as a string and the provided str2
Args:
str2 (Union[str, bytes]): String to find matches against
min_value (int, optional): Minimum value of continious matches. Defaults to 10.
Returns:
Chepy: The Chepy object.
"""
str1 = self._convert_to_bytes()
if isinstance(str2, str):
str2 = str2.encode()
combined_data = str1 + str2
data_length = len(combined_data)
patterns = []

for length in range(1, data_length + 1):
for start in range(data_length - length + 1):
pattern = combined_data[start : start + length]

if pattern in str1 and pattern in str2 and len(pattern) > min_value:
patterns.append(pattern)

self.state = patterns
return self

@ChepyDecorators.call_stack
def find_longest_continious_pattern(self, str2: str):
"""Find longest continious pattern
Args:
str2 (Union[str, bytes]): String to find match against
Returns:
Chepy: The Chepy object.
"""
str1 = self._convert_to_bytes()
if isinstance(str2, str):
str2 = str2.encode()
combined_data = str1 + str2
data_length = len(combined_data)
matches = []

for length in range(1, data_length + 1):
for start in range(data_length - length + 1):
pattern = combined_data[start : start + length]

if (
pattern in str1
and pattern in str2
and len(pattern) > len(matches[-1:])
):
matches.append(pattern)

self.state = max(matches, key=len) if matches else ""
return self
4 changes: 3 additions & 1 deletion chepy/modules/extractors.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ..core import ChepyCore
from typing import Any, TypeVar
from typing import Any, TypeVar, Union

ExtractorsT = TypeVar('ExtractorsT', bound='Extractors')

Expand Down Expand Up @@ -34,3 +34,5 @@ class Extractors(ChepyCore):
def extract_dsa_private(self: ExtractorsT) -> ExtractorsT: ...
def extract_jwt_token(self: ExtractorsT) -> ExtractorsT: ...
def extract_base64(self: ExtractorsT, min: int=...) -> ExtractorsT: ...
def find_continuous_patterns(self: ExtractorsT, str2: Union[str, bytes], min_value: int=...) -> ExtractorsT: ...
def find_longest_continious_pattern(self: ExtractorsT, str2: Union[str, bytes]) -> ExtractorsT: ...
23 changes: 22 additions & 1 deletion tests/test_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@


def test_extract_strings():
assert Chepy("tests/files/hello").load_file().extract_strings().o.splitlines()[0] == '__PAGEZERO'
assert (
Chepy("tests/files/hello").load_file().extract_strings().o.splitlines()[0]
== "__PAGEZERO"
)


def test_extract_hashes():
Expand Down Expand Up @@ -214,3 +217,21 @@ def test_extract_b64():
when an unknown printer took a galley of type c2VjdXJpc2VjLnRlc3QuZGF0YQo= and scrambled it to make a type specimen book.
"""
assert Chepy(data).extract_base64().o == "c2VjdXJpc2VjLnRlc3QuZGF0YQo="


def test_find_longest_continious_pattern():
str1 = "Helhello worldlo World"
str2 = "hello world"
assert Chepy(str1).find_longest_continious_pattern(str2).o == b"hello world"
str1 = b"Helhello worldlo World"
str2 = b"hello world"
assert Chepy(str1).find_longest_continious_pattern(str2).o == b"hello world"


def test_find_continuous_patterns():
str1 = "Helhello worldlo World"
str2 = "hello world"
assert len(Chepy(str1).find_continuous_patterns(str2, 3).o) == 73
str1 = b"Helhello worldlo World"
str2 = b"hello world"
assert len(Chepy(str1).find_continuous_patterns(str2, 3).o) == 73

0 comments on commit cc603e7

Please sign in to comment.