Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 43 additions & 58 deletions modules/test/services/python/src/services_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
import xmltodict
from test_module import TestModule
import os
from jinja2 import Environment, FileSystemLoader

LOG_NAME = 'test_services'
MODULE_REPORT_FILE_NAME = 'services_report.html'
MODULE_REPORT_FILE_NAME = 'services_report.j2.html'
NMAP_SCAN_RESULTS_SCAN_FILE = 'services_scan_results.json'
LOGGER = None
REPORT_TEMPLATE_FILE = 'report_template.jinja2'


class ServicesModule(TestModule):
"""Services Test module"""

def __init__(self,
def __init__(self, # pylint: disable=R0917
module,
conf_file=None,
results_dir=None,
Expand All @@ -52,6 +54,22 @@ def __init__(self,
self._run_nmap()

def generate_module_report(self):
# Load Jinja2 template
loader=FileSystemLoader(self._report_template_folder)
template = Environment(loader=loader).get_template(REPORT_TEMPLATE_FILE)
module_header = 'Services Module'
summary_headers = [
'TCP ports open',
'UDP ports open',
'Total ports open',
]
module_data_headers = [
'Port',
'State',
'Service',
'Version',
]

# Use os.path.join to create the complete file path
nmap_scan_results_file = os.path.join(self._nmap_scan_results_path,
NMAP_SCAN_RESULTS_SCAN_FILE)
Expand Down Expand Up @@ -81,65 +99,32 @@ def generate_module_report(self):
else:
udp_open += 1

html_content = '<h4 class="page-heading">Services Module</h4>'

# Add summary table
html_content += (f'''
<table class="module-summary">
<thead>
<tr>
<th>TCP ports open</th>
<th>UDP ports open</th>
<th>Total ports open</th>
</tr>
</thead>
<tbody>
<tr>
<td>{tcp_open}</td>
<td>{udp_open}</td>
<td>{tcp_open + udp_open}</td>
</tr>
</table>
''')
summary_data = [
tcp_open,
udp_open,
tcp_open + udp_open,
]

module_data = []
if (tcp_open + udp_open) > 0:

table_content = '''
<table class="module-data">
<thead>
<tr>
<th>Port</th>
<th>State</th>
<th>Service</th>
<th>Version</th>
</tr>
</thead>
<tbody>'''

for row in nmap_table_data:

table_content += (f'''
<tr>
<td>{row['Port']}/{row['Type']}</td>
<td>{row['State']}</td>
<td>{row['Service']}</td>
<td>{row['Version']}</td>
</tr>''')

table_content += '''
</tbody>
</table>
'''

html_content += table_content

else:

html_content += ('''
<div class="callout-container info">
<div class="icon"></div>
No open ports detected
</div>''')
port = row['Port']
type_ = row['Type']
module_data.append({
'Port': f'{port}/{type_}',
'State': row['State'],
'Service': row['Service'],
'Version': row['Version'],
})

html_content = template.render(
base_template=self._base_template_file,
module_header=module_header,
summary_headers=summary_headers,
summary_data=summary_data,
module_data_headers=module_data_headers,
module_data=module_data,
)

LOGGER.debug('Module report:\n' + html_content)

Expand Down
29 changes: 29 additions & 0 deletions modules/test/services/resources/report_template.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% extends base_template %}
{% block content %}
{% if module_data %}
<table class="module-data">
<thead>
<tr>
{% for header in module_data_headers %}
<th>{{ header }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in module_data %}
<tr>
<td>{{ row['Port'] }}</td>
<td>{{ row['State'] }}</td>
<td>{{ row['Service'] }}</td>
<td>{{ row['Version'] }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<div class="callout-container info">
<div class="icon"></div>
No open ports detected
</div>
{% endif %}
{% endblock %}
5 changes: 4 additions & 1 deletion modules/test/services/services.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ COPY $MODULE_DIR/conf /testrun/conf
COPY $MODULE_DIR/bin /testrun/bin

# Copy over all python files
COPY $MODULE_DIR/python /testrun/python
COPY $MODULE_DIR/python /testrun/python

# Copy Jinja template
COPY $MODULE_DIR/resources/report_template.jinja2 $REPORT_TEMPLATE_PATH/
88 changes: 67 additions & 21 deletions testing/unit/services/reports/services_report_all_closed_local.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,67 @@
<h4 class="page-heading">Services Module</h4>
<table class="module-summary">
<thead>
<tr>
<th>TCP ports open</th>
<th>UDP ports open</th>
<th>Total ports open</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</table>

<div class="callout-container info">
<div class="icon"></div>
No open ports detected
</div>

<div class="page">
<div class="header">
<div class="header-info">
{# Badge #}
<p class="header-info-badge">
{% if json_data['device']['test_pack'] == 'Device Qualification' %}
<img src="data:image/png;base64, {{ icon_qualification }}" alt="">
<span>Device Qualification</span>
{% else %}
<img src="data:image/png;base64, {{ icon_pilot }}" alt="">
<span>Pilot Assessment</span>
{% endif %}
</p>
<span style="font-weight:600">{{ title }}</span>
</div>
<span style="margin-top:0;max-width:700px;font-size:24px;font-weight:600;">
{{ device['manufacturer'] }}
{{ device['model']}}
</span>
<img src="data:image/png;base64, {{ logo }}" alt="Testrun" width="90" style="position: absolute;top: 40%; right: 0px;"></img>
</div>

<div class="module-page-content">

<h4 class="page-heading">Services Module</h4>
<table class="module-summary">

<thead>
<tr>

<th>TCP ports open</th>

<th>UDP ports open</th>

<th>Total ports open</th>

</tr>
</thead>
<tbody>
<tr>

<td>0</td>

<td>0</td>

<td>0</td>

</tr>
</tbody>
</table>


<div class="callout-container info">
<div class="icon"></div>
No open ports detected
</div>


</div>

<div class="footer">
<div class="footer-label">Testrun {{ version }}</div>
<div class="footer-label page-index" style="right: 0px"></div>
</div>
</div>
<div style="break-after:page"></div>
Loading
Loading