Example #1
0
def post_common(request, user):
    # Feed作成
    feed = Feed()
    # ManyToMany をクリアする
    feed.files.clear()
    feed.sharing_people.clear()
    # POSTデータ格納
    if KEY_POST not in request.POST:
        raise Exception('No Post.')
    post = request.POST[KEY_POST]
    post = post.strip()
    if len(post) == 0:
        raise Exception('No Content.')
    # Title格納
    if KEY_TITLE not in request.POST:
        raise Exception('No Title.')
    feed.title = request.POST[KEY_TITLE]
    # TLP格納
    if KEY_TLP not in request.POST:
        raise Exception('No TLP.')
    feed.tlp = request.POST[KEY_TLP]

    # stix2 投稿か?
    is_stix2 = is_stix2_post(request)
    stix2_titles = []
    stix2_contents = []
    if KEY_STIX2_TITLES in request.POST:
        stix2_titles = json.loads(request.POST[KEY_STIX2_TITLES])
        # 同一 language が複数に定義されている場合はエラー
        if is_duplicate_languages(stix2_titles):
            raise Exception('Duplicate Same Language Title')
        # stix2_titles から stix 1.x に格納する title を決める
        # default は 先頭
        feed.title = stix2_titles[0]['title']
        for stix2_title in stix2_titles:
            if stix2_title['language'] == request.user.language:
                feed.title = stix2_title['title']
                break

    if KEY_STIX2_CONTENTS in request.POST:
        stix2_contents = json.loads(request.POST[KEY_STIX2_CONTENTS])
        # 同一 language が複数に定義されている場合はエラー
        if is_duplicate_languages(stix2_contents):
            raise Exception('Duplicate Same Language Content')
        # stix2_contents から stix 1.x に格納する post を決める
        # default は 先頭
        post = stix2_contents[0]['content']
        for stix2_content in stix2_contents:
            if stix2_content['language'] == request.user.language:
                post = stix2_content['content']
                break

    # anonymous投稿か?
    if KEY_ANONYMOUS in request.POST:
        # 投稿ユーザーはアノニマス
        feed.user = STIPUser.get_anonymous_user()
    else:
        feed.user = user

    # publication取得
    if KEY_PUBLICATION in request.POST:
        publication = request.POST[KEY_PUBLICATION]
    else:
        publication = PUBLICATION_VALUE_ALL

    # referred_url 取得
    if KEY_REFERRED_URL in request.POST:
        referred_url = request.POST[KEY_REFERRED_URL]
        if len(referred_url) == 0:
            referred_url = None
    else:
        referred_url = None

    feed.referred_url = referred_url

    group = None
    people = None
    # Sharing Rangeがgroup
    if publication == PUBLICATION_VALUE_GROUP:
        group = request.POST[KEY_GROUP]
        feed.sharing_range_type = const.SHARING_RANGE_TYPE_KEY_GROUP
        feed.sharing_group = Group.objects.get(en_name=group)
    # Sharing Rangeがpeople
    elif publication == PUBLICATION_VALUE_PEOPLE:
        feed.sharing_range_type = const.SHARING_RANGE_TYPE_KEY_PEOPLE
        people = request.POST[KEY_PEOPLE].split(',')
        feed.tmp_sharing_people = []
        for user_id in people:
            # user_id は STIPUser の id
            stip_user = STIPUser.objects.get(id=user_id)
            # 一時的に sharing_people リストに格納
            feed.tmp_sharing_people.append(stip_user)
    # Sharing Rangeがall
    elif publication == PUBLICATION_VALUE_ALL:
        feed.sharing_range_type = const.SHARING_RANGE_TYPE_KEY_ALL
    feed.save()

    # ファイル添付対応
    for f in request.FILES.values():
        attach_file = save_attach_file(f.name, f, feed.package_id)
        feed.files.add(attach_file)

    # indicators があるか
    if KEY_INDICATORS in request.POST:
        indicators = json.loads(request.POST[KEY_INDICATORS])
    else:
        indicators = []

    # ttps があるか
    if KEY_TTPS in request.POST:
        ttps = json.loads(request.POST[KEY_TTPS])
    else:
        ttps = []

    # threat_actors があるか
    if KEY_TAS in request.POST:
        tas = json.loads(request.POST[KEY_TAS])
    else:
        tas = []

    # POSTする
    save_post(request, feed, post, indicators, ttps, tas, is_stix2,
              stix2_titles, stix2_contents)
    return feed
Example #2
0
    def post(self, user, data):
        try:
            # msg分解
            msg = email.message_from_string(data)

            # Subject取得
            try:
                # content-type指定でdecode
                (subject_str,
                 subject_type) = email.Header.decode_header(msg['Subject'])[0]
                subject = subject_str.decode(subject_type)
            except BaseException:
                # 存在しない場合はそのまま使用
                subject = msg['Subject']

            from ctirs.models import Feed
            # Feed作成
            feed = Feed()
            # Title は Subject
            feed.title = subject
            # TLP は UserのデフォルトTLP
            feed.tlp = user.tlp
            # Use
            feed.user = user

            # 本文/Attachement取得
            attachements = []
            payloads = msg.get_payload()
            # 添付がある場合は list, ない場合はstr
            if isinstance(payloads, str):
                content_type = self.get_char_set_from_content_type(
                    msg['Content-Type'])
                content_type = content_type.split(':')[0]
                if content_type is not None:
                    body = payloads.decode(content_type)
                else:
                    body = payloads
            elif isinstance(payloads, list):
                # bodyは payloads[0]
                body_payload = payloads[0]
                body = self.get_unicode_content(body_payload)
                # それ以降はattachement
                for index in range(1, len(payloads)):
                    payload = payloads[index]
                    attachements.append(payload)

            # Sharing Rangeはall
            feed.sharing_range_type = SHARING_RANGE_TYPE_KEY_ALL
            # 一旦Feedを保存しSTIXを作成する
            feed.save()

            # 添付ファイル処理
            from feeds.views import save_post, save_attach_file
            for payload in attachements:
                file_name = self.get_file_name(payload)
                content = self.get_content(payload)
                # content を file stream にする
                import io
                o = io.BytesIO(content)
                attach_file = save_attach_file(file_name, o, feed.id)
                feed.files.add(attach_file)
            feed.save()

            # POSTする
            save_post(None, feed, body)

        except BaseException:
            import traceback
            traceback.print_exc()
        return