def set_releasability(self, indicators, feed): for indicator in indicators: if self.was_saved(indicator): set_releasability_flag(str(self.imported[indicator.id_][0]), str(self.imported[indicator.id_][1].id), 'taxii', feed, self.package.id_) return
def add_new_relationship(request): """ Add a new relationship. Should be an AJAX POST. :param request: Django request object (Required) :type request: :class:`django.http.HttpRequest` :returns: :class:`django.http.HttpResponse` """ if request.method == 'POST' and request.is_ajax(): form = ForgeRelationshipForm(request.POST) choices = [(c, c) for c in RelationshipTypes.values(sort=True)] form.fields['forward_relationship'].choices = choices if form.is_valid(): cleaned_data = form.cleaned_data results = forge_relationship( type_=cleaned_data.get('forward_type'), id_=cleaned_data.get('forward_value'), right_type=cleaned_data.get('reverse_type'), right_id=cleaned_data.get('dest_id'), rel_type=cleaned_data.get('forward_relationship'), rel_date=cleaned_data.get('relationship_date'), user=request.user.username, rel_reason=cleaned_data.get('rel_reason'), rel_confidence=cleaned_data.get('rel_confidence'), get_rels=True) if results['success'] == True: set_releasability_flag(cleaned_data.get('forward_type'), cleaned_data.get('forward_value'), request.user.username) relationship = { 'type': cleaned_data.get('forward_type'), 'url_key': cleaned_data.get('forward_value') } message = render_to_string( 'relationships_listing_widget.html', { 'relationship': relationship, 'nohide': True, 'relationships': results['relationships'] }, RequestContext(request)) result = {'success': True, 'message': message} else: message = "Error adding relationship: %s" % results['message'] result = {'success': False, 'message': message} else: message = "Invalid Form: %s" % form.errors form = form.as_table() result = {'success': False, 'form': form, 'message': message} return HttpResponse(json.dumps(result), mimetype="application/json") else: error = "Expected AJAX POST" return render_to_response("error.html", {"error": error}, RequestContext(request))
def comment_update(cleaned_data, obj_type, obj_id, subscr, analyst): """ Update an existing comment. :param cleaned_data: Cleaned data from the Django form submission. :type cleaned_data: dict :param obj_type: The top-level object type to find the comment to update. :type obj_type: str :param obj_id: The top-level ObjectId to find the comment to update. :type obj_id: str :param subscr: The subscription information for the top-level object. :type subscr: dict :param analyst: The user updating the comment. :type analyst: str :returns: :class:`django.http.HttpResponse` """ result = None date = cleaned_data['parent_date'] comment = Comment.objects(obj_id=obj_id, created=date).first() if not comment: message = "Cannot find comment to update!" result = {'success': False, 'message': message} elif comment.analyst != analyst: # Should admin users be able to edit others comments? message = "You cannot edit comments from other analysts!" result = {'success': False, 'message': message} else: comment.edit_comment(cleaned_data['comment']) comment.private = cleaned_data['private'] try: comment.save() comment.comment_to_html() if not comment.private: set_releasability_flag(obj_type, obj_id, analyst) html = render_to_string( 'comments_row_widget.html', { 'comment': comment, 'user': { 'username': analyst }, 'subscription': subscr }) message = "Comment updated successfully!" result = {'success': True, 'html': html, 'message': message} except ValidationError, e: result = {'success': False, 'message': e}
def edit_campaign(request, ctype, objectid): """ Edit an attributed Campaign for a top-level object. Should be an AJAX POST. :param request: Django request object (Required) :type request: :class:`django.http.HttpRequest` :param ctype: CRITs type for the top-level object. :type ctype: str :param objectid: The ObjectId of the top-level object. :type objectid: str :returns: :class:`django.http.HttpResponse` """ if request.method == "POST" and request.is_ajax(): form = CampaignForm(request.POST) if form.is_valid(): data = form.cleaned_data campaign = data['name'] confidence = data['confidence'] description = data['description'] related = data['related'] analyst = request.user.username try: date = datetime.datetime.strptime(data['date'], settings.PY_DATETIME_FORMAT) except ValueError: date = datetime.datetime.now() result = campaign_edit(ctype, objectid, campaign, confidence, description, date, related, analyst) if result['success']: set_releasability_flag(ctype, objectid, analyst) return HttpResponse(json.dumps(result), mimetype="application/json") else: result.update({'form': form.as_table()}) return HttpResponse(json.dumps(result), mimetype="application/json") else: return HttpResponse(json.dumps({ 'success': False, 'form': form.as_table() }), mimetype="application/json") else: return HttpResponse(json.dumps({'success': False}), mimetype="application/json")
def campaign_add(request, ctype, objectid): """ Attribute a Campaign to a top-level object. Should be an AJAX POST. :param request: Django request object (Required) :type request: :class:`django.http.HttpRequest` :param ctype: CRITs type for the top-level object. :type ctype: str :param objectid: The ObjectId of the top-level object. :type objectid: str :returns: :class:`django.http.HttpResponse` """ if request.method == "POST" and request.is_ajax(): form = CampaignForm(request.POST) result = {} if form.is_valid(): data = form.cleaned_data campaign = data['name'] confidence = data['confidence'] description = data['description'] related = data['related'] analyst = request.user.username result = campaign_addh(campaign, confidence, description, related, analyst, ctype, objectid, update=False) if result['success']: set_releasability_flag(ctype, objectid, analyst) return HttpResponse(json.dumps(result), mimetype="application/json") result['form'] = form.as_table() result['success'] = False return HttpResponse(json.dumps(result), mimetype="application/json") else: return HttpResponse(json.dumps({ 'success': False, 'message': "Expected AJAX request." }), mimetype="application/json")
def update_header_value(request, email_id): """ Update the header value of an email. Should be an AJAX POST. :param request: Django request object (Required) :type request: :class:`django.http.HttpRequest` :param email_id: The ObjectId of the email to update a header for. :type email_id: str :returns: :class:`django.http.HttpResponse` """ if request.method == "POST" and request.is_ajax(): type_ = request.POST.get('type', None) value = request.POST.get('value', None) analyst = request.user.username result = update_email_header_value(email_id, type_, value, analyst) set_releasability_flag('Email', email_id, analyst) return HttpResponse(json.dumps(result), mimetype="application/json") else: return render_to_response('error.html', {'error': "Expected AJAX POST"}, RequestContext(request))
def set_sample_filenames(request): """ Set Sample filenames. Should be an AJAX POST. :param request: Django request object (Required) :type request: :class:`django.http.HttpRequest` :returns: :class:`django.http.HttpResponse` """ if request.method == "POST" and request.is_ajax(): tags = request.POST.get('tags', "").split(",") id_ = request.POST.get('id', None) analyst = request.user.username result = modify_sample_filenames(id_, tags, analyst) set_releasability_flag('Sample', id_, analyst) return HttpResponse(json.dumps(result), mimetype="application/json") else: error = "Expected POST" return render_to_response("error.html", {"error": error}, RequestContext(request))
def set_sample_filename(request): """ Set a Sample filename. Should be an AJAX POST. :param request: Django request object (Required) :type request: :class:`django.http.HttpRequest` :returns: :class:`django.http.HttpResponse` """ if request.method == 'POST': filename = request.POST.get('filename', None) id_ = request.POST.get('id', None) analyst = request.user.username result = update_sample_filename(id_, filename, analyst) set_releasability_flag('Sample', id_, analyst) return HttpResponse(json.dumps(result), mimetype="application/json") else: error = "Expected POST" return render_to_response("error.html", {"error": error}, RequestContext(request))
def campaign_aliases(request): """ Set Campaign aliases. Should be an AJAX POST. :param request: Django request object (Required) :type request: :class:`django.http.HttpRequest` :returns: :class:`django.http.HttpResponse` """ if request.method == "POST" and request.is_ajax(): tags = request.POST.get('tags', "").split(",") name = request.POST.get('name', None) res = modify_campaign_aliases(name, tags, request.user.username) campaign = Campaign.objects(name=name).first() set_releasability_flag('Campaign', campaign.id, request.user.username) return HttpResponse(json.dumps(res), mimetype="application/json") else: error = "Expected POST" return render_to_response("error.html", {"error": error}, RequestContext(request))
def campaign_ttp(request, cid): """ Add/edit/remove a TTP from a Campaign. Should be an AJAX POST. :param request: Django request object (Required) :type request: :class:`django.http.HttpRequest` :param cid: The ObjectId of the Campaign. :type cid: str :returns: :class:`django.http.HttpResponse` """ if request.method == "POST" and request.is_ajax(): action = request.POST['action'] analyst = request.user.username if action == "add": result = add_ttp(cid, request.POST['ttp'], analyst) elif action == "edit": result = edit_ttp(cid, request.POST['old_ttp'], request.POST['new_ttp'], analyst) elif action == "remove": result = remove_ttp(cid, request.POST['ttp'], analyst) else: result = {'success': False, 'message': "Invalid action."} if 'campaign' in result: campaign = result['campaign'] html = render_to_string('campaign_ttps_data_widget.html', {'campaign_detail': campaign}, RequestContext(request)) del result['campaign'] result['html'] = html set_releasability_flag('Campaign', cid, analyst) return HttpResponse(json.dumps(result), mimetype="application/json") else: return render_to_response("error.html", {"error": 'Expected AJAX POST.'}, RequestContext(request))
def comment_add(cleaned_data, obj_type, obj_id, method, subscr, analyst, date=None, source_analyst=None): """ Add a new comment. :param cleaned_data: Cleaned data from the Django form submission. :type cleaned_data: dict :param obj_type: The top-level object type to add the comment to. :type obj_type: str :param obj_id: The top-level ObjectId to add the comment to. :type obj_id: str :param method: If this is a reply or not (set method to "reply"). :type method: str :param subscr: The subscription information for the top-level object. :type subscr: dict :param analyst: The user adding the comment. :type analyst: str :returns: dict with keys: 'success' (boolean), 'message': (str), 'html' (str) if successful. """ comment = Comment() comment.comment = cleaned_data['comment'] comment.parse_comment() comment.set_parent_object(obj_type, obj_id) if method == "reply": comment.set_parent_comment(cleaned_data['parent_date'], cleaned_data['parent_analyst']) comment.analyst = analyst comment.set_url_key(cleaned_data['url_key']) if source_analyst: source = create_embedded_source(name=source_analyst, analyst=analyst) else: source = create_embedded_source(name=get_user_organization(analyst), analyst=analyst) comment.source = [source] comment.private = cleaned_data['private'] if date: comment.created = date comment.edit_date = date try: comment.save(username=analyst) # this is silly :( in the comment object the dates are still # accurate to .###### seconds, but in the database are only # accurate to .### seconds. This messes with the template's ability # to compare creation and edit times. comment.reload() comment.comment_to_html() if not comment.private: set_releasability_flag(obj_type, obj_id, analyst) html = render_to_string( 'comments_row_widget.html', { 'comment': comment, 'user': { 'username': analyst }, 'subscription': subscr }) message = "Comment added successfully!" result = {'success': True, 'html': html, 'message': message} except ValidationError, e: result = {'success': False, 'message': e}