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 382 383 384 385 386 387 388 389 390
| #include <ntddk.h> #pragma warning(push) #pragma warning(disable:4201) #pragma warning(disable:4995) #include <fwpsk.h> #pragma warning(pop) #include <ndis.h> #include <fwpmk.h> #include <limits.h> #include <ws2ipdef.h> #include <in6addr.h> #include <ip2string.h> #include <strsafe.h> #define INITGUID #include <guiddef.h> #define bool BOOLEAN #define true TRUE #define false FALSE #define DEVICE_NAME L"\\Device\\WFP_TEST" #define DEVICE_DOSNAME L"\\DosDevices\\WFP_TEST" #define kmalloc(_s) ExAllocatePoolWithTag(NonPagedPool, _s, 'SYSQ') #define kfree(_p) ExFreePool(_p)
DEFINE_GUID ( GUID_ALE_AUTH_CONNECT_CALLOUT_V4, 0x6812fc83, 0x7d3e, 0x499a, 0xa0, 0x12, 0x55, 0xe0, 0xd8, 0x5f, 0x34, 0x8b );
PDEVICE_OBJECT gDevObj;
HANDLE gEngineHandle = 0; HANDLE gInjectHandle = 0;
UINT32 gAleConnectCalloutId = 0;
UINT64 gAleConnectFilterId = 0;
NTSTATUS NTAPI WallNotifyFn ( IN FWPS_CALLOUT_NOTIFY_TYPE notifyType, IN const GUID *filterKey, IN const FWPS_FILTER *filter ) { KdPrint(("NotifyFn\n")); return STATUS_SUCCESS; }
VOID NTAPI WallFlowDeleteFn ( IN UINT16 layerId, IN UINT32 calloutId, IN UINT64 flowContext ) { KdPrint(("FlowDeleteFn\n")); return; }
char* ProtocolIdToName(UINT16 id) { char *ProtocolName = kmalloc(16); RtlZeroMemory(ProtocolName,16); switch (id) { case 1: strcpy_s(ProtocolName, 4 + 1, "ICMP"); break; case 2: strcpy_s(ProtocolName, 4 + 1, "IGMP"); break; case 6: strcpy_s(ProtocolName, 3 + 1, "TCP"); break; case 17: strcpy_s(ProtocolName, 3 + 1, "UDP"); break; case 27: strcpy_s(ProtocolName, 3 + 1, "RDP"); break; default: strcpy_s(ProtocolName, 7 + 1, "UNKNOWN"); break; } return ProtocolName; }
void NTAPI WallALEConnectClassify ( IN const FWPS_INCOMING_VALUES0* inFixedValues,
IN const FWPS_INCOMING_METADATA_VALUES0* inMetaValues, IN OUT void* layerData,
IN const void* classifyContext, IN const FWPS_FILTER* filter, IN UINT64 flowContext,
OUT FWPS_CLASSIFY_OUT* classifyOut ) {
char *ProtocolName = NULL; DWORD LocalIp, RemoteIP; PWCHAR Path = NULL;
do { Path = ExAllocatePoolWithTag(NonPagedPool, inMetaValues->processPath->size+2, 'NET'); if (NULL==Path) { break; } RtlZeroMemory(Path, inMetaValues->processPath->size + 2); wcscpy_s(Path, inMetaValues->processPath->size / 2, (WCHAR*)inMetaValues->processPath->data);
LocalIp = inFixedValues->incomingValue[FWPS_FIELD_ALE_AUTH_CONNECT_V4_IP_LOCAL_ADDRESS].value.uint32; RemoteIP = inFixedValues->incomingValue[FWPS_FIELD_ALE_AUTH_CONNECT_V4_IP_REMOTE_ADDRESS].value.uint32; ProtocolName = ProtocolIdToName(inFixedValues->incomingValue[FWPS_FIELD_ALE_AUTH_CONNECT_V4_IP_PROTOCOL].value.uint16);
DbgPrint("进程ID : %d", (DWORD)(inMetaValues->processId)); DbgPrint("Path=%S", Path); DbgPrint("本地地址=%u.%u.%u.%u ", (LocalIp >> 24) & 0xFF, (LocalIp >> 16) & 0xFF, (LocalIp >> 8) & 0xFF, LocalIp & 0xFF); DbgPrint("端口%d\n", inFixedValues->incomingValue[FWPS_FIELD_ALE_AUTH_CONNECT_V4_IP_LOCAL_PORT].value.uint16);
DbgPrint("远程地址=%u.%u.%u.%u ", (RemoteIP >> 24) & 0xFF, (RemoteIP >> 16) & 0xFF, (RemoteIP >> 8) & 0xFF, RemoteIP & 0xFF); DbgPrint("端口%d\n", inFixedValues->incomingValue[FWPS_FIELD_ALE_AUTH_CONNECT_V4_IP_REMOTE_PORT].value.uint16); DbgPrint("协议名: %s\n", ProtocolName);
kfree(ProtocolName); } while (0);
if (NULL != Path) { ExFreePoolWithTag(Path, 'NET'); } classifyOut->actionType = FWP_ACTION_PERMIT;
return; }
NTSTATUS RegisterCalloutForLayer ( IN const GUID* layerKey, IN const GUID* calloutKey, IN FWPS_CALLOUT_CLASSIFY_FN classifyFn, IN FWPS_CALLOUT_NOTIFY_FN notifyFn, IN FWPS_CALLOUT_FLOW_DELETE_NOTIFY_FN flowDeleteNotifyFn, OUT UINT32* calloutId, OUT UINT64* filterId ) { NTSTATUS status = STATUS_SUCCESS; FWPS_CALLOUT sCallout = { 0 }; FWPM_FILTER mFilter = { 0 }; FWPM_FILTER_CONDITION mFilter_condition[1] = { 0 }; FWPM_CALLOUT mCallout = { 0 }; FWPM_DISPLAY_DATA mDispData = { 0 }; BOOLEAN bCalloutRegistered = FALSE;
sCallout.calloutKey = *calloutKey; sCallout.classifyFn = classifyFn; sCallout.flowDeleteFn = flowDeleteNotifyFn; sCallout.notifyFn = notifyFn;
status = FwpsCalloutRegister(gDevObj, &sCallout, calloutId ); if (!NT_SUCCESS(status)) goto exit;
bCalloutRegistered = TRUE;
mDispData.name = L"WFP TEST"; mDispData.description = L"yxp's WFP TEST"; mCallout.applicableLayer = *layerKey;
mCallout.calloutKey = *calloutKey; mCallout.displayData = mDispData;
status = FwpmCalloutAdd(gEngineHandle, &mCallout, NULL, NULL); if (!NT_SUCCESS(status)) goto exit;
mFilter.action.calloutKey = *calloutKey; mFilter.action.type = FWP_ACTION_CALLOUT_TERMINATING;
mFilter.displayData.name = L"WFP TEST"; mFilter.displayData.description = L"yxp's WFP TEST";
mFilter.layerKey = *layerKey;
mFilter.numFilterConditions = 0; mFilter.filterCondition = mFilter_condition;
mFilter.subLayerKey = FWPM_SUBLAYER_UNIVERSAL;
mFilter.weight.type = FWP_EMPTY;
status = FwpmFilterAdd(gEngineHandle, &mFilter, NULL, filterId); if (!NT_SUCCESS(status)) goto exit; exit: if (!NT_SUCCESS(status)) { if (bCalloutRegistered) { FwpsCalloutUnregisterById(*calloutId); } } return status; }
NTSTATUS WallRegisterCallouts() { NTSTATUS status = STATUS_SUCCESS; BOOLEAN bInTransaction = FALSE; BOOLEAN bEngineOpened = FALSE; FWPM_SESSION session = { 0 };
session.flags = FWPM_SESSION_FLAG_DYNAMIC;
status = FwpmEngineOpen(NULL, RPC_C_AUTHN_WINNT, NULL, &session, &gEngineHandle); if (!NT_SUCCESS(status)) goto exit; bEngineOpened = TRUE; status = FwpmTransactionBegin(gEngineHandle, 0); if (!NT_SUCCESS(status)) goto exit; bInTransaction = TRUE; status = RegisterCalloutForLayer( &FWPM_LAYER_ALE_AUTH_CONNECT_V4, &GUID_ALE_AUTH_CONNECT_CALLOUT_V4, WallALEConnectClassify, WallNotifyFn, WallFlowDeleteFn, &gAleConnectCalloutId, &gAleConnectFilterId); if (!NT_SUCCESS(status)) { DbgPrint("RegisterCalloutForLayer失败!\n"); goto exit; } status = FwpmTransactionCommit(gEngineHandle); if (!NT_SUCCESS(status)) goto exit; bInTransaction = FALSE; exit: if (!NT_SUCCESS(status)) { if (bInTransaction) { FwpmTransactionAbort(gEngineHandle); } if (bEngineOpened) { FwpmEngineClose(gEngineHandle); gEngineHandle = 0; } } return status; }
NTSTATUS WallUnRegisterCallouts() { if (gEngineHandle != 0) { FwpmFilterDeleteById(gEngineHandle, gAleConnectFilterId); FwpmCalloutDeleteById(gEngineHandle, gAleConnectCalloutId); gAleConnectFilterId = 0; FwpsCalloutUnregisterById(gAleConnectCalloutId); gAleConnectCalloutId = 0; FwpmEngineClose(gEngineHandle); gEngineHandle = 0; } return STATUS_SUCCESS; }
VOID DriverUnload(PDRIVER_OBJECT driverObject) { NTSTATUS status; UNICODE_STRING deviceDosName = { 0 }; status = WallUnRegisterCallouts(); if (!NT_SUCCESS(status)) { DbgPrint("WallUnRegisterCallouts失败\n"); return; } RtlInitUnicodeString(&deviceDosName, DEVICE_DOSNAME); IoDeleteSymbolicLink(&deviceDosName); if (gDevObj) { IoDeleteDevice(gDevObj); gDevObj = NULL; } DbgPrint("驱动卸载成功\n"); }
NTSTATUS DriverEntry(PDRIVER_OBJECT driverObject, PUNICODE_STRING registryPath) { UNICODE_STRING deviceName = { 0 }; UNICODE_STRING deviceDosName = { 0 }; NTSTATUS status = STATUS_SUCCESS; driverObject->DriverUnload = DriverUnload; RtlInitUnicodeString(&deviceName, DEVICE_NAME); status = IoCreateDevice(driverObject, 0, &deviceName, FILE_DEVICE_NETWORK, 0, FALSE, &gDevObj); if (!NT_SUCCESS(status)) { DbgPrint("创建设备对象失败\n"); return STATUS_UNSUCCESSFUL; } RtlInitUnicodeString(&deviceDosName, DEVICE_DOSNAME); status = IoCreateSymbolicLink(&deviceDosName, &deviceName); if (!NT_SUCCESS(status)) { DbgPrint("创建符号链接失败\n"); return STATUS_UNSUCCESSFUL; } status = WallRegisterCallouts(); if (!NT_SUCCESS(status)) { DbgPrint("WallRegisterCallouts 失败!\n"); return STATUS_UNSUCCESSFUL; } DbgPrint("过滤驱动加载成功\n"); return status; }
|