示例#1
0
def generate_mockup_data(networks=2, days=7, events_day=10, entries=5, hosts=5,
                         services=3):
    """Generate mockup data."""
    users = models.User.query.all()
    for user in users:
        sample_services = ['MySQL', 'Chrome', 'Firefox', 'Safari', 'Nginx',
                           'Apache', 'SSL', 'Android', 'Windows', 'Linux']
        for _ in itertools.repeat(None, hosts):
            chosen_services = []
            for _ in itertools.repeat(None, services):
                chosen_services.append(random.choice(sample_services))
            models.set_host(user.id,
                            ''.join(random.choice(
                                string.letters) for _ in range(15)),
                            chosen_services)
        for net in range(networks):
            dns2 = '8.8.8.8.6' if net % 2 == 0 else None
            models.set_network(str(user.id), 'Home ' + str(net +1),
                               'eth' + str(net +1),
                               'FC:AA:25:32:FC:' + str(random.randint(10, 32)),
                               '1000 Mb/s', 'None',
                               '192.168.1.' + str(random.randint(1, 254)),
                               '255.255.255.0', '192.168.1.1', '8.8.8.8.8',
                               dns2=dns2)
            now = datetime.datetime.now()
            for day in range(days):
                then = now - datetime.timedelta(days=day)
                for _ in itertools.repeat(None, events_day):
                    for network in models.get_user_networks(user.id):
                        models.save_event(user.id, network['id'],
                                          'Event ' + str(random.randint(1, 100)),
                                          then.strftime(const.STRTIME_DATE),
                                          then.strftime(const.STRTIME_DAY),
                                          then.strftime(const.STRTIME_TIME),
                                          const.PRIORITY_COLOUR[
                                              random.randint(0, 3)])
        netids = [net['id'] for net in models.get_user_networks(user.id)]
        for let in ['W', 'B']:
            models.save_entry(str(user.id), let,
                              ''.join(random.choice(string.letters) for _ in range(15)),
                              'FC:AA:25:32:FC:' + str(random.randint(10, 32)),
                              '192.168.1.' + str(random.randint(1, 254)),
                              [random.choice(netids)])
        for _ in itertools.repeat(None, entries - 2):
            models.save_entry(str(user.id), random.choice(['B', 'W']),
                              ''.join(random.choice(string.letters) for _ in range(15)),
                              'FC:AA:25:32:FC:' + str(random.randint(10, 32)),
                              '192.168.1.' + str(random.randint(1, 254)),
                              [random.choice(netids)])
