Ejemplo n.º 1
0
def api_get_file(request, hash):
    """ Return file from given hash
    """
    try:
        file = FileModel.objects.get(hash=hash)
        content = ""
        with open(file.filepath, "rb") as fd:
            content = fd.read()
        # Direct download
        if 'raw' in request.GET.keys():
            content = ""
            with open(file.filepath, "rb") as fd:
                content = fd.read()
            content_type = "application/octet-stream"
            response = HttpResponse(content, content_type=content_type)
            content_disposition = "attachment; filename=%s" % file.filename
            response["Content-Disposition"] = content_disposition
            return response

        # Graph download
        if 'graph' in request.GET.keys():
            if file.graph_file != '' and file.graph_file != False:
                content = ""
                with open(file.graph_file, "rb") as fd:
                    content = fd.read()

                content_type = "image/png"
                response = HttpResponse(content, content_type=content_type)
                return response
            elif file.graph_file == False:
                return HttpResponse("no graph")
            elif file.graph_file == '':
                try:
                    workspace = file.firmware.all()[0].filepath.replace(
                        "firmware", "")
                    parse_elf(workspace, file)
                    with open(file.graph_file, "rb") as fd:
                        content = fd.read()

                    content_type = "image/png"
                    response = HttpResponse(content, content_type=content_type)
                except NotImplementedError:
                    file.graph_file = False
                    file.save()
                    return HttpResponse("no graph")

        loots = []
        for loot in file.loots.all():
            loots.append({"type": loot.type.name, "info": loot.info})

        response = {
            "loots": loots,
            "hash": file.hash,
            "type": file.file_type,
            "filename": file.filename,
            "filesize": file.filesize
        }

        if is_elf(file):
            response["imports"] = file.imports
            response["informations"] = file.informations

            if file.graph_file == "":
                response["graph"] = ''
            else:
                if file.graph_file == False:
                    response["graph"] = False
                else:
                    response["graph"] = True

        if "text" in file.file_type:
            content = ""
            with open(file.filepath, "r") as fd:
                content = fd.read()
            response["content"] = content

        return JsonResponse(response)
    except FileModel.DoesNotExist:
        return JsonResponse({"error": "file not found", "hash": hash})
Ejemplo n.º 2
0
    def find_loots(self, file):
        # Find loots based on filenames
        loots_refs = settings.LOOTS_FILENAMES
        for type, values in loots_refs.items():
            try:
                loot_type = LootTypeModel.objects.get(name=type)
                loot_type.save()
            except LootTypeModel.DoesNotExist:
                loot_type = LootTypeModel()
                loot_type.name = type
                loot_type.save()
            for value in values:
                if fnmatch.fnmatch(file.filename, value):
                    loot = LootModel()
                    loot.file = file
                    loot.type = loot_type
                    loot.info = "Filename looks interesting"
                    loot.save()

        # Find greppable loots
        loots_refs = settings.LOOTS_GREP
        
        with open(file.filepath, "rb") as fd:
            content = fd.read()

        for type, values in loots_refs.items():
            try:
                loot_type = LootTypeModel.objects.get(name=type)
            except LootTypeModel.DoesNotExist:
                loot_type = LootTypeModel()
                loot_type.name = type
                loot_type.save()
            for value in values:
                matchs = re.findall(str.encode(value),
                                    content,
                                    re.IGNORECASE|re.MULTILINE)
                if matchs:
                    for  match in matchs:
                        try:
                            loot = LootModel.objects.get(type=loot_type, 
                                                         file=file, 
                                                         info=match.decode("utf-8"))
                            continue
                        except LootModel.DoesNotExist:
                            loot = LootModel()
                            loot.file = file
                            loot.type = loot_type
                            loot.info = match.decode("utf-8")
                            loot.save()

        if is_elf(file):
            handle = open_pipe(file)
            insecure_imports(file, handle)
            binary_informations(file, handle)

        if is_parsable(file.filepath):
            type = "static source analysis"
            try:
                loot_type = LootTypeModel.objects.get(name=type)
            except LootTypeModel.DoesNotExist:
                loot_type = LootTypeModel()
                loot_type.name = type
                loot_type.save()
            for msg in parse(file.filepath):
                loot = LootModel()    
                loot.file = file
                loot.type = loot_type
                loot.info = msg
                loot.save()


        if is_cert(file.filepath):
            type = "certificate"
            try:
                loot_type = LootTypeModel.objects.get(name=type)
            except LootTypeModel.DoesNotExist:
                loot_type = LootTypeModel()
                loot_type.name = type
                loot_type.save()
            try:
                for msg in check_cert(file.filepath):
                    loot = LootModel()    
                    loot.file = file
                    loot.type = loot_type
                    loot.info = msg
                    loot.save()
            except:
                pass
