securisec / chepy
1 4
import regex as re
2

3 4
from ..core import ChepyDecorators, ChepyCore
4

5

6 4
class Search(ChepyCore):
7
    """Class that is geared towards regex searches of secrets
8

9
    `Reference <https://github.com/dxa4481/truffleHog>`__
10
    """
11

12 4
    @ChepyDecorators.call_stack
13 4
    def search(self, pattern: str):
14
        """Search. Group matches are returned as tuples.
15
        
16
        Args:
17
            pattern (str): String pattern to search
18
        
19
        Returns:
20
            Chepy: The Chepy object. 
21

22
        Examples:
23
            >>> Chepy("abcdefg123 and again abcdefg123").search("abc(de)fg(12)(3)").o
24
            [('abcdefg123', 'de', '12', '3'), ('abcdefg123', 'de', '12', '3')]
25
        """
26 4
        self.state = re.findall('({})'.format(pattern), self._convert_to_str())
27 4
        return self
28

29 4
    @ChepyDecorators.call_stack
30 4
    def search_ctf_flags(self, prefix: str, postfix: str = ".+?\{*\}"):
31
        """Search CTF style flags. 
32

33
        This by default assumes that the flag format is similar 
34
        to something like picoCTF{some_flag} as an example. 
35
        
36
        Args:
37
            prefix (str): Prefix of the flag. Like `picoCTF`
38
            postfix (str, optional): Regex for the remainder of the flag. 
39
                Defaults to '.+\{.+}'.
40
        
41
        Returns:
42
            Chepy: The Chepy object. 
43

44
        Examples:
45
            >>> Chepy("tests/files/flags").read_file().search_ctf_flags("pico").get_by_index(0)
46
            picoCTF{r3source_pag3_f1ag}
47
        """
48 4
        self.state = re.findall(prefix + postfix, self._convert_to_str(), re.IGNORECASE)
49 4
        return self
50

51 4
    @ChepyDecorators.call_stack
52
    def search_slack_tokens(self):
53
        """Search slack tokens
54
        
55
        Returns:
56
            Chepy: The Chepy object. 
57

58
        Examples:
59
            >>> Chepy("tests/files/flags").read_file().search_slack_tokens().get_by_index(0)
60
            xoxp...859
61
        """
62 4
        self.state = re.findall(
63
            "(xox[p|b|o|a]-[0-9]{12}-[0-9]{12}-[0-9]{12}-[a-z0-9]{32})",
64
            self._convert_to_str(),
65
        )
66 4
        return self
67

68 4
    @ChepyDecorators.call_stack
69
    def search_slack_webhook(self):
70
        """Search slack webhook
71
        
72
        Returns:
73
            Chepy: The Chepy object. 
74
        """
75 4
        self.state = re.findall(
76
            "https://hooks\.slack\.com/services/T[a-zA-Z0-9_]{8}/B[a-zA-Z0-9_]{8}/[a-zA-Z0-9_]{24}",
77
            self._convert_to_str(),
78
        )
79 4
        return self
80

81 4
    @ChepyDecorators.call_stack
82
    def search_private_key(self):
83
        """Search varios private key headers
84
        
85
        Returns:
86
            Chepy: The Chepy object. 
87
        """
88 4
        self.state = re.findall(
89
            "-----BEGIN (RSA|OPENSSH|DSA|EC) PRIVATE KEY-----", self._convert_to_str()
90
        )
91 4
        return self
92

93 4
    @ChepyDecorators.call_stack
94
    def search_twilio_key(self):
95
        """Search for Twilio api key
96
        
97
        Returns:
98
            Chepy: The Chepy object. 
99
        """
100 4
        self.state = re.findall("SK[a-z0-9]{32}", self._convert_to_str())
101 4
        return self
102

103 4
    @ChepyDecorators.call_stack
104
    def search_aws_key(self):
105
        """Search for AWS key id
106
        
107
        Returns:
108
            Chepy: The Chepy object. 
109
        """
110 4
        self.state = re.findall("AKIA[0-9A-Z]{16}", self._convert_to_str())
111 4
        return self

Read our documentation on viewing source code .

Loading