Exemple #1
0
def add_screenshot(description, tags, source, method, reference, analyst,
                   screenshot, screenshot_ids, oid, otype):
    """
    Add a screenshot or screenshots to a top-level object.

    :param description: The description of the screenshot.
    :type description: str
    :param tags: Tags associated with this screenshot.
    :type tags: str, list
    :param source: The source who provided the screenshot.
    :type source_name: str,
                :class:`crits.core.crits_mongoengine.EmbeddedSource`,
                list of :class:`crits.core.crits_mongoengine.EmbeddedSource`
    :param method: The method of acquiring this screenshot.
    :type method: str
    :param reference: A reference to the source of this screenshot.
    :type reference: str
    :param analyst: The user adding the screenshot.
    :type analyst: str
    :param screenshot: The screenshot to add.
    :type screenshot: file handle
    :param screenshot_ids: A list of ObjectIds of existing screenshots to add.
    :type screenshot_ids: str, list
    :param oid: The ObjectId of the top-level object to add to.
    :type oid: str
    :param otype: The top-level object type.
    :type otype: str
    :returns: dict with keys:
              'success' (boolean),
              'message' (str),
              'id' (str) if successful,
              'html' (str) if successful,
    """

    result = {'success': False}
    if not source:
        result['message'] = "Must provide a source"
        return result
    obj = class_from_id(otype, oid)
    if not obj:
        result['message'] = "Could not find the top-level object."
        return result

    final_screenshots = []

    if screenshot_ids:
        if not isinstance(screenshot_ids, list):
            screenshot_list = screenshot_ids.split(',')
        else:
            screenshot_list = screenshot_ids
        for screenshot_id in screenshot_list:
            screenshot_id = screenshot_id.strip().lower()
            s = Screenshot.objects(id=screenshot_id).first()
            if s:
                if isinstance(source, basestring) and len(source) > 0:
                    s_embed = create_embedded_source(source,
                                                     method=method,
                                                     reference=reference,
                                                     analyst=analyst)
                    s.add_source(s_embed)
                elif isinstance(source, EmbeddedSource):
                    s.add_source(source, method=method, reference=reference)
                elif isinstance(source, list) and len(source) > 0:
                    for x in source:
                        if isinstance(x, EmbeddedSource):
                            s.add_source(x, method=method, reference=reference)

                s.add_tags(tags)
                s.save()
                obj.screenshots.append(screenshot_id)
                obj.save()
                final_screenshots.append(s)
    else:
        md5 = hashlib.md5(screenshot.read()).hexdigest()
        check = Screenshot.objects(md5=md5).first()
        if check:
            s = check
            s.add_tags(tags)
        else:
            s = Screenshot()
            s.analyst = analyst
            s.description = description
            s.md5 = md5
            screenshot.seek(0)
            s.add_screenshot(screenshot, tags)
        if isinstance(source, basestring) and len(source) > 0:
            s_embed = create_embedded_source(source,
                                             method=method,
                                             reference=reference,
                                             analyst=analyst)
            s.add_source(s_embed)
        elif isinstance(source, EmbeddedSource):
            s.add_source(source, method=method, reference=reference)
        elif isinstance(source, list) and len(source) > 0:
            for x in source:
                if isinstance(x, EmbeddedSource):
                    s.add_source(x, method=method, reference=reference)

        if not s.screenshot and not s.thumb:
            result[
                'message'] = "Problem adding screenshot to GridFS. No screenshot uploaded."
            return result
        try:
            s.save(username=analyst)
            final_screenshots.append(s)
        except Exception, e:
            result['message'] = str(e)
            return result
        obj.screenshots.append(str(s.id))
        obj.save(username=analyst)
Exemple #2
0
def add_screenshot(description, tags, source, method, reference, tlp, analyst,
                   screenshot, screenshot_ids, oid, otype):
    """
    Add a screenshot or screenshots to a top-level object.

    :param description: The description of the screenshot.
    :type description: str
    :param tags: Tags associated with this screenshot.
    :type tags: str, list
    :param source: The source who provided the screenshot.
    :type source_name: str,
                :class:`crits.core.crits_mongoengine.EmbeddedSource`,
                list of :class:`crits.core.crits_mongoengine.EmbeddedSource`
    :param method: The method of acquiring this screenshot.
    :type method: str
    :param reference: A reference to the source of this screenshot.
    :type reference: str
    :param tlp: The TLP Sharing of this screenshot.
    :type tlp: str
    :param analyst: The user adding the screenshot.
    :type analyst: str
    :param screenshot: The screenshot to add.
    :type screenshot: file handle
    :param screenshot_ids: A list of ObjectIds of existing screenshots to add.
    :type screenshot_ids: str, list
    :param oid: The ObjectId of the top-level object to add to.
    :type oid: str
    :param otype: The top-level object type.
    :type otype: str
    :returns: dict with keys:
              'success' (boolean),
              'message' (str),
              'id' (str) if successful,
              'html' (str) if successful,
    """

    result = {'success': False}
    if not source:
        result['message'] = "Must provide a source"
        return result
    obj = class_from_id(otype, oid)
    if not obj:
        result['message'] = "Could not find the top-level object."
        return result

    final_screenshots = []

    if screenshot_ids:
        if not isinstance(screenshot_ids, list):
            screenshot_list = screenshot_ids.split(',')
        else:
            screenshot_list = screenshot_ids
        for screenshot_id in screenshot_list:
            screenshot_id = screenshot_id.strip().lower()
            s = Screenshot.objects(id=screenshot_id).first()
            if s:
                if isinstance(source, basestring) and len(source) > 0:
                    s_embed = create_embedded_source(source, method=method,
                                                    reference=reference,
                                                    analyst=analyst,
                                                     tlp=tlp)
                    s.add_source(s_embed)
                elif isinstance(source, EmbeddedSource):
                    s.add_source(source=source, method=method,
                                 reference=reference, analyst=analyst, tlp=tlp)
                elif isinstance(source, list) and len(source) > 0:
                    for x in source:
                        if isinstance(x, EmbeddedSource):
                            s.add_source(x, method=method, reference=reference,
                                         analyst=analyst, tlp=tlp)
                s.add_tags(tags)
                s.save()
                obj.screenshots.append(screenshot_id)
                obj.save()
                final_screenshots.append(s)
    else:
        md5 = hashlib.md5(screenshot.read()).hexdigest()
        check = Screenshot.objects(md5=md5).first()
        if check:
            s = check
            s.add_tags(tags)
        else:
            s = Screenshot()
            s.analyst = analyst
            s.description = description
            s.md5 = md5
            screenshot.seek(0)
            s.add_screenshot(screenshot, tags)
        if isinstance(source, basestring) and len(source) > 0:
            s_embed = create_embedded_source(source, method=method,
                                             reference=reference,
                                            analyst=analyst,
                                             tlp=tlp)
            s.add_source(s_embed)
        elif isinstance(source, EmbeddedSource):
            s.add_source(source, method=method, reference=reference,
                         analyst=analyst, tlp=tlp)
        elif isinstance(source, list) and len(source) > 0:
            for x in source:
                if isinstance(x, EmbeddedSource):
                    s.add_source(x, method=method, reference=reference,
                                 analyst=analyst, tlp=tlp)
        if not s.screenshot and not s.thumb:
            result['message'] = "Problem adding screenshot to GridFS. No screenshot uploaded."
            return result
        try:
            s.save(username=analyst)
            final_screenshots.append(s)
        except Exception, e:
            result['message'] = str(e)
            return result
        obj.screenshots.append(str(s.id))
        obj.save(username=analyst)