示例#1
0
def upload_indicator(request):
    """
    Upload new indicators (individual, blob, or CSV file).

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :returns: :class:`django.http.HttpResponse`
              :class:`django.http.HttpResponseRedirect`
    """

    if request.method == "POST":
        username = request.user.username
        failed_msg = ''
        result = None

        if request.POST['svalue'] == "Upload CSV":
            form = UploadIndicatorCSVForm(username, request.POST,
                                          request.FILES)
            if form.is_valid():
                result = handle_indicator_csv(
                    request.FILES['filedata'],
                    request.POST['source'],
                    request.POST['method'],
                    request.POST['reference'],
                    "file",
                    username,
                    add_domain=True,
                    related_id=request.POST['related_id'],
                    related_type=request.POST['related_type'],
                    relationship_type=request.POST['relationship_type'])
                if result['success']:
                    message = {
                        'message':
                        ('<div>%s <a href="%s">Go to all'
                         ' indicators</a></div>' %
                         (result['message'],
                          reverse('crits.indicators.views.indicators_listing'))
                         )
                    }
                else:
                    failed_msg = '<div>%s</div>' % result['message']

        if request.POST['svalue'] == "Upload Text":
            form = UploadIndicatorTextForm(username, request.POST)
            if form.is_valid():
                result = handle_indicator_csv(
                    request.POST['data'],
                    request.POST['source'],
                    request.POST['method'],
                    request.POST['reference'],
                    "ti",
                    username,
                    add_domain=True,
                    related_id=request.POST['related_id'],
                    related_type=request.POST['related_type'],
                    relationship_type=request.POST['relationship_type'])
                if result['success']:
                    message = {
                        'message':
                        ('<div>%s <a href="%s">Go to all'
                         ' indicators</a></div>' %
                         (result['message'],
                          reverse('crits.indicators.views.indicators_listing'))
                         )
                    }
                else:
                    failed_msg = '<div>%s</div>' % result['message']

        if request.POST['svalue'] == "Upload Indicator":
            form = UploadIndicatorForm(username, request.POST)
            if form.is_valid():
                result = handle_indicator_ind(
                    request.POST['value'],
                    request.POST['source'],
                    request.POST['indicator_type'],
                    request.POST['threat_type'],
                    request.POST['attack_type'],
                    username,
                    request.POST['method'],
                    request.POST['reference'],
                    add_domain=True,
                    description=request.POST['description'],
                    campaign=request.POST['campaign'],
                    campaign_confidence=request.POST['campaign_confidence'],
                    confidence=request.POST['confidence'],
                    impact=request.POST['impact'],
                    bucket_list=request.POST[
                        form_consts.Common.BUCKET_LIST_VARIABLE_NAME],
                    ticket=request.POST[
                        form_consts.Common.TICKET_VARIABLE_NAME],
                    related_id=request.POST['related_id'],
                    related_type=request.POST['related_type'],
                    relationship_type=request.POST['relationship_type'])
                if result['success']:
                    indicator_link = (
                        (' - <a href=\"%s\">Go to this '
                         'indicator</a> or <a href="%s">all '
                         'indicators</a>.</div>') %
                        (reverse('crits.indicators.views.indicator',
                                 args=[result['objectid']]),
                         reverse('crits.indicators.views.indicators_listing')))

                    if result.get('is_new_indicator', False) == False:
                        message = {
                            'message': ('<div>Warning: Updated existing'
                                        ' Indicator!' + indicator_link)
                        }
                    else:
                        message = {
                            'message': ('<div>Indicator added '
                                        'successfully!' + indicator_link)
                        }
                else:
                    failed_msg = result['message'] + ' - '

        if result == None or not result['success']:
            failed_msg += (
                '<a href="%s"> Go to all indicators</a></div>' %
                reverse('crits.indicators.views.indicators_listing'))
            message = {'message': failed_msg, 'form': form.as_table()}
        elif result != None:
            message['success'] = result['success']

        if request.is_ajax():
            return HttpResponse(json.dumps(message),
                                content_type="application/json")
        else:  #file upload
            return render_to_response('file_upload_response.html',
                                      {'response': json.dumps(message)},
                                      RequestContext(request))
