Skip to content

Finding Templates API

reptor.api.TemplatesAPI.TemplatesAPI

API client for interacting with SysReptor finding templates.

Example
1
2
3
4
5
6
7
8
9
from reptor import Reptor

reptor = Reptor(
    server=os.environ.get("REPTOR_SERVER"),
    token=os.environ.get("REPTOR_TOKEN"),
)

# TemplatesAPI is available as reptor.api.templates, e.g.:
reptor.api.templates.search()
Source code in reptor/api/TemplatesAPI.py
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class TemplatesAPI(APIClient):
    """API client for interacting with SysReptor finding templates.

    Example:
        ```python
        from reptor import Reptor

        reptor = Reptor(
            server=os.environ.get("REPTOR_SERVER"),
            token=os.environ.get("REPTOR_TOKEN"),
        )

        # TemplatesAPI is available as reptor.api.templates, e.g.:
        reptor.api.templates.search()
        ```
    """
    def __init__(self, **kwargs) -> None:
        super().__init__(**kwargs)

        self.base_endpoint = urljoin(
            self.reptor.get_config().get_server(), "api/v1/findingtemplates/"
        )

    def get_template(self, template_id: str) -> FindingTemplate:
        """Gets a finding template by ID.

        Args:
            template_id (str): Finding template ID

        Returns:
            The FindingTemplate object with all its data

        Example:
            ```python
            reptor.api.templates.get_template("123e4567-e89b-12d3-a456-426614174000")
            ```
        """
        response = self.get(urljoin(self.base_endpoint, template_id))
        return FindingTemplate(response.json())

    def search(self, search_term: str = "") -> typing.List[FindingTemplate]:
        """Searches through the templates using a search term.

        Args:
            search_term (str, optional): Term to search in finding templates. 
                                       Defaults to empty string which returns all templates.

        Returns:
            List of templates matching the search criteria

        Example:
            ```python
            reptor.api.templates.search("SQL Injection")
            ```
        """
        templates_raw = self.get_paginated(self.base_endpoint, params={"search": search_term})
        return [FindingTemplate(template_raw) for template_raw in templates_raw]

    def upload_template(
        self, template: FindingTemplate
    ) -> typing.Optional[FindingTemplate]:
        """Uploads a new Finding Template.

        Args:
            template (FindingTemplate): The template model data to upload

        Returns:
            The uploaded template with server-assigned ID, or None if a template with the same title already exists

        Example:
            ```python
            new_template = FindingTemplate(template_data)
            uploaded = reptor.api.templates.upload_template(new_template)
            if uploaded:
                print(f"Template uploaded with ID: {uploaded.id}")
            ```
        """
        existing_templates = self.search(template.translations[0].data.title)
        if any([t.translations[0].data.title == template.translations[0].data.title for t in existing_templates]):
            self.display(f"Template with title {template.translations[0].data.title} exists. Skipping.")
            return None

        res = self.post(
            self.base_endpoint,
            json=template.to_dict(),
        )
        return FindingTemplate(res.json())

    def delete_template(self, template_id: str) -> None:
        """Deletes a finding template by ID.

        Args:
            template_id (str): Finding template ID

        Returns:
            :

        Example:
            ```python
            reptor.api.templates.delete_template("123e4567-e89b-12d3-a456-426614174000")
            ```
        """
        self.delete(urljoin(self.base_endpoint, template_id))
        return

    def export(self, template_id: str) -> bytes:
        """Exports a template in archive format (tar.gz).

        Args:
            template_id (str): Finding template ID

        Returns:
            The template archive content as bytes

        Example:
            ```python
            archive_data = reptor.api.templates.export("123e4567-e89b-12d3-a456-426614174000")
            with open("template.tar.gz", "wb") as f:
                f.write(archive_data)
            ```
        """
        url = urljoin(self.base_endpoint, f"{template_id}/export/")
        return self.post(url).content

    def get_templates_by_tag(self, tag: str) -> typing.List[FindingTemplate]:
        """Retrieves templates that contain a specific tag.

        Args:
            tag (str): The tag to search for in template tags

        Returns:
            List of templates that contain the specified tag

        Example:
            ```python
            web_templates = reptor.api.templates.get_templates_by_tag("web")
            print(f"Found {len(web_templates)} web-related templates")
            ```
        """
        matched_templates = list()
        for finding_template in self.search(tag):
            if tag in finding_template.tags:
                matched_templates.append(finding_template)
        return matched_templates
        return matched_templates

get_template

get_template(template_id)

Gets a finding template by ID.

Parameters:

  • template_id (str) –

    Finding template ID

Returns:

Example
1
reptor.api.templates.get_template("123e4567-e89b-12d3-a456-426614174000")
Source code in reptor/api/TemplatesAPI.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def get_template(self, template_id: str) -> FindingTemplate:
    """Gets a finding template by ID.

    Args:
        template_id (str): Finding template ID

    Returns:
        The FindingTemplate object with all its data

    Example:
        ```python
        reptor.api.templates.get_template("123e4567-e89b-12d3-a456-426614174000")
        ```
    """
    response = self.get(urljoin(self.base_endpoint, template_id))
    return FindingTemplate(response.json())

