Automated Report Generation and Visualization
local graph
Chapter 11 — Automated Report Generation and Visualization
Section titled “Chapter 11 — Automated Report Generation and Visualization”In this chapter, you will learn how to simplify the process of creating detailed and insightful security reports by automating repetitive tasks with bash scripting. Writing comprehensive reports can often be time-consuming, but with the right automation techniques, you can save time and maintain consistency across your documentation.
We will guide you through the creation of reusable report templates, show you how to automatically populate these templates with data from your security assessments, and introduce basic techniques for generating visualizations that make your findings more accessible and impactful. By the end of this chapter, you will have the tools needed to produce professional-grade reports efficiently, allowing you to focus more on analysis and less on manual report creation.
In this chapter, we are going to cover the following main topics:
- Creating and using automated report templates
- Extracting key findings with scripting
- Basic data visualization (graphs, charts)
Technical requirements
Section titled “Technical requirements”To follow along in this chapter, you will need the following:
- Kali Linux
- A code editor of your choice, such as Microsoft visual studio code
If you do not have all these requirements, please read Chapter 2 to get them. The code for this chapter is available in this book’s GitHub repository.
Creating and using automated report templates
Section titled “Creating and using automated report templates”Automated report generation starts with templates. A good template provides a consistent structure for your reports, ensuring that all necessary information is included and formatted correctly. In this section, we’ll discuss what report templates are, how to create a basic one, and how to use bash scripting to automate the process of filling these templates with data.
Understanding report templates
Section titled “Understanding report templates”A report template is essentially a predefined structure for your report. It outlines the sections that should be included, such as the introduction, methodology, findings, and conclusion. For a security report, you might also include sections for vulnerabilities discovered, recommendations, and technical details.
Templates help maintain consistency across multiple reports, making them easier to read and compare. In a bash script, a template can be as simple as a text file with placeholders for dynamic content. These placeholders (for example, [REPORT_TITLE], [INTRODUCTION]) are later replaced with actual data during the report generation process.
Here’s an example of a straightforward template structure:
# Report Title: [REPORT_TITLE]## Introduction[INTRODUCTION]## Methodology[METHODOLOGY]## Findings[FINDINGS]## Conclusion[CONCLUSION]## Recommendations[RECOMMENDATIONS]Templates like this one can be adapted for various types of security documentation, from penetration test reports to network security audits. They help ensure that each report covers the necessary sections and is formatted for clarity, which is especially beneficial when sharing results with non-technical stakeholders or clients.
If you’re looking to build more comprehensive templates, resources like the OWASP Penetration Testing Framework (https://owasp.org/www-project-web-security-testing-guide/v42/5-Reporting/README) and Zaproxy (https://www.zaproxy.org/docs/desktop/addons/report-generation/templates/) offer detailed examples that include sections for CVSS scoring, vulnerability classifications, and more. These templates can serve as helpful models for creating detailed reports suitable for larger assessments or complex reporting requirements.
Creating a basic report template
Section titled “Creating a basic report template”To get started, let’s create a basic report template as a text file. You can store this template in a directory dedicated to your reports. Here’s a simple example:
#!/bin/bash# Define the templateTEMPLATE="report_template.txt"# Create the template filecat << EOF > $TEMPLATE# Report Title: [REPORT_TITLE]## Introduction[INTRODUCTION]## Methodology[METHODOLOGY]## Findings[FINDINGS]## Conclusion[CONCLUSION]## Recommendations[RECOMMENDATIONS]EOFecho "Template created: $TEMPLATE"This script creates a text file named report_template.txt with placeholders for various sections. The next step, as we’ll see soon, is to replace these placeholders with actual data.
Automating report generation with bash
Section titled “Automating report generation with bash”Now that we have a template, the next step is to automate the generation of reports using bash. The idea is to replace the placeholders in the template with actual data, such as the results of your security tests or other findings.
Let’s write a bash script that automates this process:
#!/bin/bash# Define the template and output reportTEMPLATE="report_template.txt"OUTPUT="report_$(date +%Y%m%d).md"# Replace placeholders with actual contentsed -e "s/\[REPORT_TITL\]/Automated Security Report/" \ -e "s/\[INTRODUCTION\]/This report details the findings of the recent security assessment./" \ -e "s/\[METHODOLOGY\]/The assessment was conducted using automated tools and manual testing methods./" \ -e "s/\[FINDINGS\]/No critical vulnerabilities were found. However, several medium and low-risk issues were identified./" \ -e "s/\[CONCLUSION\]/Overall, the system is secure with a few areas that could be improved./" \ -e "s/\[RECOMMENDATIONS\]/1. Implement regular updates.\n2. Improve access controls.\n3. Conduct periodic security assessments./" \ $TEMPLATE > $OUTPUTecho "Report generated: $OUTPUT"In this script, the following features are notable:
- We use
sedto replace the placeholders in the template with actual content. - The
datecommand is used to create a unique filename for the report based on the current date. - The final report is saved as a Markdown file (
.md), which can be easily converted to HTML or PDF for better presentation.
Extracting key findings with scripting
Section titled “Extracting key findings with scripting”To generate valuable and actionable security reports, it’s essential to extract key findings from the raw data generated by your tools. This section delves into the process of using bash scripting to parse the outputs of security tools, extract relevant vulnerabilities, and summarize the findings in a format that can be seamlessly integrated into your report templates.
Parsing tool outputs
Section titled “Parsing tool outputs”The first step in extracting key findings is to parse the outputs from the various security tools you use during assessments. These tools often produce results in a variety of formats, such as plain text, XML, JSON, or even HTML. bash provides a suite of powerful text processing utilities—such as grep, awk, sed, and jq —that can help you filter and extract the specific information you need.
Parsing nmap output
Section titled “Parsing nmap output”Let’s consider a scenario where you’ve used nmap to perform a network scan, and you need to extract all open ports from the scan result. Here’s how you can do it using bash:
#!/bin/bash# Run nmap scannmap -oG nmap_scan.txt 192.168.1.0/24# Extract open ports using grep and awkgrep "/open/" nmap_scan.txt | awk '{print $2, $3}' > open_ports.txtecho "Open ports extracted to open_ports.txt"In this script, we can identify the following:
- The
nmapscan results are saved in a Grepable format (oGoption). grepsearches for lines that contain/open/, indicating an open port.awkextracts the relevant fields, such as the IP address and port, and saves them to a file.
Extracting vulnerabilities
Section titled “Extracting vulnerabilities”After parsing the tool outputs, we need to focus on identifying and extracting vulnerabilities. Vulnerability scanning tools like OpenVAS, Nessus, or custom scripts often produce large amounts of data, but not all of it is equally important. Your goal is to filter out the noise and extract the most critical vulnerabilities that need to be addressed.
Extracting vulnerabilities from JSON output
Section titled “Extracting vulnerabilities from JSON output”Let’s say you’re working with a tool that outputs results in JSON format, and you want to extract all vulnerabilities with a Common Vulnerability Scoring System (CVSS) score greater than 7.0, which is typically considered high or critical severity. The CVSS score is a standardized way to measure the severity of vulnerabilities, making it easier to prioritize risks based on their potential impact. If you’re not already familiar with CVSS, knowing these scores helps you focus on addressing the most critical issues first.
You can use jq to parse JSON data and filter out vulnerabilities based on their CVSS scores, as shown:
#!/bin/bash# Parse JSON output to extract high/critical vulnerabilitiesjq '.vulnerabilities[] | select(.cvss >= 7.0) | {id: .id, description: .description, cvss: .cvss}' scan_results.json > high_critical_vulns.jsonecho "High/critical vulnerabilities extracted to high_critical_vulns.json"In this script, note the following:
jqis used to parse the JSON output, selecting vulnerabilities with a CVSS score of 7.0 or higher.- The selected vulnerabilities, along with their IDs, descriptions, and CVSS scores, are saved to a new JSON file.
Summarizing findings
Section titled “Summarizing findings”With your vulnerabilities extracted, the final step is to summarize the findings. Summarizing helps provide a clear and concise overview of the security assessment, highlighting the most critical issues while also providing context for less severe findings. This summary can then be integrated into your report template.
Summarizing findings in bash
Section titled “Summarizing findings in bash”Here’s a simple bash script that counts the number of high, medium, and low-severity vulnerabilities and outputs a summary:
#!/bin/bash# Count vulnerabilities by severityhigh=$(jq '[.vulnerabilities[] | select(.cvss >= 7.0)] | length' scan_results.json)medium=$(jq '[.vulnerabilities[] | select(.cvss >= 4.0 and .cvss < 7.0)] | length' scan_results.json)low=$(jq '[.vulnerabilities[] | select(.cvss < 4.0)] | length' scan_results.json)# Output summaryecho "Vulnerability Summary:"echo "High: $high"echo "Medium: $medium"echo "Low: $low"In this script, we can see the following:
jqis used again to count the number of vulnerabilities in each severity category.- The results are then output as a simple summary that can be included in your report.
Basic data visualization (graphs, charts)
Section titled “Basic data visualization (graphs, charts)”Visualizing data is a powerful way to enhance your reports, making them not only more engaging but also easier to understand. Graphs and charts can help convey complex data in a visually appealing format, highlighting trends, patterns, and outliers that might not be immediately apparent from raw data alone.
When it comes to data visualization in a Linux environment, several tools allow you to generate graphs and charts directly from the command line. Two popular options are Gnuplot and bash scripts combined with text processing tools, such as awk and sed.
Choosing between Gnuplot and bash scripts
Section titled “Choosing between Gnuplot and bash scripts”Gnuplot is a versatile, command-line-based utility that generates both 2D and 3D plots. It supports a wide range of output formats, including PNG, SVG, and PDF, making it an excellent choice for embedding high-quality visualizations in reports. Gnuplot excels when you need visually polished graphs and charts, as it provides advanced customization options for creating detailed plots.
bash scripts with awk and sed, on the other hand, offer a lightweight alternative for simpler visualizations. While they lack the graphical sophistication of Gnuplot, they’re ideal for generating quick ASCII charts or CSV files directly in the terminal. This option can be useful for users who want fast, in-terminal representations of data, or need to export data into formats compatible with spreadsheet software for further analysis.
If you need high-quality, reusable visualizations, Gnuplot is generally the preferred tool. However, for straightforward, in-terminal representations, bash scripts with awk and sed provide a simpler solution that integrates directly with other text-processing tools in Linux.
In this section, we’ll focus on Gnuplot for generating polished graphs and demonstrate how to create simple charts with bash scripts.
Generating graphs with Gnuplot
Section titled “Generating graphs with Gnuplot”Gnuplot is a powerful tool that has been widely used for decades to generate various types of plots, including line graphs, bar charts, scatter plots, and more. It is particularly useful when you need to visualize data directly from the command line, making it an invaluable addition to your bash scripting toolkit.
Gnuplot stands out because of its specific features:
- Cross-platform compatibility: Gnuplot runs on almost any platform, including Linux, Windows, and macOS, making it a versatile tool in any environment.
- Extensive output formats: Whether you need a quick PNG for a report or a high-quality PDF for a presentation, Gnuplot can export your visualizations in numerous formats.
- Scripting capability: With Gnuplot, you can create reusable scripts to automate the generation of graphs, ensuring consistency and efficiency in your reporting workflow.
Generating a simple line graph
Section titled “Generating a simple line graph”Let’s say you’ve collected data on the number of vulnerabilities found in different network segments over time, and you want to visualize this data as a line graph.
- First, create a data file (
vulnerabilities.dat) with the following content:# Time SegmentA SegmentB SegmentC1 5 3 82 7 4 93 6 2 74 8 6 55 9 7 4 - Next, create a Gnuplot script (
plot_vulnerabilities.gnuplot) to generate the line graph:set terminal png size 800,600set output 'vulnerabilities.png'set title "Vulnerabilities Over Time"set xlabel "Time"set ylabel "Number of Vulnerabilities"set gridplot "vulnerabilities.dat" using 1:2 with lines title "Segment A", \"vulnerabilities.dat" using 1:3 with lines title "Segment B", \"vulnerabilities.dat" using 1:4 with lines title "Segment C" - Finally, run the Gnuplot script:
gnuplot plot_vulnerabilities.gnuplot
This will generate a PNG image (vulnerabilities.png) that visualizes the number of vulnerabilities in each network segment over time:

