Ejemplo n.º 1
0
        public Benchmark()
        {
            // get dump
            string memoryDumpPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\memorydump.dat";

            if (!File.Exists(memoryDumpPath))
            {
                memoryDumpPath = Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "..", "..", ".."), "Memorydump") + @"\memorydump.dat";
                if (!File.Exists(memoryDumpPath))
                {
                    throw new FileNotFoundException("Memory dump not found: " + memoryDumpPath);
                }
            }

            // read bytes and check hash
            byte[] moduleMemory = File.ReadAllBytes(memoryDumpPath);
            using (SHA1Managed sha1Managed = new SHA1Managed())
            {
                byte[] hash = sha1Managed.ComputeHash(moduleMemory);
                string sha1 = string.Concat(hash.Select(b => b.ToString("x2")));
                if (!sha1.Equals(TARGET_HASH, StringComparison.OrdinalIgnoreCase))
                {
                    throw new BadImageFormatException("Memory dump corrupted");
                }
            }
            this.CbMemory = moduleMemory;

            // generate byte patterns
            this.MemoryPatterns = new List <MemoryPattern>();
            foreach (KeyValuePair <long, string> entry in TARGET_PATTERNS)
            {
                MemoryPattern memoryPattern = new MemoryPattern(entry.Key, entry.Value);
                this.MemoryPatterns.Add(memoryPattern);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Compares C# patterscan implementations.
        /// Uses a full memory dump from blender 2.64a 64bit as a target.
        /// </summary>
        /// <remarks>https://download.blender.org/release/Blender2.64/blender-2.64a-release-windows64.zip</remarks>
        internal static void Init(string[] args)
        {
            Console.Title           = "Patternscan Benchmark";
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine(@"    ____        __  __                ");
            Console.WriteLine(@"   / __ \____ _/ /_/ /____  _________ ");
            Console.WriteLine(@"  / /_/ / __ `/ __/ __/ _ \/ ___/ __ \");
            Console.WriteLine(@" / ____/ /_/ / /_/ /_/  __/ /  / / / /");
            Console.WriteLine(@"/_/    \__,_/\__/\__/\___/_/  /_/ /_/ ");
            Console.WriteLine(@"                                      ");
            Console.WriteLine("            scan benchmark");
            Console.WriteLine("            - C# version -");
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("To start press ENTER...");
            Console.ReadLine();
            Console.WriteLine("");

            if (IntPtr.Size != 8)
            {
                throw new PlatformNotSupportedException("Supports x64 only");
            }

            // get dump
            string memoryDumpPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\memorydump.dat";

            if (!File.Exists(memoryDumpPath))
            {
                memoryDumpPath = Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "..", "..", ".."), "Memorydump") + @"\memorydump.dat";
                if (!File.Exists(memoryDumpPath))
                {
                    throw new FileNotFoundException("Memory dump not found");
                }
            }

            // read bytes and check hash
            byte[] moduleMemory = File.ReadAllBytes(memoryDumpPath);
            using (SHA1Managed sha1Managed = new SHA1Managed())
            {
                byte[] hash = sha1Managed.ComputeHash(moduleMemory);
                string sha1 = string.Concat(hash.Select(b => b.ToString("x2")));
                if (!sha1.Equals(TARGET_HASH, StringComparison.OrdinalIgnoreCase))
                {
                    throw new BadImageFormatException("Memory dump corrupted");
                }
            }

            // generate byte patterns and masks from string patterns
            List <MemoryPattern> memoryPatterns = new List <MemoryPattern>();

            foreach (KeyValuePair <long, string> entry in TARGET_PATTERNS)
            {
                MemoryPattern memoryPattern = new MemoryPattern(entry.Key, entry.Value);
                memoryPatterns.Add(memoryPattern);
            }

            // bench all algorithms
            Stopwatch stopWatch = new Stopwatch();

            foreach (KeyValuePair <string, PatternScanAlgorithm> patternScanAlgorithm in PATTERN_SCAN_ALGORITHMS)
            {
                PrintInfo(patternScanAlgorithm.Key + " - by " + patternScanAlgorithm.Value.Creator);
                bool algoSuccess = true;
                stopWatch.Restart();
                string message = patternScanAlgorithm.Value.Init(in moduleMemory);
                foreach (MemoryPattern memoryPattern in memoryPatterns)
                {
                    if (patternScanAlgorithm.Value.FindPattern(in moduleMemory, in memoryPattern.CbPattern, memoryPattern.SzMask) == memoryPattern.ExpectedAddress)
                    {
                        continue;
                    }
                    algoSuccess = false;
                    break;
                }
                stopWatch.Stop();
                if (!algoSuccess)
                {
                    PrintError("failed..." + (message != "" ? " (" + message + ")" : ""), 1);
                }
                else
                {
                    PrintResult(stopWatch.ElapsedMilliseconds + "ms" + (message != "" ? " (" + message + ")" : ""), 1);
                }
            }

            Console.WriteLine("");
            Console.WriteLine("finished...");
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Compares C# patterscan implementations.
        /// Uses a memory dump from main module of blender 2.64a 64bit as a target.
        /// </summary>
        /// <remarks>https://download.blender.org/release/Blender2.64/blender-2.64a-release-windows64.zip</remarks>
        internal static void Main(string[] args)
        {
            Console.Title           = "Patternscan Benchmark";
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine(@"    ____        __  __                ");
            Console.WriteLine(@"   / __ \____ _/ /_/ /____  _________ ");
            Console.WriteLine(@"  / /_/ / __ `/ __/ __/ _ \/ ___/ __ \");
            Console.WriteLine(@" / ____/ /_/ / /_/ /_/  __/ /  / / / /");
            Console.WriteLine(@"/_/    \__,_/\__/\__/\___/_/  /_/ /_/ ");
            Console.WriteLine(@"                                      ");
            Console.WriteLine("            scan benchmark");
            Console.WriteLine("            - C# version -");
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Gray;

            PrintInfo(string.Format("{0} iterations | {1} patterns | {2} implementations", ITERATIONS, TARGET_PATTERNS.Count, PATTERN_SCAN_ALGORITHMS.Count));
            Console.Write("To start press ENTER...");
            Console.ReadLine();
            Console.WriteLine("");

            if (IntPtr.Size != 8)
            {
                throw new PlatformNotSupportedException("Supports x64 only");
            }

            // get dump
            string memoryDumpPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\memorydump.dat";

            if (!File.Exists(memoryDumpPath))
            {
                memoryDumpPath = Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "..", "..", ".."), "Memorydump") + @"\memorydump.dat";
                if (!File.Exists(memoryDumpPath))
                {
                    throw new FileNotFoundException("Memory dump not found");
                }
            }

            // read bytes and check hash
            byte[] moduleMemory = File.ReadAllBytes(memoryDumpPath);
            using (SHA1Managed sha1Managed = new SHA1Managed())
            {
                byte[] hash = sha1Managed.ComputeHash(moduleMemory);
                string sha1 = string.Concat(hash.Select(b => b.ToString("x2")));
                if (!sha1.Equals(TARGET_HASH, StringComparison.OrdinalIgnoreCase))
                {
                    throw new BadImageFormatException("Memory dump corrupted");
                }
            }
            GC.KeepAlive(moduleMemory);

            // generate byte patterns and masks from string patterns
            List <MemoryPattern> memoryPatterns = new List <MemoryPattern>();

            foreach (KeyValuePair <long, string> entry in TARGET_PATTERNS)
            {
                MemoryPattern memoryPattern = new MemoryPattern(entry.Key, entry.Value);
                memoryPatterns.Add(memoryPattern);
            }
            GC.KeepAlive(memoryPatterns);

            GC.KeepAlive(PATTERN_SCAN_ALGORITHMS);

            // bench all algorithms
            foreach (KeyValuePair <string, PatternScanAlgorithm> patternScanAlgorithm in PATTERN_SCAN_ALGORITHMS)
            {
                PrintInfo(patternScanAlgorithm.Key + " - by " + patternScanAlgorithm.Value.Creator);
                Task <BenchmarkResult> benchMark = RunBenchmark(patternScanAlgorithm, memoryPatterns, moduleMemory);
                if (!benchMark.Wait(25000))
                {
                    PrintError("timeout...");
                }
                else if (!benchMark.Result.success)
                {
                    PrintError("failed..." + (benchMark.Result.message != "" ? " (" + benchMark.Result.message + ")" : ""));
                }
                else
                {
                    PrintResults(benchMark.Result.timings, benchMark.Result.message);
                }
            }

            Console.WriteLine("");
            Console.WriteLine("finished...");
            Console.ReadLine();
        }