Ejemplo n.º 3
0
def get_file(request, hash):
    """ Return file from given hash
    """
    try:
        file = FileModel.objects.get(hash=hash)
        content = ""
        with open(file.filepath, "rb") as fd:
            content = fd.read()
        # Direct download
        if 'raw' in request.GET.keys():
            content = ""
            with open(file.filepath, "rb") as fd:
                content = fd.read()
            content_type = "application/octet-stream"
            response = HttpResponse(content, content_type=content_type)
            content_disposition = "attachment; filename=%s" % file.filename
            response["Content-Disposition"] = content_disposition
            return response

        # Graph download
        if 'graph' in request.GET.keys():
            if file.graph_file != '' and file.graph_file != False:
                content = ""
                with open(file.graph_file, "rb") as fd:
                    content = fd.read()

                content_type = "image/png"
                response = HttpResponse(content, content_type=content_type)
                return response
            elif file.graph_file == False:
                return HttpResponse("no graph")
            elif file.graph_file == '':
                try:
                    workspace = file.firmware.all()[0].filepath.replace("firmware",
                                                                        "")
                    parse_elf(workspace, file)
                    with open(file.graph_file, "rb") as fd:
                        content = fd.read()

                    content_type = "image/png"
                    response = HttpResponse(content, content_type=content_type)
                except:
                    file.graph_file = False
                    file.save()
                    return HttpResponse("no graph")

        loots = []
        for loot in file.loots.all():
            loots.append({"type": loot.type.name, "info": loot.info})

        response = {"loots": loots,
                    "hash": file.hash,
                    "type": file.file_type,
                    "filename": file.filename,
                    "filesize": file.filesize}

        if is_elf(file):
            response["imports"] = file.imports
            response["informations"] = file.informations
            
            if file.graph_file == "":
                response["graph"] = ''
            else:
                if file.graph_file == False:
                    response["graph"] = False
                else:
                    response["graph"] = True   

        if "ASCII text" in file.file_type:
            content = ""
            with open(file.filepath, "r") as fd:
                content = fd.read()
            response["content"] = content

        return JsonResponse(response)
    except FileModel.DoesNotExist:
        return JsonResponse({"error": "file not found", "hash": hash})
Ejemplo n.º 4
0
    def find_loots(self, file):
        # Find loots based on filenames
        loots_refs = settings.LOOTS_FILENAMES
        for type, values in loots_refs.items():
            try:
                loot_type = LootTypeModel.objects.get(name=type)
                loot_type.save()
            except LootTypeModel.DoesNotExist:
                loot_type = LootTypeModel()
                loot_type.name = type
                loot_type.save()
            for value in values:
                if fnmatch.fnmatch(file.filename, value):
                    loot = LootModel()
                    loot.file = file
                    loot.type = loot_type
                    loot.info = "Filename looks interesting"
                    loot.save()

        # Find greppable loots
        loots_refs = settings.LOOTS_GREP

        with open(file.filepath, "rb") as fd:
            content = fd.read()

        for type, values in loots_refs.items():
            try:
                loot_type = LootTypeModel.objects.get(name=type)
            except LootTypeModel.DoesNotExist:
                loot_type = LootTypeModel()
                loot_type.name = type
                loot_type.save()
            for value in values:
                matchs = re.findall(str.encode(value), content,
                                    re.IGNORECASE | re.MULTILINE)
                if matchs:
                    for match in matchs:
                        try:
                            loot = LootModel.objects.get(
                                type=loot_type,
                                file=file,
                                info=match.decode("utf-8"))
                            continue
                        except LootModel.DoesNotExist:
                            loot = LootModel()
                            loot.file = file
                            loot.type = loot_type
                            loot.info = match.decode("utf-8")
                            loot.save()

        if is_elf(file):
            handle = open_pipe(file)
            insecure_imports(file, handle)
            binary_informations(file, handle)

        if is_parsable(file.filepath):
            type = "static source analysis"
            try:
                loot_type = LootTypeModel.objects.get(name=type)
            except LootTypeModel.DoesNotExist:
                loot_type = LootTypeModel()
                loot_type.name = type
                loot_type.save()
            for msg in parse(file.filepath):
                loot = LootModel()
                loot.file = file
                loot.type = loot_type
                loot.info = msg
                loot.save()

        if is_cert(file.filepath):
            type = "certificate"
            try:
                loot_type = LootTypeModel.objects.get(name=type)
            except LootTypeModel.DoesNotExist:
                loot_type = LootTypeModel()
                loot_type.name = type
                loot_type.save()
            try:
                for msg in check_cert(file.filepath):
                    loot = LootModel()
                    loot.file = file
                    loot.type = loot_type
                    loot.info = msg
                    loot.save()
            except:
                pass