def created(self, user, page, max_metadata=20): offset = (page - 1) * max_metadata results = [] pages = 0 if (offset < 0) or (not isinstance(user, User)): return (results, pages) try: matches = Function.objects(metadata__user=user).only('metadata') total = Function.objects(metadata__user=user).count() + 0.0 pages = int(math.ceil(total / max_metadata)) if page > pages: return (results, pages) matches = matches.skip(offset).limit(max_metadata) except ValueError: return (results, pages) for function in matches: for metadata in function.metadata: if user == metadata.user: temp = metadata.dump() temp['id'] = FIRSTDB.make_id(metadata.id, 0) results.append(temp) # Bail out of inner loop early since a user can only # create one metadata entry per function break return (results, pages)
def find_function(self, _id=None, opcodes=None, apis=None, architecture=None, h_sha256=None): try: # User function ID if None != _id: return Function.objects(id=bson.objectid.ObjectId(_id)).get() # User opcodes and apis elif None not in [opcodes, apis]: return Function.objects(opcodes=opcodes, apis=apis).get() # Use hash, architecture elif None not in [architecture, h_sha256]: return Function.objects(sha256=h_sha256, architecture=architecture).get() else: return None except DoesNotExist: return None
def applied(self, sample, user, _id, is_engine=False): ''' @returns Boolean. True if added to the applied list False if not added to the applied list ''' if (not isinstance(user, User)) or (not isinstance(sample, Sample)): return False key = [str(sample.id), str(user.id)] if is_engine: engine_id = bson.objectid.ObjectId(_id) engine = Engine.objects(id=engine_id, applied__contains=key) # Check if user has already applied the signature if len(engine): return True try: engine = Engine.objects(id=engine_id).get() except DoesNotExist: # Engine does not exist return False engine.applied.append(key) engine.save() else: metadata_id = bson.objectid.ObjectId(_id) functions = Function.objects(metadata__id=metadata_id, metadata__applied__contains=key) # Check if user has already applied the signature if len(functions): return True try: function = Function.objects(metadata__id=metadata_id).get() except DoesNotExist: # Metadata does not exist return False # Get metadata for metadata in function.metadata: if metadata.id == metadata_id: metadata.applied.append(key) break function.save() return True
def delete_metadata(self, user, metadata_id): if not isinstance(user, User): return False user_metadata, engine_metadata = self.separate_metadata([metadata_id]) if not user_metadata: return False # User must be the creator of the metadata to delete it metadata_id = bson.objectid.ObjectId(user_metadata[0]) try: Function.objects(metadata__user=user, metadata__id=metadata_id).update_one( pull__metadata__id=metadata_id) return True except DoesNotExist: return False
def get_function(self, opcodes, architecture, apis, create=False, **kwargs): function = None try: function = Function.objects.get( sha256=hashlib.sha256(opcodes).hexdigest(), opcodes=bson.Binary(opcodes), architecture=architecture, apis=apis) except DoesNotExist: # Create function and add it to sample function = Function(sha256=hashlib.sha256(opcodes).hexdigest(), opcodes=bson.Binary(opcodes), architecture=architecture, apis=apis) function.save() return function
def get_architectures(self): standards = FIRSTDB.standards.copy() standards.update(Function.objects().distinct(field='architecture')) return list(standards)