|
| 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() |
0 commit comments