Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Magic;
  6.  
  7. namespace OFPHook
  8. {
  9.     public class HookData : Hook
  10.     {
  11.         public BlackMagic Memory = new BlackMagic();
  12.  
  13.         public uint OrigAddress { get; set; }
  14.         public uint HookAddress { get; set; }
  15.         public byte[] Origbytes { get; set; }
  16.         public byte[] CodeCave { get; set; }
  17.         public string PatchName { get; set; }
  18.         public bool Applied { get; set; }
  19.         public bool Initialized { get; set; }
  20.  
  21.         /// <summary>
  22.         /// Ctor
  23.         /// </summary>
  24.         /// <param name="processID"></param>
  25.         public HookData(int processID) : base(processID)
  26.         {
  27.             Memory.OpenProcessAndThread(processID);
  28.         }
  29.  
  30.         /// <summary>
  31.         /// Retourne si un hook est actif en mémoire ou non
  32.         /// </summary>
  33.         /// <returns>true / false</returns>
  34.         public virtual bool IsApplied()
  35.         {
  36.             return Applied;
  37.         }
  38.  
  39.         /// <summary>
  40.         /// Retourne si un hook à été initialisé ou non
  41.         /// </summary>
  42.         /// <returns>true / false</returns>
  43.         public virtual bool IsInitialized()
  44.         {
  45.             return Initialized;
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Applique un hook en mémoire
  50.         /// </summary>
  51.         public virtual void Apply()
  52.         {
  53.             if (!Initialized || Applied)
  54.                 return;
  55.  
  56.             //writing hook
  57.             //jmp HookAddress (long jump)
  58.             //E9h HookAddress
  59.             Memory.WriteByte(OrigAddress, 0xE9);
  60.             Memory.WriteUInt(OrigAddress + 1, HookAddress);
  61.  
  62.             Applied = true;
  63.         }
  64.  
  65.         /// <summary>
  66.         /// Retire un hook actif en mémoire
  67.         /// </summary>
  68.         public virtual void Remove()
  69.         {
  70.             if (!Applied)
  71.                 return;
  72.  
  73.             //rewrite originals bytes
  74.             Memory.WriteBytes(OrigAddress, Origbytes);
  75.  
  76.             Applied = false;
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Initialise un hook en mémoire
  81.         /// Sauvegarde les octets qui vont être réécrit (overwriting), écrit le code du hook, ecrit le saut final.
  82.         /// </summary>
  83.         public virtual void Initialize()
  84.         {
  85.             //saving originals bytes
  86.             Origbytes = Memory.ReadBytes(OrigAddress, 5);
  87.  
  88.             //allocating destination
  89.             HookAddress = Memory.AllocateMemory(CodeCave.Length + 0x100);
  90.  
  91.             //writing codecave
  92.             Memory.WriteBytes(HookAddress, CodeCave);
  93.  
  94.             //wrinting the end's detour
  95.             //jmp OrigAddress + 6
  96.             //E9h OrigAddress + 6
  97.             Memory.WriteByte(HookAddress + (uint)(CodeCave.Length), 0xE9);
  98.             Memory.WriteUInt(HookAddress + (uint)(CodeCave.Length) + 1, OrigAddress + 6);
  99.  
  100.             Initialized = true;
  101.         }
  102.  
  103.         /// <summary>
  104.         /// Détruit un hook en mémoire (il devras être réajouté dans la liste et réinitialisé
  105.         /// </summary>
  106.         public virtual void Delete()
  107.         {
  108.             //rewriting original code
  109.             Remove();
  110.  
  111.             //clearing allocated memory
  112.             Memory.FreeMemory(HookAddress);
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement