Skip to content
Snippets Groups Projects
scan_gvm_library.py 14.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
    from gvm.connections import TLSConnection
    from gvm.protocols.gmpv208 import Gmp, AliveTest 
    from gvm.transforms import EtreeTransform
    from gvm.xml import pretty_print
    from time import time, sleep
    import logging
    import json
    import base64
    
    local_ip = "127.0.0.1"
    connection = TLSConnection(hostname=local_ip)
    transform = EtreeTransform()
    config = {'id':"9866edc1-8869-4e80-acac-d15d5647b4d9"}
    scanner = {'id': "08b69003-5fc2-4037-a479-93b440211c73"}
    ovs_ssh_credential = {'id': "b9af5845-8b87-4378-bca4-cee39a894c17"}
    
    
    def get_version_old(auth_name, auth_passwd):
    	with Gmp(connection, transform=transform) as gmp:
    		gmp.authenticate(auth_name, auth_passwd)	
    		pretty_print(gmp.get_version())
    
    def create_connection(auth_name, auth_passwd):
        connection_retries = 5
        retry = connection_retries
        while(retry > 0):
            try:
                gmp = Gmp(connection, transform=transform)
                gmp.authenticate(auth_name, auth_passwd)
                return gmp
            except:
                logging.warning(f"Connection error with the gmp endpoint. Remaining {retry} retries")
                retry -= 1
                sleep(0.5)
        raise Exception("Impossible connect to the gmp endpoint even after 5 retries")
    
    def get_version():
        gmp = create_connection()
        res = gmp.get_version()
        return res.xpath('version/text()')[0]
    
    ########## PORT LIST ##################################
    
    def create_port_list(port_list_name, ports):
        gmp = create_connection()
        res = gmp.create_port_list(port_list_name, ','.join(ports))
        status = res.xpath('@status')[0]
        status_text = res.xpath('@status_text')[0]
        if status == "201":
            id = res.xpath('@id')[0]
            logging.debug(f'Created port list obj. Name: {port_list_name}, id: {id}, ports: {ports}')
            return {'name': port_list_name, 'id': id}
        else:
            logging.error(f"ERROR during Port list creation. Status code: {status}, msg: {status_text}")
            msg = f"ERROR during Port list creation. Status code: {status}, msg: {status_text}"
            raise Exception(msg) 
    
    def get_port_lists(filter_str="rows=-1"):
        l_o = []
        gmp = create_connection()
        res = gmp.get_port_lists(filter_string=filter_str)
        for pl in res.xpath('port_list'):
            o = dict()
            o['name'] = pl.xpath('name/text()')[0]
            o['id'] = pl.xpath('@id')[0]
            o['in_use'] = pl.xpath('in_use/text()')[0]
            l_o.append(o)
        return l_o
    
    def delete_port_list(port_list):
        gmp = create_connection()
        res = gmp.delete_port_list(port_list['id'])
        status = res.xpath('@status')[0]
        status_text = res.xpath('@status_text')[0]
        if status == "200":
            logging.info(f"Port_list with id: {port_list['id']} and name: {port_list['name']} DELETED") 
        else:
            logging.error(f"ERROR {status}: {status_text}")
    
    def get_or_create_port_list(port_list_name, ports):
        res = get_port_lists(port_list_name)
        if len(res) == 0:
            port_list = create_port_list(port_list_name, ports)
            return get_port_lists(port_list['id'])[0]
        elif len(res) == 1:
            return res[0]
        else:
            logging.warning(f"Found {len(res)} results.")
            return res
    
    ############## TARGET  ##################################
    
    def create_target(name,ip,port_list):
        o = dict()
        gmp = create_connection()
        res = gmp.create_target(
                name=name,
                comment = "",
                hosts=[ip],
                port_list_id = port_list['id'],
                ssh_credential_id = ovs_ssh_credential['id'],
                alive_test=AliveTest('Consider Alive'))
        status = res.xpath('@status')[0]
        status_text = res.xpath('@status_text')[0]
        if status == "201":
            id = res.xpath('@id')[0]
            return {'name': name, 'id': id}
        else:
            msg = f"ERROR during Target creation. Status code: {status}, msg: {status_text}"
            raise Exception(msg) 
    
    def get_targets(filter_str):
        res = []
        gmp = create_connection()
        targets = gmp.get_targets(filter_string=filter_str)
        for target in targets.xpath('target'):
            o = dict()
            o['name'] = target.xpath('name/text()')[0]
            o['hosts'] = target.xpath('hosts/text()')[0]
            o['id'] = target.xpath('@id')[0]
            o['in_use'] = target.xpath('in_use/text()')[0]
            res.append(o)
        return res
    
    def delete_target(target):
        gmp = create_connection()
        res = gmp.delete_target(target['id'])
        status = res.xpath('@status')[0]
        status_text = res.xpath('@status_text')[0]
        if status == "200":
            logging.info(f"Port_list with id: {target['id']} and name: {target['name']} DELETED") 
        else:
            logging.error(f"ERROR {status}: {status_text}")
    
    def get_or_create_target(target_name,ip,port_list):
        res = get_targets(target_name)
        if len(res) == 0:
            t = create_target(target_name,ip,port_list)
            return get_targets(t['id'])[0]
        elif len(res) == 1:
            return res[0]
        else:
            print(f"Found {len(res)} results. Return None")
            return res
    
    def search_and_delete_target(target_name):
        targets = get_targets(target_name)
        if len(targets) == 1:
            delete_target(targets[0]['id'])
        else:
            raise("Multiple results for search")
    
    def search_and_delete_all_targets(target_name):
        targets = get_targets(target_name)
        for target in targets:
            delete_target(target)
    
    ############## TASK ##################################
    
    def create_task(name, target):
        o = dict()
        gmp = create_connection()
        res = gmp.create_task(
                name=name,
                config_id=config['id'],
                target_id=target['id'],
                scanner_id=scanner['id'])
        status = res.xpath('@status')[0]
        status_text = res.xpath('@status_text')[0]
        if status == "201":
            id = res.xpath('@id')[0]
            return {'name': name, 'id': id}
        else:
            msg = f"ERROR during Task creation. Status code: {status}, msg: {status_text}"
            raise Exception(msg)
    
    def get_tasks(filter_str):
        res = []
        gmp = create_connection()
        tasks = gmp.get_tasks(filter_string=filter_str)
        for task in tasks.xpath('task'):
                o = dict()
                o['name'] = task.xpath('name/text()')[0]
                o['id'] = task.xpath('@id')[0]
                o['progress'] = task.xpath('progress/text()')[0]
                o['in_use'] = task.xpath('in_use/text()')[0]
                o['status'] = task.xpath('status/text()')[0]
                o['target_id'] = task.xpath('target/@id')[0]
                try:
                    o['report_id'] = task.xpath('last_report/report/@id')[0]
                except:
                    pass
                res.append(o)
        return res
    
    def get_or_create_task(task_name, target):
        res = get_tasks(task_name)
        if len(res) == 0:
            t = create_task(task_name, target)
            return get_tasks(t['id'])[0]
        elif len(res) == 1:
            return res[0]
        else:
            print(f"Found {len(res)} results. Return None")
            return res
    
    def get_all_tasks():
        res = []
        gmp = create_connection()
        tasks = gmp.get_tasks(filter_string="rows=-1")
        for task in tasks.xpath('task'):
                o = dict()
                o['name'] = task.xpath('name/text()')[0]
                o['id'] = task.xpath('@id')[0]
                o['progress'] = task.xpath('progress/text()')[0]
                o['in_use'] = task.xpath('in_use/text()')[0]
                o['status'] = task.xpath('status/text()')[0]
                o['target_id'] = task.xpath('target/@id')[0]
                try:
                    o['report_id'] = task.xpath('last_report/report/@id')[0]
                except:
                    pass
                res.append(o)
        return res
    
    def search_and_delete_all_tasks(filter_str):
        tasks = get_tasks(filter_str)
        for task in tasks:
            delete_task(task)
    
    def start_task(task):
        gmp = create_connection()
        res = gmp.start_task(task['id'])
        task['report_id'] = res.xpath('report_id/text()')[0]
        return task        
    
    def stop_task(task):
        gmp = create_connection()
        res = gmp.stop_task(task['id'])
        pretty_print(res)
    
    def delete_task(task):
        gmp = create_connection()
        res = gmp.delete_task(task['id'])
        status = res.xpath('@status')[0]
        status_text = res.xpath('@status_text')[0]
        if status == "200":
            logging.info(f"Target with id: {task['id']} and name: {task['name']} DELETED") 
        else:
            logging.error(f"ERROR {status}: {status_text}")
            
    ############## REPORTS #####################################3
    
    class report_formats:
        anonymous_xml = "5057e5cc-b825-11e4-9d0e-28d24461215b"
        csv_results   = "c1645568-627a-11e3-a660-406186ea4fc5"
        itg           = "77bd6c4a-1f62-11e1-abf0-406186ea4fc5"
        pdf           = "c402cc3e-b531-11e1-9163-406186ea4fc5"
        txt           = "a3810a62-1f62-11e1-9219-406186ea4fc5"
        xml           = "a994b278-1f62-11e1-96ac-406186ea4fc5"
    
    def get_report_formats():
        gmp = create_connection()  
        res =  gmp.get_report_formats()
        for f in res.xpath('report_format'):
            name = f.xpath('name/text()')[0]
            id = f.xpath('@id')[0]
            print(id,name)
    
    def get_report_format(id):
        gmp = create_connection()
        res =  gmp.get_report_formats()
        pretty_print(res)  
    
    def get_progress(task):
        task_info = get_tasks(task['id'])[0]
        status = task_info['status']         #   New -> Requested -> Queued -> Running  -> Done
        progress = int(task_info['progress'])#    0         0           0      0 -> 100     -1
        return status, progress
    
    def wait_for_task_ending(task, timeout=3600):
        start_time = time()
        logging.info("Waiting for scans ends the task")
        while True:
            status, progress = get_progress(task)
            if status not in ["New","Requested","Queued","Running","Done"]: # ["Interrupted", ...]
                logging.warning(f"Waiting for scans ends the task. Status: {status}")
                return False
            if status == "Done" and progress == -1:
                logging.info(f"Waiting for scans ends the task. Status: {status}")
                return True
            if time() - start_time > timeout:
                    logging.error("TIMEOUT during waiting for task ending")
                    return False
            logging.debug(f"Waiting for the task ends. Now {int(time() - start_time)}s from start. Status: {status}")
            sleep(10)
        
    def save_report(task,report_format_id, report_filename ):
        gmp = create_connection()
        res = gmp.get_report(task['report_id'],
                                    report_format_id=report_format_id, 
                                    ignore_pagination=True,
                                    details="1")
        code = str(res.xpath('report/text()')[0])
        with open(report_filename, "wb") as fh:
            fh.write(base64.b64decode(code))
    
    def save_severity_report(task, severity_filename):
        dict_severity = {"Log": 0, "Low": 1, "Medium": 2, "High": 3}
        gmp = create_connection()
        res = gmp.get_report(task['report_id'],
                            report_format_id=report_formats.anonymous_xml, 
                            ignore_pagination=True,
                            details="1")
        severities = res.xpath('report/report/ports/port/threat/text()')
        old_num_severity = 0
        severity = "Log"
        for sev in severities:
            if dict_severity[sev] > old_num_severity:
                old_num_severity = dict_severity[sev]
                severity = sev
        with open(severity_filename, "w") as f:
            f.write(severity)
    
    def get_report_info(task):
        report = dict()
        gmp = create_connection()
        res = gmp.get_report(task['report_id'],
                            report_format_id=report_formats.anonymous_xml,
                            ignore_pagination=True,
                            details="1")
        threats = res.xpath('report/report/ports/port/threat/text()')
        ports = res.xpath('report/report/ports/port/text()')
        severities = res.xpath('report/report/ports/port/severity/text()')
        severities = list(map(lambda a : float(a), severities))
        for p,t,s in zip(ports, threats, severities):
            report[p] = {'severity': s, 'threat': t}
        glob_severity = -1 # returned severities are null or positive
        glob_threat = 'Log'
        for threat,severity in zip(threats,severities):
            if severity > glob_severity:
                glob_severity = severity
                glob_threat = threat
                glob_severity = severity
    
        report['global'] = {'threat': glob_threat, 'severity': glob_severity}
        return report
    
                
    def get_reports(filter_str="rows=-1"):
        lo = []
        gmp = create_connection()
        reports = gmp.get_reports(filter_string = filter_str)
        for report in reports.xpath('report'):
            o = dict()
            o['task_name'] = report.xpath('task/name/text()')[0]
            o['id'] = report.xpath('@id')[0]
            lo.append(o)
        return lo
    
    def get_numeric_severity(severity):
        if severity == "Log":
            return 0
        elif severity == "Low":
            return 1
        elif severity == "Medium":
            return 2
        elif severity == "High":
            return 3
        else:
            return 4
    
    def get_severity_from_number(num):
        if num == 0:
            return "Low"
        elif num == 1:
            return "Low"
        elif num == 2:
            return "Medium"
        elif num == 3:
            return "High"
        else:
            return "Undefined"
    
    def process_global_reports_info(reports):
        glob_severity = -1
        glob_threat = 'Log'
        for host in reports:
            host_glob_severity = reports[host]['global']['severity']
            if host_glob_severity > glob_severity:
                glob_severity = host_glob_severity
                glob_threat = reports[host]['global']['threat']
        reports['deployment'] = {'severity': glob_severity, 
                                 'threat': glob_threat}
        if reports['deployment']['severity'] < 4:
            reports['global'] = "OK"
        else:
            reports['global'] = "NOK"
        return reports
    
    def pretty_json(j):
        return json.dumps(j,sort_keys=True,indent=4)
            
    def import_dep_info(file_path, endpoints_to_scan):
        with open(file_path) as f:
            data = json.load(f)    
        endpoints = dict()
        for key in data['outputs'].keys():
            if key in endpoints_to_scan:
                endpoint = str(data['outputs'][key])
                prefix,url = endpoint.split("://")
                if ":" in url:
                    host,port = url.split(":")
                else:
                    host = url
                    if prefix == "https":
                        port = '443'
                    elif prefix == 'http':
                        port = '80'
                    else:
                        raise Exception(f"Impossible to parse the endpoint port. Endpoint: {endpoint}")
                logging.info(f"Endpoint: {host}:{port}")
                if host not in endpoints:
                    endpoints[host] = {"22"}
                endpoints[host].add(port)
        for host,ports in endpoints.items():
            endpoints[host] = sorted(list(ports))
        return endpoints