cryptosploit_modules.encodings.hex

View Source
 0from binascii import Error
 1from codecs import encode, decode
 2from os.path import isfile
 3
 4from cryptosploit.cprint import Printer
 5from cryptosploit_modules import BaseModule
 6from cryptosploit.exceptions import ArgError
 7
 8
 9class Hex(BaseModule):
10    def __init__(self):
11        super().__init__()
12        self.env.check_var = self.check_var
13
14    @staticmethod
15    def check_var(name, value):
16        """Must return isvalid_variable: bool, error_msg: str"""
17        match name:
18            case "mode":
19                if value in ("to_hex", "from_hex"):
20                    return True, ""
21                return False, "May be to_hex/from_hex"
22            case "input":
23                if len(bytes(value, encoding="utf-8")) == len(value):
24                    return True, ""
25                return (
26                    False,
27                    "Your string must be a utf-8 string or path to the file to be processed",
28                )
29            case _:
30                return True, ""
31
32    def to_hex_command(self, inp, delimiter):
33        if isfile(inp):
34            with open(inp) as f:
35                inp = f.read()
36        inp = bytes(inp, encoding="utf-8")
37        output = encode(inp, "hex").decode("utf-8")
38        output = delimiter + delimiter.join(
39            [output[i : i + 2] for i in range(0, len(output), 2)]
40        )
41        Printer.positive("Encoded string:\n" + output)
42
43    def from_hex_command(self, inp, delimiter):
44        if isfile(inp):
45            with open(inp) as f:
46                inp = f.read()
47        inp = bytes("".join(inp.split(delimiter)), encoding="utf-8")
48        try:
49            output = decode(inp, "hex")
50        except Error as err:
51            raise ArgError("Your input is not valid hex string") from err
52        Printer.positive("Decoded string:\n" + output.decode("utf-8"))
53
54    def run(self):
55        inp = self.env.get_var("input").value
56        delimiter = self.env.get_var("delimiter").value
57        if inp and delimiter:
58            func = getattr(self, self.env.get_var("mode").value + "_command")
59            return func(inp, delimiter)
60        raise ArgError("All variables must be set")
61
62
63module = Hex
View Source
10class Hex(BaseModule):
11    def __init__(self):
12        super().__init__()
13        self.env.check_var = self.check_var
14
15    @staticmethod
16    def check_var(name, value):
17        """Must return isvalid_variable: bool, error_msg: str"""
18        match name:
19            case "mode":
20                if value in ("to_hex", "from_hex"):
21                    return True, ""
22                return False, "May be to_hex/from_hex"
23            case "input":
24                if len(bytes(value, encoding="utf-8")) == len(value):
25                    return True, ""
26                return (
27                    False,
28                    "Your string must be a utf-8 string or path to the file to be processed",
29                )
30            case _:
31                return True, ""
32
33    def to_hex_command(self, inp, delimiter):
34        if isfile(inp):
35            with open(inp) as f:
36                inp = f.read()
37        inp = bytes(inp, encoding="utf-8")
38        output = encode(inp, "hex").decode("utf-8")
39        output = delimiter + delimiter.join(
40            [output[i : i + 2] for i in range(0, len(output), 2)]
41        )
42        Printer.positive("Encoded string:\n" + output)
43
44    def from_hex_command(self, inp, delimiter):
45        if isfile(inp):
46            with open(inp) as f:
47                inp = f.read()
48        inp = bytes("".join(inp.split(delimiter)), encoding="utf-8")
49        try:
50            output = decode(inp, "hex")
51        except Error as err:
52            raise ArgError("Your input is not valid hex string") from err
53        Printer.positive("Decoded string:\n" + output.decode("utf-8"))
54
55    def run(self):
56        inp = self.env.get_var("input").value
57        delimiter = self.env.get_var("delimiter").value
58        if inp and delimiter:
59            func = getattr(self, self.env.get_var("mode").value + "_command")
60            return func(inp, delimiter)
61        raise ArgError("All variables must be set")
#   Hex()
View Source
11    def __init__(self):
12        super().__init__()
13        self.env.check_var = self.check_var
#  
@staticmethod
def check_var(name, value):
View Source
15    @staticmethod
16    def check_var(name, value):
17        """Must return isvalid_variable: bool, error_msg: str"""
18        match name:
19            case "mode":
20                if value in ("to_hex", "from_hex"):
21                    return True, ""
22                return False, "May be to_hex/from_hex"
23            case "input":
24                if len(bytes(value, encoding="utf-8")) == len(value):
25                    return True, ""
26                return (
27                    False,
28                    "Your string must be a utf-8 string or path to the file to be processed",
29                )
30            case _:
31                return True, ""

Must return isvalid_variable: bool, error_msg: str

