Skip to content

Commit 602d19d

Browse files
committed
Enhance Shodan2DB: Improve database schema, optimize JSON parsing, and revamp HTML report template
- Updated database schema in shodan2db.py for better performance and integrity, including new indexes and constraints. - Refactored JSON parsing logic to minimize memory usage and improve error handling. - Introduced a new report template with enhanced styling and layout for better readability and user experience. - Added support for dynamic filtering of hosts in the HTML report. - Improved error messages and validation for file paths and database connections.
1 parent 9603f19 commit 602d19d

5 files changed

Lines changed: 628 additions & 438 deletions

File tree

README.md

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
# Shodan2DB
22

3-
🔌 Shodan export to SQLite database and generate an HTML report.
3+
🔌 Utility designed to parse Shodan JSON exports, store them into an optimized SQLite database, and generate comprehensive HTML vulnerability exposure reports.
44

55
## Purpose
66

7-
The purpose of this tool is to parse Shodan export files and put them into a SQLite database.
7+
The primary objective of this tool is to centralize Shodan export data into a structured SQLite environment for advanced querying and infrastructure monitoring.
88

9-
Exports bypass the restriction on "**vuln**" and "**tag**" tags, which are only available with Small Business, Corporate or Enterprise accounts. These data are included present in Shodan exports.
9+
**Account Restriction Bypass :** Shodan JSON exports natively include valuable fields like `"vulns"` and `"tags"` regardless of your tier. This tool extracts these premium attributes—normally locked behind expensive Small Business, Corporate, or Enterprise API accounts—and surfaces them directly for analysis.
1010

11-
Once in the database, it's easier to analyze the data and extract a list of machines with CVEs.
11+
## Key Features
1212

13-
Generate a report of found CVEs with HTML template.
14-
Customize the report in `templates/` folder using jinja2 and Bulma CSS.
13+
- **Blazing Fast Imports:** Leverages SQLite WAL (Write-Ahead Logging) and atomic transaction batching, reducing disk I/O bottlenecks. Perfect for running on lightweight hardware.
14+
- **Robust Exception Handling:** Implements defensive data extraction wrappers preventing common `KeyError` crashes caused by incomplete Shodan geographic metadata.
15+
- **Dynamic HTML Indexing:** Generates responsive, elegant dashboards styled with **Bulma CSS** and **Bootstrap Icons**, complete with client-side filtering and real-time live search.
16+
- **Strict Data Integrity:** Enforces database-level composite unique constraints (`UNIQUE(ip, cveid)`) preventing redundant storage overhead during overlapping historical imports.
1517

1618
## Requirements
1719

20+
Ensure your local execution environment satisfies the necessary dependencies:
21+
1822
```bash
1923
pip install -r requirements.txt
2024
```
2125

22-
## Usage and options
26+
## Usage and Options
2327

2428
```bash
2529
Usage: shodan2db.py [OPTIONS] COMMAND [ARGS]...
@@ -32,7 +36,7 @@ Commands:
3236
parse Parse the Shodan JSON export file and store data in the database.
3337
```
3438

35-
- *Command parse*
39+
### Command : parse
3640

3741
```bash
3842
Usage: shodan2db.py parse [OPTIONS]
@@ -41,69 +45,87 @@ Usage: shodan2db.py parse [OPTIONS]
4145

4246
Options:
4347
-i, --input-file PATH JSON export file from Shodan. [required]
44-
-d, --database TEXT Database name. [required]
48+
-d, --database TEXT Database name or path. [required]
4549
-v, --verbose Verbose mode.
4650
-h, --help Show this message and exit.
4751
```
4852

49-
- *Command export*
53+
### Command : export
5054

