-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkillnet.c
218 lines (188 loc) · 5.87 KB
/
killnet.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
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "killnet.h"
WCHAR* getProcessPath(DWORD pid)
{
static WCHAR filename[MAX_PATH] = L"";
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (hProcess == NULL) {
DWORD error = GetLastError();
wprintf(L"[!] Error getting process handle. Error code: %lu\n", error);
return NULL;
}
if (GetModuleFileNameExW(hProcess, NULL, filename, MAX_PATH)) {
wprintf(L"Process ID: %u\n", pid);
wprintf(L"Executable path: %s\n", filename);
}
else {
DWORD error = GetLastError();
wprintf(L"[!] Error getting executable path. Error code: %lu\n", error);
CloseHandle(hProcess);
return NULL;
}
CloseHandle(hProcess);
return filename;
}
void applyFilter(WCHAR* filename, FWPM_FILTER filter, DWORD result, HANDLE hEngine)
{
FWP_BYTE_BLOB* appId = NULL;
result = FwpmGetAppIdFromFileName(filename, &appId);
if (result != ERROR_SUCCESS) {
printf("[!] Error retrieving App ID from filename. Error code: %d\n", result);
free(filename);
FwpmEngineClose(hEngine);
return;
}
FWPM_FILTER_CONDITION condition;
condition.fieldKey = FWPM_CONDITION_ALE_APP_ID;
condition.matchType = FWP_MATCH_EQUAL;
condition.conditionValue.type = FWP_BYTE_BLOB_TYPE;
condition.conditionValue.byteBlob = appId;
filter.filterCondition = &condition;
filter.numFilterConditions = 1;
// Add the filter for IPv4
result = FwpmFilterAdd(hEngine, &filter, NULL, NULL);
if (result != ERROR_SUCCESS) {
printf("[!] Error adding IPv4 filter. Error code: %d\n", result);
free(filename);
FwpmEngineClose(hEngine);
return;
}
// Add the filter for IPv6
filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
filter.filterKey = connectv6;
result = FwpmFilterAdd(hEngine, &filter, NULL, NULL);
if (result != ERROR_SUCCESS) {
printf("[!] Error adding IPv4 filter. Error code: %d\n", result);
free(filename);
FwpmEngineClose(hEngine);
return;
}
// Add the filter for IPv4
filter.layerKey = FWPM_LAYER_ALE_AUTH_RECV_ACCEPT_V4;
filter.filterKey = recv4;
result = FwpmFilterAdd(hEngine, &filter, NULL, NULL);
if (result != ERROR_SUCCESS) {
printf("[!] Error adding IPv4 filter. Error code: %d\n", result);
free(filename);
FwpmEngineClose(hEngine);
return;
}
// Add the filter for IPv6
filter.layerKey = FWPM_LAYER_ALE_AUTH_RECV_ACCEPT_V6;
filter.filterKey = recv6;
result = FwpmFilterAdd(hEngine, &filter, NULL, NULL);
if (result != ERROR_SUCCESS) {
printf("[!] Error adding IPv4 filter. Error code: %d\n", result);
free(filename);
FwpmEngineClose(hEngine);
return;
}
printf("[*] Filter added successfully\n");
// Cleanup
free(filename);
}
int deleteFilter(DWORD result, HANDLE hEngine)
{
result = FwpmFilterDeleteByKey(hEngine, &connectv4);
if (result != ERROR_SUCCESS)
{
printf("Error deleting filters\n");
return -1;
}
result = FwpmFilterDeleteByKey(hEngine, &connectv6);
if (result != ERROR_SUCCESS)
{
printf("Error deleting filters\n");
return -1;
}
result = FwpmFilterDeleteByKey(hEngine, &recv4);
if (result != ERROR_SUCCESS)
{
printf("Error deleting filters\n");
return -1;
}
result = FwpmFilterDeleteByKey(hEngine, &recv6);
if (result != ERROR_SUCCESS)
{
printf("Error deleting filters\n");
return -1;
}
return 0;
}
void prepareFilter(char* argument, int type)
{
HANDLE hEngine = NULL;
DWORD result = FwpmEngineOpen(NULL, RPC_C_AUTHN_DEFAULT, NULL, NULL, &hEngine);
if (result != ERROR_SUCCESS) {
printf("[!] Error opening WFP engine. Error code: %d\n", result);
return;
}
WCHAR filterName[] = L"Killnet block";
FWPM_FILTER filter = { 0 };
filter.displayData.name = filterName;
filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
filter.filterKey = connectv4;
filter.action.type = FWP_ACTION_BLOCK;
// PID
if (type == PID_TYPE) {
DWORD pid = stringToDWORD(argument);
WCHAR* filename = getProcessPath(pid);
applyFilter(filename, filter, result, hEngine);
}
// Filepath
else if (type == FILENAME_TYPE) {
// Get appId from argv[2]
int wcharSize = MultiByteToWideChar(CP_ACP, 0, argument, -1, NULL, 0);
if (wcharSize == 0) {
printf("[!] Error converting filename to WCHAR\n");
FwpmEngineClose(hEngine);
return;
}
WCHAR* filename = (WCHAR*)malloc(wcharSize * sizeof(WCHAR));
if (filename == NULL) {
printf("[!] Error allocating memory for filename\n");
FwpmEngineClose(hEngine);
return;
}
MultiByteToWideChar(CP_ACP, 0, argument, -1, filename, wcharSize);
applyFilter(filename, filter, result, hEngine);
}
else if (type == DELETE_TYPE)
{
deleteFilter(result, hEngine);
printf("[+] Filters deleted!\n");
}
else
{
printf("[!] Unrecognized type\n");
}
FwpmEngineClose(hEngine);
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
printf("[!] Usage examples:\n* killnet.exe -p <PID>\n* killnet.exe -f <Filename path>\n* killnet.exe -delete");
return -1;
}
if (strcmp(argv[1], "-p") == 0)
{
printf("[*] Applying block to PID %s\n", argv[2]);
prepareFilter(argv[2], PID_TYPE);
}
else if (strcmp(argv[1], "-f") == 0)
{
printf("[*] Applying block to file %s\n", argv[2]);
prepareFilter(argv[2], FILENAME_TYPE);
}
else if (strcmp(argv[1], "-delete") == 0)
{
printf("[*] Deleting filters...\n");
prepareFilter(argv[2], DELETE_TYPE);
}
else
{
printf("[!] Invalid argument. Use -p or -f.\n");
return -1;
}
return 0;
}