Skip to content

Commit

Permalink
🗓 Jul 2, 2024 11:59:14 PM
Browse files Browse the repository at this point in the history
💚 build steps added/updated
✨ aws_account_id_from_access_key
✨ expand_alpha_range
  • Loading branch information
securisec committed Jul 3, 2024
1 parent 3bc516b commit aae8022
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests_multi_os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ jobs:
- name: Install test requirements
run: |
pip install sphinx recommonmark pytest==8.1.1 pytest-cov pyperclip
pip install sphinx recommonmark pytest==8.1.1 pytest-cov==5.0.0 pyperclip
- name: Test with pytest
run: |
pytest -v --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
COVERAGE_CORE=sysmon pytest -v --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
coverage report -m
- name: Test plugins osx
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@


test:
python -m pytest --noconftest -v --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/
COVERAGE_CORE=sysmon python -m pytest --noconftest -v --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc tests/

test-all: test
python -m pytest --noconftest -v --disable-pytest-warnings tests_plugins/
COVERAGE_CORE=sysmon python -m pytest --noconftest -v --disable-pytest-warnings tests_plugins/

# git log --format=%B 4.0.0..5.0.0 | sed '/^\s*$/d' | sort | uniq
17 changes: 17 additions & 0 deletions chepy/modules/extractors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math
from binascii import unhexlify
import base64
from typing import TypeVar, Union, List
from urllib.parse import urlparse as _pyurlparse
import lazy_import
Expand Down Expand Up @@ -684,3 +685,19 @@ def extract_html_comments(self):
filter(lambda x: x != "", self._parsel_obj().xpath("//comment()").getall())
)
return self

@ChepyDecorators.call_stack
def aws_account_id_from_access_key(self):
"""Extract AWS account id from access key
Returns:
Chepy: The Chepy object.
"""
trimmed_AWSKeyID = self._convert_to_str()[4:]
x = base64.b32decode(trimmed_AWSKeyID)
y = x[0:6]
z = int.from_bytes(y, byteorder='big', signed=False)
mask = int.from_bytes(unhexlify(b'7fffffffff80'), byteorder='big', signed=False)

self.state = (z & mask)>>7
return self
1 change: 1 addition & 0 deletions chepy/modules/extractors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ class Extractors(ChepyCore):
def css_selector(self: ExtractorsT, query: str) -> ExtractorsT: ...
def extract_html_comments(self: ExtractorsT) -> ExtractorsT: ...
def extract_html_tags(self: ExtractorsT, tag: List[str]) -> ExtractorsT: ...
def aws_account_id_from_access_key(self: ExtractorsT) -> ExtractorsT: ...
50 changes: 50 additions & 0 deletions chepy/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,3 +819,53 @@ def pick(self, *values: Any):
else: # pragma: no cover
raise TypeError("Input should be a list, dictionary, string, or bytes")
return self

@ChepyDecorators.call_stack
def expand_alpha_range(self, join_by: Union[str, None] = None):
"""Get all alphanumberic or hex chars for the specified range
Args:
join_by (str, optional): Join by. Defaults to Union[str, None].
Returns:
Chepy: The Chepy object.
"""
alph_str = self._convert_to_str()
hold = []

def expand_range(start, end):
return [str(x) for x in range(int(start), int(end) + 1)]

def expand_char_range(start, end):
return [chr(x) for x in range(ord(start), ord(end) + 1)]

hold = []
i = 0
length = len(alph_str)

while i < length:
# Match numerical ranges like 10-20
num_match = re.match(r"(\d+)-(\d+)", alph_str[i:])
if num_match:
start, end = num_match.groups()
hold.extend(expand_range(start, end))
i += len(start) + len(end) + 1 # move past the number range
elif i < length - 2 and alph_str[i + 1] == "-" and alph_str[i] != "\\":
# Handle character ranges like a-z
start = alph_str[i]
end = alph_str[i + 2]
hold.extend(expand_char_range(start, end))
i += 2
elif (
i < length - 2 and alph_str[i] == "\\" and alph_str[i + 1] == "-"
): # pragma: no cover
hold.append("-")
i += 1
else:
hold.append(alph_str[i])
i += 1

