Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using Microsoft.Win32.SafeHandles;
  8.  
  9. //
  10. // sigScan C# Implementation - Written by atom0s [aka Wiccaan]
  11. // Class Version: 2.0.0
  12. //
  13. // [ CHANGE LOG ] -------------------------------------------------------------------------
  14. //
  15. // 2.0.0
  16. // - Updated to no longer require unsafe or fixed code.
  17. // - Removed unneeded methods and code.
  18. //
  19. // 1.0.0
  20. // - First version written and release.
  21. //
  22. // [ CREDITS ] ----------------------------------------------------------------------------
  23. //
  24. // sigScan is based on the FindPattern code written by
  25. // dom1n1k and Patrick at GameDeception.net
  26. //
  27. // Full credit to them for the purpose of this code. I, atom0s, simply
  28. // take credit for converting it to C#.
  29. //
  30. // [ USAGE ] ------------------------------------------------------------------------------
  31. //
  32. // Examples:
  33. //
  34. // SigScan _sigScan = new SigScan();
  35. // _sigScan.Process = someProc;
  36. // _sigScan.Address = new IntPtr(0x123456);
  37. // _sigScan.Size = 0x1000;
  38. // IntPtr pAddr = _sigScan.FindPattern(new byte[]{ 0xFF, 0xFF, 0xFF, 0xFF, 0x51, 0x55, 0xFC, 0x11 }, "xxxx?xx?", 12);
  39. //
  40. // SigScan _sigScan = new SigScan(someProc, new IntPtr(0x123456), 0x1000);
  41. // IntPtr pAddr = _sigScan.FindPattern(new byte[]{ 0xFF, 0xFF, 0xFF, 0xFF, 0x51, 0x55, 0xFC, 0x11 }, "xxxx?xx?", 12);
  42. //
  43. // ----------------------------------------------------------------------------------------
  44. namespace SigScan.Classes
  45. {
  46. public class SigScan
  47. {
  48. /// <summary>
  49. /// ReadProcessMemory
  50. ///
  51. /// API import definition for ReadProcessMemory.
  52. /// </summary>
  53. /// <param name="hProcess">Handle to the process we want to read from.</param>
  54. /// <param name="lpBaseAddress">The base address to start reading from.</param>
  55. /// <param name="lpBuffer">The return buffer to write the read data to.</param>
  56. /// <param name="dwSize">The size of data we wish to read.</param>
  57. /// <param name="lpNumberOfBytesRead">The number of bytes successfully read.</param>
  58. /// <returns></returns>
  59. [DllImport("kernel32.dll", SetLastError = true)]
  60. private static extern bool ReadProcessMemory(
  61. IntPtr hProcess,
  62. IntPtr lpBaseAddress,
  63. [Out()] byte[] lpBuffer,
  64. int dwSize,
  65. out int lpNumberOfBytesRead
  66. );
  67.  
  68. /// <summary>
  69. /// m_vDumpedRegion
  70. ///
  71. /// The memory dumped from the external process.
  72. /// </summary>
  73. private byte[] m_vDumpedRegion;
  74.  
  75. /// <summary>
  76. /// m_vProcess
  77. ///
  78. /// The process we want to read the memory of.
  79. /// </summary>
  80. private Process m_vProcess;
  81.  
  82. /// <summary>
  83. /// m_vAddress
  84. ///
  85. /// The starting address we want to begin reading at.
  86. /// </summary>
  87. private IntPtr m_vAddress;
  88.  
  89. /// <summary>
  90. /// m_vSize
  91. ///
  92. /// The number of bytes we wish to read from the process.
  93. /// </summary>
  94. private Int32 m_vSize;
  95.  
  96.  
  97. #region "sigScan Class Construction"
  98. /// <summary>
  99. /// SigScan
  100. ///
  101. /// Main class constructor that uses no params.
  102. /// Simply initializes the class properties and
  103. /// expects the user to set them later.
  104. /// </summary>
  105. public SigScan()
  106. {
  107. this.m_vProcess = null;
  108. this.m_vAddress = IntPtr.Zero;
  109. this.m_vSize = 0;
  110. this.m_vDumpedRegion = null;
  111. }
  112. /// <summary>
  113. /// SigScan
  114. ///
  115. /// Overloaded class constructor that sets the class
  116. /// properties during construction.
  117. /// </summary>
  118. /// <param name="proc">The process to dump the memory from.</param>
  119. /// <param name="addr">The started address to begin the dump.</param>
  120. /// <param name="size">The size of the dump.</param>
  121. public SigScan(Process proc, IntPtr addr, int size)
  122. {
  123. this.m_vProcess = proc;
  124. this.m_vAddress = addr;
  125. this.m_vSize = size;
  126. }
  127. #endregion
  128.  
  129. #region "sigScan Class Private Methods"
  130. /// <summary>
  131. /// DumpMemory
  132. ///
  133. /// Internal memory dump function that uses the set class
  134. /// properties to dump a memory region.
  135. /// </summary>
  136. /// <returns>Boolean based on RPM results and valid properties.</returns>
  137. private bool DumpMemory()
  138. {
  139. try
  140. {
  141. // Checks to ensure we have valid data.
  142. if (this.m_vProcess == null)
  143. return false;
  144. if (this.m_vProcess.HasExited == true)
  145. return false;
  146. if (this.m_vAddress == IntPtr.Zero)
  147. return false;
  148. if (this.m_vSize == 0)
  149. return false;
  150.  
  151. // Create the region space to dump into.
  152. this.m_vDumpedRegion = new byte[this.m_vSize];
  153.  
  154. bool bReturn = false;
  155. int nBytesRead = 0;
  156.  
  157. // Dump the memory.
  158. bReturn = ReadProcessMemory(
  159. this.m_vProcess.Handle, this.m_vAddress, this.m_vDumpedRegion, this.m_vSize, out nBytesRead
  160. );
  161.  
  162. // Validation checks.
  163. if (bReturn == false || nBytesRead != this.m_vSize)
  164. return false;
  165. return true;
  166. }
  167. catch (Exception ex)
  168. {
  169. return false;
  170. }
  171. }
  172.  
  173. /// <summary>
  174. /// MaskCheck
  175. ///
  176. /// Compares the current pattern byte to the current memory dump
  177. /// byte to check for a match. Uses wildcards to skip bytes that
  178. /// are deemed unneeded in the compares.
  179. /// </summary>
  180. /// <param name="nOffset">Offset in the dump to start at.</param>
  181. /// <param name="btPattern">Pattern to scan for.</param>
  182. /// <param name="strMask">Mask to compare against.</param>
  183. /// <returns>Boolean depending on if the pattern was found.</returns>
  184. private bool MaskCheck(int nOffset, byte[] btPattern, string strMask)
  185. {
  186. // Loop the pattern and compare to the mask and dump.
  187. for (int x = 0; x < btPattern.Length; x++)
  188. {
  189. // If the mask char is a wildcard, just continue.
  190. if (strMask[x] == '?')
  191. continue;
  192.  
  193. // If the mask char is not a wildcard, ensure a match is made in the pattern.
  194. if ((strMask[x] == 'x') && (btPattern[x] != this.m_vDumpedRegion[nOffset + x]))
  195. return false;
  196. }
  197.  
  198. // The loop was successful so we found the pattern.
  199. return true;
  200. }
  201. #endregion
  202.  
  203. #region "sigScan Class Public Methods"
  204. /// <summary>
  205. /// FindPattern
  206. ///
  207. /// Attempts to locate the given pattern inside the dumped memory region
  208. /// compared against the given mask. If the pattern is found, the offset
  209. /// is added to the located address and returned to the user.
  210. /// </summary>
  211. /// <param name="btPattern">Byte pattern to look for in the dumped region.</param>
  212. /// <param name="strMask">The mask string to compare against.</param>
  213. /// <param name="nOffset">The offset added to the result address.</param>
  214. /// <returns>IntPtr - zero if not found, address if found.</returns>
  215. public IntPtr FindPattern(byte[] btPattern, string strMask, int nOffset)
  216. {
  217. try
  218. {
  219. // Dump the memory region if we have not dumped it yet.
  220. if (this.m_vDumpedRegion == null || this.m_vDumpedRegion.Length == 0)
  221. {
  222. if (!this.DumpMemory())
  223. return IntPtr.Zero;
  224. }
  225.  
  226. // Ensure the mask and pattern lengths match.
  227. if (strMask.Length != btPattern.Length)
  228. return IntPtr.Zero;
  229.  
  230. // Loop the region and look for the pattern.
  231. for (int x = 0; x < this.m_vDumpedRegion.Length; x++)
  232. {
  233. if (this.MaskCheck(x, btPattern, strMask))
  234. {
  235. // The pattern was found, return it.
  236. return new IntPtr((int)this.m_vAddress + (x + nOffset));
  237. }
  238. }
  239.  
  240. // Pattern was not found.
  241. return IntPtr.Zero;
  242. }
  243. catch (Exception ex)
  244. {
  245. return IntPtr.Zero;
  246. }
  247. }
  248.  
  249. /// <summary>
  250. /// ResetRegion
  251. ///
  252. /// Resets the memory dump array to nothing to allow
  253. /// the class to redump the memory.
  254. /// </summary>
  255. public void ResetRegion()
  256. {
  257. this.m_vDumpedRegion = null;
  258. }
  259. #endregion
  260.  
  261. #region "sigScan Class Properties"
  262. public Process Process
  263. {
  264. get { return this.m_vProcess; }
  265. set { this.m_vProcess = value; }
  266. }
  267. public IntPtr Address
  268. {
  269. get { return this.m_vAddress; }
  270. set { this.m_vAddress = value; }
  271. }
  272. public Int32 Size
  273. {
  274. get { return this.m_vSize; }
  275. set { this.m_vSize = value; }
  276. }
  277. #endregion
  278.  
  279. }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement