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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
| #include <Ntifs.h> #include <ntimage.h> #include <ntstrsafe.h> #include <Tdikrnl.h> #define MEM_TAG 'YCAI' #define malloc_np(size) ExAllocatePoolWithTag(NonPagedPool, (size), MEM_TAG) #define free(ptr) ExFreePool(ptr) #define TDI_ADDRESS_MAX_LENGTH TDI_ADDRESS_LENGTH_OSI_TSAP #define TA_ADDRESS_MAX (sizeof(TA_ADDRESS) - 1 + TDI_ADDRESS_MAX_LENGTH) #define TDI_ADDRESS_INFO_MAX (sizeof(TDI_ADDRESS_INFO) - 1 + TDI_ADDRESS_MAX_LENGTH)
enum { FILTER_ALLOW = 1, FILTER_DENY, FILTER_PACKET_LOG, FILTER_PACKET_BAD, FILTER_DISCONNECT }; typedef struct { PIO_COMPLETION_ROUTINE old_cr; PVOID old_context; PIO_COMPLETION_ROUTINE new_cr; PVOID new_context; PFILE_OBJECT fileobj; PDEVICE_OBJECT new_devobj; UCHAR old_control; } TDI_SKIP_CTX; struct completion { PIO_COMPLETION_ROUTINE routine; PVOID context; }; typedef struct { TDI_ADDRESS_INFO* tai; PFILE_OBJECT fileobj; } TDI_CREATE_ADDROBJ2_CTX; struct tdi_obj { PFILE_OBJECT fileobj, associate_obj; UCHAR local_addr[TA_ADDRESS_MAX]; }; extern "C" { NTKERNELAPI NTSTATUS ObReferenceObjectByName(IN PUNICODE_STRING ObjectName, IN ULONG Attributes, IN PACCESS_STATE PassedAccessState OPTIONAL, IN ACCESS_MASK DesiredAccess OPTIONAL, IN POBJECT_TYPE ObjectType OPTIONAL, IN KPROCESSOR_MODE AccessMode, IN OUT PVOID ParseContext OPTIONAL, OUT PVOID* Object); VOID DriverUnload(PDRIVER_OBJECT DriverObject); NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegPath); extern PVOID* IoDriverObjectType; }
PDEVICE_OBJECT g_tcpfltobj = NULL; PDEVICE_OBJECT g_udpfltobj = NULL; PDEVICE_OBJECT g_tcpfltobj6 = NULL; PDEVICE_OBJECT g_udpfltobj6 = NULL; DRIVER_OBJECT g_old_DriverObject; PDRIVER_OBJECT new_DriverObject; unsigned long ntohl(unsigned long netlong) { unsigned long result = 0; ((char*)&result)[0] = ((char*)&netlong)[3]; ((char*)&result)[1] = ((char*)&netlong)[2]; ((char*)&result)[2] = ((char*)&netlong)[1]; ((char*)&result)[3] = ((char*)&netlong)[0]; return result; }
unsigned short ntohs(unsigned short netshort) { unsigned short result = 0; ((char*)&result)[0] = ((char*)&netshort)[1]; ((char*)&result)[1] = ((char*)&netshort)[0]; return result; }
NTSTATUS get_device_object(wchar_t* name, PDEVICE_OBJECT* devobj) { UNICODE_STRING str; NTSTATUS status; PFILE_OBJECT fileobj;
RtlInitUnicodeString(&str, name);
status = IoGetDeviceObjectPointer(&str, FILE_ALL_ACCESS, &fileobj, devobj); if (status == STATUS_SUCCESS) ObDereferenceObject(fileobj);
return status; }
NTSTATUS tdi_skip_complete(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Context) { TDI_SKIP_CTX* ctx = (TDI_SKIP_CTX*)Context; NTSTATUS status = STATUS_SUCCESS; PIO_STACK_LOCATION irps;
if (Irp->IoStatus.Status != STATUS_SUCCESS) DbgPrint("tdi_skip_complete: status 0x%x\n", Irp->IoStatus.Status);
Irp->CurrentLocation--; Irp->Tail.Overlay.CurrentStackLocation--;
irps = IoGetCurrentIrpStackLocation(Irp);
DeviceObject = irps->DeviceObject;
if (ctx->new_cr != NULL) { irps->FileObject = ctx->fileobj; irps->DeviceObject = ctx->new_devobj;
status = ctx->new_cr(ctx->new_devobj, Irp, ctx->new_context);
} else status = STATUS_SUCCESS;
irps->CompletionRoutine = ctx->old_cr; irps->Context = ctx->old_context; irps->Control = ctx->old_control;
irps->DeviceObject = DeviceObject;
Irp->CurrentLocation++; Irp->Tail.Overlay.CurrentStackLocation++;
if (ctx->old_cr != NULL) { if (status != STATUS_MORE_PROCESSING_REQUIRED) { BOOLEAN b_call = FALSE; if (Irp->Cancel) { if (ctx->old_control & SL_INVOKE_ON_CANCEL) b_call = TRUE; } else { if (Irp->IoStatus.Status >= STATUS_SUCCESS) { if (ctx->old_control & SL_INVOKE_ON_SUCCESS) b_call = TRUE; } else { if (ctx->old_control & SL_INVOKE_ON_ERROR) b_call = TRUE; } } if (b_call) status = ctx->old_cr(DeviceObject, Irp, ctx->old_context); } else { irps->Control = ctx->old_control; } }
free(ctx);
return status; }
NTSTATUS tdi_dispatch_complete(PDEVICE_OBJECT devobj, PIRP irp, int filter, PIO_COMPLETION_ROUTINE cr, PVOID context) { PIO_STACK_LOCATION irps = IoGetCurrentIrpStackLocation(irp); NTSTATUS status;
if (filter == FILTER_DENY) { if (irp->IoStatus.Status == STATUS_SUCCESS) { status = irp->IoStatus.Status = STATUS_ACCESS_DENIED; } else { status = irp->IoStatus.Status; }
IoCompleteRequest(irp, IO_NO_INCREMENT); } else if (filter == FILTER_ALLOW) {
if (cr != NULL) { TDI_SKIP_CTX* ctx = (TDI_SKIP_CTX*)malloc_np(sizeof(*ctx)); if (ctx == NULL) { DbgPrint("tdi_send_irp_to_old_driver: malloc_np\n");
status = irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES; IoCompleteRequest(irp, IO_NO_INCREMENT);
return status; }
ctx->old_cr = irps->CompletionRoutine; ctx->old_context = irps->Context; ctx->new_cr = cr; ctx->new_context = context; ctx->fileobj = irps->FileObject; ctx->new_devobj = devobj;
ctx->old_control = irps->Control;
irps->Context = ctx; irps->CompletionRoutine = (PIO_COMPLETION_ROUTINE)tdi_skip_complete; irps->Control = SL_INVOKE_ON_ERROR | SL_INVOKE_ON_SUCCESS | SL_INVOKE_ON_CANCEL; } status = g_old_DriverObject.MajorFunction[irps->MajorFunction](devobj, irp); } else { status = irp->IoStatus.Status = STATUS_SUCCESS; IoCompleteRequest(irp, IO_NO_INCREMENT); }
return status; }
NTSTATUS tdi_create_addrobj_complete2(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Context) { NTSTATUS status; TDI_CREATE_ADDROBJ2_CTX* ctx = (TDI_CREATE_ADDROBJ2_CTX*)Context; TA_ADDRESS* addr = ctx->tai->Address.Address; struct tdi_obj* obj_item = NULL; unsigned long Ip = ntohl(((TDI_ADDRESS_IP*)(addr->Address))->in_addr); unsigned long* IpAddress = &Ip; DbgPrint("tdi_create_addrobj_complete2: IPaddress : %d.%d.%d.%d:%u fileobj0x%llX\n", ((UCHAR*)IpAddress)[3], ((UCHAR*)IpAddress)[2], ((UCHAR*)IpAddress)[1], ((UCHAR*)IpAddress)[0], ntohs(((TDI_ADDRESS_IP*)(addr->Address))->sin_port), ctx->fileobj);
status = STATUS_SUCCESS; if (Irp->MdlAddress != NULL) { IoFreeMdl(Irp->MdlAddress); Irp->MdlAddress = NULL; }
free(ctx->tai); free(ctx);
return STATUS_SUCCESS; }
NTSTATUS tdi_generic_complete(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Context) {
if (Irp->PendingReturned) { DbgPrint("tdi_generic_complete: PENDING\n"); IoMarkIrpPending(Irp); }
return STATUS_SUCCESS; }
NTSTATUS tdi_create_addrobj_complete(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Context) { NTSTATUS status = STATUS_SUCCESS; PIO_STACK_LOCATION irps = IoGetCurrentIrpStackLocation(Irp); PIRP query_irp = (PIRP)Context; PDEVICE_OBJECT devobj; TDI_CREATE_ADDROBJ2_CTX* ctx = NULL; PMDL mdl = NULL;
if (Irp->IoStatus.Status != STATUS_SUCCESS) { DbgPrint("tdi_create_addrobj_complete: status 0x%x\n", Irp->IoStatus.Status); status = Irp->IoStatus.Status; goto done; }
ctx = (TDI_CREATE_ADDROBJ2_CTX*)malloc_np(sizeof(TDI_CREATE_ADDROBJ2_CTX)); if (ctx == NULL) { DbgPrint("tdi_create_addrobj_complete: malloc_np\n"); status = STATUS_INSUFFICIENT_RESOURCES; goto done; }
ctx->fileobj = irps->FileObject;
ctx->tai = (TDI_ADDRESS_INFO*)malloc_np(TDI_ADDRESS_INFO_MAX); if (ctx->tai == NULL) { DbgPrint("tdi_create_addrobj_complete: malloc_np!\n"); status = STATUS_INSUFFICIENT_RESOURCES; goto done; }
mdl = IoAllocateMdl(ctx->tai, TDI_ADDRESS_INFO_MAX, FALSE, FALSE, NULL); if (mdl == NULL) { DbgPrint("tdi_create_addrobj_complete: IoAllocateMdl!\n"); status = STATUS_INSUFFICIENT_RESOURCES; goto done; } MmBuildMdlForNonPagedPool(mdl);
devobj = DeviceObject; if (devobj == NULL) { DbgPrint("tdi_create_addrobj_complete: get_original_devobj!\n");
status = STATUS_INVALID_PARAMETER; goto done; }
TdiBuildQueryInformation(query_irp, devobj, irps->FileObject, tdi_create_addrobj_complete2, ctx, TDI_QUERY_ADDRESS_INFO, mdl);
status = IoCallDriver(devobj, query_irp); query_irp = NULL; mdl = NULL; ctx = NULL;
if (status != STATUS_SUCCESS) { DbgPrint("tdi_create_addrobj_complete: IoCallDriver: 0x%x STATUS_PENDING==0x103\n", status); goto done; }
status = STATUS_SUCCESS;
done: if (mdl != NULL) IoFreeMdl(mdl);
if (ctx != NULL) { if (ctx->tai != NULL) free(ctx->tai); free(ctx); }
if (query_irp != NULL) IoCompleteRequest(query_irp, IO_NO_INCREMENT);
Irp->IoStatus.Status = status;
return tdi_generic_complete(DeviceObject, Irp, Context); }
int tdi_create(PIRP irp, PIO_STACK_LOCATION irps, struct completion* completion) { FILE_FULL_EA_INFORMATION* ea = (FILE_FULL_EA_INFORMATION*)irp->AssociatedIrp.SystemBuffer;
if (ea != NULL) { PDEVICE_OBJECT devobj;
devobj = irps->DeviceObject; if (devobj == NULL) { DbgPrint("tdi_create: unknown device object 0x%llX!\n", irps->DeviceObject); return FILTER_DENY; }
if (ea->EaNameLength == TDI_TRANSPORT_ADDRESS_LENGTH && memcmp(ea->EaName, TdiTransportAddress, TDI_TRANSPORT_ADDRESS_LENGTH) == 0) { PIRP query_irp; query_irp = TdiBuildInternalDeviceControlIrp(TDI_QUERY_INFORMATION, devobj, irps->FileObject, NULL, NULL); if (query_irp == NULL) { DbgPrint("tdi_create: TdiBuildInternalDeviceControlIrp\n"); return FILTER_DENY; }
completion->routine = tdi_create_addrobj_complete; completion->context = query_irp;
} else if (ea->EaNameLength == TDI_CONNECTION_CONTEXT_LENGTH && memcmp(ea->EaName, TdiConnectionContext, TDI_CONNECTION_CONTEXT_LENGTH) == 0) {
CONNECTION_CONTEXT conn_ctx = *(CONNECTION_CONTEXT*)(ea->EaName + ea->EaNameLength + 1); }
}
return FILTER_ALLOW; }
int tdi_connect(PIRP irp, PIO_STACK_LOCATION irps, struct completion* completion) { PTDI_REQUEST_KERNEL_CONNECT param = (PTDI_REQUEST_KERNEL_CONNECT)(&irps->Parameters); TA_ADDRESS* remote_addr = ((TRANSPORT_ADDRESS*)(param->RequestConnectionInformation->RemoteAddress))->Address; unsigned long Ip = ntohl(((TDI_ADDRESS_IP*)(remote_addr->Address))->in_addr); unsigned long* IpAddress = &Ip;
DbgPrint("pid:%d tdi_connect: connobj 0x%llX, remote:%d.%d.%d.%d:%u\n", PsGetCurrentProcessId(), irps->FileObject, ((UCHAR*)IpAddress)[3], ((UCHAR*)IpAddress)[2], ((UCHAR*)IpAddress)[1], ((UCHAR*)IpAddress)[0], ntohs(((TDI_ADDRESS_IP*)(remote_addr->Address))->sin_port));
return FILTER_ALLOW; } int tdi_associate_address(PIRP irp, PIO_STACK_LOCATION irps, struct completion* completion) { HANDLE addr_handle = ((TDI_REQUEST_KERNEL_ASSOCIATE*)(&irps->Parameters))->AddressHandle; PFILE_OBJECT addrobj = NULL; NTSTATUS status; int result = FILTER_DENY;
status = ObReferenceObjectByHandle(addr_handle, GENERIC_READ, NULL, KernelMode, (PVOID*)&addrobj, NULL); if (status != STATUS_SUCCESS) { DbgPrint("[tdi_fw] tdi_associate_address: ObReferenceObjectByHandle: 0x%x\n", status); }
DbgPrint("tdi_associate_address: connobj = 0x%llX ---> addrobj = 0x%llX\n", irps->FileObject, addrobj);
result = FILTER_ALLOW; if (addrobj != NULL) ObDereferenceObject(addrobj);
return result; }
NTSTATUS TdiHookDeviceDispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP irp) { PIO_STACK_LOCATION irps; NTSTATUS status; int result; struct completion completion = { 0 }; if (irp == NULL) return STATUS_SUCCESS;
irps = IoGetCurrentIrpStackLocation(irp);
if (DeviceObject == g_tcpfltobj || DeviceObject == g_udpfltobj) { switch (irps->MajorFunction) { case IRP_MJ_CREATE: result = tdi_create(irp, irps, &completion); status = tdi_dispatch_complete(DeviceObject, irp, result, completion.routine, completion.context); break;
case IRP_MJ_INTERNAL_DEVICE_CONTROL: if (irps->MinorFunction == TDI_CONNECT) { tdi_connect(irp, irps, &completion); } else if (irps->MinorFunction == TDI_ASSOCIATE_ADDRESS) { tdi_associate_address(irp, irps, &completion); }
status = tdi_dispatch_complete(DeviceObject, irp, FILTER_ALLOW, completion.routine, completion.context); break; default: DbgPrint("default \n"); status = tdi_dispatch_complete(DeviceObject, irp, FILTER_ALLOW, NULL, NULL); return status; }
} else status = g_old_DriverObject.MajorFunction[irps->MajorFunction](DeviceObject, irp);
return status; }
void HookTcpIp() {
UNICODE_STRING drv_name; NTSTATUS status; int i;
status = get_device_object(L"\\Device\\Tcp", &g_tcpfltobj); status = get_device_object(L"\\Device\\Udp", &g_udpfltobj); RtlInitUnicodeString(&drv_name, L"\\Driver\\tdx");
status = ObReferenceObjectByName(&drv_name, OBJ_CASE_INSENSITIVE, NULL, 0, (POBJECT_TYPE)*IoDriverObjectType, KernelMode, NULL, (PVOID*)&new_DriverObject); if (status != STATUS_SUCCESS) { DbgPrint("ObReferenceObjectByName failed \n"); return; } ObDereferenceObject(new_DriverObject);
for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++) { g_old_DriverObject.MajorFunction[i] = new_DriverObject->MajorFunction[i]; new_DriverObject->MajorFunction[i] = TdiHookDeviceDispatch; } }
VOID DriverUnload(PDRIVER_OBJECT DriverObject) { int i; for (i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++) new_DriverObject->MajorFunction[i] = g_old_DriverObject.MajorFunction[i]; DbgPrint("See You !\n"); }
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegPath) {
DriverObject->DriverUnload = DriverUnload; HookTcpIp(); return STATUS_SUCCESS; }
|