if join_by is not None:
hold = join_by.join(hold)
self.state = hold
return self
1 change: 1 addition & 0 deletions chepy/modules/utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ class Utils(ChepyCore):
def drop_bytes(self: UtilsT, start: int, length: int) -> UtilsT: ...
def without(self: UtilsT, *values: Any) -> UtilsT: ...
def pick(self: UtilsT, *values: Any) -> UtilsT: ...
def expand_alpha_range(self, join_by: Union[str, None]=None) -> UtilsT: ...
14 changes: 9 additions & 5 deletions tests/test_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ def test_find_continuous_patterns():

def test_zero_with_chars_tags():
assert (
Chepy("this 󠁮󠁩󠁣is 󠁣󠁻󠀰just 󠁲󠁟󠀱a 󠀵󠁟󠀱simple 󠀷󠁽text file")
.extract_zero_width_chars_tags()
.o
Chepy("this 󠁮󠁩󠁣is 󠁣󠁻󠀰just 󠁲󠁟󠀱a 󠀵󠁟󠀱simple 󠀷󠁽text file").extract_zero_width_chars_tags().o
== b"nicc{0r_15_17}"
)

Expand All @@ -252,7 +250,7 @@ def test_decode_zero_width():
"e2808be2808be2808be2808befbbbfe280ace2808b68656c6c6fe2808be2808be2808be2808befbbbfe2808be2808ce2808be2808be2808be2808be280acefbbbfefbbbfe2808be2808be2808be2808befbbbfe2808defbbbfe2808be2808be2808be2808befbbbfe2808be2808ce2808be2808be2808be2808befbbbfe280ace2808c"
)
.from_hex()
.decode_zero_width("\u200B\u200c\u200d\u202c\ufeff")
.decode_zero_width("\u200b\u200c\u200d\u202c\ufeff")
.o["hidden"]
== "secret"
)
Expand All @@ -271,7 +269,7 @@ def test_decode_zero_width():
)
.from_hex()
.decode_zero_width(
"\u200B\u200C\u200D\u200E\u202A\u202C\u202D\u2062\u2063\ufeff"
"\u200b\u200c\u200d\u200e\u202a\u202c\u202d\u2062\u2063\ufeff"
)
.o["hidden"]
== "secret"
Expand All @@ -296,3 +294,9 @@ def test_html_tags():
def test_html_comments():
c = Chepy("tests/files/test.html").load_file().extract_html_comments()
assert len(c.o) == 3


def test_aws_access_key_from_account_id():
assert (
Chepy("ASIAQNZGKIQY56JQ7WML").aws_account_id_from_access_key().o == 29608264753
)
43 changes: 32 additions & 11 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,38 @@ def test_shuffle():
def test_drop_bytes():
assert Chepy("hello").drop_bytes(2, 2).o == b"heo"


def test_without_pick():
data1 = 'hello'
data2 = [1,2,'a', 'b']
data3 = {'a':1, 2: 3}
data1 = "hello"
data2 = [1, 2, "a", "b"]
data3 = {"a": 1, 2: 3}
# test without
assert Chepy(data1).without('ll').o == b'heo'
assert Chepy(data1).without('l', b'l').o == b'heo'
assert Chepy(data2).without(1, 'a').o == [2,'b']
assert Chepy(data3).without('a').o == {2: 3}
assert Chepy(data1).without("ll").o == b"heo"
assert Chepy(data1).without("l", b"l").o == b"heo"
assert Chepy(data2).without(1, "a").o == [2, "b"]
assert Chepy(data3).without("a").o == {2: 3}
# test pick
assert Chepy(data1).pick('ll').o == b'll'
assert Chepy(data1).pick('l', b'l').o == b'll'
assert Chepy(data2).pick(1, 'a').o == [1,'a']
assert Chepy(data3).pick('a').o == {'a': 1}
assert Chepy(data1).pick("ll").o == b"ll"
assert Chepy(data1).pick("l", b"l").o == b"ll"
assert Chepy(data2).pick(1, "a").o == [1, "a"]
assert Chepy(data3).pick("a").o == {"a": 1}


def test_alpha_range():
assert Chepy("a-e").expand_alpha_range().o == ["a", "b", "c", "d", "e"]
assert Chepy("a-cA-C0-2").expand_alpha_range().o == [
"a",
"b",
"c",
"A",
"B",
"C",
"0",
"1",
"2",
]
assert (
Chepy("a-cA-C0-2").expand_alpha_range("").o
== "".join(["a", "b", "c", "A", "B", "C", "0", "1", "2"]).encode()
)
assert Chepy(" a-c:").expand_alpha_range().o == [" ", "a", "b", "c", ":"]

0 comments on commit aae8022

Please sign in to comment.