#   def to_hex_command(self, inp, delimiter):
View Source
33    def to_hex_command(self, inp, delimiter):
34        if isfile(inp):
35            with open(inp) as f:
36                inp = f.read()
37        inp = bytes(inp, encoding="utf-8")
38        output = encode(inp, "hex").decode("utf-8")
39        output = delimiter + delimiter.join(
40            [output[i : i + 2] for i in range(0, len(output), 2)]
41        )
42        Printer.positive("Encoded string:\n" + output)
#   def from_hex_command(self, inp, delimiter):
View Source
44    def from_hex_command(self, inp, delimiter):
45        if isfile(inp):
46            with open(inp) as f:
47                inp = f.read()
48        inp = bytes("".join(inp.split(delimiter)), encoding="utf-8")
49        try:
50            output = decode(inp, "hex")
51        except Error as err:
52            raise ArgError("Your input is not valid hex string") from err
53        Printer.positive("Decoded string:\n" + output.decode("utf-8"))
#   def run(self):
View Source
55    def run(self):
56        inp = self.env.get_var("input").value
57        delimiter = self.env.get_var("delimiter").value
58        if inp and delimiter:
59            func = getattr(self, self.env.get_var("mode").value + "_command")
60            return func(inp, delimiter)
61        raise ArgError("All variables must be set")

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

View Source
10class Hex(BaseModule):
11    def __init__(self):
12        super().__init__()
13        self.env.check_var = self.check_var
14
15    @staticmethod
16    def check_var(name, value):
17        """Must return isvalid_variable: bool, error_msg: str"""
18        match name:
19            case "mode":
20                if value in ("to_hex", "from_hex"):
21                    return True, ""
22                return False, "May be to_hex/from_hex"
23            case "input":
24                if len(bytes(value, encoding="utf-8")) == len(value):
25                    return True, ""
26                return (
27                    False,
28                    "Your string must be a utf-8 string or path to the file to be processed",
29                )
30            case _:
31                return True, ""
32
33    def to_hex_command(self, inp, delimiter):
34        if isfile(inp):
35            with open(inp) as f:
36                inp = f.read()
37        inp = bytes(inp, encoding="utf-8")
38        output = encode(inp, "hex").decode("utf-8")
39        output = delimiter + delimiter.join(
40            [output[i : i + 2] for i in range(0, len(output), 2)]
41        )
42        Printer.positive("Encoded string:\n" + output)
43
44    def from_hex_command(self, inp, delimiter):
45        if isfile(inp):
46            with open(inp) as f:
47                inp = f.read()
48        inp = bytes("".join(inp.split(delimiter)), encoding="utf-8")
49        try:
50            output = decode(inp, "hex")
51        except Error as err:
52            raise ArgError("Your input is not valid hex string") from err
53        Printer.positive("Decoded string:\n" + output.decode("utf-8"))
54
55    def run(self):
56        inp = self.env.get_var("input").value
57        delimiter = self.env.get_var("delimiter").value
58        if inp and delimiter:
59            func = getattr(self, self.env.get_var("mode").value + "_command")
60            return func(inp, delimiter)
61        raise ArgError("All variables must be set")
#   module()
View Source
11    def __init__(self):
12        super().__init__()
13        self.env.check_var = self.check_var
#  
@staticmethod
def check_var(name, value):
View Source
15    @staticmethod
16    def check_var(name, value):
17        """Must return isvalid_variable: bool, error_msg: str"""
18        match name:
19            case "mode":
20                if value in ("to_hex", "from_hex"):
21                    return True, ""
22                return False, "May be to_hex/from_hex"
23            case "input":
24                if len(bytes(value, encoding="utf-8")) == len(value):
25                    return True, ""
26                return (
27                    False,
28                    "Your string must be a utf-8 string or path to the file to be processed",
29                )
30            case _:
31                return True, ""

Must return isvalid_variable: bool, error_msg: str

#   def to_hex_command(self, inp, delimiter):
View Source
33    def to_hex_command(self, inp, delimiter):
34        if isfile(inp):
35            with open(inp) as f:
36                inp = f.read()
37        inp = bytes(inp, encoding="utf-8")
38        output = encode(inp, "hex").decode("utf-8")
39        output = delimiter + delimiter.join(
40            [output[i : i + 2] for i in range(0, len(output), 2)]
41        )
42        Printer.positive("Encoded string:\n" + output)
#   def from_hex_command(self, inp, delimiter):
View Source
44    def from_hex_command(self, inp, delimiter):
45        if isfile(inp):
46            with open(inp) as f:
47                inp = f.read()
48        inp = bytes("".join(inp.split(delimiter)), encoding="utf-8")
49        try:
50            output = decode(inp, "hex")
51        except Error as err:
52            raise ArgError("Your input is not valid hex string") from err
53        Printer.positive("Decoded string:\n" + output.decode("utf-8"))
#   def run(self):
View Source
55    def run(self):
56        inp = self.env.get_var("input").value
57        delimiter = self.env.get_var("delimiter").value
58        if inp and delimiter:
59            func = getattr(self, self.env.get_var("mode").value + "_command")
60            return func(inp, delimiter)
61        raise ArgError("All variables must be set")

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