示例#2
0
文件: views.py 项目: plouzek/crits-1
def upload_indicator(request):
    """
    Upload new indicators (individual, blob, or CSV file).

    :param request: Django request object (Required)
    :type request: :class:`django.http.HttpRequest`
    :returns: :class:`django.http.HttpResponse`
              :class:`django.http.HttpResponseRedirect`
    """

    if request.method == "POST":
        username = request.user.username
        failed_msg = ''
        result = None

        if request.POST['svalue'] == "Upload CSV":
            form = UploadIndicatorCSVForm(username, request.POST,
                                          request.FILES)
            if form.is_valid():
                result = handle_indicator_csv(request.FILES['filedata'],
                                              request.POST['source'],
                                              request.POST['reference'],
                                              "file",
                                              username,
                                              add_domain=True)
                if result['success']:
                    message = {
                        'message':
                        ('<div>CSV added successfully! <a '
                         'href="%s">Go to all indicators</a>'
                         '.</div>' %
                         reverse('crits.indicators.views.indicators_listing'))
                    }
                else:
                    failed_msg = '<div>%s</div>' % result['message']

        if request.POST['svalue'] == "Upload Text":
            form = UploadIndicatorTextForm(username, request.POST)
            if form.is_valid():
                result = handle_indicator_csv(request.POST['data'],
                                              request.POST['source'],
                                              request.POST['reference'],
                                              "ti",
                                              username,
                                              add_domain=True)
                if result['success']:
                    message = {
                        'message':
                        ('<div>Indicators added successfully! '
                         '<a href="%s">Go to all indicators</a>'
                         '.</div>' %
                         reverse('crits.indicators.views.indicators_listing'))
                    }
                else:
                    failed_msg = '<div>%s</div>' % result['message']

        if request.POST['svalue'] == "Upload Indicator":
            all_ind_type_choices = [(c[0], c[0], {
                'datatype': c[1].keys()[0],
                'datatype_value': c[1].values()[0]
            }) for c in get_object_types(
                active=False, query={'datatype.file': {
                    '$exists': 0
                }})]
            form = UploadIndicatorForm(username, all_ind_type_choices,
                                       request.POST)
            if form.is_valid():
                if request.POST[
                        'indicator_type'] == "URI - URL" and "://" not in request.POST[
                            'value'].split('.')[0]:
                    result = {
                        "success":
                        False,
                        "message":
                        "URI - URL must contain protocol prefix (e.g. http://, https://, ftp://)"
                    }
                else:
                    result = handle_indicator_ind(
                        request.POST['value'],
                        request.POST['source'],
                        request.POST['reference'],
                        request.POST['indicator_type'],
                        username,
                        add_domain=True,
                        campaign=request.POST['campaign'],
                        campaign_confidence=request.
                        POST['campaign_confidence'],
                        confidence=request.POST['confidence'],
                        impact=request.POST['impact'],
                        bucket_list=request.POST[
                            form_consts.Common.BUCKET_LIST_VARIABLE_NAME],
                        ticket=request.POST[
                            form_consts.Common.TICKET_VARIABLE_NAME])
                if result['success']:
                    indicator_link = '<a href=\"%s\">Go to this indicator</a> or <a href="%s">all indicators</a>.</div>' % (
                        reverse('crits.indicators.views.indicator',
                                args=[result['objectid']]),
                        reverse('crits.indicators.views.indicators_listing'))

                    if result.get('is_new_indicator', False) == False:
                        message = {
                            'message':
                            ('<div>Warning: Updated indicator since indicator already exists! '
                             + indicator_link)
                        }
                    else:
                        message = {
                            'message': ('<div>Indicator added successfully! ' +
                                        indicator_link)
                        }
                else:
                    failed_msg = result['message']

        if result == None or not result['success']:
            failed_msg += (
                '<a href="%s">Go to all indicators</a>'
                '.</div>' %
                reverse('crits.indicators.views.indicators_listing'))
            message = {'message': failed_msg, 'form': form.as_table()}
        elif result != None:
            message['success'] = result['success']

        if request.is_ajax():
            return HttpResponse(json.dumps(message),
                                mimetype="application/json")
        else:  #file upload
            return render_to_response('file_upload_response.html',
                                      {'response': json.dumps(message)},
                                      RequestContext(request))