Beispiel #1
0
def email_raw_add(request):
    """
    Upload an email using Raw. Should be an AJAX POST.

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

    fields_form = EmailRawUploadForm(request.user, request.POST)
    json_reply = {
                   'form': fields_form.as_table(),
                   'success': False,
                   'message': ""
                 }
    if request.method != "POST":
        message = "Must submit via POST"
        if request.is_ajax():
            json_reply['message'] = message
            return HttpResponse(json.dumps(json_reply), mimetype="application/json")
        else:
            return render_to_response('error.html', {'error': message}, RequestContext(request))

    if not fields_form.is_valid():
        message = "Form is invalid."
        if request.is_ajax():
            json_reply['message'] = message
            return HttpResponse(json.dumps(json_reply), mimetype="application/json")
        else:
            return render_to_response('error.html', {'error': message}, RequestContext(request))

    method = "Raw Upload"
    if fields_form.cleaned_data['source_method']:
        method = method + " - " + fields_form.cleaned_data['source_method']

    obj = handle_pasted_eml(fields_form.cleaned_data['raw_email'],
                    fields_form.cleaned_data['source'],
                    fields_form.cleaned_data['source_reference'],
                    request.user.username,
                    method,
                    campaign=fields_form.cleaned_data['campaign'],
                    confidence=fields_form.cleaned_data['campaign_confidence'],
                    bucket_list=fields_form.cleaned_data['bucket_list'],
                    ticket=fields_form.cleaned_data['ticket'])
    if not obj['status']:
        if request.is_ajax():
            json_reply['message'] = obj['reason']
            return HttpResponse(json.dumps(json_reply), mimetype="application/json")
        else:
            return render_to_response('error.html', {'error': obj['reason']}, RequestContext(request))

    if request.is_ajax():
        json_reply['success'] = True
        del json_reply['form']
        json_reply['message'] = 'Email uploaded successfully. <a href="%s">View email.</a>' % reverse('crits.emails.views.email_detail', args=[obj['object'].id])
        return HttpResponse(json.dumps(json_reply), mimetype="application/json")
    else:
        return HttpResponseRedirect(reverse('crits.emails.views.email_detail', args=[obj['object'].id]))
Beispiel #2
0
def email_raw_add(request):
    """
    Upload an email using Raw. Should be an AJAX POST.

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

    fields_form = EmailRawUploadForm(request.user, request.POST)
    json_reply = {"form": fields_form.as_table(), "success": False, "message": ""}
    if request.method != "POST":
        message = "Must submit via POST"
        if request.is_ajax():
            json_reply["message"] = message
            return HttpResponse(json.dumps(json_reply), mimetype="application/json")
        else:
            return render_to_response("error.html", {"error": message}, RequestContext(request))

    if not fields_form.is_valid():
        message = "Form is invalid."
        if request.is_ajax():
            json_reply["message"] = message
            return HttpResponse(json.dumps(json_reply), mimetype="application/json")
        else:
            return render_to_response("error.html", {"error": message}, RequestContext(request))

    method = "Raw Upload"
    if fields_form.cleaned_data["source_method"]:
        method = method + " - " + fields_form.cleaned_data["source_method"]

    obj = handle_pasted_eml(
        fields_form.cleaned_data["raw_email"],
        fields_form.cleaned_data["source"],
        fields_form.cleaned_data["source_reference"],
        request.user.username,
        method,
        campaign=fields_form.cleaned_data["campaign"],
        confidence=fields_form.cleaned_data["campaign_confidence"],
    )
    if not obj["status"]:
        if request.is_ajax():
            json_reply["message"] = obj["reason"]
            return HttpResponse(json.dumps(json_reply), mimetype="application/json")
        else:
            return render_to_response("error.html", {"error": obj["reason"]}, RequestContext(request))

    if request.is_ajax():
        json_reply["success"] = True
        del json_reply["form"]
        json_reply["message"] = 'Email uploaded successfully. <a href="%s">View email.</a>' % reverse(
            "crits.emails.views.email_detail", args=[obj["object"].id]
        )
        return HttpResponse(json.dumps(json_reply), mimetype="application/json")
    else:
        return HttpResponseRedirect(reverse("crits.emails.views.email_detail", args=[obj["object"].id]))
