Exemple #1
0
        public Program(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("] Peach 3 -- Minset");
            Console.WriteLine("] Copyright (c) Deja vu Security\n");

            string samples    = null;
            string traces     = null;
            bool   kill       = false;
            string executable = null;
            string arguments  = "";
            string minset     = null;

            var p = new OptionSet()
            {
                { "h|?|help", v => Syntax() },
                { "k", v => kill = true },
                { "s|samples=", v => samples = v },
                { "t|traces=", v => traces = v },
                { "m|minset=", v => minset = v }
            };

            List <string> extra = p.Parse(args);

            if (extra.Count == 0 && samples == null && traces == null && minset == null)
            {
                Syntax();
            }

            if (extra.Count != 0 && samples == null && traces == null)
            {
                Syntax();
            }

            // Build command line
            if (extra.Count > 0)
            {
                foreach (string e in extra)
                {
                    if (executable == null)
                    {
                        executable = e;
                    }
                    else
                    {
                        arguments += e + " ";
                    }
                }

                if (arguments.IndexOf("%s") == -1)
                {
                    Console.WriteLine("Error, command missing '%s'.\n");
                    return;
                }
            }

            // Ensure peach platform assemblies are loaded
            Platform.LoadAssembly();

            var ms = new Minset();

            ms.TraceCompleted += new TraceCompletedEventHandler(ms_TraceCompleted);
            ms.TraceStarting  += new TraceStartingEventHandler(ms_TraceStarting);
            var both = false;

            if (extra.Count > 0 && minset != null && traces != null && samples != null)
            {
                both = true;
                Console.WriteLine("[*] Running both trace and coverage analysis");
            }

            if (both || (executable != null && minset == null))
            {
                var sampleFiles = GetFiles(samples);

                Console.WriteLine("[*] Running trace analysis on " + sampleFiles.Length + " samples...");
                ms.RunTraces(executable, arguments, traces, sampleFiles, kill);

                Console.WriteLine("\n[*] Finished");
            }

            if (both || (extra.Count == 0 && minset != null && traces != null && samples != null))
            {
                Console.WriteLine("[*] Running coverage analysis...");
                var sampleFiles = GetFiles(samples);
                var minsetFiles = ms.RunCoverage(sampleFiles, GetFiles(traces));

                Console.WriteLine("[-]   " + minsetFiles.Length + " files were selected from a total of " + sampleFiles.Count() + ".");
                Console.WriteLine("[*] Copying over selected files...");

                if (!Directory.Exists(minset))
                {
                    Directory.CreateDirectory(minset);
                }

                foreach (string fileName in minsetFiles)
                {
                    Console.WriteLine("[-]   " + Path.Combine(samples, Path.GetFileName(fileName)) + " -> " + Path.Combine(minset, Path.GetFileName(fileName)));
                    File.Copy(Path.Combine(samples, Path.GetFileName(fileName)), Path.Combine(minset, Path.GetFileName(fileName)), true);
                }

                Console.WriteLine("\n[*] Finished");
            }
        }
Exemple #2
0
 void ms_TraceCompleted(Minset sender, string fileName, int count, int totalCount)
 {
     Console.WriteLine("done.");
 }
Exemple #3
0
 void ms_TraceStarting(Minset sender, string fileName, int count, int totalCount)
 {
     Console.Write("[{0}:{1}]   Converage trace of {2}...",
                   count, totalCount, fileName);
 }
Exemple #4
0
        public Program(string[] args)
        {
            Console.WriteLine();
            Console.WriteLine("] Peach 3 -- Minset");
            Console.WriteLine("] Copyright (c) Deja vu Security\n");

            int    verbose    = 0;
            string samples    = null;
            string traces     = null;
            bool   kill       = false;
            string executable = null;
            string arguments  = null;
            string minset     = null;

            var p = new OptionSet()
            {
                { "h|?|help", v => Syntax() },
                { "k", v => kill = true },
                { "v", v => verbose = 1 },
                { "s|samples=", v => samples = v },
                { "t|traces=", v => traces = v },
                { "m|minset=", v => minset = v }
            };

            var extra = p.Parse(args);

            executable = extra.FirstOrDefault();
            arguments  = string.Join(" ", extra.Skip(1));

            if (args.Length == 0)
            {
                throw new SyntaxException();
            }

            if (samples == null)
            {
                throw new SyntaxException("Error, 'samples' argument is required.");
            }

            if (traces == null)
            {
                throw new SyntaxException("Error, 'traces' argument is required.");
            }

            if (minset == null && executable == null)
            {
                throw new SyntaxException("Error, 'minset' or command argument is required.");
            }

            if (executable != null && arguments.IndexOf("%s") == -1)
            {
                throw new SyntaxException("Error, command argument missing '%s'.");
            }

            Peach.Core.Runtime.Program.ConfigureLogging(verbose);

            var sampleFiles = GetFiles(samples, "sample");

            // If we are generating traces, ensure we can write to the traces folder
            if (executable != null)
            {
                VerifyDirectory(traces);
            }

            // If we are generating minset, ensure we can write to the minset folder
            if (minset != null)
            {
                VerifyDirectory(minset);
            }

            var ms = new Minset();

            sw.Start();

            if (verbose == 0)
            {
                ms.TraceCompleted += new TraceEventHandler(ms_TraceCompleted);
                ms.TraceStarting  += new TraceEventHandler(ms_TraceStarting);
                ms.TraceFailed    += new TraceEventHandler(ms_TraceFailed);
            }

            if (minset != null && executable != null)
            {
                Console.WriteLine("[*] Running both trace and coverage analysis\n");
            }

            if (executable != null)
            {
                Console.WriteLine("[*] Running trace analysis on " + sampleFiles.Length + " samples...");

                ms.RunTraces(executable, arguments, traces, sampleFiles, kill);

                Console.WriteLine("\n[{0}] Finished\n", sw.Elapsed);
            }

            if (minset != null)
            {
                var traceFiles = GetFiles(traces, "trace");

                Console.WriteLine("[*] Running coverage analysis...");

                ValidateTraces(ref sampleFiles, ref traceFiles);

                var minsetFiles = ms.RunCoverage(sampleFiles, traceFiles);

                Console.WriteLine("[-]   {0} files were selected from a total of {1}.", minsetFiles.Length, sampleFiles.Length);

                if (minsetFiles.Length > 0)
                {
                    Console.WriteLine("[*] Copying over selected files...");
                }

                foreach (var src in minsetFiles)
                {
                    var file = Path.GetFileName(src);
                    var dst  = Path.Combine(minset, file);

                    Console.Write("[-]   {0} -> {1}", src, dst);

                    try
                    {
                        File.Copy(src, dst, true);
                        Console.WriteLine();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(" failed: {0}", ex.Message);
                    }
                }

                Console.WriteLine("\n[{0}] Finished\n", sw.Elapsed);
            }
        }
Exemple #5
0
 void ms_TraceFailed(Minset sender, string fileName, int count, int totalCount)
 {
     Console.WriteLine(" Failed");
 }
Exemple #6
0
 void ms_TraceStarting(Minset sender, string fileName, int count, int totalCount)
 {
     Console.Write("[{0}] ({1}:{2}) Converage trace of {3}...",
                   sw.Elapsed, count, totalCount, fileName);
 }