-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentSmith.cpp
More file actions
381 lines (308 loc) · 12.9 KB
/
AgentSmith.cpp
File metadata and controls
381 lines (308 loc) · 12.9 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// AgentSmith.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <filesystem>
#include <conio.h>
#include <fwpmtypes.h>
#include <fwpmu.h>
#include <initguid.h>
#include <chrono>
using namespace std::chrono_literals;
#include <thread>
#pragma comment(lib, "Fwpuclnt.lib")
#include <colorconsole.hpp> // https://github.com/aafulei/color-console
#include <cpr/cpr.h>
char getKeyPress()
{
if (_kbhit())
{
return _getch();
}
return '\0';
}
class Isolator final
{
public:
Isolator()
{
}
void Free()
{
if (!_isolated)
return;
std::cout << dye::yellow("#########################################") << std::endl;
std::cout << dye::yellow("Free endpoint so everyone may communicate") << std::endl;
std::cout << dye::yellow("#########################################") << std::endl;
_engine.Close();
_isolated = false;
}
void Isolate(const std::vector<std::filesystem::path>& trustedApps)
{
if (!_isolated && trustedApps.size() == 0)
return;
if (_isolated && trustedApps.size() == 0)
{
Free();
return;
}
if (_isolated)
{
_engine.Close();
_isolated = false;
}
Install();
std::cout << dye::yellow("###########################################") << std::endl;
std::cout << dye::yellow("Isolate endpoint so only these trusted apps may communicate:") << std::endl;
// https://learn.microsoft.com/en-us/windows/win32/fwp/filter-arbitration
std::vector<FWPM_FILTER_CONDITION0> conds;
std::vector<FWP_BYTE_BLOB*> appBlobs;
for (const auto& ta : trustedApps)
{
std::cout << hue::yellow << ta << hue::reset << std::endl;
FWPM_FILTER_CONDITION0 cond;
// appPath must be a fully-qualified file name, and the file must exist on the local machine.
FWP_BYTE_BLOB* appBlob = nullptr;
DWORD result = ::FwpmGetAppIdFromFileName0(ta.c_str(), &appBlob);
cond.fieldKey = FWPM_CONDITION_ALE_APP_ID;
cond.matchType = FWP_MATCH_EQUAL;
cond.conditionValue.type = FWP_BYTE_BLOB_TYPE;
cond.conditionValue.byteBlob = appBlob;
conds.push_back(cond);
appBlobs.push_back(appBlob);
}
std::cout << dye::yellow("###########################################") << std::endl;
// Permit filter for all trusted apps
FWPM_FILTER0 filter {};
filter.displayData.name = const_cast<wchar_t*>(L"AgentSmith isolation <permit> filter");
filter.flags = FWPM_FILTER_FLAG_CLEAR_ACTION_RIGHT; // hard permit
filter.providerKey = Provider::Key;
filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
filter.subLayerKey = __uuidof(SubLayer);
filter.weight.type = FWP_UINT8;
filter.weight.uint8 = 15; // should be highest in our sublayer
filter.numFilterConditions = (UINT32)conds.size();
filter.filterCondition = &conds[0];
filter.action.type = FWP_ACTION_PERMIT;
try
{
DWORD result = ::FwpmTransactionBegin0(_engine, 0);
ThrowIfFailed("FwpmTransactionBegin0", result);
result = ::FwpmFilterAdd0(_engine, &filter, nullptr, nullptr);
ThrowIfFailed("FwpmFilterAdd0", result);
// same filter for IPv6
filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
result = ::FwpmFilterAdd0(_engine, &filter, nullptr, nullptr);
ThrowIfFailed("FwpmFilterAdd0", result);
// Block ALL filter (hard block)
filter.displayData.name = const_cast<wchar_t*>(L"AgentSmith isolation <block> filter");
filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V4;
filter.weight.uint8 = 0; // lower prio than the above permit filter
filter.numFilterConditions = 0;
filter.filterCondition = nullptr;
filter.action.type = FWP_ACTION_BLOCK;
// TODO Should we only block TCP?
result = ::FwpmFilterAdd0(_engine, &filter, nullptr, nullptr);
ThrowIfFailed("FwpmFilterAdd0", result);
// same filter for IPv6
filter.layerKey = FWPM_LAYER_ALE_AUTH_CONNECT_V6;
result = ::FwpmFilterAdd0(_engine, &filter, nullptr, nullptr);
ThrowIfFailed("FwpmFilterAdd0", result);
// TODO block all ingress? => FWPM_LAYER_ALE_AUTH_LISTEN_V4/FWPM_LAYER_ALE_AUTH_RECV_ACCEPT_V4
// https://learn.microsoft.com/en-us/windows/win32/fwp/tcp-packet-flows
// TODO How about local comm? Allow loopback?
result = ::FwpmTransactionCommit0(_engine);
ThrowIfFailed("FwpmTransactionCommit0", result);
_isolated = true;
}
catch (std::exception& ex)
{
::FwpmTransactionAbort0(_engine);
std::cout << dye::red("###########################################") << std::endl;
std::cout << hue::red << ex.what() << hue::reset << std::endl;
std::cout << dye::red("###########################################") << std::endl;
}
// free appBlobs
for (const auto& appBlob : appBlobs)
{
::FwpmFreeMemory0((void**)&appBlob);
}
}
private:
bool _installed = false;
bool _isolated = false;
class Engine final
{
public:
static constexpr const wchar_t* const SessionName = L"AgentSmith Isolation Session";
void Open()
{
if (_handle)
return;
FWPM_SESSION0 session {};
// The session name isn't required but may be useful for diagnostics.
session.displayData.name = const_cast<wchar_t*>(SessionName);
// Set an infinite wait timeout, so we don't have to handle FWP_E_TIMEOUT
// errors while waiting to acquire the transaction lock.
session.txnWaitTimeoutInMSec = INFINITE;
// When this flag is set, any objects added during the session are automatically deleted when the session
// ends.
session.flags = FWPM_SESSION_FLAG_DYNAMIC;
// The authentication service should always be RPC_C_AUTHN_DEFAULT.
DWORD result = ::FwpmEngineOpen0(NULL, RPC_C_AUTHN_DEFAULT, NULL, &session, &_handle);
ThrowIfFailed("FwpmEngineOpen0", result);
}
void Close()
{
if (!_handle)
return;
::FwpmEngineClose0(_handle);
_handle = nullptr;
}
~Engine()
{
Close();
}
operator HANDLE()
{
return _handle;
}
private:
HANDLE _handle = nullptr;
};
Engine _engine;
struct __declspec(uuid("{DE6F528A-8578-4B0E-A127-EB1EE5C6817F}")) Provider
{
static constexpr GUID* Key = const_cast<GUID*>(&__uuidof(Provider));
static constexpr const wchar_t* const Name = L"AgentSmith Isolation Provider";
static void Install(HANDLE engine)
{
FWPM_PROVIDER0 provider {};
// The provider and sublayer keys are going to be used repeatedly when
// adding filters and other objects. It's easiest to use well-known GUIDs
// defined in a header somewhere, rather than having BFE generate the keys.
provider.providerKey = __uuidof(Provider);
// For MUI compatibility, object names should be indirect strings. See
// SHLoadIndirectString for details.
provider.displayData.name = const_cast<wchar_t*>(Provider::Name);
// Since we always want the provider and sublayer to be present, it's
// easiest to add them as persistent objects during install. Alternatively,
// we could add non-persistent objects every time our service starts.
provider.flags = FWPM_PROVIDER_FLAG_PERSISTENT;
DWORD result = ::FwpmProviderAdd0(engine, &provider, nullptr);
// Ignore FWP_E_ALREADY_EXISTS. This allows install to be re-run as needed
// to repair a broken configuration.
if (result != FWP_E_ALREADY_EXISTS)
{
ThrowIfFailed("FwpmProviderAdd0", result);
}
}
};
struct __declspec(uuid("{076119FA-6A84-4F93-97E5-F0A16BB50DAC}")) SubLayer
{
static constexpr const wchar_t* const Name = L"AgentSmith Isolation SubLayer";
static void Install(HANDLE engine)
{
FWPM_SUBLAYER0 subLayer {};
subLayer.subLayerKey = __uuidof(SubLayer);
subLayer.displayData.name = const_cast<wchar_t*>(Name);
// Causes sublayer to be persistent, surviving across BFE stop/start.
subLayer.flags = FWPM_SUBLAYER_FLAG_PERSISTENT;
// Link all our other objects to our provider. When multiple providers are
// installed on a computer, this makes it easy to determine who added what.
subLayer.providerKey = Provider::Key;
// We don't care what our sublayer weight is, so we pick a weight in the
// middle and let BFE assign the closest available.
subLayer.weight = 0x8000;
DWORD result = ::FwpmSubLayerAdd0(engine, &subLayer, NULL);
if (result != FWP_E_ALREADY_EXISTS)
{
ThrowIfFailed("FwpmSubLayerAdd0", result);
}
}
};
void Install()
{
// https://learn.microsoft.com/en-us/windows/win32/fwp/installing-a-provider
_engine.Open();
if (_installed)
return;
// We add the provider and sublayer from within a single transaction to make
// it easy to clean up partial results in error paths.
DWORD result = ::FwpmTransactionBegin0(_engine, 0);
ThrowIfFailed("FwpmTransactionBegin0", result);
Provider::Install(_engine);
SubLayer::Install(_engine);
// Once all the adds have succeeded, we commit the transaction to persist
// the new objects.
result = ::FwpmTransactionCommit0(_engine);
ThrowIfFailed("FwpmTransactionCommit0", result);
_installed = true;
}
static void ThrowIfFailed(const char* name, DWORD result)
{
if (result == ERROR_SUCCESS)
return;
throw new std::exception(std::format("{} failed with {}", name, result).c_str());
}
};
int main(int argc, char* argv[])
{
std::filesystem::path programPath {argv[0]};
auto processName = programPath.stem().string();
std::vector<std::filesystem::path> trustedApps;
std::vector<std::filesystem::path> trustedAppsEx;
std::unique_ptr<Isolator> isolator;
if (processName == "Neo")
{
isolator = std::make_unique<Isolator>();
trustedApps.push_back(programPath);
trustedAppsEx.push_back(programPath);
auto trinity = programPath.replace_filename(L"Trinity.exe");
trustedAppsEx.push_back(trinity);
}
std::cout << hue::green << "Hello " << processName << hue::reset << std::endl;
// https://docs.libcpr.org/advanced-usage.html#session-objects
cpr::Url url = cpr::Url {"https://api64.ipify.org"};
cpr::Session session;
session.SetUrl(url);
int send = 10;
while (1)
{
char c = getKeyPress();
if (c != '\0')
{
if (c == 'x')
break;
if (isolator.get())
{
if (c == 'f')
isolator->Free();
else if (c == 'i')
isolator->Isolate(trustedApps);
else if (c == 'e')
isolator->Isolate(trustedAppsEx);
}
}
if (--send == 0)
{
send = 10;
cpr::Response r = session.Get();
if (r.status_code == 200)
std::cout << r.text << std::endl;
else
std::cout << hue::red << "Request failed with " << r.status_code << hue::reset << std::endl;
}
std::this_thread::sleep_for(100ms);
}
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files
// to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file