Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- namespace XynivilusAPI
- {
- public class Xynivilus
- {
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- private static extern IntPtr CreateRemoteThread(
- IntPtr hProcess,
- IntPtr lpThreadAttributes,
- uint dwStackSize,
- IntPtr lpStartAddress,
- IntPtr lpParameter,
- uint dwCreationFlags,
- IntPtr lpThreadId
- );
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern bool VirtualProtectEx(
- IntPtr hProcess,
- IntPtr lpAddress,
- IntPtr dwSize,
- uint flNewProtect,
- out uint lpflOldProtect
- );
- [DllImport("kernel32.dll")]
- private static extern IntPtr OpenProcess(
- uint dwDesiredAccess,
- bool bInheritHandle,
- int dwProcessId
- );
- [DllImport("kernel32.dll")]
- private static extern bool ReadProcessMemory(
- IntPtr hProcess,
- IntPtr lpBaseAddress,
- [Out] byte[] lpBuffer,
- int dwSize,
- out IntPtr lpNumberOfBytesRead
- );
- [DllImport("kernel32.dll")]
- private static extern bool WriteProcessMemory(
- IntPtr hProcess,
- IntPtr lpBaseAddress,
- byte[] lpBuffer,
- int nSize,
- out IntPtr lpNumberOfBytesWritten
- );
- private static byte[] Shellcode = new byte[] {
- // Insert your shellcode here
- };
- public static void Inject(int pid)
- {
- try
- {
- var processHandle = OpenProcess(0x001F0FFF, false, pid);
- IntPtr address = VirtualAllocEx(
- processHandle,
- IntPtr.Zero,
- Shellcode.Length,
- 0x1000,
- 0x40
- );
- IntPtr bytesWritten = IntPtr.Zero;
- bool result = WriteProcessMemory(
- processHandle,
- address,
- Shellcode,
- Shellcode.Length,
- out bytesWritten
- );
- IntPtr threadId = IntPtr.Zero;
- IntPtr hThread = CreateRemoteThread(
- processHandle,
- IntPtr.Zero,
- 0,
- address,
- IntPtr.Zero,
- 0,
- threadId
- );
- CloseHandle(hThread);
- VirtualFreeEx(processHandle, address, 0, 0x8000);
- CloseHandle(processHandle);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- private static IntPtr VirtualAllocEx(
- IntPtr hProcess,
- IntPtr lpAddress,
- int dwSize,
- uint flAllocationType,
- uint flProtect
- )
- {
- IntPtr result = VirtualAllocEx(
- hProcess,
- lpAddress,
- (IntPtr)dwSize,
- flAllocationType,
- flProtect
- );
- if (result == IntPtr.Zero)
- {
- throw new Exception("Error allocating memory");
- }
- return result;
- }
- [DllImport("kernel32.dll")]
- private static extern bool VirtualFreeEx(
- IntPtr hProcess,
- IntPtr lpAddress,
- int dwSize,
- uint dwFreeType
- );
- [DllImport("kernel32.dll")]
- private static extern bool CloseHandle(IntPtr hObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement