|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Export all scans on every target with a specific status to CSV format |
| 4 | +
|
| 5 | +Run: |
| 6 | +
|
| 7 | +$ python3 scans_by_state_csv.py -s <STATUS> -o <OUTPUT_FILE_PATH> |
| 8 | +
|
| 9 | +""" |
| 10 | +import argparse |
| 11 | +import requests |
| 12 | +import csv |
| 13 | +from urllib.parse import urljoin |
| 14 | +from datetime import datetime |
| 15 | + |
| 16 | +# Define the JWT or it will be asked when you run the script |
| 17 | +jwt_token = None |
| 18 | + |
| 19 | +api_base_url = 'https://api.probely.com' |
| 20 | +scans_endpoint = urljoin(api_base_url, "scans/?length=1000&page=1&scan_profile_name=true&search=&status={status}&exclude=target_options") |
| 21 | + |
| 22 | +def map_severity(probely_severity): |
| 23 | + if probely_severity == 10: |
| 24 | + return 'Low' |
| 25 | + elif probely_severity == 20: |
| 26 | + return 'Medium' |
| 27 | + elif probely_severity == 30: |
| 28 | + return 'High' |
| 29 | + else: |
| 30 | + return None |
| 31 | + |
| 32 | +def main(): |
| 33 | + parser = argparse.ArgumentParser() |
| 34 | + parser.add_argument('-s', '--status', help='Status', required=True, choices=[ |
| 35 | + 'started', 'paused', 'under_review', 'completed', 'failed', 'canceled']) |
| 36 | + parser.add_argument('-o', '--output', help='Output file', type=argparse.FileType('w'), required=True) |
| 37 | + args = parser.parse_args() |
| 38 | + |
| 39 | + if jwt_token is None: |
| 40 | + token = input("API Token:") |
| 41 | + else: |
| 42 | + token = jwt_token |
| 43 | + |
| 44 | + if token is None or token == '': |
| 45 | + print('Error: JWT is required') |
| 46 | + return |
| 47 | + headers = {'Authorization': "JWT {}".format(token)} |
| 48 | + |
| 49 | + response_scans = requests.get( |
| 50 | + scans_endpoint.format(status=args.status), |
| 51 | + headers=headers |
| 52 | + ) |
| 53 | + response_scans.raise_for_status() |
| 54 | + scans_res = response_scans.json()['results'] |
| 55 | + |
| 56 | + csv_writer = csv.writer( |
| 57 | + args.output, delimiter=",", quotechar='"', quoting=csv.QUOTE_ALL |
| 58 | + ) |
| 59 | + row = ['Target ID', 'Target URL', 'Scan profile', 'Started', 'Completed', 'Status'] |
| 60 | + csv_writer.writerow(row) |
| 61 | + for scan in scans_res: |
| 62 | + row = [ |
| 63 | + scan['target']['id'], |
| 64 | + scan['target']['site']['url'], |
| 65 | + scan['scan_profile']['name'], |
| 66 | + datetime.strptime(scan['started'], "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%Y-%m-%d %H:%M:%S") if scan['started'] is not None else '', |
| 67 | + datetime.strptime(scan['completed'], "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%Y-%m-%d %H:%M:%S") if scan['completed'] is not None else '', |
| 68 | + scan['status'] |
| 69 | + ] |
| 70 | + csv_writer.writerow(row) |
| 71 | + |
| 72 | + print('Done') |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + main() |
0 commit comments