Beispiel #3
0
def email_raw_add(request):
    """
    Upload an email using Raw. Should be an AJAX POST.

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

    raw_form = EmailRawUploadForm(request.user, request.POST)
    user = request.user

    json_reply = {'form': raw_form.as_table(), 'success': False}

    if request.method != "POST":
        message = "Must submit via POST"
    else:
        if not raw_form.is_valid():
            message = "Form is invalid."
        elif not user.has_access_to(EmailACL.WRITE):
            message = "User does not have permission to add email."
        else:
            form_data = raw_form.cleaned_data
            method = "Raw Upload"
            if form_data['source_method']:
                method = method + " - " + form_data['source_method']

            result = handle_pasted_eml(
                form_data['raw_email'], form_data['source_name'],
                form_data['source_reference'], form_data['source_method'],
                form_data['source_tlp'], user, form_data['campaign'],
                form_data['campaign_confidence'], form_data['bucket_list'],
                form_data['ticket'], form_data['related_id'],
                form_data['related_type'], form_data['relationship_type'])

            if result['status']:
                redirect = reverse('crits-emails-views-email_detail',
                                   args=[result['object'].id])
                if not request.is_ajax():
                    return HttpResponseRedirect(redirect)
                json_reply['success'] = True
                del json_reply['form']
                message = 'Email uploaded successfully'
                if result.get('reason'):
                    message += ', but %s' % result['reason']
                message += ('. <a href="%s">View email.</a>' % redirect)
            else:
                message = result['reason']

    if request.is_ajax():
        json_reply['message'] = message
        return HttpResponse(json.dumps(json_reply),
                            content_type="application/json")
    else:
        return render(request, 'error.html', {'error': message})
Beispiel #4
0
def email_raw_add(request):
    """
    Upload an email using Raw. Should be an AJAX POST.

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

    raw_form = EmailRawUploadForm(request.user, request.POST)
    user = request.user

    json_reply = {
                   'form': raw_form.as_table(),
                   'success': False
                 }

    if request.method != "POST":
        message = "Must submit via POST"
    else:
        if not raw_form.is_valid():
            message = "Form is invalid."
        elif not user.has_access_to(EmailACL.WRITE):
            message = "User does not have permission to add email."
        else:
            form_data = raw_form.cleaned_data
            method = "Raw Upload"
            if form_data['source_method']:
                method = method + " - " + form_data['source_method']

            result = handle_pasted_eml(form_data['raw_email'],
                                       form_data['source_name'],
                                       form_data['source_reference'],
                                       form_data['source_method'],
                                       form_data['source_tlp'],
                                       request.user,
                                       form_data['campaign'],
                                       form_data['campaign_confidence'],
                                       form_data['bucket_list'],
                                       form_data['ticket'],
                                       form_data['related_id'],
                                       form_data['related_type'],
                                       form_data['relationship_type'])

            if result['status']:
                redirect = reverse('crits.emails.views.email_detail',
                                   args=[result['object'].id])
                if not request.is_ajax():
                    return HttpResponseRedirect(redirect)
                json_reply['success'] = True
                del json_reply['form']
                message = 'Email uploaded successfully'
                if result.get('reason'):
                    message += ', but %s' % result['reason']
                message += ('. <a href="%s">View email.</a>' % redirect)
            else:
                message = result['reason']


    if request.is_ajax():
        json_reply['message'] = message
        return HttpResponse(json.dumps(json_reply),
                            content_type="application/json")
    else:
        return render_to_response('error.html',
                                  {'error': message},
                                  RequestContext(request))