예제 #1
0
    def calculate_difference(preview, original_image_id):
        """Calculate difference between two images.
        @param preview: preview dict
        @param original_image_id: original image ID
        @return: difference, difference percentage
        """
        try:
            i1 = utils.str2image(get_file(original_image_id).read())
        except IOError as e:
            logger.warning("Comparer error reading image: {0}".format(e))
            return

        # Check if thumb was resized.
        if "original_file" in preview:
            i2 = utils.str2image(get_file(preview["original_file"]).read())
        else:
            i2 = utils.str2image(get_file(preview["file"]).read())

        # Resize.
        width, height = i2.size
        try:
            i1 = i1.resize([width, height], Image.ANTIALIAS)
        except IOError as e:
            logger.warning("Comparer error reading image: {0}".format(e))
            return

        # Checks.
        #assert i1.mode == i2.mode, "Different kinds of images."
        #assert i1.size == i2.size, "Different sizes."

        # Calculate difference.
        pairs = izip(i1.getdata(), i2.getdata())
        if len(i1.getbands()) == 1:
            # for gray-scale jpegs
            dif = sum(abs(p1 - p2) for p1, p2 in pairs)
        else:
            dif = sum(
                abs(c1 - c2) for p1, p2 in pairs for c1, c2 in zip(p1, p2))

        ncomponents = i1.size[0] * i1.size[1] * 3

        # Get diff percentage.
        diff_perc = int((dif / 255.0 * 100) / ncomponents)

        # Binary option.
        if diff_perc >= 15:
            diff = True
        else:
            diff = False

        return diff, diff_perc
예제 #2
0
    def calculate_difference(preview, original_image_id):
        """Calculate difference between two images.
        @param preview: preview dict
        @param original_image_id: original image ID
        @return: difference, difference percentage
        """
        try:
            i1 = utils.str2image(get_file(original_image_id).read())
        except IOError as e:
            logger.warning("Comparer error reading image: {0}".format(e))
            return

        # Check if thumb was resized.
        if "original_file" in preview:
            i2 = utils.str2image(get_file(preview["original_file"]).read())
        else:
            i2 = utils.str2image(get_file(preview["file"]).read())

        # Resize.
        width, height = i2.size
        try:
            i1 = i1.resize([width, height], Image.ANTIALIAS)
        except IOError as e:
            logger.warning("Comparer error reading image: {0}".format(e))
            return

        # Checks.
        # assert i1.mode == i2.mode, "Different kinds of images."
        # assert i1.size == i2.size, "Different sizes."

        # Calculate difference.
        pairs = izip(i1.getdata(), i2.getdata())
        if len(i1.getbands()) == 1:
            # for gray-scale jpegs
            dif = sum(abs(p1 - p2) for p1, p2 in pairs)
        else:
            dif = sum(abs(c1 - c2) for p1, p2 in pairs for c1, c2 in zip(p1, p2))

        ncomponents = i1.size[0] * i1.size[1] * 3

        # Get diff percentage.
        diff_perc = int((dif / 255.0 * 100) / ncomponents)

        # Binary option.
        if diff_perc >= 15:
            diff = True
        else:
            diff = False

        return diff, diff_perc
예제 #3
0
    def __init__(self, mongo_id, file_name=None):
        # Results storage.
        self.results = AutoVivification()

        # Store image id.
        if mongo_id:
            self.orig_id = mongo_id
        else:
            raise Exception("You have to pass the original image ID")

        if file_name:
            self.file_name = file_name
        else:
            self.file_name = None

        # Read image data.
        try:
            self.file_data = get_file(self.orig_id).read()
        except gridfs.errors.NoFile:
            raise Exception("Image not found on GridFS storage")

        # Save a temporary file, used for analysis which needs a file on disk.
        temp_image = tempfile.NamedTemporaryFile(delete=False)
        temp_image.write(self.file_data)
        temp_image.close(
        )  # File is not immediately deleted because we used delete=False
        self.temp_image = temp_image.name
