Пример #1
0
def parse_row_to_bound_ip_form(request, rowData, cache):
    """
    Parse a row out of mass object adder into the
    :class:`crits.ips.forms.AddIPForm`.

    :param request: The Django request.
    :type request: :class:`django.http.HttpRequest`
    :param rowData: The data for that row.
    :type rowData: dict
    :param cache: Cached data, typically for performance enhancements
                  during bulk operations.
    :type cache: dict
    :returns: :class:`crits.ips.forms.AddIPForm`.
    """

    # TODO Add common method to convert data to string
    ip = rowData.get(form_consts.IP.IP_ADDRESS, "")
    ip_type = rowData.get(form_consts.IP.IP_TYPE, "")
    # analyst = rowData.get(form_consts.IP.ANALYST, "")
    analyst = request.user
    campaign = rowData.get(form_consts.IP.CAMPAIGN, "")
    confidence = rowData.get(form_consts.IP.CAMPAIGN_CONFIDENCE, "")
    source = rowData.get(form_consts.IP.SOURCE, "")
    source_method = rowData.get(form_consts.IP.SOURCE_METHOD, "")
    source_reference = rowData.get(form_consts.IP.SOURCE_REFERENCE, "")
    is_add_indicator = convert_string_to_bool(
        rowData.get(form_consts.IP.ADD_INDICATOR, "False"))
    indicator_reference = rowData.get(form_consts.IP.INDICATOR_REFERENCE, "")
    bucket_list = rowData.get(form_consts.Common.BUCKET_LIST, "")
    ticket = rowData.get(form_consts.Common.TICKET, "")

    data = {
        'ip': ip,
        'ip_type': ip_type,
        'analyst': analyst,
        'campaign': campaign,
        'confidence': confidence,
        'source': source,
        'source_method': source_method,
        'source_reference': source_reference,
        'add_indicator': is_add_indicator,
        'indicator_reference': indicator_reference,
        'bucket_list': bucket_list,
        'ticket': ticket
    }

    bound_form = cache.get('ip_form')

    if bound_form == None:
        bound_form = AddIPForm(request.user, None, data)
        cache['ip_form'] = bound_form
    else:
        bound_form.data = data

    bound_form.full_clean()
    return bound_form
Пример #2
0
def parse_row_to_bound_ip_form(request, rowData, cache):
    """
    Parse a row out of mass object adder into the
    :class:`crits.ips.forms.AddIPForm`.

    :param request: The Django request.
    :type request: :class:`django.http.HttpRequest`
    :param rowData: The data for that row.
    :type rowData: dict
    :param cache: Cached data, typically for performance enhancements
                  during bulk operations.
    :type cache: dict
    :returns: :class:`crits.ips.forms.AddIPForm`.
    """

    # TODO Add common method to convert data to string
    ip = rowData.get(form_consts.IP.IP_ADDRESS, "")
    ip_type = rowData.get(form_consts.IP.IP_TYPE, "")
    # analyst = rowData.get(form_consts.IP.ANALYST, "")
    analyst = request.user
    campaign = rowData.get(form_consts.IP.CAMPAIGN, "")
    confidence = rowData.get(form_consts.IP.CAMPAIGN_CONFIDENCE, "")
    source = rowData.get(form_consts.IP.SOURCE, "")
    source_method = rowData.get(form_consts.IP.SOURCE_METHOD, "")
    source_reference = rowData.get(form_consts.IP.SOURCE_REFERENCE, "")
    is_add_indicator = convert_string_to_bool(rowData.get(form_consts.IP.ADD_INDICATOR, "False"))
    indicator_reference = rowData.get(form_consts.IP.INDICATOR_REFERENCE, "")
    bucket_list = rowData.get(form_consts.Common.BUCKET_LIST, "")
    ticket = rowData.get(form_consts.Common.TICKET, "")

    data = {
        "ip": ip,
        "ip_type": ip_type,
        "analyst": analyst,
        "campaign": campaign,
        "confidence": confidence,
        "source": source,
        "source_method": source_method,
        "source_reference": source_reference,
        "add_indicator": is_add_indicator,
        "indicator_reference": indicator_reference,
        "bucket_list": bucket_list,
        "ticket": ticket,
    }

    bound_form = cache.get("ip_form")

    if bound_form == None:
        bound_form = AddIPForm(request.user, None, data)
        cache["ip_form"] = bound_form
    else:
        bound_form.data = data

    bound_form.full_clean()
    return bound_form
Пример #3
0
def bulk_add_ip(request):
    """
    Bulk add IPs via a bulk upload form.

    Args:
        request: The Django context which contains information about the
            session and key/value pairs for the bulk add IPs request

    Returns:
        If the request is not a POST and not a Ajax call then:
            Returns a rendered HTML form for a bulk add of IPs
        If the request is a POST and a Ajax call then:
            Returns a response that contains information about the
            status of the bulk uploaded IPs. This may include information
            such as IPs that failed or successfully added. This may
            also contain helpful status messages about each operation.
    """

    formdict = form_to_dict(AddIPForm(request.user, None))

    if request.method == "POST" and request.is_ajax():
        response = process_bulk_add_ip(request, formdict)

        return HttpResponse(json.dumps(response, default=json_handler),
                            content_type="application/json")
    else:
        return render_to_response(
            'bulk_add_default.html', {
                'formdict': formdict,
                'title': "Bulk Add IPs",
                'table_name': 'ip',
                'local_validate_columns': [form_consts.IP.IP_ADDRESS],
                'is_bulk_add_objects': True
            }, RequestContext(request))
