Advertisement
CorrM

Test WriteProcess

Oct 13th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 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 System.Threading.Tasks;
  8.  
  9. namespace CSharpConsole
  10. {
  11.     class Program
  12.     {
  13.         [DllImport("kernel32.dll")]
  14.         public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  15.  
  16.         [DllImport("kernel32.dll", SetLastError = true)]
  17.         static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesWritten);
  18.  
  19.         [DllImport("kernel32.dll")]
  20.         static extern bool VirtualProtectEx(int hProcess, int lpAddress, int dwSize, uint flNewProtect, ref uint lpflOldProtect);
  21.  
  22.         static IntPtr processHandle;
  23.         const int PROCESS_ALL_ACCESS = 0x1F0FFF;
  24.  
  25.         static void Main(string[] args)
  26.         {
  27.             Process process = Process.GetProcessesByName("ConsoleApplication1")[0];
  28.             processHandle = OpenProcess(PROCESS_ALL_ACCESS, false, process.Id);
  29.  
  30.             int Add = 0x00CFF840;
  31.             Write<string>(Add, "ZZZZZ");
  32.  
  33.             Console.ReadKey();
  34.         }
  35.  
  36.         public static bool Write<T>(int address, T t)
  37.         {
  38.             int bytesWritten = 0;
  39.             uint oldProtect = 0;
  40.             Byte[] Buffer;
  41.             if (typeof(T) == typeof(string))
  42.             {
  43.                 Buffer = System.Text.Encoding.ASCII.GetBytes(t + "\0");
  44.                 //Buffer = System.Text.Encoding.Unicode.GetBytes(t + "\0");
  45.             }
  46.             else
  47.             {
  48.                 Buffer = new Byte[Marshal.SizeOf(typeof(T))];
  49.             }
  50.            
  51.             bool bVProtect = VirtualProtectEx((int)processHandle, 0x00CFF840, Buffer.Length, 0x40, ref oldProtect);
  52.             bool bWProc = WriteProcessMemory((int)processHandle, address, Buffer, Buffer.Length, ref bytesWritten);
  53.  
  54.             return bVProtect && bWProc;
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement