Beispiel #1
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()
Beispiel #2
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"),
        make_option("--recurse",
                    "-r",
                    dest="recurse",
                    default=False,
                    action="store_true",
                    help="Recurse inside subdirectories"),
    )

    help = "Task submission"

    def handle(self, *args, **options):
        """Runs command."""
        # Validation.
        if not options["username"] or not options["case"] or not options[
                "target"]:
            print "Options -t (target), -c (case) and -u (user are mandatory. Exiting."
            sys.exit(1)

        # 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"]) and options["recurse"]:
            for dirname, dirnames, filenames in os.walk(options["target"]):
                for filename in filenames:
                    target = os.path.join(dirname, filename)
                    print "INFO: processing {0}".format(target)
                    self._add_task(target, case, user)
        elif 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
        """
        # File type check.
        mime = magic.Magic(mime=True)
        content_type = mime.from_file(file)
        if not check_allowed_content(content_type):
            print "WARNING: Skipping %s: file type not allowed." % file
        else:
            # Add to analysis queue.
            task = Analysis()
            task.owner = user
            task.case = case
            task.file_name = os.path.basename(file)
            task.image_id = save_file(file_path=file,
                                      content_type=content_type)
            task.thumb_id = create_thumb(file)
            task.save()
Beispiel #3
0
            url_temp.write(url.read())
            url_temp.flush()

            # Convert to File object.
            url_file = File(url_temp).name

            # Check content type.
            mime = magic.Magic(mime=True)
            content_type = mime.from_file(url_file)
            if not check_allowed_content(content_type):
                return render_to_response(
                    "error.html", {"error": "File type not supported"},
                    context_instance=RequestContext(request))

            # Create analysis task.
            task = Analysis()
            task.owner = request.user
            task.case = case
            task.file_name = os.path.basename(
                urlparse.urlparse(request.POST.get("url")).path)
            task.image_id = save_file(file_path=url_file,
                                      content_type=content_type)
            task.thumb_id = create_thumb(url_file)
            task.save()
            # Auditing.
            log_activity(
                "I", "Created new analysis %s from URL %s" %
                (task.file_name, request.POST.get("url")), request)
            return HttpResponseRedirect(
                reverse("analyses.views.show_case", args=(case.id, "list")))
    else:
Beispiel #4
0
def new_image(request):
    """Upload a new image."""
    user = api_authenticate(request.POST.get("api_key"))

    case = get_object_or_404(Case, pk=request.POST.get("case_id"))

    # Security check.
    if not user.is_superuser and not user in case.users.all():
        return HttpResponse("You are not authorized to add image to this",
                            status=400)

    if case.state == "C":
        return HttpResponse("You cannot add an image to a closed case",
                            status=400)

    task = Analysis(owner=user,
                    case=case,
                    file_name=request.FILES["image"].name,
                    image_id=save_file(
                        file_path=request.FILES["image"].temporary_file_path(),
                        content_type=request.FILES["image"].content_type),
                    thumb_id=create_thumb(
                        request.FILES["image"].temporary_file_path()))
    task.save()

    # Auditing.
    log_activity("I",
                 "Created new analysis via API %s" % task.file_name,
                 request,
                 user=user)

    response_data = {"id": task.id}