Skip to content

Commit

Permalink
refactor: making mmh3 an optional dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
13ph03nix committed Jul 26, 2023
1 parent 3542809 commit 08b1749
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
22 changes: 16 additions & 6 deletions pocsuite3/lib/yaml/nuclei/protocols/common/expressions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from typing import get_type_hints, Union

import chardet
import mmh3 as py_mmh3
from pkg_resources import parse_version
from pocsuite3.lib.core.log import LOGGER as logger
from pocsuite3.lib.yaml.nuclei.protocols.common.expressions.safe_eval import safe_eval
Expand Down Expand Up @@ -120,9 +119,11 @@ def base64_py(src: Union[bytes, str]) -> str:
Example:
Input: base64_py("Hello")
Output: SGVsbG8=
Output: "SGVsbG8=\n"
"""
return base64(src)
if not isinstance(src, bytes):
src = src.encode('utf-8')
return py_built_in_base64.encodebytes(src).decode('utf-8')


@auto_convert_types
Expand Down Expand Up @@ -373,15 +374,24 @@ def md5(inp: Union[str, bytes]) -> str:


@auto_convert_types
def mmh3(inp: str) -> int:
def mmh3(inp: str) -> str:
"""
Calculates the MMH3 (MurmurHash3) hash of an input
Example:
Input: mmh3("Hello")
Output: 316307400
Output: "316307400"
"""
return py_mmh3.hash(inp)

try:
import mmh3 as py_mmh3
except ImportError:
logger.error('Python extension for MurmurHash (MurmurHash3) is not installed. '
'Reason: https://github.com/knownsec/pocsuite3/issues/359, '
'You can locate the packages here: https://pypi.org/project/mmh3/')
return "0"

return str(py_mmh3.hash(inp))


def print_debug(*args) -> None:
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@ pycryptodomex >= 3.9.0
dacite >= 1.6.0
PyYAML >= 6.0
lxml >= 4.6.0
mmh3 >= 3.0.0
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ def find_packages(where='.'):
"dacite",
"PyYAML",
"lxml",
"mmh3"
],
extras_require={
'complete': [
'pyOpenSSL',
'jq'
'jq',
'mmh3'
],
}
)
4 changes: 2 additions & 2 deletions tests/test_nuclei_helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_base64_decode(self):
self.assertEqual(base64_decode("SGVsbG8="), b"Hello")

def test_base64_py(self):
self.assertEqual(base64_py("Hello"), "SGVsbG8=")
self.assertEqual(base64_py("Hello"), "SGVsbG8=\n")

def test_concat(self):
self.assertEqual(concat("Hello", 123, "world"), "Hello123world")
Expand Down Expand Up @@ -74,7 +74,7 @@ def test_md5(self):
self.assertEqual(md5("Hello"), "8b1a9953c4611296a827abf8c47804d7")

def test_mmh3(self):
self.assertEqual(mmh3("Hello"), 316307400)
self.assertEqual(mmh3("Hello"), "316307400")

def test_rand_base(self):
self.assertRegex(rand_base(5, "abc"), r"[abc]{5}")
Expand Down

0 comments on commit 08b1749

Please sign in to comment.