summaryrefslogtreecommitdiff
path: root/general/obcallback/control/utils.cpp
blob: 3c1cb6177e0dd397442903a924318be4c150e90e (plain)
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
//
// Module:  utils.cpp
//
// Helper functions for Ob sample code tests.
//
// Notice:
//
//    Use this sample code at your own risk; there is no support from Microsoft for the sample code.
//    In addition, this sample code is licensed to you under the terms of the Microsoft Public License
//    (http://www.microsoft.com/opensource/licenses.mspx)
//
//

#include "pch.h"
#include "common.h"

//
// Globals
//

SC_HANDLE TcScmHandle = NULL;
HANDLE TcDeviceHandle = INVALID_HANDLE_VALUE;

WCHAR TcDriverPath[MAX_PATH];


//
// TcUnprotectCallback
//
// Sends unprotect callback ioctl to the driver.
//

BOOL TcUnprotectCallback ()
{
    TD_UNPROTECT_CALLBACK_INPUT UnprotectCallbackInput = {0};

    DWORD BytesReturned = 0;

    LOG_INFO (L"TcUnprotectCallback: entering");

    BOOL Result = DeviceIoControl (
        TcDeviceHandle,
        TD_IOCTL_UNPROTECT_CALLBACK,
        &UnprotectCallbackInput,
        sizeof(UnprotectCallbackInput),
        NULL,
        0,
        &BytesReturned,
        NULL
    );

    if (Result == TRUE)
    {
        LOG_INFO (L"TcUnprotectCallback: succeeded");
    }
    else
    {
        LOG_INFO_FAILURE (L"TcUnprotectCallback: DeviceIoControl failed, last error 0x%x", GetLastError());
    }


    LOG_INFO (L"TcUnprotectCallback: exiting");
    return Result;
}


//
// TcUnprotectCallback
//
// Sends unprotect callback ioctl to the driver.
//

BOOL TcProcessNameCallback (
    _In_reads_(NAME_SIZE+1) PCWSTR  pnametoprotect,
    _In_ ULONG ulOperation
)
{
    TD_PROTECTNAME_INPUT ProtectNameCallbackInput = {0};
    BOOL Result = FALSE;
    DWORD BytesReturned = 0;

    LOG_INFO (L"TcProtectNameCallback: entering - nametoprotect %ls", pnametoprotect);

    // Copy the name of the exececutible to protect into IOCTL structure
    if (!pnametoprotect) {
        LOG_INFO_FAILURE (L"TcProcessNameCallback: NULL Protect Name");
        Result = FALSE;
        goto Exit;
    }
    wcsncpy_s(ProtectNameCallbackInput.Name, pnametoprotect, NAME_SIZE);
    ProtectNameCallbackInput.Operation = ulOperation;


    LOG_INFO (L"TcProtectNameCallback: IOCTL sending nametoprotect %ls", ProtectNameCallbackInput.Name);

    Result = DeviceIoControl (
        TcDeviceHandle,
        TD_IOCTL_PROTECT_NAME_CALLBACK,
        &ProtectNameCallbackInput,
        sizeof(ProtectNameCallbackInput),
        NULL,
        0,
        &BytesReturned,
        NULL
    );

    if (Result == TRUE)
    {
        LOG_INFO (L"TcProcessNameCallback: succeeded");
    }
    else
    {
        LOG_INFO_FAILURE (L"TcProcessNameCallback: DeviceIoControl failed, last error 0x%x", GetLastError());
    }

Exit:

    LOG_INFO (L"TcProcessNameCallback: exiting");
    return Result;
}



//
// TcInitializeGlobals
//

