Exemplo n.º 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)])
Exemplo n.º 2
0
def check_stats():
    """Returns a json with the latests statistics."""
    now = datetime.datetime.now()
    nets = models.get_user_networks(current_user.id)
    # order of the days
    order = []
    # net id to name
    net_idname = {}
    net_ids = []
    # per day stats
    type_count_day = {}
    net_count_day = {}
    # per week stats
    type_count_week = {'R': 0, 'O': 0, 'B': 0}
    net_count_week = {}
    for net in nets:
        net_ids.append(int(net['id']))
        net_count_week[net['id']] = 0
        net_idname[net['id']] = net['name']
    for day in range(6, -1, -1): # 6 - 0
        then = now - datetime.timedelta(days=day)
        day_str = then.strftime(const.STRTIME_DAY)
        order.append(day_str)
        type_count_day[day_str] = {'R': 0, 'O': 0, 'B': 0}
        net_count_day[day_str] = {}
        for net in nets:
            net_count_day[day_str][net['id']] = 0
        for event in models.get_user_events_date(current_user.id,
                                                 then.strftime(
                                                     const.STRTIME_DATE)):
            if event['priority'] != 'G':
                type_count_day[day_str][event['priority']] += 1
                type_count_week[event['priority']] += 1
                net_count_day[day_str][event['net'][0]] += 1
                net_count_week[event['net'][0]] += 1
    return jsonify({'status': 200, 'days' : order, 'nets' : net_idname,
                    'net_ids': net_ids, 'type_day': type_count_day,
                    'type_week': type_count_week, 'net_day': net_count_day,
                    'net_week': net_count_week})
Exemplo n.º 3
0
def get_network_options(user_id):
    """Returns the available networks for a given user."""
    options = []
    for network in models.get_user_networks(user_id):
        options.append((network['id'], network['name']))
    return options
Exemplo n.º 4
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))