Пример #1
0
        public async Task <bool> Insert(QueryHashResponse item)
        {
            return(await Task.Run(() =>
            {
                try
                {
                    if (item == null)
                    {
                        throw new ArgumentNullException(nameof(item));
                    }

                    if (string.IsNullOrEmpty(item.MD5Hash))
                    {
                        throw new ArgumentNullException(nameof(item.MD5Hash));
                    }

                    using (var db = new LiteDB.LiteDatabase(DB_FILE_NAME))
                    {
                        var collection = db.GetCollection <QueryHashResponse>();

                        return collection.Insert(item) > 0;
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, $"Attempting to insert {item} into {Name}");

                    return false;
                }
            }));
        }
Пример #2
0
        public async Task <QueryHashResponse> Post(IFormFile file)
        {
            var response = new QueryHashResponse
            {
                Guid = Guid.NewGuid()
            };

            try
            {
                if (file == null)
                {
                    throw new ArgumentNullException(nameof(file));
                }

                using (var memoryStream = new MemoryStream())
                {
                    await file.CopyToAsync(memoryStream);

                    if (Settings.CacheEnabled && Cache != null)
                    {
                        var md5Hash = MD5.Create().ComputeHash(memoryStream.ToArray()).ToString();

                        var cacheResult = await Cache.GetResponseAsync(md5Hash);

                        if (cacheResult != null)
                        {
                            response.Status      = ResponseStatus.SCANNED;
                            response.MD5Hash     = md5Hash;
                            response.IsMalicious = cacheResult.IsMalicious;

                            Logger.LogDebug(response.ToString());

                            return(response);
                        }

                        Logger.LogDebug($"{Cache.Name} did not contain {md5Hash}");
                    }

                    Logger.LogDebug($"Adding {response.Guid} to the Queue");

                    await Queue.AddToQueueAsync(memoryStream.ToArray(), response.Guid);
                }

                response.Status = ResponseStatus.SUBMITTED;

                Logger.LogDebug(response.ToString());

                return(response);
            }
            catch (Exception ex)
            {
                return(ReturnErrorResponse(ex, response.Guid, "Failed to add to queue"));
            }
        }
Пример #3
0
        public static async Task SubscribeMethod(byte[] data, AdvancedMessageContext context)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var processor = _processors.FirstOrDefault(a => !a.Running);

            if (processor == null)
            {
                // Keep message queued
                context.RetryLater(TimeSpan.FromSeconds(Constants.QUEUE_RETRY_SECONDS));

                return;
            }

            var isMalicious = processor.IsMalicious(data);

            var queryHashResponse = new QueryHashResponse
            {
                Guid        = context.GlobalRequestId,
                Status      = ResponseStatus.SCANNED,
                IsMalicious = isMalicious,
                MD5Hash     = MD5.Create().ComputeHash(data).ToString()
            };

            var insertResult = await _serviceProvider.GetService <StorageManager>().InsertAsync(queryHashResponse);

            if (!insertResult)
            {
                Log.Error("Failed to write to storage, adding back into the queue");

                context.RetryLater(TimeSpan.FromSeconds(Constants.QUEUE_RETRY_SECONDS));

                return;
            }

            if (_serviceProvider.GetService <Settings>().CacheEnabled)
            {
                var cacheResult = await _serviceProvider.GetService <ICache>().AddResponseAsync(queryHashResponse);

                if (!cacheResult)
                {
                    Log.Error($"Failed to write to {_serviceProvider.GetService<ICache>().Name} cache");
                }
            }
        }
        public async Task GetGuidValidTest()
        {
            var response = new QueryHashResponse
            {
                Guid         = Guid.NewGuid(),
                Status       = ResponseStatus.SUBMITTED,
                MD5Hash      = "1234",
                ErrorMessage = string.Empty,
                IsMalicious  = false
            };

            await new LiteDBDatabase().Insert(response);

            await createQueryController().Get(response.Guid);
        }
        protected QueryHashResponse ReturnErrorResponse(Exception exception, Guid guid, string additionalError = null)
        {
            var response = new QueryHashResponse
            {
                Guid         = guid,
                Status       = ResponseStatus.ERROR,
                ErrorMessage = string.IsNullOrEmpty(additionalError)
                    ? exception.ToString()
                    : $"Exception: {exception} | Additional Information: {additionalError}"
            };

            Logger.LogError(response.ToString());

            return(response);
        }
Пример #6
0
        public async Task InsertPartialGoodTest()
        {
            var mongo = new MongoDatabase(_settings);

            var response = new QueryHashResponse
            {
                Guid         = Guid.NewGuid(),
                MD5Hash      = string.Empty,
                Status       = ResponseStatus.SCANNED,
                ErrorMessage = string.Empty,
                IsMalicious  = false
            };

            var result = await mongo.Insert(response);

            Assert.IsFalse(result);
        }
Пример #7
0
        public async Task InsertGoodTest()
        {
            var liteDB = new LiteDBDatabase();

            var queryHashResponse = new QueryHashResponse
            {
                Guid         = Guid.NewGuid(),
                Status       = ResponseStatus.PENDING,
                MD5Hash      = "1244",
                ErrorMessage = string.Empty,
                IsMalicious  = false
            };

            var result = await liteDB.Insert(queryHashResponse);

            Assert.IsTrue(result);
        }
Пример #8
0
        public async Task InsertDefault()
        {
            var storageManager = new StorageManager(new List <IStorageDatabase>
            {
                new LiteDBDatabase()
            });

            var query = new QueryHashResponse
            {
                Guid         = Guid.NewGuid(),
                MD5Hash      = "1234",
                Status       = ResponseStatus.PENDING,
                ErrorMessage = string.Empty,
                IsMalicious  = false
            };

            var result = await storageManager.InsertAsync(query);

            Assert.IsTrue(result);
        }
Пример #9
0
        /// <summary>
        /// Inserts the QueryHashResponse item into the MongoDB
        /// </summary>
        /// <param name="item">QueryHashResponse to be added to the database</param>
        /// <returns>True if successfull, false otherwise</returns>
        public async Task <bool> Insert(QueryHashResponse item)
        {
            try
            {
                if (string.IsNullOrEmpty(item.MD5Hash))
                {
                    throw new ArgumentNullException(nameof(item.MD5Hash));
                }

                var collection = _db.GetCollection <QueryHashResponse>("results");

                await collection.InsertOneAsync(item);

                return(true);
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Error in {Name} when attempting to write {item}");

                return(false);
            }
        }
Пример #10
0
        public async Task <bool> AddResponseAsync(QueryHashResponse response)
        {
            try
            {
                if (response == null)
                {
                    throw new ArgumentNullException(nameof(response));
                }

                return(await _database.StringSetAsync(response.MD5Hash, response.ToJSON()));
            }
            catch (ArgumentNullException)
            {
                Log.Error("Response was null");

                return(false);
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Error attempting to save {response?.MD5Hash} into {Name}");

                return(false);
            }
        }
Пример #11
0
        public void ToJSONTest()
        {
            var response = new QueryHashResponse();

            Assert.IsNotNull(response.ToJSON());
        }