BOOL TcInitializeGlobals()
{
    WCHAR SysDir[MAX_PATH];
    BOOL ReturnValue = FALSE;

#if !defined (_WIN64)

    BOOL Result = FALSE;
    BOOL Wow64Process = FALSE;
    PVOID OldWowRedirectionValue = NULL;

    Result = IsWow64Process (
        GetCurrentProcess(),
        &Wow64Process
    );

    if (Result == FALSE) 
    {
        LOG_INFO_FAILURE (L"IsWow64Process failed, last error 0x%x", GetLastError());
        goto Exit;
    }

    if (Wow64Process == TRUE)
    {
        //
        // Disable FS redirection to make sure a 32 bit test process will
        // copy our (64 bit) driver to system32\drivers rather than syswow64\drivers.
        //

        Result = Wow64DisableWow64FsRedirection (&OldWowRedirectionValue);

        if (Result == FALSE) 
        {
            LOG_INFO_FAILURE (L"Wow64DisableWow64FsRedirection failed, last error 0x%x", GetLastError());
            goto Exit;
        }
    }

#endif

    //
    // Open the service control manager if not already open
    //

    if (TcScmHandle == NULL) {
        TcScmHandle = OpenSCManager (
            NULL,
            NULL,
            SC_MANAGER_ALL_ACCESS
        );
    
        if (TcScmHandle == NULL)
        {
            LOG_INFO_FAILURE (L"OpenSCManager failed, last error 0x%x", GetLastError());
            goto Exit;
        }
    }
    //
    // Construct driver path.
    //

    UINT Size = GetSystemDirectory (SysDir, ARRAYSIZE(SysDir));

    if (Size == 0)
    {
        LOG_INFO_FAILURE (L"GetSystemDirectory failed, last error 0x%x", GetLastError());
        goto Exit;
    }

    HRESULT hr = StringCchPrintf (
        TcDriverPath,
        ARRAYSIZE(TcDriverPath),
        L"%ls\\drivers\\%ls.sys",
        SysDir,
        TD_DRIVER_NAME
    );

    if (FAILED (hr))
    {
        LOG_INFO_FAILURE (L"StringCchPrintf failed, hr 0x%08x", hr);
        goto Exit;
    }

    ReturnValue = TRUE;

Exit:
    return ReturnValue;
}


//
// TcUnInitialize
//

BOOL TcCleanupSCM()
{
    if (TcScmHandle != NULL) {
        CloseServiceHandle(TcScmHandle);
        TcScmHandle = NULL;
    }

    return TRUE;
}

//
// TcLoadDriver
//

BOOL TcLoadDriver()
{
    BOOL ReturnValue = FALSE;

    LOG_INFO(L"TcLoadDriver: Entering");

    //
    // First, uninstall and unload the driver. 
    //

    ReturnValue = TcUnloadDriver();

    if (ReturnValue != TRUE)
    {
        LOG_INFO_FAILURE (L"TcUnloadDriver failed");
        goto Exit;
    }

    //
    // Copy the driver to system32\drivers
    //

    ReturnValue = CopyFile (TD_DRIVER_NAME_WITH_EXT, TcDriverPath, FALSE);

    if (ReturnValue == FALSE)
    {
        LOG_INFO_FAILURE (
            L"CopyFile(%ls, %ls) failed, last error 0x%x",
            TD_DRIVER_NAME_WITH_EXT, TcDriverPath, GetLastError()
        );

        goto Exit;
    }

    //
    // Install the driver.
    //

    ReturnValue = TcCreateService();

    if (ReturnValue == FALSE)
    {
        LOG_INFO_FAILURE (L"TcCreateService failed");
        goto Exit;
    }

    //
    // Load the driver.
    //

    ReturnValue = TcStartService();

    if (ReturnValue == FALSE)
    {
        LOG_INFO_FAILURE (L"TcStartService failed");
        goto Exit;
    }


    ReturnValue = TRUE;

Exit:

    LOG_INFO(L"TcLoadDriver: Exiting");
    return ReturnValue;
}



//
// TcUnloadDriver
//

BOOL TcUnloadDriver()
{
    BOOL ReturnValue = FALSE;

    LOG_INFO(L"TcUnloadDriver: Entering");


    //
    // Unload the driver.
    //

    ReturnValue = TcStopService();

    if (ReturnValue == FALSE)
    {
        LOG_INFO_FAILURE (L"TcStopService failed");
        goto Exit;
    }

    //
    // Delete the service.
    //

    ReturnValue = TcDeleteService();

    if (ReturnValue == FALSE)
    {
        LOG_INFO_FAILURE (L"TcDeleteService failed");
        goto Exit;
    }

    ReturnValue = TRUE;

Exit:

    LOG_INFO(L"TcUnloadDriver: Exiting");

    return ReturnValue;
}

//
// TcGetServiceState
//

