示例#1
0
        /// <summary>
        /// Main entrypoint for the download program.
        /// </summary>
        /// <param name="args">parameters passed in from the user</param>
        static async Task Main(string[] args)
        {
            var detectBackdoorTool = new DetectBackdoorTool();

            Logger.Debug($"Microsoft OSS Gadget - {TOOL_NAME} {VERSION}");
            detectBackdoorTool.ParseOptions(args);

            if (((IList <string>)detectBackdoorTool.Options["target"]).Count > 0)
            {
                var characteristicTool = new CharacteristicTool();
                characteristicTool.Options["target"] = detectBackdoorTool.Options["target"];
                characteristicTool.Options["disable-default-rules"] = true;
                characteristicTool.Options["custom-rule-directory"] = RULE_DIRECTORY;

                foreach (var target in (IList <string>)detectBackdoorTool.Options["target"])
                {
                    try
                    {
                        var purl = new PackageURL(target);
                        characteristicTool.AnalyzePackage(purl).Wait();
                    }
                    catch (Exception ex)
                    {
                        Logger.Warn(ex, "Error processing {0}: {1}", target, ex.Message);
                    }
                }
            }
            else
            {
                Logger.Warn("No target provided; nothing to analyze.");
                DetectBackdoorTool.ShowUsage();
                Environment.Exit(1);
            }
        }
        /// <summary>
        ///     Main entrypoint for the download program.
        /// </summary>
        /// <param name="args"> parameters passed in from the user </param>
        private static async Task Main(string[] args)
        {
            var detectBackdoorTool = new DetectBackdoorTool();
            var parsedOptions      = detectBackdoorTool.ParseOptions <Options>(args).Value;
            var detectionResults   = await detectBackdoorTool.RunAsync(parsedOptions);

            foreach (var result in detectionResults)
            {
                foreach (var entry in result)
                {
                    if (entry.Value == null || entry.Value.Metadata == null || entry.Value.Metadata.Matches == null)
                    {
                        continue;
                    }

                    if (parsedOptions.Format == "text")
                    {
                        foreach (var match in entry.Value.Metadata.Matches.OrderByDescending(x => x.Confidence))
                        {
                            WriteMatch(match);
                        }
                        Console.WriteLine($"{entry.Value.Metadata.TotalMatchesCount} matches found.");
                    }

                    void WriteMatch(MatchRecord match)
                    {
                        var filename = match.FileName;

                        if (filename == null)
                        {
                            return;
                        }
                        var sourcePathLength = entry.Value.Metadata.SourcePath?.Length;

                        if (sourcePathLength.HasValue)
                        {
                            if (entry.Value.Metadata.SourcePath != null && filename.StartsWith(entry.Value.Metadata.SourcePath))
                            {
                                filename = filename[sourcePathLength.Value..];
                            }
                        }
                        Console.WriteLine($"{match.Tags?.First()} - {filename}:{match.StartLocationLine} - {match.RuleName} ({match.Severity} - {match.Confidence})");
                    }
示例#3
0
        /// <summary>
        ///     Main entrypoint for the download program.
        /// </summary>
        /// <param name="args"> parameters passed in from the user </param>
        private static async Task Main(string[] args)
        {
            ShowToolBanner();

            DetectBackdoorTool?detectBackdoorTool = new DetectBackdoorTool();
            Options?           parsedOptions      = detectBackdoorTool.ParseOptions <Options>(args).Value;
            List <Dictionary <string, AnalyzeResult?> >?detectionResults = await detectBackdoorTool.RunAsync(parsedOptions);

            foreach (Dictionary <string, AnalyzeResult?>?result in detectionResults)
            {
                foreach (KeyValuePair <string, AnalyzeResult?> entry in result)
                {
                    if (entry.Value == null || entry.Value.Metadata == null || entry.Value.Metadata.Matches == null)
                    {
                        continue;
                    }

                    if (parsedOptions.Format == "text")
                    {
                        IOrderedEnumerable <MatchRecord>?matchEntries = entry.Value.Metadata.Matches.OrderByDescending(x => x.Confidence);
                        int matchEntriesCount = matchEntries.Count();
                        int matchIndex        = 1;

                        foreach (MatchRecord?match in matchEntries)
                        {
                            WriteMatch(match, matchIndex, matchEntriesCount);
                            matchIndex++;
                        }
                        Console.WriteLine($"{entry.Value.Metadata.TotalMatchesCount} matches found.");
                    }

                    void WriteMatch(MatchRecord match, int index, int matchCount)
                    {
                        string?filename = match.FileName;

                        if (filename == null)
                        {
                            return;
                        }
                        int?sourcePathLength = entry.Value.Metadata.SourcePath?.Length;

                        if (sourcePathLength.HasValue)
                        {
                            if (entry.Value.Metadata.SourcePath != null && filename.StartsWith(entry.Value.Metadata.SourcePath))
                            {
                                filename = filename[sourcePathLength.Value..];
                            }
                        }
                        Console.WriteLine(Red($"--[ ") + Blue("Match #") + Yellow(index.ToString()) + Blue(" of ") + Yellow(matchCount.ToString()) + Red(" ]--"));
                        Console.WriteLine("   Rule Id: " + Blue(match.Rule.Id));
                        Console.WriteLine("       Tag: " + Blue(match.Tags?.First()));
                        Console.WriteLine("  Severity: " + Cyan(match.Severity.ToString()) + ", Confidence: " + Cyan(match.Confidence.ToString()));
                        Console.WriteLine("  Filename: " + Yellow(filename));
                        Console.WriteLine("   Pattern: " + Green(match.MatchingPattern.Pattern));
                        foreach (string?line in match.Excerpt.Split(new[] { "\r", "\n", "\r\n" }, StringSplitOptions.None))
                        {
                            string?s = line;
                            if (s.Length > 100)
                            {
                                s = s.Substring(0, 100);
                            }
                            Console.WriteLine(Bright.Black("  | ") + Magenta(s));
                        }
                        Console.WriteLine();
                    }