Deathmax

Untitled

Apr 14th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. namespace Patch_libnp
  6. {
  7.     internal class Program
  8.     {
  9.         private static void Main()
  10.         {
  11.             if (!File.Exists("libnp.dll"))
  12.             {
  13.                 Console.WriteLine("libnp.dll not found, exiting...");
  14.                 return;
  15.             }
  16.             Console.WriteLine("Loading libnp.dll....");
  17.             _file = File.ReadAllBytes("libnp.dll");
  18.             Console.WriteLine("Finding patch point...");
  19.             var index =
  20.                 FindPattern(
  21.                     new byte[]
  22.                         {
  23.                             0x55, 0x8b, 0xec, 0x83, 0xec, 0x00, 0x53, 0x56, 0x57, 0x8b, 0x45, 0x00, 0xa3, 0x00, 0x00,
  24.                             0x00, 0x00, 0x6a, 0x00, 0xff, 0x15, 0x00, 0x00, 0x00, 0x00, 0x83, 0xc4, 0x00, 0x5f, 0x5e,
  25.                             0x5b, 0x8b, 0xe5
  26.                         },
  27.                     "xxxxx?xxxxx?x????x?xx????xx?xxxxx");
  28.             if (index == -1)
  29.             {
  30.                 Console.WriteLine("Patch point not found! Exiting...");
  31.                 return;
  32.             }
  33.             _file[index] = 0xc3;
  34.             Console.WriteLine("Backing up libnp.dll as libnp.dll.bak");
  35.             File.Move("libnp.dll", "libnp.dll.bak");
  36.             Console.WriteLine("Writing patches...");
  37.             File.WriteAllBytes("libnp.dll", _file);
  38.             Console.WriteLine("File written.");
  39.             Console.Read();
  40.         }
  41.  
  42.         /// <summary>
  43.         /// Byte array to be used by FindPattern
  44.         /// </summary>
  45.         private static byte[] _file;
  46.         /// <summary>
  47.         /// Finds a specific pattern in a byte array
  48.         /// Example:
  49.         /// FindPattern(
  50.         ///            new byte[]
  51.         ///                {
  52.         ///                    0x55, 0x8b, 0xec, 0x83, 0xec, 0x00, 0x53, 0x56, 0x57, 0x8b, 0x45, 0x00, 0xa3, 0x00, 0x00,
  53.         ///                    0x00, 0x00, 0x6a, 0x00, 0xff, 0x15, 0x00, 0x00, 0x00, 0x00, 0x83, 0xc4, 0x00, 0x5f, 0x5e,
  54.         ///                    0x5b, 0x8b, 0xe5
  55.         ///                },
  56.         ///            "xxxxx?xxxxx?x????x?xx????xx?xxxxx");
  57.         /// </summary>
  58.         /// <param name="btPattern">Byte array containing pattern to search</param>
  59.         /// <param name="strMask">x for exact match, ? for wildcard</param>
  60.         /// <returns>Position of pattern in _file, -1 if pattern not found</returns>
  61.         private static int FindPattern(byte[] btPattern, string strMask)
  62.         {
  63.             try
  64.             {
  65.                 if (strMask.Length != btPattern.Length)
  66.                     return -1;
  67.                 for (int x = 0; x < _file.Length; x++)
  68.                 {
  69.                     if (MaskCheck(x, btPattern, strMask))
  70.                     {
  71.                         return x;
  72.                     }
  73.                 }
  74.                 return -1;
  75.             }
  76.             catch (Exception ex)
  77.             {
  78.                 return -1;
  79.             }
  80.         }
  81.  
  82.         private static bool MaskCheck(int nOffset, byte[] btPattern, string strMask)
  83.         {
  84.             return !btPattern.Where((t, x) => strMask[x] != '?' && ((strMask[x] == 'x') && (t != _file[nOffset + x]))).Any();
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment