python-sploitkit icon indicating copy to clipboard operation
python-sploitkit copied to clipboard

Exception when setting boolean option `False` or integer `0` value in Module Option

Open tterletskyy opened this issue 1 year ago • 1 comments

Description

Sploitkit throws an exception when setting the boolean value False or integer 0 for Module Option. When setting True it works correctly. Sploitkit version: sploitkit==0.6.0 Python version: 3.12+

Exceptions

% python -B main.py
Traceback (most recent call last):
  File ".../main.py", line 28, in <module>
    main()
  File ".../main.py", line 23, in main
    test.config['SEND_PAYLOAD'] = False
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File ".../venv_macos31244/lib/python3.12/site-packages/sploitkit/core/components/config.py", line 481, in __setitem__
    return c.__setitem__(key, value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv_macos31244/lib/python3.12/site-packages/sploitkit/core/components/config.py", line 67, in __setitem__
    key = self._setkey(key, value)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv_macos31244/lib/python3.12/site-packages/sploitkit/core/components/config.py", line 159, in _setkey
    raise ValueError(f"Invalid value '{value}' for key '{key.name}'")
ValueError: Invalid value 'False' for key 'SEND_PAYLOAD'

############################################################################################

Traceback (most recent call last):
  File ".../main.py", line 28, in <module>
    main()
  File ".../main.py", line 23, in main
    test.config['SEND_PAYLOAD'] = 0
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File ".../venv_macos31244/lib/python3.12/site-packages/sploitkit/core/components/config.py", line 481, in __setitem__
    return c.__setitem__(key, value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv_macos31244/lib/python3.12/site-packages/sploitkit/core/components/config.py", line 67, in __setitem__
    key = self._setkey(key, value)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../venv_macos31244/lib/python3.12/site-packages/sploitkit/core/components/config.py", line 159, in _setkey
    raise ValueError(f"Invalid value '{value}' for key '{key.name}'")
ValueError: Invalid value '0' for key 'SEND_PAYLOAD'

Code

from pprint import pprint

from sploitkit import Config, Module, Option


class TestModule(Module):
    config = Config(
        {
            Option(
                name='SEND_PAYLOAD',
                description='Boolean or integer value',
                required=True,
            ): None,
        }
    )

    def run(self, *args, **kwargs) -> None:
        pprint(dict(self.config))


def main() -> None:
    test = TestModule()
    test.config['SEND_PAYLOAD'] = False # or 0
    test.run()


if __name__ == '__main__':
    main()

tterletskyy avatar Sep 11 '24 10:09 tterletskyy

Hi @tterletskyy ! Thank you for reporting. Given your setting, you shall use the following :

class TestModule(Module):
    config = Config(
        {
            Option(
                name='SEND_PAYLOAD',
                description='Boolean or integer value',
                required=True,
                transform=lambda s, v: str(v).lower() in ["true", "1"],                # this will ensure the boolean value
                validate=lambda s, v: str(v).lower() in ["false", "true", "0", "1"],   # defines the set of values that can serve as a boolean
            ): None,
        }
    )
[...]

A bit ugly however ; this implementation does not pre-define some data types yet.

dhondta avatar Sep 28 '24 15:09 dhondta