Exemplo n.º 1
0
        private static void Main()
        {
            Console.WriteLine(Green($"green {Bold("bold")} {Red("red")} green"));
            Console.WriteLine("normal");
            Console.WriteLine(Green().Reversed().Text("green"));
            Console.WriteLine(Green().Reversed("green"));
            Console.WriteLine($"{Bright.Green().Text("Bright")} and {Green("normal")} green");

            Console.WriteLine(Green($"The difference {Bold("between bold")}, {Bright.Green("bright green")} and {Dim("dim")}"));

            Console.WriteLine(Green().Bold().Underline().Reversed().Text("hoi!"));

            Console.WriteLine(
                Bold().Green().Text($"starting green {Red("then red")} must be green again"));

            Console.WriteLine(Rgb(55, 115, 155).Text("from rgb!"));
            Console.WriteLine(Black().Background.Rgb(55, 115, 155).Text("from rgb!"));
            Console.WriteLine(Rgb(55, 115, 155).Background.Green().Text("from rgb!"));

            Console.WriteLine(Red().Reversed().Green("green"));

            var rainbow = new Rainbow(0.5);

            for (var i = 0; i < 15; i++)
            {
                Console.WriteLine(rainbow.Next().Bold().Text("rainbow"));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Prints all built-in styles for quick browsing.
        /// </summary>
        public static void PrintDemo()
        {
            NewPara();

            Write("Standard:   ")
            .Red.Write("Red").Default.Write("         ")
            .Green.Write("Green").Default.Write("    ")
            .Blue.Write("Blue").Default.Write("        ")
            .Cyan.Write("Cyan").Default.Write("          ")
            .Yellow.Write("Yellow").Default.Write("        ")
            .Gold.Write("Gold").Default.Write("      ")
            .Muted.Write("Muted").Default.Write("    ")
            .Bright.WriteLine("Bright");

            Write("Messaging:  ")
            .Error.Write("Error").Default.Write("       ")
            .OK.Write("OK").Default.Write("       ")
            .Warning.Write("Warning").Default.Write("     ")
            .Tag.Write("Tag").Default.Write("           ")
            .Note.Write("Note").Default.Write("          ")
            .Tip.Write("Tip").Default.Write("       ")
            .Label.Write("Label").Default.Write("    ")
            .Terminal.WriteLine("Terminal");

            Write("Extended:   ")
            .Graphite.Write("Graphite").Default.Write("    ")
            .Azure.Write("Azure").Default.Write("    ")
            .Eggplant.Write("Eggplant").Default.Write("    ")
            .Strawberry.Write("Strawberry").Default.Write("    ")
            .Watermelon.Write("Watermelon").Default.Write("    ")
            .Squash.Write("Squash").Default.Write("    ")
            .Lime.Write("Lime").Default.Write("     ")
            .Tomato.WriteLine("Tomato");

            NewPara();

            Console.WriteLine("Standard:\tMessaging:\tExtended:");
            Red.Write("Red\t\t").Error.Write("Error\t\t").Graphite.WriteLine("Graphite");
            Green.Write("Green\t\t").OK.Write("OK\t\t").Azure.WriteLine("Azure");
            Blue.Write("Blue\t\t").Warning.Write("Warning\t\t").Eggplant.WriteLine("Eggplant");
            Cyan.Write("Cyan\t\t").Tag.Write("Tag\t\t").Strawberry.WriteLine("Strawberry");
            Yellow.Write("Yellow\t\t").Tip.Write("Tip\t\t").Squash.WriteLine("Squash");
            Gold.Write("Gold\t\t").Note.Write("Note\t\t").Watermelon.WriteLine("Watermelon");
            Muted.Write("Muted\t\t").Label.Write("Label\t\t").Lime.WriteLine("Lime");
            Bright.Write("Bright\t\t").Terminal.Write("Terminal\t").Tomato.WriteLine("Tomato");

            NewPara();
        }
Exemplo n.º 3
0
 /// <summary>
 /// Returns a verbose indented string for debugging
 /// </summary>
 /// <returns>a debugging string</returns>
 public override string ToString()
 {
     return
         ("  ForegroundColor: " + ForegroundColor.ToString() + "\n" +
          "  ForegroundRgb: " + (ForegroundRgb == null ? "<null>" : ForegroundRgb.ToString()) + "\n" +
          "  BackgroundColor: " + BackgroundColor.ToString() + "\n" +
          "  BackgroundRgb: " + (BackgroundRgb == null ? "<null>" : BackgroundRgb.ToString()) + "\n" +
          "  Bright: " + Bright.ToString() + "\n" +
          "  Standout: " + Standout.ToString() + "\n" +
          "  Underscore: " + Underscore.ToString() + "\n" +
          "  Blink: " + Blink.ToString() + "\n" +
          "  Reverse: " + Reverse.ToString() + "\n" +
          "  Hidden: " + Hidden.ToString() + "\n" +
          "  Protected: " + Protected.ToString() + "\n"
         );
 }
Exemplo n.º 4
0
        public IActionResult RemovePost(int postId)
        {
            int?userid = HttpContext.Session.GetInt32("userid");

            if (userid != null)
            {
                Bright removePost = _context.posts.SingleOrDefault(p => p.postid == postId);
                var    allLikers  = _context.likes.Where(l => l.postsid == postId);
                foreach (var each in allLikers)
                {
                    _context.likes.Remove(each);
                }
                _context.posts.Remove(removePost);
                _context.SaveChanges();
                return(RedirectToAction("bright"));
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 5
0
        public IActionResult addPost(BrightViewModel add)
        {
            int?userid = HttpContext.Session.GetInt32("userid");

            if (userid != null)
            {
                if (ModelState.IsValid)
                {
                    Bright newPost = new Bright
                    {
                        post     = add.post,
                        likebyid = (int)userid
                    };
                    _context.Add(newPost);
                    _context.SaveChanges();
                }
                return(RedirectToAction("bright"));
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 6
0
        public override string ToString()
        {
            string channelName;
            string channelNumber;
            string volume;
            string bright;

            if (State)
            {
                channelName   = tvChannelList[Channel];
                channelNumber = Channel.ToString();
                volume        = Volume.ToString();
                bright        = Bright.ToString();
            }
            else
            {
                channelName   = "--";
                channelNumber = "--";
                volume        = "--";
                bright        = "--";
            }
            return(base.ToString() + "<br />" + "Channel: " + channelNumber + " : " + channelName + "<br />" + "Volume: " + volume + "<br />" + "Bright: " + bright);
        }
Exemplo n.º 7
0
 void Instance_PrefetchThumbnailCompleted(Bright.Data.GroupingElement obj)
 {
     try
     {
         if (Core.CurrentOperation != null &&
             elemList.IsVisible(Core.CurrentOperation.Data.IndexOfElement(obj)))
         {
             this.Invoke(new Action(() => this.Refresh()));
         }
     }
     catch (InvalidOperationException) { }
 }
Exemplo n.º 8
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();

            DetectCryptographyTool detectCryptographyTool = new DetectCryptographyTool();

            detectCryptographyTool.ParseOptions(args);

            // select output destination and format
            detectCryptographyTool.SelectOutput((string?)detectCryptographyTool.Options["output-file"] ?? "");
            IOutputBuilder outputBuilder = detectCryptographyTool.SelectFormat((string?)detectCryptographyTool.Options["format"] ?? "text");

            if (detectCryptographyTool.Options["target"] is IList <string> targetList && targetList.Count > 0)
            {
                StringBuilder?sb = new StringBuilder();
                foreach (string?target in targetList)
                {
                    sb.Clear();
                    try
                    {
                        List <IssueRecord>?results = null;
                        if (target.StartsWith("pkg:", StringComparison.InvariantCulture))
                        {
                            PackageURL?purl = new PackageURL(target);
                            results = await(detectCryptographyTool.AnalyzePackage(purl,
                                                                                  (string?)detectCryptographyTool.Options["download-directory"] ?? string.Empty,
                                                                                  (bool?)detectCryptographyTool.Options["use-cache"] == true) ??
                                            Task.FromResult(new List <IssueRecord>()));
                        }
                        else if (System.IO.Directory.Exists(target))
                        {
                            results = await(detectCryptographyTool.AnalyzeDirectory(target) ??
                                            Task.FromResult(new List <IssueRecord>()));
                        }
                        else if (File.Exists(target))
                        {
                            string?targetDirectoryName = null;
                            while (targetDirectoryName == null || System.IO.Directory.Exists(targetDirectoryName))
                            {
                                targetDirectoryName = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                            }

                            string?path = await ArchiveHelper.ExtractArchiveAsync(targetDirectoryName, Path.GetFileName(target), File.OpenRead(target));

                            results = await detectCryptographyTool.AnalyzeDirectory(path);

                            // Clean up after ourselves
                        }
                        else
                        {
                            Logger.Warn($"{target} was neither a Package URL, directory, nor a file.");
                            continue;
                        }

                        if (results == null)
                        {
                            Logger.Warn("No results were generated.");
                            continue;
                        }
                        else
                        {
                            sb.AppendLine("Summary Results:");

                            sb.AppendLine(Blue("Cryptographic Implementations:"));
                            IOrderedEnumerable <string>?implementations = results.SelectMany(r => r.Issue.Rule.Tags ?? Array.Empty <string>())
                                                                          .Distinct()
                                                                          .Where(t => t.StartsWith("Cryptography.Implementation."))
                                                                          .Select(t => t.Replace("Cryptography.Implementation.", ""))
                                                                          .OrderBy(s => s);
                            if (implementations.Any())
                            {
                                foreach (string?tag in implementations)
                                {
                                    sb.AppendLine(Bright.Blue($" * {tag}"));
                                }
                            }
                            else
                            {
                                sb.AppendLine(Bright.Black("  No implementations found."));
                            }

                            sb.AppendLine();
                            sb.AppendLine(Red("Cryptographic Library References:"));
                            IOrderedEnumerable <string>?references = results.SelectMany(r => r.Issue.Rule.Tags ?? Array.Empty <string>())
                                                                     .Distinct()
                                                                     .Where(t => t.StartsWith("Cryptography.Reference."))
                                                                     .Select(t => t.Replace("Cryptography.Reference.", ""))
                                                                     .OrderBy(s => s);

                            if (references.Any())
                            {
                                foreach (string?tag in references)
                                {
                                    sb.AppendLine(Bright.Red($" * {tag}"));
                                }
                            }
                            else
                            {
                                sb.AppendLine(Bright.Black("  No library references found."));
                            }

                            sb.AppendLine();
                            sb.AppendLine(Green("Other Cryptographic Characteristics:"));
                            IOrderedEnumerable <string>?characteristics = results.SelectMany(r => r.Issue.Rule.Tags ?? Array.Empty <string>())
                                                                          .Distinct()
                                                                          .Where(t => t.Contains("Crypto", StringComparison.InvariantCultureIgnoreCase) &&
                                                                                 !t.StartsWith("Cryptography.Implementation.") &&
                                                                                 !t.StartsWith("Cryptography.Reference."))
                                                                          .Select(t => t.Replace("Cryptography.", ""))
                                                                          .OrderBy(s => s);
                            if (characteristics.Any())
                            {
                                foreach (string?tag in characteristics)
                                {
                                    sb.AppendLine(Bright.Green($" * {tag}"));
                                }
                            }
                            else
                            {
                                sb.AppendLine(Bright.Black("  No additional characteristics found."));
                            }

                            if ((bool?)detectCryptographyTool.Options["verbose"] == true)
                            {
                                IOrderedEnumerable <IGrouping <string, IssueRecord> >?items = results.GroupBy(k => k.Issue.Rule.Name).OrderByDescending(k => k.Count());
                                foreach (IGrouping <string, IssueRecord>?item in items)
                                {
                                    sb.AppendLine();
                                    sb.AppendLine($"There were {item.Count()} finding(s) of type [{item.Key}].");

                                    foreach (IssueRecord?result in results)
                                    {
                                        if (result.Issue.Rule.Name == item.Key)
                                        {
                                            sb.AppendLine($" {result.Filename}:");
                                            if (result.Issue.Rule.Id == "_CRYPTO_DENSITY")
                                            {
                                                // No excerpt for cryptographic density
                                                // TODO: We stuffed the density in the unused 'Description' field. This is code smell.
                                                sb.AppendLine($"  | The maximum cryptographic density is {result.Issue.Rule.Description}.");
                                            }
                                            else
                                            {
                                                // Show the excerpt
                                                foreach (string?line in result.TextSample.Split(new char[] { '\n', '\r' }))
                                                {
                                                    if (!string.IsNullOrWhiteSpace(line))
                                                    {
                                                        sb.AppendLine($"  | {line.Trim()}");
                                                    }
                                                }
                                            }
                                            sb.AppendLine();
                                        }
                                    }
                                }
                            }

                            if (Logger.IsDebugEnabled)
                            {
                                foreach (IssueRecord?result in results)
                                {
                                    Logger.Debug($"Result: {result.Filename} {result.Issue.Rule.Name} {result.TextSample}");
                                }
                            }
                        }
                        Console.WriteLine(sb.ToString());
                    }
                    catch (Exception ex)
                    {
                        Logger.Warn(ex, "Error processing {0}: {1}", target, ex.Message);
                        Logger.Warn(ex.StackTrace);
                    }
                }
            }
            else
            {
                Logger.Warn("No target provided; nothing to analyze.");
                DetectCryptographyTool.ShowUsage();
                Environment.Exit(1);
            }
        }
Exemplo n.º 9
0
        private async Task RunAsync(Options options)
        {
            if (options.ShowAllDifferences)
            {
                options.ShowDifferences = true;
            }

            // Validate strategies (before we do any other processing
            IEnumerable <Type>?runSpecificStrategies = null;

            if (options.SpecificStrategies != null)
            {
                IEnumerable <string>?requestedStrategies = options.SpecificStrategies.Split(',').Select(s => s.Trim().ToLowerInvariant()).Distinct();
                runSpecificStrategies = typeof(BaseStrategy).Assembly.GetTypes()
                                        .Where(t => t.IsSubclassOf(typeof(BaseStrategy)))
                                        .Where(t => requestedStrategies.Contains(t.Name.ToLowerInvariant()));
                Logger.Debug("Specific strategies requested: {0}", string.Join(", ", runSpecificStrategies.Select(t => t.Name)));

                if (requestedStrategies.Count() != runSpecificStrategies.Count())
                {
                    Logger.Debug("Invalid strategies.");
                    Console.WriteLine("Invalid strategy, available options are:");
                    IEnumerable <Type>?allStrategies = typeof(BaseStrategy).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(BaseStrategy)));
                    foreach (Type?s in allStrategies)
                    {
                        Console.WriteLine($" * {s.Name}");
                    }
                    Console.WriteLine("Example: oss-reproducible --specific-strategies AutoBuildProducesSamePackage,PackageMatchesSourceStrategy pkg:npm/[email protected]");
                    return;
                }
            }

            // Expand targets
            List <string>?targets = new List <string>();

            foreach (string?target in options.Targets ?? Array.Empty <string>())
            {
                PackageURL?       purl       = new PackageURL(target);
                PackageDownloader?downloader = new PackageDownloader(purl, ProjectManagerFactory, "temp");
                foreach (PackageURL?version in downloader.PackageVersions)
                {
                    targets.Add(version.ToString());
                }
            }
            List <ReproducibleToolResult>?finalResults = new List <ReproducibleToolResult>();

            foreach (string?target in targets)
            {
                try
                {
                    Console.WriteLine($"Analyzing {target}...");
                    Logger.Debug("Processing: {0}", target);

                    PackageURL?purl = new PackageURL(target);
                    if (purl.Version == null)
                    {
                        Logger.Error("Package is missing a version, which is required for this tool.");
                        continue;
                    }

                    string?tempDirectoryName = Path.Join(Path.GetTempPath(), Guid.NewGuid().ToString().Substring(0, 8));
                    FileSystemHelper.RetryDeleteDirectory(tempDirectoryName);
                    // Download the package
                    Console.WriteLine("Downloading package...");
                    PackageDownloader?packageDownloader = new PackageDownloader(purl, ProjectManagerFactory, Path.Join(tempDirectoryName, "package"));
                    List <string>?    downloadResults   = await packageDownloader.DownloadPackageLocalCopy(purl, false, true);

                    if (!downloadResults.Any())
                    {
                        Logger.Debug("Unable to download package.");
                    }

                    // Locate the source
                    Console.WriteLine("Locating source...");
                    FindSourceTool?findSourceTool             = new FindSourceTool(ProjectManagerFactory);
                    Dictionary <PackageURL, double>?sourceMap = await findSourceTool.FindSourceAsync(purl);

                    if (sourceMap.Any())
                    {
                        List <KeyValuePair <PackageURL, double> >?sourceMapList = sourceMap.ToList();
                        sourceMapList.Sort((a, b) => a.Value.CompareTo(b.Value));
                        PackageURL?bestSourcePurl = sourceMapList.Last().Key;
                        if (string.IsNullOrEmpty(bestSourcePurl.Version))
                        {
                            // Tie back the original version to the new PackageURL
                            bestSourcePurl = new PackageURL(bestSourcePurl.Type, bestSourcePurl.Namespace, bestSourcePurl.Name,
                                                            purl.Version, bestSourcePurl.Qualifiers, bestSourcePurl.Subpath);
                        }
                        Logger.Debug("Identified best source code repository: {0}", bestSourcePurl);

                        // Download the source
                        Console.WriteLine("Downloading source...");
                        foreach (string?reference in new[] { bestSourcePurl.Version, options.OverrideSourceReference, "master", "main" })
                        {
                            if (string.IsNullOrWhiteSpace(reference))
                            {
                                continue;
                            }
                            Logger.Debug("Trying to download package, version/reference [{0}].", reference);
                            PackageURL?purlRef = new PackageURL(bestSourcePurl.Type, bestSourcePurl.Namespace, bestSourcePurl.Name, reference, bestSourcePurl.Qualifiers, bestSourcePurl.Subpath);
                            packageDownloader = new PackageDownloader(purlRef, ProjectManagerFactory, Path.Join(tempDirectoryName, "src"));
                            downloadResults   = await packageDownloader.DownloadPackageLocalCopy(purlRef, false, true);

                            if (downloadResults.Any())
                            {
                                break;
                            }
                        }
                        if (!downloadResults.Any())
                        {
                            Logger.Debug("Unable to download source.");
                        }
                    }
                    else
                    {
                        Logger.Debug("Unable to locate source repository.");
                    }

                    // Execute all available strategies
                    StrategyOptions?strategyOptions = new StrategyOptions()
                    {
                        PackageDirectory   = Path.Join(tempDirectoryName, "package"),
                        SourceDirectory    = Path.Join(tempDirectoryName, "src"),
                        PackageUrl         = purl,
                        TemporaryDirectory = Path.GetFullPath(tempDirectoryName),
                        DiffTechnique      = options.DiffTechnique
                    };

                    // First, check to see how many strategies apply
                    IEnumerable <Type>?strategies = runSpecificStrategies;
                    if (strategies == null || !strategies.Any())
                    {
                        strategies = BaseStrategy.GetStrategies(strategyOptions) ?? Array.Empty <Type>();
                    }

                    int numStrategiesApplies = 0;
                    foreach (Type?strategy in strategies)
                    {
                        ConstructorInfo?ctor = strategy.GetConstructor(new Type[] { typeof(StrategyOptions) });
                        if (ctor != null)
                        {
                            BaseStrategy?strategyObject = (BaseStrategy)(ctor.Invoke(new object?[] { strategyOptions }));
                            if (strategyObject.StrategyApplies())
                            {
                                numStrategiesApplies++;
                            }
                        }
                    }

                    Console.Write($"Out of {Yellow(strategies.Count().ToString())} potential strategies, {Yellow(numStrategiesApplies.ToString())} apply. ");
                    if (options.AllStrategies)
                    {
                        Console.WriteLine("Analysis will continue even after a successful strategy is found.");
                    }
                    else
                    {
                        Console.WriteLine("Analysis will stop after the first successful strategy is found.");
                    }
                    List <StrategyResult> strategyResults = new List <StrategyResult>();

                    Console.WriteLine($"\n{Blue("Results: ")}");

                    bool hasSuccessfulStrategy = false;

                    foreach (Type?strategy in strategies)
                    {
                        ConstructorInfo?ctor = strategy.GetConstructor(new Type[] { typeof(StrategyOptions) });
                        if (ctor != null)
                        {
                            // Create a temporary directory, copy the contents from source/package
                            // so that this strategy can modify the contents without affecting other strategies.
                            StrategyOptions?tempStrategyOptions = new StrategyOptions
                            {
                                PackageDirectory   = Path.Join(strategyOptions.TemporaryDirectory, strategy.Name, "package"),
                                SourceDirectory    = Path.Join(strategyOptions.TemporaryDirectory, strategy.Name, "src"),
                                TemporaryDirectory = Path.Join(strategyOptions.TemporaryDirectory, strategy.Name),
                                PackageUrl         = strategyOptions.PackageUrl,
                                IncludeDiffoscope  = options.ShowDifferences
                            };

                            try
                            {
                                OssReproducibleHelpers.DirectoryCopy(strategyOptions.PackageDirectory, tempStrategyOptions.PackageDirectory);
                                OssReproducibleHelpers.DirectoryCopy(strategyOptions.SourceDirectory, tempStrategyOptions.SourceDirectory);
                            }
                            catch (Exception ex)
                            {
                                Logger.Debug(ex, "Error copying directory for strategy. Aborting execution.");
                            }

                            System.IO.Directory.CreateDirectory(tempStrategyOptions.PackageDirectory);
                            System.IO.Directory.CreateDirectory(tempStrategyOptions.SourceDirectory);

                            try
                            {
                                BaseStrategy?  strategyObject = (BaseStrategy)(ctor.Invoke(new object?[] { tempStrategyOptions }));
                                StrategyResult?strategyResult = strategyObject.Execute();

                                if (strategyResult != null)
                                {
                                    strategyResults.Add(strategyResult);
                                }

                                if (strategyResult != null)
                                {
                                    if (strategyResult.IsSuccess)
                                    {
                                        Console.WriteLine($" [{Bold().Yellow("PASS")}] {Yellow(strategy.Name)}");
                                        hasSuccessfulStrategy = true;
                                    }
                                    else
                                    {
                                        Console.WriteLine($" [{Red("FAIL")}] {Red(strategy.Name)}");
                                    }

                                    if (options.ShowDifferences)
                                    {
                                        foreach (StrategyResultMessage?resultMessage in strategyResult.Messages)
                                        {
                                            if (resultMessage.Filename != null && resultMessage.CompareFilename != null)
                                            {
                                                Console.WriteLine($"  {Bright.Black("(")}{Blue("P ")}{Bright.Black(")")} {resultMessage.Filename}");
                                                Console.WriteLine($"  {Bright.Black("(")}{Blue(" S")}{Bright.Black(")")} {resultMessage.CompareFilename}");
                                            }
                                            else if (resultMessage.Filename != null)
                                            {
                                                Console.WriteLine($"  {Bright.Black("(")}{Blue("P+")}{Bright.Black(")")} {resultMessage.Filename}");
                                            }
                                            else if (resultMessage.CompareFilename != null)
                                            {
                                                Console.WriteLine($"  {Bright.Black("(")}{Blue("S+")}{Bright.Black(")")} {resultMessage.CompareFilename}");
                                            }

                                            IEnumerable <DiffPiece>?differences = resultMessage.Differences ?? Array.Empty <DiffPiece>();

                                            int maxShowDifferences = 20;
                                            int numShowDifferences = 0;

                                            foreach (DiffPiece?diff in differences)
                                            {
                                                if (!options.ShowAllDifferences && numShowDifferences > maxShowDifferences)
                                                {
                                                    Console.WriteLine(Background.Blue(Bold().White("NOTE: Additional differences exist but are not shown. Pass --show-all-differences to view them all.")));
                                                    break;
                                                }

                                                switch (diff.Type)
                                                {
                                                case ChangeType.Inserted:
                                                    Console.WriteLine($"{Bright.Black(diff.Position + ")")}\t{Red("+")} {Blue(diff.Text)}");
                                                    ++numShowDifferences;
                                                    break;

                                                case ChangeType.Deleted:
                                                    Console.WriteLine($"\t{Green("-")} {Green(diff.Text)}");
                                                    ++numShowDifferences;
                                                    break;

                                                default:
                                                    break;
                                                }
                                            }
                                            if (numShowDifferences > 0)
                                            {
                                                Console.WriteLine();
                                            }
                                        }

                                        string?diffoscopeFile = Guid.NewGuid().ToString() + ".html";
                                        File.WriteAllText(diffoscopeFile, strategyResult.Diffoscope);
                                        Console.WriteLine($"  Diffoscope results written to {diffoscopeFile}.");
                                    }
                                }
                                else
                                {
                                    Console.WriteLine(Green($"  [-] {strategy.Name}"));
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.Warn(ex, "Error processing {0}: {1}", strategy, ex.Message);
                                Logger.Debug(ex.StackTrace);
                            }
                        }

                        if (hasSuccessfulStrategy && !options.AllStrategies)
                        {
                            break;  // We don't need to continue
                        }
                    }

                    ReproducibleToolResult?reproducibilityToolResult = new ReproducibleToolResult
                    {
                        PackageUrl = purl.ToString(),
                        Results    = strategyResults
                    };

                    finalResults.Add(reproducibilityToolResult);

                    (double score, string scoreText) = GetReproducibilityScore(reproducibilityToolResult);
                    Console.WriteLine($"\n{Blue("Summary:")}");
                    string?scoreDisplay = $"{(score * 100.0):0.#}";
                    if (reproducibilityToolResult.IsReproducible)
                    {
                        Console.WriteLine($"  [{Yellow(scoreDisplay + "%")}] {Yellow(scoreText)}");
                    }
                    else
                    {
                        Console.WriteLine($"  [{Red(scoreDisplay + "%")}] {Red(scoreText)}");
                    }

                    if (options.LeaveIntermediateFiles)
                    {
                        Console.WriteLine();
                        Console.WriteLine($"Intermediate files are located in [{tempDirectoryName}].");
                    }
                    else
                    {
                        FileSystemHelper.RetryDeleteDirectory(tempDirectoryName);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex, "Error processing {0}: {1}", target, ex.Message);
                    Logger.Debug(ex.StackTrace);
                }
            }

            if (finalResults.Any())
            {
                // Write the output somewhere
                string?jsonResults = Newtonsoft.Json.JsonConvert.SerializeObject(finalResults, Newtonsoft.Json.Formatting.Indented);
                if (!string.IsNullOrWhiteSpace(options.OutputFile) && !string.Equals(options.OutputFile, "-", StringComparison.InvariantCultureIgnoreCase))
                {
                    try
                    {
                        File.WriteAllText(options.OutputFile, jsonResults);
                        Console.WriteLine($"Detailed results are available in {options.OutputFile}.");
                    }
                    catch (Exception ex)
                    {
                        Logger.Warn(ex, "Unable to write to {0}. Writing to console instead.", options.OutputFile);
                        Console.WriteLine(jsonResults);
                    }
                }
                else if (string.Equals(options.OutputFile, "-", StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine(jsonResults);
                }
            }
            else
            {
                Logger.Debug("No results were produced.");
            }
        }
Exemplo n.º 10
0
 void Instance_PrefetchCompleted(Bright.Data.GroupingElement obj)
 {
     if (obj == curElem)
         this.Invoke(new Action(Refresh));
 }
Exemplo n.º 11
0
 public static string ToYesNo(this bool thisValue)
 {
     return(thisValue
                                         ? Bright.Green("Yes")
                                         : Bright.Red("No"));
 }
Exemplo n.º 12
0
        private bool DoMoveCopy(string source, ref string dest, Bright.Data.GroupingElement.DistributionModes distMode)
        {
            if (source == dest)
                return false;
            if (File.Exists(dest))
            {
                DialogResult dr = AllTrap;
                if (dr == DialogResult.None)
                {
                    using (var dp = new Duplication(source, dest))
                    {
                        dr = dp.ShowDialog();
                        if (dp.AllTrap)
                            AllTrap = dr;
                    }
                }
                switch (dr)
                {
                    case DialogResult.Cancel:
                        return false;
                    case DialogResult.Abort:
                        Finished = true;
                        return false;
                    case DialogResult.OK:
                        FileSystem.DeleteFile(dest, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
                        break;
                    case DialogResult.Retry:
                        string destn = Path.Combine(Path.GetDirectoryName(dest), Path.GetFileNameWithoutExtension(dest));
                        string ext = Path.GetExtension(dest);
                        int ctor = 1;
                        while (File.Exists(destn + "(" + ctor + ")" + ext))
                            ctor++;
                        dest = destn + "(" + ctor + ")" + ext;
                        break;
                    default:
                        throw new Exception("Duplicate processing error.");
                }
            }
            try
            {
                if (distMode == Bright.Data.GroupingElement.DistributionModes.Copy)
                {
                    File.Copy(source, dest);
                }
                else if (distMode == Bright.Data.GroupingElement.DistributionModes.Move)
                {
                    File.Move(source, dest);
                }
                else if (distMode == Bright.Data.GroupingElement.DistributionModes.Link)
                {
                    using (var shortcut = new ShellLink())
                    {
                        shortcut.Description = "Image file link, created by typict";
                        shortcut.TargetPath = source;
                        shortcut.ShowCommand = SW.NORMAL;

                        shortcut.Save(dest + ".lnk");
                    }
                }
                else
                {
                    throw new ArgumentException("distribution mode is invalid: value " + ((int)distMode).ToString());
                }
            }
            catch (Exception e)
            {
                if (OnHandledException != null)
                    OnHandledException.Invoke(e);
                return false;
            }
            return true;
        }
Exemplo n.º 13
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();
                    }