Figure 11.1 – Vulnerability trends across network segments
Figure 11.1 – Vulnerability trends across network segments
Let’s look at another example.
Generating a bar chart
Section titled “Generating a bar chart”Imagine you have conducted several penetration tests across different departments within an organization over the past quarter. You’ve gathered data on the number of critical vulnerabilities discovered in each department, and you want to represent this data as a bar chart to highlight areas that require more immediate attention.
- First, create a data file (
department_vulnerabilities.dat) with the following content:# Department CriticalVulnerabilitiesIT 15HR 7Finance 20Sales 5R&D 12 - Next, create a Gnuplot script (
plot_department_vulnerabilities.gnuplot) to generate the bar chart:set terminal png size 800,600set output 'department_vulnerabilities.png'set title "Critical Vulnerabilities by Department"set boxwidth 0.5set style fill solidset xlabel "Departments"set ylabel "Number of Critical Vulnerabilities"set gridset yrange [0:*]set xtics nomirrorset ytics nomirrorplot "department_vulnerabilities.dat" using 2:xtic(1) with boxes lc rgb "red" title "Critical Vulnerabilities", \'' using 0:2:2 with labels offset 0,1 notitle - Finally, run the Gnuplot script:
gnuplot plot_department_vulnerabilities.gnuplot
This will generate a PNG image (department_vulnerabilities.png) that visually represents the number of critical vulnerabilities found in each department as a bar chart:

