Exemplo n.º 1
0
        private void GetRepoLastUpdated(GlobalConfig.SignatureSource repo)
        {
            System.Net.Http.HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("rgat", CONSTANTS.PROGRAMVERSION.RGAT_VERSION_SEMANTIC.ToString()));
            try
            {
                string commitsPath = $"https://api.github.com/repos/{repo.OrgName}/{repo.RepoName}/commits/master";

                Task <HttpResponseMessage> request = client.GetAsync(commitsPath, _token);
                request.Wait(_token);
                if (request.Result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    Task <string> content = request.Result.Content.ReadAsStringAsync();
                    content.Wait(_token);

                    if (JObject.Parse(content.Result).TryGetValue("commit", out JToken? tok1) &&
                        tok1.Type == JTokenType.Object &&
                        ((JObject)tok1).TryGetValue("committer", out JToken? tok2) &&
                        tok2.Type == JTokenType.Object &&
                        ((JObject)tok2).TryGetValue("date", out JToken? updateTok))
                    {
                        if (updateTok.Type == Newtonsoft.Json.Linq.JTokenType.Date)
                        {
                            repo.LastUpdate       = updateTok.ToObject <DateTime>();
                            repo.LastCheck        = DateTime.Now;
                            repo.LastRefreshError = null;
                            GlobalConfig.Settings.Signatures.UpdateSignatureSource(repo);
                            return;
                        }
                    }
                    Logging.RecordError($"No valid 'updated_at' field in repo response from github while refreshing {repo.FetchPath}");
                    repo.LastRefreshError = "Github Error";
                    GlobalConfig.Settings.Signatures.UpdateSignatureSource(repo);
                    return;
                }
                repo.LastRefreshError = $"{request.Result.StatusCode}";
                Logging.RecordError($"Error updating {repo.FetchPath} => {request.Result.StatusCode}:{request.Result.ReasonPhrase}");
                return;
            }
            catch (Exception e)
            {
                if (e.InnerException != null && e.InnerException.GetType() == typeof(HttpRequestException))
                {
                    string message = ((HttpRequestException)e.InnerException).Message;
                    repo.LastRefreshError = message;
                }
                else
                {
                    repo.LastRefreshError = "See Logs";
                }
                GlobalConfig.Settings.Signatures.UpdateSignatureSource(repo);
                Logging.RecordException($"Exception updating {repo.FetchPath} => {e.Message}", e);
            }
        }
Exemplo n.º 2
0
        private static string?GetRepoDirectory(ref GlobalConfig.SignatureSource repo, string sigsdir)
        {
            try
            {
                if (!Directory.Exists(sigsdir))
                {
                    Logging.RecordError($"Base signatures directory {sigsdir} does not exist");
                    return(null);
                }

                if (!sigsdir.ToLower().Contains("signature"))
                {
                    Logging.RecordError($"Base signatures directory {sigsdir} does not contain the word 'signature' anywhere in the path." +
                                        $" This is a safety measure as the contents will be deleted on download. Please change it in the Settings->Files menu");
                    return(null);
                }

                string repoSpecific  = repo.RepoName + repo.SubDir;
                string repoDirectory = Path.Combine(sigsdir, repo.OrgName + "_" + MurmurHash.MurmurHash2.Hash(repoSpecific));

                if (!new Uri(GlobalConfig.GetSettingPath(CONSTANTS.PathKey.YaraRulesDirectory)).IsBaseOf(new Uri(repoDirectory)))
                {
                    repo.LastDownloadError = "Bad Repo Name";
                    Logging.RecordError($"Repo download directory {repoDirectory} is not in the signatures directory {sigsdir}");
                    GlobalConfig.Settings.Signatures.UpdateSignatureSource(repo);
                    return(null);
                }

                return(repoDirectory);
            }
            catch (Exception e)
            {
                Logging.RecordException($"Error initing signature directory for repo {repo.FetchPath}: {e.Message}", e);
                return(null);
            }
        }