예제 #4
0
def hexview(image_id, length=8):
    """Hexdump representation.
    @param image_id: gridfs image id
    @return: hexdump
    @see: code inspired to http://code.activestate.com/recipes/142812/
    """

    # Get image from gridfs.
    try:
        file = get_file(image_id)
    except (InvalidId, TypeError):
        return None

    # Read data.
    src = file.read()

    hex_dump = []

    # Deal with unicode.
    if isinstance(src, unicode):
        digits = 4
    else:
        digits = 2

    # Create hex view.
    for i in xrange(0, len(src), length):
        line = {}
        s = src[i:i + length]
        hexa = b" ".join(["%0*X" % (digits, ord(x)) for x in s])
        text = b"".join([x if 0x20 <= ord(x) < 0x7F else b"." for x in s])
        line["address"] = b"%04X" % i
        line["hex"] = b"%-*s" % (length * (digits + 1), hexa)
        line["text"] = text
        hex_dump.append(line)
    return hex_dump
예제 #5
0
    def __init__(self, mongo_id, file_name=None):
        # Results storage.
        self.results = AutoVivification()

        # Store image id.
        if mongo_id:
            self.orig_id = mongo_id
        else:
            raise Exception("You have to pass the original image ID")

        if file_name:
            self.file_name = file_name
        else:
            self.file_name = None

        # Read image data.
        try:
            self.file_data = get_file(self.orig_id).read()
        except gridfs.errors.NoFile:
            raise Exception("Image not found on GridFS storage")

        # Save a temporary file, used for analysis which needs a file on disk.
        temp_image = tempfile.NamedTemporaryFile(delete=False)
        temp_image.write(self.file_data)
        temp_image.close()  # File is not immediately deleted because we used delete=False
        self.temp_image = temp_image.name
예제 #6
0
    def handle(self, *args, **options):
        print("Starting")

        if os.path.exists(options["path"]):
            dst_path = os.path.join(options["path"], "ghiro_output")
            if os.path.exists(dst_path):
                print "ERROR: a folder 'ghiro_output' already exist in that path!"
                sys.exit()
            else:
                # Create destination folder.
                os.mkdir(dst_path)

                # We are fine, run!
                for analysis in Analysis.objects.all():
                    try:
                        file = get_file(analysis.image_id)
                    except (InvalidId, TypeError) as e:
                        print("Unable to dump %s: %s" % (analysis.id, e))
                        continue
                    else:
                        with open(
                                os.path.join(dst_path,
                                             "analysis_%s" % analysis.id),
                                "a") as the_file:
                            the_file.write(file.read())
        else:
            print("ERROR: path not found!")
예제 #7
0
파일: utils.py 프로젝트: eyeyunianto/ghiro
def hexdump(image_id, length=8):
    """Hexdump representation.
    @param image_id: gridfs image id
    @return: hexdump
    @see: code inspired to http://code.activestate.com/recipes/142812/
    """

    # Get image from gridfs.
    try:
       file = get_file(image_id)
    except (InvalidId, TypeError):
        return  None

    # Read data.
    src = file.read()

    hex_dump = []

    # Deal with unicode.
    if isinstance(src, unicode):
        digits = 4
    else:
        digits = 2

    # Create hex view.
    for i in xrange(0, len(src), length):
        line = {}
        s = src[i:i+length]
        hexa = b" ".join(["%0*X" % (digits, ord(x)) for x in s])
        text = b"".join([x if 0x20 <= ord(x) < 0x7F else b"." for x in s])
        line["address"] = b"%04X" % i
        line["hex"] = b"%-*s" % (length*(digits + 1), hexa)
        line["text"] = text
        hex_dump.append(line)
    return hex_dump
예제 #8
0
    def handle(self, *args, **options):
        print "Starting"

        if os.path.exists(options["path"]):
            dst_path = os.path.join(options["path"], "ghiro_output")
            if os.path.exists(dst_path):
                print "ERROR: a folder 'ghiro_output' already exist in that path!"
                sys.exit()
            else:
                # Create destination folder.
                os.mkdir(dst_path)
                # Mongo connection.
                db = mongo_connect()
                fs = gridfs.GridFS(db)
                # We are fine, run!
                for analysis in Analysis.objects.all():
                    try:
                       file = get_file(analysis.image_id)
                    except (InvalidId, TypeError) as e:
                        print "Unable to dump %s: %s" % (analysis.id, e)
                        continue
                    else:
                        with open(os.path.join(dst_path, "analysis_%s" % analysis.id), "a") as the_file:
                            the_file.write(file.read())
        else:
            print "ERROR: path not found!"