Figure 11.2 – Distribution of critical vulnerabilities across departments
Figure 11.2 – Distribution of critical vulnerabilities across departments
The boxwidth and style fill solid commands ensure that the bars are properly formatted and filled, while the yrange [0:*] command ensures that the y-axis starts at 0 and automatically scales to fit the highest value. The plot command also includes a label for each bar, showing the exact number of vulnerabilities above each bar, making the chart more informative.
Creating charts with bash scripts
Section titled “Creating charts with bash scripts”While Gnuplot is great for more complex visualizations, you can also create simple charts directly within your bash scripts. For example, you might want to generate an ASCII bar chart to quickly visualize data in the terminal or create a CSV file that can be opened in a spreadsheet application.
Creating an ASCII bar chart
Section titled “Creating an ASCII bar chart”Here’s a bash script that generates a simple ASCII bar chart showing the number of high, medium, and low-severity vulnerabilities:
#!/bin/bash# Sample datahigh=15medium=10low=5# Function to print barsprint_bar() { local label=$1 local count=$2 echo -n "$label: " for ((i=0; i<count; i++)); do echo -n "#" done echo " ($count)"}# Print the chartecho "Vulnerability Severity Chart"print_bar "High" $highprint_bar "Medium" $mediumprint_bar "Low" $lowRunning this script will output a simple bar chart directly in the terminal:
Vulnerability Severity ChartHigh: ############### (15)Medium: ########## (10)Low: ##### (5)Exporting data to CSV
Section titled “Exporting data to CSV”If you need to create a chart using spreadsheet software, you can export your data to a CSV file with a bash script:
#!/bin/bash# Sample dataecho "Severity,Count" > vulnerability_summary.csvecho "High,15" >> vulnerability_summary.csvecho "Medium,10" >> vulnerability_summary.csvecho "Low,5" >> vulnerability_summary.csvecho "Data exported to vulnerability_summary.csv"You can then open this CSV file in any spreadsheet application, like Excel or Google Sheets, and create charts using the built-in tools.
Summary
Section titled “Summary”That’s it! We have reached the end of this chapter on automated report generation and visualization. I hope you found the content as engaging to read as it was to write. Throughout this chapter, you’ve learned how to automate the tedious aspects of report creation using bash scripting, ensuring that your security reports are both consistent and professional.
By creating automated report templates and populating them with data directly from your security assessments, you’ve significantly reduced the time and effort required to produce high-quality documentation. Additionally, the introduction of basic visualization techniques has provided you with the tools to make your findings more accessible and impactful.
This approach offers significant advantages, including the ability to standardize your reporting process, minimize human error, and enhance the readability of your findings through visual elements. As you continue your journey in ethical hacking, these skills will prove invaluable, allowing you to focus more on analyzing security issues and less on the administrative task of report generation.
With the foundation you’ve built in this chapter, you are now well-equipped to tackle the next stage in your learning journey. The upcoming chapter will immerse you in practical, real-world scenarios where you’ll apply your skills to complete penetration testing lifecycles. You will gain hands-on experience in executing penetration tests from planning to assessment, preparing you for the comprehensive security challenges that lie ahead.