Skip to content

Commit c5c3687

Browse files
JosephPerriSteveMcGrath
authored andcommitted
Create rawexport.py
Create requirements.txt Update requirements.txt Create readme.md Update readme.md Update rawexport.py Update rawexport.py Update rawexport.py Update rawexport.py Update rawexport.py Update readme.md Update rawexport.py
1 parent b0faf83 commit c5c3687

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/python3
2+
3+
from tenable.io import TenableIO
4+
import json
5+
import os
6+
import click
7+
import logging
8+
import time
9+
import shutil
10+
11+
@click.command()
12+
@click.option('--access-key', '-a', help='Tenable.io API Access Key')
13+
@click.option('--secret-key', '-s', help='Tenable.io API Secret Key')
14+
@click.option('--days-since', '-d', type=click.INT, default=90, help='Vulnerability last indexed_at in days')
15+
@click.option('--plugins', '-p', default=None, type=click.STRING, help='Comma separated list of plugin(s)')
16+
@click.option('--include-unlicensed', '-l', is_flag=True, help='Export findings from unlicensed assets')
17+
@click.option('--uuid', '-u', default=None, help='Vuln Export Uuid')
18+
@click.option('--working-dir', '-w', type=click.Path(exists=True), default=os.getcwd(), help='Output directory to write vulns and debug')
19+
@click.option('--threads', '-t', type=click.INT, default=4, help='Number of CPU threads')
20+
@click.option('--zip', '-z', is_flag=True, help='Zip results')
21+
def cli(access_key, secret_key, days_since, plugins, include_unlicensed, uuid, working_dir, threads, zip):
22+
'''
23+
Export -> RAW Writer
24+
25+
Exports raw export chunks to a <current-time>-vulns directory.
26+
'''
27+
28+
# Define output directory that will store the chunk export and pytenable logs.
29+
current_time = int(time.time())
30+
dir_name = f'{current_time}-vulns'
31+
output_dir = os.path.join(working_dir, dir_name)
32+
33+
if not os.path.exists(output_dir):
34+
os.makedirs(output_dir)
35+
36+
# Enable logging
37+
logging.basicConfig(
38+
level=logging.DEBUG,
39+
format='%(asctime)s [%(levelname)s] %(message)s',
40+
handlers=[
41+
logging.FileHandler(os.path.join(output_dir, 'pytenable.logs')),
42+
logging.StreamHandler()
43+
]
44+
)
45+
46+
# Function that writes vuln chunks to the output directory.
47+
def write_chunk(data,
48+
export_uuid: str,
49+
export_type: str,
50+
export_chunk_id: int
51+
):
52+
fn = os.path.join(output_dir, f'{export_type}-{export_uuid}-{export_chunk_id}.json')
53+
with open(fn, 'w') as fobj:
54+
json.dump(data, fobj)
55+
56+
# Convert the days_ago vartaible to unix timestamp
57+
indexed_at = current_time - (days_since * 24 * 60 * 60)
58+
59+
# Initalize TenableIO
60+
tio = TenableIO(access_key, secret_key)
61+
62+
# If export uuid is set:
63+
if uuid:
64+
export = tio.exports.vulns(uuid=uuid)
65+
66+
else:
67+
# Apply plugins filter
68+
if plugins:
69+
plugin_id = plugins.split(",")
70+
export = tio.exports.vulns(plugin_id=plugin_id, indexed_at=indexed_at, include_unlicensed=include_unlicensed, state=['OPEN', 'REOPENED', 'FIXED'])
71+
else:
72+
export = tio.exports.vulns(indexed_at=indexed_at, include_unlicensed=include_unlicensed, state=['OPEN', 'REOPENED', 'FIXED'])
73+
74+
# Export vulns
75+
export.run_threaded(write_chunk, num_threads=threads)
76+
77+
if zip:
78+
shutil.make_archive(output_dir, 'zip', output_dir)
79+
80+
if __name__ == '__main__':
81+
cli()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Vuln Export RAW Generator
2+
3+
Generates a folder called '<current_time>-vulns' and saves vulns export chunks to this folder, along with pyTenable DEBUG logs, and zips results. Supports parameters
4+
such as a Vulns Export UUID, plugin ids, include-unlicensed, and days-since.
5+
## Install
6+
7+
```bash
8+
pip install -r requirements.txt
9+
```
10+
11+
## Usage
12+
13+
Export all vulnerabilities with default settings:
14+
15+
```bash
16+
./rawexport.py --access-key <ACCESS_KEY> --secret-key <SECRET_KEY>
17+
```
18+
19+
Exports all plugin 187240 and 172360 since 180 days ago and write and archive the results to the /tmp directory
20+
21+
```bash
22+
./rawexport.py --access-key <ACCESS_KEY> --secret-key <SECRET_KEY> -w /tmp -p 187240,172360 -d 180 -z
23+
```
24+
25+
In-tool help:
26+
27+
```
28+
Usage: rawexport.py [OPTIONS] OUTPUT
29+
30+
Export -> RAW Writer
31+
32+
Exports raw export chunks to a <current-time>-vulns directory.
33+
34+
Options:
35+
-a, --access-key TEXT Tenable.io API Access Key
36+
-s, --secret-key TEXT Tenable.io API Secret Key
37+
-d, --days-since INTEGER Vulnerability last indexed_at in days
38+
-p, --plugins TEXT Comma separated list of plugin(s)
39+
-l, --include-unlicensed Export findings from unlicensed assets
40+
-u, --uuid TEXT Vuln Export Uuid
41+
-w, --working-dir PATH Output directory to write vulns and debug
42+
-t, --threads INTEGER Number of CPU threads
43+
-z, --zip Zip results
44+
--help Show this message and exit.
45+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
click>=7.0
2+
pytenable>=0.3.15

0 commit comments

Comments
 (0)