Exemple #1
0
    def _get_previews(self):
        """Extract previews."""
        if len(self.metadata.get_preview_properties()) > 0:
            # Fetched previews key.
            self.results["preview"] = []

            for preview in self.metadata.get_preview_properties():
                p = utils.AutoVivification()
                p["mime_type"] = preview.get_mime_type()
                p["size"] = len(self.metadata.get_preview_image(preview).get_data())
                p["ext"] = preview.get_extension()
                p["dimensions"] = [preview.get_width(), preview.get_height()]

                # Resize if needed, and store.
                try:
                    img = utils.str2image(self.metadata.get_preview_image(preview).get_data())
                    if preview.get_width() > 256 or preview.get_height() > 160:
                        p["original_file"] = db.save_file(utils.image2str(img), content_type="image/jpeg")
                    img.thumbnail([256, 160], Image.ANTIALIAS)
                    p["file"] = db.save_file(utils.image2str(img), content_type="image/jpeg")
                except Exception as e:
                    logger.warning("Error reading preview: {0}".format(e))
                    continue
                finally:
                    # Save.
                    self.results["preview"].append(p)
Exemple #2
0
    def _get_previews(self):
        """Extract previews."""
        if len(self.metadata.get_preview_properties()) > 0:
            # Fetched previews key.
            self.results["preview"] = []

            for preview in self.metadata.get_preview_properties():
                p = utils.AutoVivification()
                p["mime_type"] = preview.get_mime_type()
                p["size"] = len(
                    self.metadata.get_preview_image(preview).get_data())
                p["ext"] = preview.get_extension()
                p["dimensions"] = [preview.get_width(), preview.get_height()]

                # Resize if needed, and store.
                try:
                    img = utils.str2image(
                        self.metadata.get_preview_image(preview).get_data())
                    if preview.get_width() > 256 or preview.get_height() > 160:
                        p["original_file"] = db.save_file(
                            utils.image2str(img), content_type="image/jpeg")
                    img.thumbnail([256, 160], Image.ANTIALIAS)
                    p["file"] = db.save_file(utils.image2str(img),
                                             content_type="image/jpeg")
                except Exception as e:
                    logger.warning("Error reading preview: {0}".format(e))
                    continue
                finally:
                    # Save.
                    self.results["preview"].append(p)
Exemple #3
0
def create_thumb(file_path):
    """Create thumbnail
    @param file_path: file path
    @return: GridFS ID
    """
    try:
        thumb = Image.open(file_path)
        thumb.thumbnail([200, 150], Image.ANTIALIAS)
        return db.save_file(data=image2str(thumb), content_type="image/jpeg")
    except:
        return None
Exemple #4
0
def create_thumb(file_path):
    """Create thumbnail
    @param file_path: file path
    @return: GridFS ID
    """
    try:
        thumb = Image.open(file_path)
        thumb.thumbnail([200, 150], Image.ANTIALIAS)
        return db.save_file(data=image2str(thumb), content_type="image/jpeg")
    except:
        return None
Exemple #5
0
    def run(self):
        """Run data analysis."""
        # Create temporary file.
        handle, resaved = tempfile.mkstemp()

        # Open file and resave it.
        try:
            im = Image.open(self.file_path)
            im.save(resaved, "JPEG", quality=95)
            resaved_im = Image.open(resaved)
        except IOError as e:
            logger.warning("ELA error opening image: {0}".format(e))
            return
        finally:
            os.close(handle)
            os.remove(resaved)

        # Trick to convert images like PNG to a format comparable with JPEG.
        if im.mode != "RGB":
            im = im.convert("RGB")

        # Create ELA image.
        try:
            ela_im = ImageChops.difference(im, resaved_im)
        except Exception as e:
            logger.warning("Unable to calculate ELA difference: {0}".format(e))
            return

        # Calculate difference
        extrema = ela_im.getextrema()
        max_diff = max([ex[1] for ex in extrema])
        if not max_diff:
            return
        scale = 255.0 / max_diff
        ela_im = ImageEnhance.Brightness(ela_im).enhance(scale)
        self.results["max_difference"] = max([ex[1] for ex in extrema])

        # Resize image if it's too big.
        width, height = ela_im.size
        if width > 1800:
            ela_im.thumbnail([1800, 1800], Image.ANTIALIAS)

        # Save image.
        try:
            img = utils.image2str(ela_im)
            self.results["ela_image"] = db.save_file(img,
                                                     content_type="image/jpeg")
        except Exception as e:
            logger.warning("ELA error saving image: {0}".format(e))
        finally:
            return self.results