示例#2
0
def dashboard(section, id):
    """Shows the app's dashboard. """
    events, lastkey, alerts, hosts, networks, acls, scans, stats = (None,) * 8
    vulns, buoys, faddnet, faddentry, faddhost = (None,) * 5
    # Checks if the user can create acl entries or manage scans (nets required)
    can_acl, can_manage = (False,) * 2
    if section != const.SEC_TIMELINE:
        red_p.unsubscribe(const.CHAN_TIMELINE)
    if request.method == 'GET':
        if section == const.SEC_TIMELINE:
            now = datetime.datetime.now()
            events = {}
            for i in range(const.TIMELINE_DAYS):
                date = now - datetime.timedelta(days=i)
                tempevents = models.get_user_events_date(
                    current_user.id, date.strftime(const.STRTIME_DATE))
                if len(tempevents) > 0:
                    events[date.strftime(const.STRTIME_DATE)] = tempevents
                    lastkey = date.strftime(const.STRTIME_DATE)
        elif section == const.SEC_ALERTS:
            faddhost = AddHostForm(prefix='add-host-f')
            alerts, hosts, vulns = _get_sec_alerts()
        elif section == const.SEC_NETWORKS:
            faddnet = AddNetworkForm(prefix='add-net-f')
            networks = models.get_user_networks(current_user.id)
        elif section == const.SEC_ACLS:
            faddentry = AddCALEntryForm(prefix='add-entry-f').new(
                current_user.id)
            can_acl = models.get_count_user_networks(current_user.id) > 0
            acls = {'W': models.get_entries('W', current_user.id),
                    'B': models.get_entries('B', current_user.id)}
        elif section == const.SEC_SCANS:
            can_manage = models.get_count_user_networks(current_user.id) > 0
            if can_manage:
                buoys = []
                for api_id in models.get_user_api_keys(current_user.id):
                    temp = models.get_api_key(api_id)
                    if temp:
                        temp_net = models.get_network(temp['network'])
                        if temp_net:
                            buoys.append({'id': api_id,
                                          'netname': temp_net['name'],
                                          'netid': temp_net['id'],
                                          'status': temp['status'],
                                          'host': temp_net['address'],
                                          'lastscan': temp['lastscan'],
                                          'generated': temp['generated'],
                                          'key': temp['key']})
        elif section == const.SEC_STATS:
            pass
        else:
            abort(404)
        return render_template('dashboard.html', section=section, events=events,
                               lastkey=lastkey, cves=alerts, nets=networks,
                               acls=acls, canacl=can_acl, scans=scans,
                               stats=stats, hosts=hosts, vulns=vulns,
                               faddnet=faddnet, faddentry=faddentry,
                               faddhost=faddhost,
                               can_manage=can_manage, net_buoys=buoys)
    elif request.method == 'POST':
        neterrors, entryerrors, entryneterror, entryincon = (False, ) * 4
        hosterrors = False
        if section == const.SEC_NETWORKS:
            faddnet = AddNetworkForm(request.form, prefix='add-net-f')
            if faddnet.validate_on_submit():
                nname = faddnet.name.data
                ipaddress = faddnet.ipaddress.data
                models.set_network(current_user.id, nname, '', '', '', '',
                                   ipaddress, '', '', '')
            else:
                neterrors = True
            networks = models.get_user_networks(current_user.id)
        elif section == const.SEC_ALERTS:
            faddhost = AddHostForm(request.form, prefix='add-host-f')
            if faddhost.validate_on_submit():
                hname = faddhost.name.data
                hservices = faddhost.services.data
                hservices = hservices.split(',')
                hservs_clean = [x.strip() for x in hservices]
                models.set_host(current_user.id, hname.strip(), hservs_clean)
                hosts = models.get_user_hosts(current_user.id)
            else:
                hosterrors = True
            alerts, hosts, vulns = _get_sec_alerts()
        elif section == const.SEC_ACLS:
            faddentry = AddCALEntryForm(request.form, prefix='add-entry-f').new(
                current_user.id)
            can_acl = models.get_count_user_networks(current_user.id) > 0
            networks = faddentry.networks.data
            mac = (faddentry.mac.data).lower()
            list_type = faddentry.type.data
            if faddentry.validate_on_submit() and networks and len(networks) > 0:
                entryincon = models.save_entry(current_user.id, list_type,
                                              '', mac, '', networks)
                entryincon = not entryincon # the method returned consistent
            else:
                entryneterror = len(networks) == 0
                entryerrors = True
            acls = {'W': models.get_entries('W', current_user.id),
                    'B': models.get_entries('B', current_user.id)}
        return render_template('dashboard.html', section=section,
                               events=events, lastkey=lastkey,
                               cves=alerts, nets=networks, acls=acls,
                               canacl=can_acl, scans=scans, stats=stats,
                               hosts=hosts, vulns=vulns,
                               faddhost=faddhost, hosterrors=hosterrors,
                               faddnet=faddnet, neterrors=neterrors,
                               faddentry=faddentry, entryerrors=entryerrors,
                               entryneterror=entryneterror,
                               inconsistent=entryincon)
    elif request.method == 'DELETE':
        if section == const.SEC_NETWORKS:
            if id is not None and len(id) > 0:
                models.delete_network(current_user.id, id)
                networks = models.get_user_networks(current_user.id)
        elif section == const.SEC_ALERTS:
            if id is not None and len(id) > 0:
                models.delete_host(current_user.id, id)
                hosts = models.get_user_hosts(current_user.id)
        elif section == const.SEC_ACLS:
            if id is not None and len(id) > 0:
                models.delete_entry(current_user.id, id)
                acls = {'W': models.get_entries('W', current_user.id),
                        'B': models.get_entries('B', current_user.id)}
        return render_template('dashboard.html', section=section, events=events,
                               lastkey=lastkey, cves=alerts, nets=networks,
                               acls=acls, canacl=can_acl, scans=scans,
                               stats=stats, hosts=hosts, vulns=vulns,
                               faddhost=AddHostForm(prefix='add-host-f'),
                               faddnet=AddNetworkForm(prefix='add-net-f'),
                               faddentry=AddCALEntryForm(
                                   prefix='add-entry-f').new(current_user.id))