cryptosploit_modules.symmetric.rot

View Source
 0import string
 1from re import compile
 2
 3from cryptosploit_modules import BaseModule
 4from cryptosploit.cprint import Printer
 5
 6
 7class Rot(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        match name:
15            case "mode":
16                if value in ("attack", "decrypt", "encrypt"):
17                    return True, ""
18                return False, "May be attack/decrypt/encrypt"
19            case "key":
20                if not value.startswith("-"):
21                    if not value.isdigit():
22                        return False, "Key must be a natural number!"
23                else:
24                    if not value[1:].isdigit():
25                        return False, "Key must be a natural number!"
26                return True, ""
27            case _:
28                return True, ""
29
30    def encrypt(self):
31        result = ""
32        key = int(self.env.get_var("key").value)
33        alphabet = self.env.get_var("alphabet").value.upper()
34        inp = self.env.get_var("input").value
35        for ind, char in enumerate(inp.upper()):
36            if char in alphabet:
37                res_char = alphabet[(alphabet.find(char) + key) % len(alphabet)]
38                result += res_char if inp[ind].isupper() else res_char.lower()
39            else:
40                result += char
41        return result
42
43    def decrypt(self):
44        key = self.env.get_var("key").value
45        self.env.set_var("key", "-" + key)
46        result = self.encrypt()
47        self.env.set_var("key", key)
48        return result
49
50    def attack(self):
51        results = []
52        pattern = compile(self.env.get_var("contains").value or ".*")
53        alphabet = self.env.get_var("alphabet").value.upper()
54        for alphabet in [
55            alphabet,
56            string.ascii_uppercase,
57            string.ascii_uppercase + string.digits,
58            string.ascii_uppercase + string.digits + string.punctuation,
59        ]:
60            for key in range(len(alphabet)):
61                self.env.set_var("key", str(key))
62                if pattern.match(r := self.decrypt()):
63                    results.append(r)
64        return "\n".join(set(results))
65
66    def run(self):
67        func = getattr(self, self.env.get_var("mode").value)
68        result = func()
69        if result:
70            Printer.positive("Result:\n" + result)
71        else:
72            Printer.negative("Result:\nNone")
73
74
75module = Rot
View Source
 8class Rot(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        match name:
16            case "mode":
17                if value in ("attack", "decrypt", "encrypt"):
18                    return True, ""
19                return False, "May be attack/decrypt/encrypt"
20            case "key":
21                if not value.startswith("-"):
22                    if not value.isdigit():
23                        return False, "Key must be a natural number!"
24                else:
25                    if not value[1:].isdigit():
26                        return False, "Key must be a natural number!"
27                return True, ""
28            case _:
29                return True, ""
30
31    def encrypt(self):
32        result = ""
33        key = int(self.env.get_var("key").value)
34        alphabet = self.env.get_var("alphabet").value.upper()
35        inp = self.env.get_var("input").value
36        for ind, char in enumerate(inp.upper()):
37            if char in alphabet:
38                res_char = alphabet[(alphabet.find(char) + key) % len(alphabet)]
39                result += res_char if inp[ind].isupper() else res_char.lower()
40            else:
41                result += char
42        return result
43
44    def decrypt(self):
45        key = self.env.get_var("key").value
46        self.env.set_var("key", "-" + key)
47        result = self.encrypt()
48        self.env.set_var("key", key)
49        return result
50
51    def attack(self):
52        results = []
53        pattern = compile(self.env.get_var("contains").value or ".*")
54        alphabet = self.env.get_var("alphabet").value.upper()
55        for alphabet in [
56            alphabet,
57            string.ascii_uppercase,
58            string.ascii_uppercase + string.digits,
59            string.ascii_uppercase + string.digits + string.punctuation,
60        ]:
61            for key in range(len(alphabet)):
62                self.env.set_var("key", str(key))
63                if pattern.match(r := self.decrypt()):
64                    results.append(r)
65        return "\n".join(set(results))
66
67    def run(self):
68        func = getattr(self, self.env.get_var("mode").value)
69        result = func()
70        if result:
71            Printer.positive("Result:\n" + result)
72        else:
73            Printer.negative("Result:\nNone")
#   Rot()
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        match name:
16            case "mode":
17                if value in ("attack", "decrypt", "encrypt"):
18                    return True, ""
19                return False, "May be attack/decrypt/encrypt"
20            case "key":
21                if not value.startswith("-"):
22                    if not value.isdigit():
23                        return False, "Key must be a natural number!"
24                else:
25                    if not value[1:].isdigit():
26                        return False, "Key must be a natural number!"
27                return True, ""
28            case _:
29                return True, ""
#   def encrypt(self):
View Source
31    def encrypt(self):
32        result = ""
33        key = int(self.env.get_var("key").value)
34        alphabet = self.env.get_var("alphabet").value.upper()
35        inp = self.env.get_var("input").value
36        for ind, char in enumerate(inp.upper()):
37            if char in alphabet:
38                res_char = alphabet[(alphabet.find(char) + key) % len(alphabet)]
39                result += res_char if inp[ind].isupper() else res_char.lower()
40            else:
41                result += char
42        return result
#   def decrypt(self):
View Source
44    def decrypt(self):
45        key = self.env.get_var("key").value
46        self.env.set_var("key", "-" + key)
47        result = self.encrypt()
48        self.env.set_var("key", key)
49        return result
#   def attack(self):
View Source
51    def attack(self):
52        results = []
53        pattern = compile(self.env.get_var("contains").value or ".*")
54        alphabet = self.env.get_var("alphabet").value.upper()
55        for alphabet in [
56            alphabet,
57            string.ascii_uppercase,
58            string.ascii_uppercase + string.digits,
59            string.ascii_uppercase + string.digits + string.punctuation,
60        ]:
61            for key in range(len(alphabet)):
62                self.env.set_var("key", str(key))
63                if pattern.match(r := self.decrypt()):
64                    results.append(r)
65        return "\n".join(set(results))
#   def run(self):
View Source
67    def run(self):
68        func = getattr(self, self.env.get_var("mode").value)
69        result = func()
70        if result:
71            Printer.positive("Result:\n" + result)
72        else:
73            Printer.negative("Result:\nNone")

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

View Source
 8class Rot(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        match name:
16            case "mode":
17                if value in ("attack", "decrypt", "encrypt"):
18                    return True, ""
19                return False, "May be attack/decrypt/encrypt"
20            case "key":
21                if not value.startswith("-"):
22                    if not value.isdigit():
23                        return False, "Key must be a natural number!"
24                else:
25                    if not value[1:].isdigit():
26                        return False, "Key must be a natural number!"
27                return True, ""
28            case _:
29                return True, ""
30
31    def encrypt(self):
32        result = ""
33        key = int(self.env.get_var("key").value)
34        alphabet = self.env.get_var("alphabet").value.upper()
35        inp = self.env.get_var("input").value
36        for ind, char in enumerate(inp.upper()):
37            if char in alphabet:
38                res_char = alphabet[(alphabet.find(char) + key) % len(alphabet)]
39                result += res_char if inp[ind].isupper() else res_char.lower()
40            else:
41                result += char
42        return result
43
44    def decrypt(self):
45        key = self.env.get_var("key").value
46        self.env.set_var("key", "-" + key)
47        result = self.encrypt()
48        self.env.set_var("key", key)
49        return result
50
51    def attack(self):
52        results = []
53        pattern = compile(self.env.get_var("contains").value or ".*")
54        alphabet = self.env.get_var("alphabet").value.upper()
55        for alphabet in [
56            alphabet,
57            string.ascii_uppercase,
58            string.ascii_uppercase + string.digits,
59            string.ascii_uppercase + string.digits + string.punctuation,
60        ]:
61            for key in range(len(alphabet)):
62                self.env.set_var("key", str(key))
63                if pattern.match(r := self.decrypt()):
64                    results.append(r)
65        return "\n".join(set(results))
66
67    def run(self):
68        func = getattr(self, self.env.get_var("mode").value)
69        result = func()
70        if result:
71            Printer.positive("Result:\n" + result)
72        else:
73            Printer.negative("Result:\nNone")
#   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        match name:
16            case "mode":
17                if value in ("attack", "decrypt", "encrypt"):
18                    return True, ""
19                return False, "May be attack/decrypt/encrypt"
20            case "key":
21                if not value.startswith("-"):
22                    if not value.isdigit():
23                        return False, "Key must be a natural number!"
24                else:
25                    if not value[1:].isdigit():
26                        return False, "Key must be a natural number!"
27                return True, ""
28            case _:
29                return True, ""
#   def encrypt(self):
View Source
31    def encrypt(self):
32        result = ""
33        key = int(self.env.get_var("key").value)
34        alphabet = self.env.get_var("alphabet").value.upper()
35        inp = self.env.get_var("input").value
36        for ind, char in enumerate(inp.upper()):
37            if char in alphabet:
38                res_char = alphabet[(alphabet.find(char) + key) % len(alphabet)]
39                result += res_char if inp[ind].isupper() else res_char.lower()
40            else:
41                result += char
42        return result
#   def decrypt(self):
View Source
44    def decrypt(self):
45        key = self.env.get_var("key").value
46        self.env.set_var("key", "-" + key)
47        result = self.encrypt()
48        self.env.set_var("key", key)
49        return result
#   def attack(self):
View Source
51    def attack(self):
52        results = []
53        pattern = compile(self.env.get_var("contains").value or ".*")
54        alphabet = self.env.get_var("alphabet").value.upper()
55        for alphabet in [
56            alphabet,
57            string.ascii_uppercase,
58            string.ascii_uppercase + string.digits,
59            string.ascii_uppercase + string.digits + string.punctuation,
60        ]:
61            for key in range(len(alphabet)):
62                self.env.set_var("key", str(key))
63                if pattern.match(r := self.decrypt()):
64                    results.append(r)
65        return "\n".join(set(results))
#   def run(self):
View Source
67    def run(self):
68        func = getattr(self, self.env.get_var("mode").value)
69        result = func()
70        if result:
71            Printer.positive("Result:\n" + result)
72        else:
73            Printer.negative("Result:\nNone")

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