5155
```bash
5256
Usage: shodan2db.py export [OPTIONS]
5357

5458
Generate an HTML report from the data in the database.
5559

5660
Options:
57-
-d, --database PATH Path to the SQLite database file. [required]
58-
-o, --report-file PATH Output path for the HTML report file. [default:
59-
shodan.html]
60-
-t, --template-file PATH Template used for the report. [default:
61-
report.html]
61+
-d, --database PATH Path to the SQLite database file. [required]
62+
-o, --report-file PATH Output path for the HTML report file. [default: shodan.html]
63+
-t, --template-file PATH Path to the Jinja2 template file. [default: templates/report.html]
6264
-v, --verbose Verbose mode.
6365
-h, --help Show this message and exit.
6466
```
6567

6668
## Quickstart
6769

68-
Do a search and click on "**Download Results**".
70+
1. Query Shodan via the web interface and click on **"Download Results"**.
6971

7072
<img src="img/Shodan Export.png">
7173

72-
Select the number of results to download.
74+
2. Select the number of results to download.
7375

7476
<img src="img/Shodan Results.png">
7577

76-
Download your results.
78+
3. Download your results.
7779

7880
<img src="img/Shodan Download.png">
7981

80-
Then import the results into the database using the command :
82+
4. Import your results and compile your threat report utilizing the CLI sequences:
8183

8284
```bash
83-
python .\shodan2db.py parse -i .\example_shodan.json -d .\example_database.db -v
84-
python .\shodan2db.py export -d .\example_database.db -o .\example_report.html -v
85+
# Step 1: Parse and seed your structured SQLite layer
86+
python shodan2db.py parse -i ./example_shodan.json -d ./example_database.db -v
87+
88+
# Step 2: Extract analytics and output your HTML dashboard
89+
python shodan2db.py export -d ./example_database.db -o ./example_report.html -v
8590
```
8691

92+
5. See report file :
93+
94+
- Dataset summary
95+
96+
<img src="img/report.png">
97+
98+
- Host details
99+
100+
<img src="img/report2.png">
101+
87102
**Tags** and **vulns** are visible directly in the **Summary** table.
88103

89104
<img src="img/Summary.png">
90105

91-
Example of report :
106+
## Database Architecture
92107

93-
<img src="img/report.png">
108+
Upon initialization, the tool optimizes SQLite pragmas and automatically structures the underlying relational objects:
94109

95-
## Templates
110+
- **`services`**: Houses core operational network service logs, banners, geo-coordinates, ISP allocations, and metadata.
111+
- **`vulnerabilities`**: Stores indexed granular mappings of CVE IDs alongside their verified CVSS scores and threat summary descriptions.
112+
- **`Summary` (View)**: An internal prioritized virtual evaluation layer sorting network entities by physical exposure risk (`nbvulns DESC`).
96113

97-
Customize the report template in [templates/report.html](templates/report.html)
114+
## Templates Customization
98115

99-
The current one uses [Bulma CSS](https://bulma.io/).
116+
The presentation architecture is entirely modular. You can seamlessly customize the structural layout or interface themes inside `templates/report.html`.
117+
118+
The default layout leverages **Bulma CSS** to render high-contrast, professional cybersecurity matrices with custom conditional coloring for CVSS threat scales.
100119

101120
## Development
102121

103-
Use Shodan2DB as python class
122+
You can import `Shodan2DB` directly into third-party automated playbooks or continuous integration loops as a native Python class:
104123

105124
```python
106-
import shodan2db as sh
125+
from shodan2db import Shodan2DB
107126

108-
sh.Shodan2DB.export(verbose=True, exportfile="test.html", database="test.db")
109-
```
127+
# Programmatically trigger parsing and reporting
128+
Shodan2DB.prepare_database(verbose=True, database="production_audit")
129+
Shodan2DB.parser(verbose=True, inputfile="raw_shodan.json", database="production_audit")
130+
Shodan2DB.export(verbose=True, exportfile="exposure.html", database="production_audit", template_file="templates/report.html")
131+
```

img/report.png

-38.4 KB
Loading

img/report2.png

54.2 KB
Loading

0 commit comments

Comments
 (0)