BOOL TcGetServiceState (
    _In_ SC_HANDLE ServiceHandle,
    _Out_ DWORD* State
)
{
    SERVICE_STATUS_PROCESS ServiceStatus;
    DWORD BytesNeeded;

    *State = 0;
	
    BOOL Result = QueryServiceStatusEx ( 
        ServiceHandle,
        SC_STATUS_PROCESS_INFO,
        (LPBYTE)&ServiceStatus,
        sizeof(ServiceStatus),
        &BytesNeeded
        );

    if (Result == FALSE)
    {
        LOG_INFO_FAILURE (L"TcGetServiceState: QueryServiceStatusEx failed, last error 0x%x", GetLastError());
        return FALSE;
    }

    *State = ServiceStatus.dwCurrentState;

    return TRUE;
}

//
// Wait for service to enter specified state.
//

BOOL TcWaitForServiceState (
    _In_ SC_HANDLE ServiceHandle,
    _In_ DWORD State
)
{
    for (;;)
    {
        LOG_INFO (L"TcWaitForServiceState: Waiting for service %p to enter state %u...", (DWORD_PTR)ServiceHandle, State);

        DWORD ServiceState;
        BOOL Result = TcGetServiceState (ServiceHandle, &ServiceState);

        if (Result == FALSE)
        {
            return FALSE;
        }

        if (ServiceState == State)
        {
            break;
        }

        Sleep (1000);
    }

    return TRUE;
}

//
// TcCreateService
//

BOOL TcCreateService()
{
    BOOL ReturnValue = FALSE;

    LOG_INFO(L"TcCreateService: Entering");

    //
    // Create the service
    //

    SC_HANDLE ServiceHandle = CreateService (
        TcScmHandle,            // handle to SC manager
        TD_DRIVER_NAME,         // name of service
        TD_DRIVER_NAME,         // display name
        SERVICE_ALL_ACCESS,     // access mask
        SERVICE_KERNEL_DRIVER,  // service type
        SERVICE_DEMAND_START,   // start type
        SERVICE_ERROR_NORMAL,   // error control
        TcDriverPath,           // full path to driver
        NULL,                   // load ordering
        NULL,                   // tag id
        NULL,                   // dependency
        NULL,                   // account name
        NULL                    // password
    );

    DWORD LastError = GetLastError();

    if (ServiceHandle == NULL && LastError != ERROR_SERVICE_EXISTS)
    {
        LOG_INFO_FAILURE (L"CreateService failed, last error 0x%x", LastError);
        goto Exit;
    }

    ReturnValue = TRUE;

Exit:

    if (ServiceHandle)
    {
        CloseServiceHandle (ServiceHandle);
    }

    LOG_INFO(L"TcCreateService: Exiting");

    return ReturnValue;
}

//
// TcStartService
//

BOOL TcStartService()
{
    BOOL ReturnValue = FALSE;

    //
    // Open the service. The function assumes that
    // TdCreateService has been called before this one
    // and the service is already installed.
    //

    SC_HANDLE ServiceHandle = OpenService (
        TcScmHandle,
        TD_DRIVER_NAME,
        SERVICE_ALL_ACCESS
    );

    if (ServiceHandle == NULL)
    {
        LOG_INFO_FAILURE (L"TcStartService: OpenService failed, last error 0x%x", GetLastError());
        goto Exit;
    }

    //
    // Start the service
    //

    if (! StartService (ServiceHandle, 0, NULL))
    {
        if (GetLastError() != ERROR_SERVICE_ALREADY_RUNNING)
        {
            LOG_INFO_FAILURE (L"TcStartService: StartService failed, last error 0x%x", GetLastError());
            goto Exit;
        }
    }

    if (FALSE == TcWaitForServiceState (ServiceHandle, SERVICE_RUNNING))
    {
        goto Exit;
    }
    
    ReturnValue = TRUE;

Exit:

    if (ServiceHandle)
    {
        CloseServiceHandle (ServiceHandle);
    }

    return ReturnValue;
}


//
// TcStopService
//