search

search(search_term='')

Searches through the templates using a search term.

Parameters:

  • search_term (str, default: '' ) –

    Term to search in finding templates. Defaults to empty string which returns all templates.

Returns:

Example
1
reptor.api.templates.search("SQL Injection")
Source code in reptor/api/TemplatesAPI.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def search(self, search_term: str = "") -> typing.List[FindingTemplate]:
    """Searches through the templates using a search term.

    Args:
        search_term (str, optional): Term to search in finding templates. 
                                   Defaults to empty string which returns all templates.

    Returns:
        List of templates matching the search criteria

    Example:
        ```python
        reptor.api.templates.search("SQL Injection")
        ```
    """
    templates_raw = self.get_paginated(self.base_endpoint, params={"search": search_term})
    return [FindingTemplate(template_raw) for template_raw in templates_raw]

upload_template

upload_template(template)

Uploads a new Finding Template.

Parameters:

Returns:

  • Optional[FindingTemplate]

    The uploaded template with server-assigned ID, or None if a template with the same title already exists

Example
1
2
3
4
new_template = FindingTemplate(template_data)
uploaded = reptor.api.templates.upload_template(new_template)
if uploaded:
    print(f"Template uploaded with ID: {uploaded.id}")
Source code in reptor/api/TemplatesAPI.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def upload_template(
    self, template: FindingTemplate
) -> typing.Optional[FindingTemplate]:
    """Uploads a new Finding Template.

    Args:
        template (FindingTemplate): The template model data to upload

    Returns:
        The uploaded template with server-assigned ID, or None if a template with the same title already exists

    Example:
        ```python
        new_template = FindingTemplate(template_data)
        uploaded = reptor.api.templates.upload_template(new_template)
        if uploaded:
            print(f"Template uploaded with ID: {uploaded.id}")
        ```
    """
    existing_templates = self.search(template.translations[0].data.title)
    if any([t.translations[0].data.title == template.translations[0].data.title for t in existing_templates]):
        self.display(f"Template with title {template.translations[0].data.title} exists. Skipping.")
        return None

    res = self.post(
        self.base_endpoint,
        json=template.to_dict(),
    )
    return FindingTemplate(res.json())

delete_template

delete_template(template_id)

Deletes a finding template by ID.

Parameters:

  • template_id (str) –

    Finding template ID

Returns:

  • None
Example
1
reptor.api.templates.delete_template("123e4567-e89b-12d3-a456-426614174000")
Source code in reptor/api/TemplatesAPI.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def delete_template(self, template_id: str) -> None:
    """Deletes a finding template by ID.

    Args:
        template_id (str): Finding template ID

    Returns:
        :

    Example:
        ```python
        reptor.api.templates.delete_template("123e4567-e89b-12d3-a456-426614174000")
        ```
    """
    self.delete(urljoin(self.base_endpoint, template_id))
    return

export

export(template_id)

Exports a template in archive format (tar.gz).

Parameters:

  • template_id (str) –

    Finding template ID

Returns:

  • bytes

    The template archive content as bytes

Example
1
2
3
archive_data = reptor.api.templates.export("123e4567-e89b-12d3-a456-426614174000")
with open("template.tar.gz", "wb") as f:
    f.write(archive_data)
Source code in reptor/api/TemplatesAPI.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def export(self, template_id: str) -> bytes:
    """Exports a template in archive format (tar.gz).

    Args:
        template_id (str): Finding template ID

    Returns:
        The template archive content as bytes

    Example:
        ```python
        archive_data = reptor.api.templates.export("123e4567-e89b-12d3-a456-426614174000")
        with open("template.tar.gz", "wb") as f:
            f.write(archive_data)
        ```
    """
    url = urljoin(self.base_endpoint, f"{template_id}/export/")
    return self.post(url).content

get_templates_by_tag

get_templates_by_tag(tag)

Retrieves templates that contain a specific tag.

Parameters:

  • tag (str) –

    The tag to search for in template tags

Returns:

  • List[FindingTemplate]

    List of templates that contain the specified tag

Example
1
2
web_templates = reptor.api.templates.get_templates_by_tag("web")
print(f"Found {len(web_templates)} web-related templates")
Source code in reptor/api/TemplatesAPI.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def get_templates_by_tag(self, tag: str) -> typing.List[FindingTemplate]:
    """Retrieves templates that contain a specific tag.

    Args:
        tag (str): The tag to search for in template tags

    Returns:
        List of templates that contain the specified tag

    Example:
        ```python
        web_templates = reptor.api.templates.get_templates_by_tag("web")
        print(f"Found {len(web_templates)} web-related templates")
        ```
    """
    matched_templates = list()
    for finding_template in self.search(tag):
        if tag in finding_template.tags:
            matched_templates.append(finding_template)
    return matched_templates
    return matched_templates