Пример #4
0
def add_update_ip(request, method):
    """
    Add/update an IP address. Should be an AJAX POST.

    :param request: Django request.
    :type request: :class:`django.http.HttpRequest`
    :param method: If this is an "add" or an "update".
    :type method: str
    :returns: :class:`django.http.HttpResponse`
    """

    if request.method == "POST" and request.is_ajax():
        request.user._setup()
        data = request.POST
        form = AddIPForm(request.user, None, data)

        if form.is_valid():
            cleaned_data = form.cleaned_data
            ip = cleaned_data['ip']
            source_name = cleaned_data['source_name']
            source_reference = cleaned_data['source_reference']
            source_method = cleaned_data['source_method']
            source_tlp= cleaned_data['source_tlp']
            campaign = cleaned_data['campaign']
            confidence = cleaned_data['confidence']
            ip_type = cleaned_data['ip_type']
            add_indicator = False
            if cleaned_data.get('add_indicator'):
                add_indicator = True
            indicator_reference = cleaned_data.get('indicator_reference')
            bucket_list = cleaned_data.get(form_consts.Common.BUCKET_LIST_VARIABLE_NAME)
            ticket = cleaned_data.get(form_consts.Common.TICKET_VARIABLE_NAME)
            related_id = cleaned_data['related_id']
            related_type = cleaned_data['related_type']
            relationship_type = cleaned_data['relationship_type']

            result = ip_add_update(ip,
                                   ip_type,
                                   source=source_name,
                                   source_method=source_method,
                                   source_reference=source_reference,
                                   source_tlp=source_tlp,
                                   campaign=campaign,
                                   confidence=confidence,
                                   user=request.user,
                                   bucket_list=bucket_list,
                                   ticket=ticket,
                                   is_add_indicator=add_indicator,
                                   indicator_reference=indicator_reference,
                                   related_id=related_id,
                                   related_type=related_type,
                                   relationship_type=relationship_type)
            if 'message' in result:
                if not isinstance(result['message'], list):
                    result['message'] = [result['message']]
            else:
                result['message'] = []
                message = ('<div>Success! Click here to view the new IP: <a '
                           'href="%s">%s</a></div>'
                           % (reverse('crits.ips.views.ip_detail',
                                      args=[ip]),
                              ip))
                result['message'].insert(0, message)
            return HttpResponse(json.dumps(result,
                                           default=json_handler),
                                content_type="application/json")

        return HttpResponse(json.dumps({'success': False,
                                        'form':form.as_table()}),
                            content_type="application/json")
    return render_to_response("error.html",
                              {'error': 'Expected AJAX/POST'},
                              RequestContext(request))
Пример #5
0
def add_update_ip(request, method):
    """
    Add/update an IP address. Should be an AJAX POST.

    :param request: Django request.
    :type request: :class:`django.http.HttpRequest`
    :param method: If this is an "add" or an "update".
    :type method: str
    :returns: :class:`django.http.HttpResponse`
    """

    if request.method == "POST" and request.is_ajax():
        request.user._setup()
        data = request.POST
        form = AddIPForm(request.user, None, data)

        if form.is_valid():
            cleaned_data = form.cleaned_data
            ip = cleaned_data['ip']
            source_name = cleaned_data['source_name']
            source_reference = cleaned_data['source_reference']
            source_method = cleaned_data['source_method']
            source_tlp = cleaned_data['source_tlp']
            campaign = cleaned_data['campaign']
            confidence = cleaned_data['confidence']
            ip_type = cleaned_data['ip_type']
            add_indicator = False
            if cleaned_data.get('add_indicator'):
                add_indicator = True
            indicator_reference = cleaned_data.get('indicator_reference')
            bucket_list = cleaned_data.get(
                form_consts.Common.BUCKET_LIST_VARIABLE_NAME)
            ticket = cleaned_data.get(form_consts.Common.TICKET_VARIABLE_NAME)
            related_id = cleaned_data['related_id']
            related_type = cleaned_data['related_type']
            relationship_type = cleaned_data['relationship_type']

            result = ip_add_update(ip,
                                   ip_type,
                                   source=source_name,
                                   source_method=source_method,
                                   source_reference=source_reference,
                                   source_tlp=source_tlp,
                                   campaign=campaign,
                                   confidence=confidence,
                                   user=request.user,
                                   bucket_list=bucket_list,
                                   ticket=ticket,
                                   is_add_indicator=add_indicator,
                                   indicator_reference=indicator_reference,
                                   related_id=related_id,
                                   related_type=related_type,
                                   relationship_type=relationship_type)
            if 'message' in result:
                if not isinstance(result['message'], list):
                    result['message'] = [result['message']]
            else:
                result['message'] = []
                message = (
                    '<div>Success! Click here to view the new IP: <a '
                    'href="%s">%s</a></div>' %
                    (reverse('crits.ips.views.ip_detail', args=[ip]), ip))
                result['message'].insert(0, message)
            return HttpResponse(json.dumps(result, default=json_handler),
                                content_type="application/json")

        return HttpResponse(json.dumps({
            'success': False,
            'form': form.as_table()
        }),
                            content_type="application/json")
    return render_to_response("error.html", {'error': 'Expected AJAX/POST'},
                              RequestContext(request))