BOOL TcStopService()
{
    BOOL ReturnValue = FALSE;

    LOG_INFO(L"TcStopService: Entering");

    //
    // Open the service so we can stop it
    //

    SC_HANDLE ServiceHandle = OpenService (
        TcScmHandle,
        TD_DRIVER_NAME,
        SERVICE_ALL_ACCESS
    );

    DWORD LastError = GetLastError();

    if (ServiceHandle == NULL)
    {
        if (LastError == ERROR_SERVICE_DOES_NOT_EXIST)
        {
            ReturnValue = TRUE;
        }
        else
        {
            LOG_INFO_FAILURE (L"TcStopService: OpenService failed, last error 0x%x", LastError);
        }

        goto Exit;
    }

    //
    // Stop the service
    //

    SERVICE_STATUS ServiceStatus;

    if (FALSE == ControlService (ServiceHandle, SERVICE_CONTROL_STOP, &ServiceStatus))
    {
        LastError = GetLastError();

        if (LastError != ERROR_SERVICE_NOT_ACTIVE)
        {
            LOG_INFO_FAILURE (L"TcStopService: ControlService failed, last error 0x%x", LastError);
            goto Exit;
        }
    }

    if (FALSE == TcWaitForServiceState (ServiceHandle, SERVICE_STOPPED))
    {
        goto Exit;
    }
    
    ReturnValue = TRUE;

Exit:

    if (ServiceHandle)
    {
        CloseServiceHandle (ServiceHandle);
    }

    LOG_INFO(L"TcStopService: Exiting");

    return ReturnValue;
}

//
// TcDeleteService
//

BOOL TcDeleteService()
{
    BOOL ReturnValue = FALSE;


    LOG_INFO(L"TcDeleteService: Entering");

    //
    // Open the service so we can delete it
    //

    SC_HANDLE ServiceHandle = OpenService (
        TcScmHandle,
        TD_DRIVER_NAME,
        SERVICE_ALL_ACCESS
    );

    DWORD LastError = GetLastError();

    if (ServiceHandle == NULL)
    {
        if (LastError == ERROR_SERVICE_DOES_NOT_EXIST)
        {
            ReturnValue = TRUE;
        }
        else
        {
            LOG_INFO_FAILURE (L"TcDeleteService: OpenService failed, last error 0x%x", LastError);
        }

        goto Exit;
    }

    //
    // Delete the service
    //

    if (! DeleteService (ServiceHandle))
    {
        LastError = GetLastError();

        if (LastError != ERROR_SERVICE_MARKED_FOR_DELETE)
        {
            LOG_INFO_FAILURE (L"TcDeleteService: DeleteService failed, last error 0x%x", LastError);
            goto Exit;
        }
    }

    ReturnValue = TRUE;

Exit:

    if (ServiceHandle)
    {
        CloseServiceHandle (ServiceHandle);
    }

    LOG_INFO(L"TcDeleteService: Exiting");

    return ReturnValue;
}

//
// TcOpenDevice
//

BOOL TcOpenDevice()
{
    BOOL ReturnValue = FALSE;

    LOG_INFO(L"TcOpenDevice: Entering");


    //
    // Open the device if not already opened
    //
    if (TcDeviceHandle == INVALID_HANDLE_VALUE) {
        TcDeviceHandle = CreateFile (
            TD_WIN32_DEVICE_NAME,
            GENERIC_READ | GENERIC_WRITE,
            0,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            NULL
        );

        if (TcDeviceHandle == INVALID_HANDLE_VALUE)
        {
            LOG_INFO_FAILURE (L"TcOpenDevice: CreateFile(%ls) failed, last error 0x%x", TD_WIN32_DEVICE_NAME, GetLastError());
            goto Exit;
        }
    }


    ReturnValue = TRUE;

Exit:

    LOG_INFO(L"TcOpenDevice: Exiting");
    return ReturnValue;
}

//
// TcOpenDevice
//

BOOL TcCloseDevice()
{
    BOOL ReturnValue = FALSE;

    LOG_INFO(L"TcCloseDevice: Entering");

    //
    // Close our handle to the device.
    //

    if (TcDeviceHandle != INVALID_HANDLE_VALUE)
    {
        CloseHandle (TcDeviceHandle);
        TcDeviceHandle = INVALID_HANDLE_VALUE;
    }

    ReturnValue = TRUE;

    LOG_INFO(L"TcCloseDevice: Exiting");
    return ReturnValue;
}