-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinjector.c
executable file
·205 lines (171 loc) · 6.13 KB
/
injector.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*-
* Copyright (c) 2007 Ryan Kwolek
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* injector.c -
* A generic, simple command line DLL injector with an option to eject libraries as well
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#define ARG_WINDOWNAME 1
#define ARG_DLLNAME 2
#define ARG_OPTIONS 3
#define VERSION_MSG "Injector 1.0\n" \
"Copyright (c) 2007 Ryan Kwolek\n" \
"This utility has been released under the Simplified BSD copyright license.\n\n"
#define HELP_MSG "injector [windowname] [pid] [dllname] -fnphs?\n" \
"\thH? - Display this help message and quit.\n" \
"\tvV - Display version message and quit.\n" \
"\tfF - Free the specified dll instead of load it.\n" \
"\tnN - Don't raise the privileges of this utility to SeDebugPrivilege.\n" \
"\tsS - Load specified library from system directory, not the current working directory." \
"\tpP - The PID is explicitly specified in place of the window name.\n\n"
int freedll;
int noelevateprivs;
int sysdir;
////////////////////////////////////////////////////////////////////////////////////////////////
void DispErr(const char *fnname) {
printf("%s failed, error %d\nquitting now.\n", GetLastError());
exit(0);
}
int main(int argc, char *argv[]) {
unsigned long pid, len;
void *remotelib;
char dllname[MAX_PATH];
HANDLE hToken, hProcess, hThread;
HMODULE kernel32lib;
LPTHREAD_START_ROUTINE procaddr;
HWND hwnd;
LUID luid;
TOKEN_PRIVILEGES tp, oldtp;
pid = 0;
if (argc < 2) {
puts("not enough args!");
return 0;
} else if (argc == 2) {
if (*argv[1] == '-') {
switch (argv[1][1]) {
case 'h':
case 'H':
case '?':
puts(HELP_MSG);
return 0;
case 'v':
case 'V':
puts(VERSION_MSG);
return 0;
}
}
} else if (argc >= 4) {
if (*argv[ARG_OPTIONS] == '-') {
char *tmp = argv[ARG_OPTIONS] + 1;
while (*tmp) {
switch (*tmp) {
case 'f': /*free dll*/
case 'F':
freedll = 1;
break;
case 'n': /*don't elevate privileges*/
case 'N':
noelevateprivs = 1;
break;
case 's': /*load from system directory*/
case 'S':
sysdir = 1;
break;
case 'p': /*specify pid to inject*/
case 'P':
pid = atoi(argv[ARG_WINDOWNAME]);
default:
printf("WARNING: unrecognized option %c, "
"ignoring.\n", *tmp);
}
tmp++;
}
}
}
if (!pid) {
hwnd = FindWindow(NULL, argv[ARG_WINDOWNAME]);
if (!hwnd)
DispErr("FindWindow");
printf("found window: 0x%x\n", hwnd);
GetWindowThreadProcessId(hwnd, &pid);
}
printf("process pid: %d\n", pid);
if (!noelevateprivs) {
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
DispErr("OpenProcessToken");
printf("process token opened, handle 0x%x\n", hToken);
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid))
DispErr("LookupPrivilegeValue");
printf("debug luid: %08x:%08x\n", luid.HighPart, luid.LowPart);
len = sizeof(TOKEN_PRIVILEGES);
tp.PrivilegeCount = 1;
tp.Privileges->Luid = luid;
tp.Privileges->Attributes = 0;
if (!AdjustTokenPrivileges(hToken, 0, &tp,
sizeof(TOKEN_PRIVILEGES), &oldtp, &len))
DispErr("AdjustTokenPrivileges");
oldtp.PrivilegeCount = 1;
oldtp.Privileges->Luid = luid;
oldtp.Privileges->Attributes |= SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, 0, &oldtp,
sizeof(TOKEN_PRIVILEGES), NULL, NULL))
DispErr("re AdjustTokenPrivileges");
puts("elevated process privileges sucessfully!");
}
hProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION |
PROCESS_VM_OPERATION | PROCESS_VM_WRITE |
PROCESS_VM_READ, 0, pid);
if (!hProcess)
DispErr("OpenProcess");
if (sysdir) {
strncpy(dllname, argv[ARG_DLLNAME], sizeof(dllname));
len = strlen(dllname) + 1;
} else {
len = GetCurrentDirectory(sizeof(dllname), dllname);
if (!len)
DispErr("GetCurrentDirectory");
*(short *)(dllname + len) = '\\';
len++;
strncpy(dllname + len, argv[ARG_DLLNAME], sizeof(dllname) - len);
len = strlen(dllname) + 1;
}
remotelib = VirtualAllocEx(hProcess, NULL, len, MEM_COMMIT, PAGE_READWRITE);
if (!remotelib)
DispErr("VirtualAllocEx");
WriteProcessMemory(hProcess, remotelib, dllname, len, NULL);
kernel32lib = GetModuleHandle("kernel32");
procaddr = (LPTHREAD_START_ROUTINE)GetProcAddress(kernel32lib,
freedll ? "FreeLibraryA" : "LoadLibraryA")
hThread = CreateRemoteThread(hProcess, NULL, 0,
procaddr, remotelib, 0, NULL);
if (!hThread)
DispErr("CreateRemoteThread");
CloseHandle(hThread);
printf("successfully %sjected %s into %d, thread handle 0x%08x.\n",
freedll ? "de" : "in", dllname, pid, hThread);
return 0;
}