Exemple #6
0
    def run(self):
        """Run data analysis."""
        # Create temporary file.
        handle, resaved = tempfile.mkstemp()

        # Open file and resave it.
        try:
            im = Image.open(self.file_path)
            im.save(resaved, "JPEG", quality=95)
            resaved_im = Image.open(resaved)
        except IOError as e:
            logger.warning("ELA error opening image: {0}".format(e))
            return
        finally:
            os.close(handle)
            os.remove(resaved)

        # Trick to convert images like PNG to a format comparable with JPEG.
        if im.mode != "RGB":
            im = im.convert("RGB")

        # Create ELA image.
        try:
            ela_im = ImageChops.difference(im, resaved_im)
        except Exception as e:
            logger.warning("Unable to calculate ELA difference: {0}".format(e))
            return

        # Calculate difference
        extrema = ela_im.getextrema()
        max_diff = max([ex[1] for ex in extrema])
        if not max_diff:
            return
        scale = 255.0/max_diff
        ela_im = ImageEnhance.Brightness(ela_im).enhance(scale)
        self.results["max_difference"] = max([ex[1] for ex in extrema])

        # Resize image if it's too big.
        width, height = ela_im.size
        if width > 1800:
            ela_im.thumbnail([1800, 1800], Image.ANTIALIAS)

        # Save image.
        try:
            img = utils.image2str(ela_im)
            self.results["ela_image"] = db.save_file(img, content_type="image/jpeg")
        except Exception as e:
            logger.warning("ELA error saving image: {0}".format(e))
        finally:
            return self.results
Exemple #7
0
 def _add_task(self, file, case, user):
     """Adds a new task to database.
     @param file: file path
     @param case: case id
     @param user: user id
     """
     task = Analysis()
     task.owner = user
     task.case = case
     task.file_name = os.path.basename(file)
     mime = magic.Magic(mime=True)
     task.image_id = save_file(file_path=file, content_type=mime.from_file(file))
     task.thumb_id = create_thumb(file)
     task.save()
Exemple #8
0
class Command(BaseCommand):
    """Image submission via command line."""

    option_list = BaseCommand.option_list + (
        make_option("--target",
                    "-t",
                    dest="target",
                    help="Path of the file or directory to submit"),
        make_option("--case",
                    "-c",
                    dest="case",
                    help="Case ID, images will be attached to it"),
        make_option("--username", "-u", dest="username", help="Username"),
    )

    help = "Task submission"

    def handle(self, *args, **options):
        """Runs command."""
        # Get options.
        user = Profile.objects.get(username=options["username"].strip())
        case = Case.objects.get(pk=options["case"].strip())

        # Add directory or files.
        if os.path.isdir(options["target"]):
            for file_name in os.listdir(options["target"]):
                print "INFO: processing {0}".format(file_name)
                self._add_task(os.path.join(options["target"], file_name),
                               case, user)
        elif os.path.isfile(options["target"]):
            print "INFO: processing {0}".format(options["target"])
            self._add_task(options["target"], case, user)
        else:
            print "ERROR: target is not a file or directory"

    def _add_task(self, file, case, user):
        """Adds a new task to database.
        @param file: file path
        @param case: case id
        @param user: user id
        """
        task = Analysis()
        task.owner = user
        task.case = case
        task.file_name = os.path.basename(file)
        mime = magic.Magic(mime=True)
        task.image_id = save_file(file_path=file,
                                  content_type=mime.from_file(file))
        task.thumb_id = create_thumb(file)
        task.save()
Exemple #9
0
def new_folder(request, case_id):
    """Load files from a local directory."""
    case = get_object_or_404(Case, pk=case_id)

    # Security check.
    if not(request.user.is_superuser or request.user in case.users.all()):
        return render_to_response("error.html",
                                  {"error": "You are not authorized to add image to this."},
                                  context_instance=RequestContext(request))

    if case.state == "C":
        return render_to_response("error.html",
                                  {"error": "You cannot add an image to a closed case."},
                                  context_instance=RequestContext(request))

    if request.method == "POST":
        form = forms.ImageFolderForm(request.POST)
        if form.is_valid():
            # Check.
            if not os.path.exists(request.POST.get("path")):
                return render_to_response("error.html",
                    {"error": "Folder does not exist."},
                    context_instance=RequestContext(request))
            elif not os.path.isdir(request.POST.get("path")):
                return render_to_response("error.html",
                    {"error": "Folder is not a directory."},
                    context_instance=RequestContext(request))
            # Add all files in directory.
            for file in os.listdir(request.POST.get("path")):
                task = Analysis()
                task.owner = request.user
                task.case = case
                task.file_name = file
                mime = magic.Magic(mime=True)
                task.image_id = save_file(file_path=os.path.join(request.POST.get("path"), file),
                                          content_type=mime.from_file(os.path.join(request.POST.get("path"), file)))
                task.thumb_id = create_thumb(os.path.join(request.POST.get("path"), file))
                task.save()
                # Auditing.
                log_activity("I",
                             "Created new analysis {0}".format(task.file_name),
                             request)
            return HttpResponseRedirect(reverse("analyses.views.show_case", args=(case.id, "list")))
    else:
        form = forms.ImageFolderForm()

    return render_to_response("analyses/images/new_folder.html",
                              {"form": form, "case": case},
                              context_instance=RequestContext(request))
Exemple #10
0
def new_image(request, case_id):
    """Upload a new image."""
    case = get_object_or_404(Case, pk=case_id)

    # Security check.
    if not request.user.is_superuser and not request.user in case.users.all():
        return render_to_response("error.html",
                                  {"error": "You are not authorized to add image to this."},
                                  context_instance=RequestContext(request))

    if case.state == "C":
        return render_to_response("error.html",
                                  {"error": "You cannot add an image to a closed case."},
                                  context_instance=RequestContext(request))

    if request.method == "POST":
        form = forms.UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            task = form.save(commit=False)
            task.owner = request.user
            task.case = case
            task.file_name = request.FILES["image"].name
            task.image_id = save_file(file_path=request.FILES["image"].temporary_file_path(),
                                      content_type=request.FILES["image"].content_type)
            task.thumb_id = create_thumb(request.FILES["image"].temporary_file_path())
            task.save()
            # Auditing.
            log_activity("I",
                         "Created new analysis {0}".format(task.file_name),
                         request)
            return HttpResponseRedirect(reverse("analyses.views.show_case", args=(case.id, "list")))
    else:
        form = forms.UploadImageForm()

    return render_to_response("analyses/images/new_image.html",
                              {"form": form, "case": case},
                              context_instance=RequestContext(request))
Exemple #11
0
    if case.state == "C":
        return render_to_response(
            "error.html",
            {"error": "You cannot add an image to a closed case."},
            context_instance=RequestContext(request))

    if request.method == "POST":
        form = forms.UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            task = form.save(commit=False)
            task.owner = request.user
            task.case = case
            task.file_name = request.FILES["image"].name
            task.image_id = save_file(
                file_path=request.FILES["image"].temporary_file_path(),
                content_type=request.FILES["image"].content_type)
            task.thumb_id = create_thumb(
                request.FILES["image"].temporary_file_path())
            task.save()
            # Auditing.
            log_activity("I",
                         "Created new analysis {0}".format(task.file_name),
                         request)
            return HttpResponseRedirect(
                reverse("analyses.views.show_case", args=(case.id, "list")))
    else:
        form = forms.UploadImageForm()

    return render_to_response("analyses/images/new_image.html", {
        "form": form,