コード例 #1
0
    def test_generate_id(self):
        random_id = generate_id(size=1, chars="a")
        self.assertEqual(random_id, 'a')

        random_id = generate_id(size=10)
        self.assertEqual(len(random_id), 10)
        self.assertTrue(random_id.isalnum())
コード例 #2
0
def generate_inner_report(rep_id, n_results, n_hosts, data, **kwargs):
    report_elem = e.Element('report', attrib={'id': rep_id})
    results_elem = e.SubElement(report_elem, 'results', {
        'max': str(n_results),
        'start': '1'
    })

    # Create Hosts, Ports, Data
    hosts = generate_random_ips(n_hosts)  # Host IPs
    ports = generate_ports(n_hosts)
    oid_dict = {host: [] for host in hosts}
    asset_dict = {host: generate_uuid() for host in hosts}
    host_names = {host: generate_id() for host in hosts}
    max_sev = 0.0

    # Create <result> tags with random data
    for _ in range(n_results):
        host_ip = choice(hosts)
        host_port = choice(ports)
        result_elem, oid, severity = generate_result_elem(
            data["vulns"],
            host_ip,
            host_port,
            asset_dict[host_ip],
            host_names[host_ip],
        )
        if float(severity) > max_sev:
            max_sev = float(severity)

        oid_dict[host_ip].append(oid)
        results_elem.append(result_elem)

    e.SubElement(report_elem, "result_count").text = str(n_results)

    sev_elem = e.Element("severity")
    e.SubElement(sev_elem, "full").text = str(max_sev)
    e.SubElement(sev_elem, "filtered").text = str(max_sev)

    report_elem.append(sev_elem)

    # Create <host> tags with random data
    for host in hosts:
        if len(oid_dict[host]) > 0:
            report_elem.append(
                generate_host_elem(
                    host,
                    oid_dict[host][0],
                    asset_dict[host],
                    host_names[host],
                    data=data,
                    **kwargs,
                ))

    return report_elem
コード例 #3
0
def create_data(gmp, count):
    config_ids = []
    target_ids = []

    for _ in range(0, count):
        name = generate_id()
        gmp.create_credential(
            name,
            login=name,
            password=name,
            credential_type=gmp.types.CredentialType.PASSWORD_ONLY,
        )
    print(str(count) + ' random credentials generated.')

    for _ in range(0, count):
        name = generate_id()
        gmp.create_port_list(name, port_range='T:1-42')
    print(str(count) + ' random port lists generated.')

    for _ in range(0, count):
        name = generate_id()
        res = gmp.create_config('085569ce-73ed-11df-83c3-002264764cea', name)
        config_ids.append(res.xpath('@id')[0])
    print(str(count) + ' random scan configs generated.')

    for _ in range(0, count):
        name = generate_id()
        res = gmp.create_target(name, hosts=['127.0.0.1'])

        target_ids.append(res.xpath('@id')[0])
    print(str(count) + ' random targets generated.')

    for _ in range(0, count):
        name = generate_id()
        config_id = choice(config_ids)
        target_id = choice(target_ids)
        gmp.create_task(
            name, config_id, target_id, '08b69003-5fc2-4037-a479-93b440211c73'
        )
    print(str(count) + ' random tasks generated.')
コード例 #4
0
def generate_result_elem(vulns, host_ip, host_port, host_asset, host_name):
    result_elem = e.Element('result', {'id': generate_uuid()})

    e.SubElement(result_elem, 'name').text = "a_result" + generate_id()
    own = e.SubElement(result_elem, 'owner')
    e.SubElement(own, 'name').text = generate_id()

    elem = e.Element('modification_time')
    e.SubElement(result_elem, 'modification_time').text = (
        time.strftime("%Y-%m-%dT%H:%M:%S%z", time.localtime(time.time()))[:-2]
        + ':00'
    )  # Hell of a Timeformat :D
    e.SubElement(result_elem, 'comment').text = ''
    e.SubElement(result_elem, 'creation_time').text = (
        time.strftime("%Y-%m-%dT%H:%M:%S%z", time.localtime(time.time() - 20))[
            :-2
        ]
        + ':00'
    )

    host_elem = e.Element('host')
    host_elem.text = host_ip
    e.SubElement(host_elem, 'asset', {'asset_id': host_asset}).text = ''
    e.SubElement(host_elem, 'hostname').text = host_name
    result_elem.append(host_elem)

    port_elem = e.Element('port')
    port_elem.text = host_port
    result_elem.append(port_elem)

    nvt = vulns[randrange(len(vulns))]
    e.SubElement(result_elem, 'severity').text = nvt['severity']
    nvt_elem = e.Element('nvt', {'oid': nvt['oid']})
    result_elem.append(nvt_elem)

    e.SubElement(result_elem, 'notes').text = 'TestNotes'

    result_elem.append(elem)

    return result_elem, nvt['oid'], nvt['severity']