cryptosploit_modules.encodings.urlencode

View Source
 0from urllib.parse import unquote, quote
 1
 2from cryptosploit.cprint import Printer
 3from cryptosploit.exceptions import ArgError
 4from cryptosploit_modules import BaseModule
 5
 6
 7class UrlEncoder(BaseModule):
 8    def __init__(self):
 9        super().__init__()
10        self.env.check_var = self.check_var
11
12    @staticmethod
13    def check_var(name, value):
14        """Must return isvalid_variable: bool, error_msg: str"""
15        match name:
16            case "mode":
17                if value in ("encode", "decode"):
18                    return True, ""
19                return False, "May be encode/decode"
20            case "input":
21                if len(bytes(value, encoding="utf-8")) == len(value):
22                    return True, ""
23                return (
24                    False,
25                    "Your string must be a utf-8 string to be processed",
26                )
27            case _:
28                return True, ""
29
30    def encode_command(self, inp):
31        safe_chars = self.env.get_var("safe_chars").value
32        try:
33            Printer.positive(
34                "Encoded string:\n"
35                + quote(inp, safe=safe_chars or "/:?=", encoding="utf-8")
36            )
37        except UnicodeEncodeError as err:
38            raise ArgError("Your string must be a utf-8 string") from err
39
40    def decode_command(self, inp):
41        try:
42            Printer.positive("Decoded string:\n" + unquote(inp, encoding="utf-8"))
43        except UnicodeDecodeError as err:
44            raise ArgError("Your string must be a utf-8 string") from err
45
46    def run(self):
47        mode = self.env.get_var("mode").value
48        inp = self.env.get_var("input").value
49        if mode and inp:
50            func = getattr(self, self.env.get_var("mode").value + "_command")
51            return func(inp)
52        raise ArgError("All variables must be set")
53
54
55module = UrlEncoder
View Source
 8class UrlEncoder(BaseModule):
 9    def __init__(self):
10        super().__init__()
11        self.env.check_var = self.check_var
12
13    @staticmethod
14    def check_var(name, value):
15        """Must return isvalid_variable: bool, error_msg: str"""
16        match name:
17            case "mode":
18                if value in ("encode", "decode"):
19                    return True, ""
20                return False, "May be encode/decode"
21            case "input":
22                if len(bytes(value, encoding="utf-8")) == len(value):
23                    return True, ""
24                return (
25                    False,
26                    "Your string must be a utf-8 string to be processed",
27                )
28            case _:
29                return True, ""
30
31    def encode_command(self, inp):
32        safe_chars = self.env.get_var("safe_chars").value
33        try:
34            Printer.positive(
35                "Encoded string:\n"
36                + quote(inp, safe=safe_chars or "/:?=", encoding="utf-8")
37            )
38        except UnicodeEncodeError as err:
39            raise ArgError("Your string must be a utf-8 string") from err
40
41    def decode_command(self, inp):
42        try:
43            Printer.positive("Decoded string:\n" + unquote(inp, encoding="utf-8"))
44        except UnicodeDecodeError as err:
45            raise ArgError("Your string must be a utf-8 string") from err
46
47    def run(self):
48        mode = self.env.get_var("mode").value
49        inp = self.env.get_var("input").value
50        if mode and inp:
51            func = getattr(self, self.env.get_var("mode").value + "_command")
52            return func(inp)
53        raise ArgError("All variables must be set")
#   UrlEncoder()
View Source
 9    def __init__(self):
10        super().__init__()
11        self.env.check_var = self.check_var
#  
@staticmethod
def check_var(name, value):
View Source
13    @staticmethod
14    def check_var(name, value):
15        """Must return isvalid_variable: bool, error_msg: str"""
16        match name:
17            case "mode":
18                if value in ("encode", "decode"):
19                    return True, ""
20                return False, "May be encode/decode"
21            case "input":
22                if len(bytes(value, encoding="utf-8")) == len(value):
23                    return True, ""
24                return (
25                    False,
26                    "Your string must be a utf-8 string to be processed",
27                )
28            case _:
29                return True, ""

Must return isvalid_variable: bool, error_msg: str