예제 #9
0
    def handle(self, *args, **options):
        print "Starting"

        if os.path.exists(options["path"]):
            dst_path = os.path.join(options["path"], "ghiro_output")
            if os.path.exists(dst_path):
                print "ERROR: a folder 'ghiro_output' already exist in that path!"
                sys.exit()
            else:
                # Mongo connection.
                db = mongo_connect()
                fs = gridfs.GridFS(db)
                # We are fine, run!
                for analysis in Analysis.objects.all():
                    try:
                        file = get_file(analysis.image_id)
                    except (InvalidId, TypeError):
                        print "Unable to dump %s" % analysis.id
                        continue
                    else:
                        with open(
                                os.path.join(dst_path,
                                             "analysis_%s" % analysis.id),
                                "a") as the_file:
                            the_file.write(file)
        else:
            print "ERROR: path not found!"
예제 #10
0
def to_base64(image_id):
    """Return a base64 representation for an image to be used in html img tag.
    @param image_id: mongo gridfs id
    @return: base64 blob
    """
    image_obj = get_file(image_id)
    image_encoded = base64.encodestring(image_obj.read())
    return "data:%s;base64,%s" % (image_obj.content_type, image_encoded)
예제 #11
0
def to_base64(image_id):
    """Return a base64 representation for an image to be used in html img tag.
    @param image_id: mongo gridfs id
    @return: base64 blob
    """
    image_obj = get_file(image_id)
    image_encoded = base64.encodestring(image_obj.read())
    return "data:%s;base64,%s" % (image_obj.content_type, image_encoded)
예제 #12
0
def to_strings(image_id):
    """Extract all strings.
    @param image_id: mongo gridfs id
    @return: strings list
    """
    data = get_file(image_id).read()
    # This strings extraction code comes form Cuckoo Sandbox.
    strings = re.findall("[\x1f-\x7e]{6,}", data)
    strings += [str(ws.decode("utf-16le")) for ws in re.findall("(?:[\x1f-\x7e][\x00]){6,}", data)]
    return strings
예제 #13
0
def to_strings(image_id):
    """Extract all strings.
    @param image_id: mongo gridfs id
    @return: strings list
    """
    data = get_file(image_id).read()
    # This strings extraction code comes form Cuckoo Sandbox.
    strings = re.findall("[\x1f-\x7e]{6,}", data)
    strings += [str(ws.decode("utf-16le")) for ws in re.findall("(?:[\x1f-\x7e][\x00]){6,}", data)]
    return strings
예제 #14
0
파일: views.py 프로젝트: josejamilena/ghiro
def image(request, id):
    try:
       file = get_file(id)
    except (InvalidId, TypeError):
        return render_to_response("error.html",
                                  {"error": "Unable to load image."},
                                  context_instance=RequestContext(request))

    data = file.read()
    response = HttpResponse(data, content_type=file.content_type)
    response["Content-Disposition"] = "attachment; filename=%s" % id
    response["Content-Length"] = len(data)
    return response
예제 #15
0
def image(request, id):
    try:
        file = get_file(id)
    except (InvalidId, TypeError):
        return render_to_response("error.html",
                                  {"error": "Unable to load image."},
                                  context_instance=RequestContext(request))

    data = file.read()
    response = HttpResponse(data, content_type=file.content_type)
    response["Content-Disposition"] = "attachment; filename=%s" % id
    response["Content-Length"] = len(data)
    return response
예제 #16
0
파일: models.py 프로젝트: Scinawa/ghiro
 def get_file_data(self):
     """Returns image file binary data."""
     try:
         return get_file(self.image_id).read()
     except gridfs.errors.NoFile:
         raise Exception("Image not found on GridFS storage")
예제 #17
0
 def get_file_data(self):
     """Returns image file binary data."""
     try:
         return get_file(self.image_id).read()
     except gridfs.errors.NoFile:
         raise Exception("Image not found on GridFS storage")