Example #1
0
def upload(request, title, post, tlp, confirm_data):
    publication = request.POST['publication']
    if 'group' in request.POST:
        group = request.POST['group']
    if 'people' in request.POST:
        people = request.POST['people']
    new_post = {}
    new_post['title'] = title
    new_post['post'] = post
    new_post['TLP'] = tlp
    new_post['publication'] = publication
    request.POST = new_post

    if not confirm_data:
        request = get_request_via_confirm_indicator(request)
    else:
        request.POST['indicators'] = json.dumps(confirm_data['indicators'])
        request.POST['tas'] = json.dumps(confirm_data['tas'])
        request.POST['ttps'] = json.dumps(confirm_data['ttps'])

    if publication == 'group':
        request.POST['group'] = group
    elif publication == 'people':
        request.POST['people'] = people
    post_common(request, request.user)
    return
Example #2
0
def post(request):
    POST_KEY_NO_EXTRACT = 'no_extract'
    if request.method != 'POST':
        return HttpResponseNotAllowed(['POST'])

    try:
        # anonymous投稿か?
        if KEY_ANONYMOUS in request.POST:
            # 投稿ユーザーはアノニマス
            user = STIPUser.get_anonymous_user()
        else:
            # usernameがない
            if KEY_USERNAME not in request.POST:
                raise Exception('No username.')
            # user情報取得
            try:
                username = request.POST[KEY_USERNAME]
                user = STIPUser.objects.get(username=username)
            except BaseException:
                raise Exception('Invalid username(%s)' % (username))
        # extract フラグ
        extract = True
        if POST_KEY_NO_EXTRACT in request.POST:
            extract = False

        if extract:
            # indicators 取得
            request.user = user
            # confirm_indicators から再度 post するデータを取得する
            request = get_request_via_confirm_indicator(request)
        feed = post_common(request, user)

        dump = {
            'status': 'OK',
            'reason': 'Successfully',
            'id': str(feed.package_id)
        }
        data = json.dumps(dump)
        return HttpResponse(data, content_type='application/json')
    except Exception as e:
        dump = {'status': 'NG',
                'reason': str(e)}
        data = json.dumps(dump)
        return HttpResponseBadRequest(data, content_type='application/json')
Example #3
0
def post_stip_from_slack(receive_data, slack_bot_channel_name, slack_user):
    wc = StipSnsBoot.get_slack_web_client()
    POST_INDEX_TITLE = 0

    if 'subtype' in receive_data:
        # 投稿以外のメッセージなので対象外
        return
    # user_id から user_info 取得
    try:
        user_id = receive_data['user']
    except KeyError:
        return
    user_profile = get_user_profile(user_id)

    # bot からの発言は対象外
    if 'bot_id' in user_profile:
        return

    # S-TIP 投稿データを取得する
    text = receive_data['text']
    stip_params = get_stip_params(text, user_profile['display_name'])
    if stip_params is None:
        return

    # 添付file データ取得
    files_for_cti_extractor, files_for_stip_post = get_attached_files(
        receive_data)

    # CTI Element Extractor 追加
    stip_params = set_extractor_info(stip_params, files_for_cti_extractor,
                                     user_profile['display_name'])

    # 本文に各種 footer 情報を追加
    post = stip_params[STIP_PARAMS_INDEX_POST]

    # command 関連
    command_stix_id = get_command_stix_id(post)
    # :stix <stix_id>
    if command_stix_id is not None:
        download_stix_id(command_stix_id)
        return

    # <!here> が含まれている場合
    post = post.replace('<!here>', '@here')
    # <!channel> が含まれている場合
    post = post.replace('<!channel>', '@channel')
    # <@user_id> が含まれている場合
    post = convert_user_id(post)
    # <#channel_id|channnel_name> が含まれている場合
    post = convert_channel_info(post)

    post = post.replace('&', '&amp;')
    post = post.replace('<', '&lt;')
    post = post.replace('>', '&gt;')
    post += '\n----------Slack Message Info----------\n'

    # 1行目がtitle
    stip_params[STIP_PARAMS_INDEX_TITLE] = post.splitlines()[POST_INDEX_TITLE]

    # channle_id から channel 情報取得
    try:
        channel_id = receive_data['channel']
        channel_name = None
        try:
            resp = wc.conversations_info(channel=channel_id)
            # public channel
            channel_name = resp['channel']['name']
        except slack.errors.SlackApiError:
            # private channnel
            resp = wc.groups_info(channel=channel_id)
            channel_name = resp['group']['name']
        except Exception:
            # チャンネル名が取れないので skip
            pass
        if channel_name != slack_bot_channel_name:
            # 該当チャンネルではないの skip
            return
        post += ('%s: %s\n' % ('Channel', channel_name))
    except KeyError:
        # チャンネル名が取れないので skip
        return

    # アカウント名
    try:
        post += ('Full name: %s\n' % (user_profile['real_name']))
    except KeyError:
        # アカウント名が取れないので skip
        return

    # メッセージ id
    if 'client_msg_id' in receive_data:
        post += ('%s: %s\n' % ('Message ID', receive_data['client_msg_id']))

    if 'ts' in receive_data:
        ts = receive_data['ts']
        dt = datetime.datetime(*time.gmtime(float(ts))[:6], tzinfo=pytz.utc)
        post += ('%s: %s\n' %
                 ('Timestamp', dt.strftime('%Y-%m-%dT%H:%M:%S.%f%z')))
    stip_params[STIP_PARAMS_INDEX_POST] = post

    # ここから SNS に投稿する
    try:
        request = HttpRequest()
        request.method = 'POST'
        request.POST = stip_params
        request.FILES = files_for_stip_post
        request.META['SERVER_NAME'] = 'localhost'
        request.user = slack_user
        post_common(request, slack_user)
    except Exception as e:
        import traceback
        traceback.print_exc()
        raise e
    return