#   def encode_command(self, inp):
View Source
31    def encode_command(self, inp):
32        safe_chars = self.env.get_var("safe_chars").value
33        try:
34            Printer.positive(
35                "Encoded string:\n"
36                + quote(inp, safe=safe_chars or "/:?=", encoding="utf-8")
37            )
38        except UnicodeEncodeError as err:
39            raise ArgError("Your string must be a utf-8 string") from err
#   def decode_command(self, inp):
View Source
41    def decode_command(self, inp):
42        try:
43            Printer.positive("Decoded string:\n" + unquote(inp, encoding="utf-8"))
44        except UnicodeDecodeError as err:
45            raise ArgError("Your string must be a utf-8 string") from err
#   def run(self):
View Source
47    def run(self):
48        mode = self.env.get_var("mode").value
49        inp = self.env.get_var("input").value
50        if mode and inp:
51            func = getattr(self, self.env.get_var("mode").value + "_command")
52            return func(inp)
53        raise ArgError("All variables must be set")

Required to be overridden in the child class. Function called by the user

View Source
 8class UrlEncoder(BaseModule):
 9    def __init__(self):
10        super().__init__()
11        self.env.check_var = self.check_var
12
13    @staticmethod
14    def check_var(name, value):
15        """Must return isvalid_variable: bool, error_msg: str"""
16        match name:
17            case "mode":
18                if value in ("encode", "decode"):
19                    return True, ""
20                return False, "May be encode/decode"
21            case "input":
22                if len(bytes(value, encoding="utf-8")) == len(value):
23                    return True, ""
24                return (
25                    False,
26                    "Your string must be a utf-8 string to be processed",
27                )
28            case _:
29                return True, ""
30
31    def encode_command(self, inp):
32        safe_chars = self.env.get_var("safe_chars").value
33        try:
34            Printer.positive(
35                "Encoded string:\n"
36                + quote(inp, safe=safe_chars or "/:?=", encoding="utf-8")
37            )
38        except UnicodeEncodeError as err:
39            raise ArgError("Your string must be a utf-8 string") from err
40
41    def decode_command(self, inp):
42        try:
43            Printer.positive("Decoded string:\n" + unquote(inp, encoding="utf-8"))
44        except UnicodeDecodeError as err:
45            raise ArgError("Your string must be a utf-8 string") from err
46
47    def run(self):
48        mode = self.env.get_var("mode").value
49        inp = self.env.get_var("input").value
50        if mode and inp:
51            func = getattr(self, self.env.get_var("mode").value + "_command")
52            return func(inp)
53        raise ArgError("All variables must be set")
#   module()
View Source
 9    def __init__(self):
10        super().__init__()
11        self.env.check_var = self.check_var
#  
@staticmethod
def check_var(name, value):
View Source
13    @staticmethod
14    def check_var(name, value):
15        """Must return isvalid_variable: bool, error_msg: str"""
16        match name:
17            case "mode":
18                if value in ("encode", "decode"):
19                    return True, ""
20                return False, "May be encode/decode"
21            case "input":
22                if len(bytes(value, encoding="utf-8")) == len(value):
23                    return True, ""
24                return (
25                    False,
26                    "Your string must be a utf-8 string to be processed",
27                )
28            case _:
29                return True, ""

Must return isvalid_variable: bool, error_msg: str

#   def encode_command(self, inp):
View Source
31    def encode_command(self, inp):
32        safe_chars = self.env.get_var("safe_chars").value
33        try:
34            Printer.positive(
35                "Encoded string:\n"
36                + quote(inp, safe=safe_chars or "/:?=", encoding="utf-8")
37            )
38        except UnicodeEncodeError as err:
39            raise ArgError("Your string must be a utf-8 string") from err
#   def decode_command(self, inp):
View Source
41    def decode_command(self, inp):
42        try:
43            Printer.positive("Decoded string:\n" + unquote(inp, encoding="utf-8"))
44        except UnicodeDecodeError as err:
45            raise ArgError("Your string must be a utf-8 string") from err
#   def run(self):
View Source
47    def run(self):
48        mode = self.env.get_var("mode").value
49        inp = self.env.get_var("input").value
50        if mode and inp:
51            func = getattr(self, self.env.get_var("mode").value + "_command")
52            return func(inp)
53        raise ArgError("All variables must be set")

Required to be overridden in the child class. Function called by the user