Sample Code

Windows Driver Samples/ ObCallback Callback Registration Driver/ C++/ control/ main.cpp/

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
/*++
 
Module Name:
 
    main.cpp
 
Abstract:
 
    Main module for for ps/Ob sample
 
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
 
     
--*/
 
#include "pch.h"
#include "common.h"
 
//
// PrintUsage
//
 
void TcPrintUsage()
{
    puts ("Usage:");
    puts ("");
    puts("    ObCallbackTestCtrl.exe -install -name NameofExe -reject NameofExe -uninstall -deprotect [-?]");
    puts("     -install        install driver");
    puts("     -uninstall      uninstall driver");
    puts("     -name NameofExe    protect/filter access to NameofExe");
    puts("     -reject NameofExe    prevents execution of NameofExe");
    puts("     -deprotect      unprotect/unfilter");
}
 
//
// wmain()
//
 
int _cdecl
wmain (
    _In_ int argc,
    _In_reads_(argc) LPCWSTR argv[]
)
{
    int ExitCode = ERROR_SUCCESS;
 
    if (argc > 1)
    {
        const wchar_t * arg = argv[1];
 
        // initialize globals and logging
        if (!TcInitialize()) {
            puts("Initialization failed - program exiting");
            ExitCode = ERROR_FUNCTION_FAILED;
            goto Exit;
        }
 
        if (0 == wcscmp (arg, L"-install")) {
            TcInstallDriver();
        } else
        if (0 == wcscmp (arg, L"-uninstall")) {
            TcUninstallDriver();
        } else
        if ((0 == wcscmp (arg, L"-?")) || (0 == wcscmp (arg, L"-h")) || (0 == wcscmp (arg, L"-help"))) {
            TcPrintUsage();
        } else
        if (0 == wcscmp (arg, L"-deprotect")) {
            TcRemoveProtection();
        } else
        if (0 == wcscmp (arg, L"-name")) {
            TcProcessName (argc, argv, TDProtectName_Protect);
        } else
        if (0 == wcscmp (arg, L"-reject")) {
            TcProcessName (argc, argv, TDProtectName_Reject);
        } else  {
            puts ("Unknown command!");
            TcPrintUsage();
        }
         
    }
    else
    {
        TcPrintUsage();
    }
 
Exit:
 
    if (!TcUnInitialize()) {
        puts("UnInitialization failed");
        ExitCode = ERROR_FUNCTION_FAILED;
    }
 
    return ExitCode;
}
 
 
 
//
// TcRemoveProtection
//
 
BOOL TcRemoveProtection ()
{
    BOOL ReturnValue = FALSE;
 
    LOG_INFO(_T("TcRemoveProtection: Entering"));
 
 
    //
    // Open a handle to the device.
    //
 
    ReturnValue = TcOpenDevice();
    if (ReturnValue != TRUE)
    {
        LOG_INFO_FAILURE (_T("TcOpenDevice failed"));
        goto Exit;
    }
 
 
    //
    // Send the command to the driver
    //
    ReturnValue = TcUnprotectCallback();
    if (ReturnValue != TRUE)
    {
        LOG_INFO_FAILURE (_T("TcUnprotectCallback failed"));
        goto Exit;
    }
 
Exit:
 
    //
    // Close our handle to the device.
    //
 
    ReturnValue = TcCloseDevice();
    if (ReturnValue != TRUE)
    {
        LOG_INFO_FAILURE (_T("TcCloseDevice failed"));
    }
    
 
    LOG_INFO(_T("TcRemoveProtection: Exiting"));
 
    return ReturnValue;
}
 
 
//
// TcProcessName
//
 
BOOL TcProcessName(
    _In_ int argc,
    _In_reads_(argc) LPCWSTR argv[],
    _In_ ULONG ulOperation
)
{
    BOOL ReturnValue = FALSE;
 
    PCWSTR pwProcessName = NULL;
 
    LOG_INFO(L"TcProcessName: Entering");
 
 
    //
    // Parse command line.
    //
    // argv[1] is "-name" so starting from arg #2 that should be the process name to protect
    //
 
    if (argc < 3) {
        LOG_INFO_FAILURE (L"TcProcessName: Too few parameters");
        LOG_INFO_FAILURE (L"TcProcessName: Usage  -name nameofExe  -reject nameofExe");
        ReturnValue = FALSE;
        goto Exit;
    }
 
    pwProcessName = argv[2];
 
    if (!pwProcessName) {
        LOG_INFO_FAILURE (L"TcProcessName: NULL process name to process");
        ReturnValue = FALSE;
        goto Exit;
    }
 
 
    LOG_INFO(L"Ready to copy process name");
    LOG_INFO(L"Name to pass to driver   %ls", pwProcessName);
 
 
    //
    // Open a handle to the device.
    //
 
    ReturnValue = TcOpenDevice();
    if (ReturnValue != TRUE)
    {
        LOG_INFO_FAILURE (L"TcProcessName: TcOpenDevice failed");
        goto Exit;
    }
 
 
    //
    // Send process name to protect and the command to the driver
    //
    ReturnValue = TcProcessNameCallback(pwProcessName, ulOperation);
    if (ReturnValue != TRUE)
    {
        LOG_INFO_FAILURE (L"TcProcessName: TcProcessNameCallback failed");
        goto Exit;
    }
 
Exit:
 
    //
    // Close our handle to the device.
    //
 
    ReturnValue = TcCloseDevice();
    if (ReturnValue != TRUE)
    {
        LOG_INFO_FAILURE (L"TcProtectProcess: TcCloseDevice failed");
    }
    
 
    LOG_INFO(L"TcProtectProcess: Exiting");
 
    return ReturnValue;
}
 
 
 
//
// TcInstallDriver  - installs the kernel driver
//
 
BOOL TcInstallDriver ()
{
    BOOL bRC = TRUE;
 
    LOG_INFO(L"TcInstallDriver: Entering");
    BOOL Result = TcLoadDriver();
 
    if (Result != TRUE)
    {
        LOG_ERROR (L"TcLoadDriver failed, exiting");
        bRC = FALSE;
        goto Exit;
    }
 
Exit:
 
    LOG_INFO(L"TcInstallDriver: Exiting");
    return bRC;
}
 
 
//
// TcUninstallDriver  - uninstalls the kernel driver
//
 
BOOL TcUninstallDriver ()
{
    BOOL bRC = TRUE;
 
    LOG_INFO(L"TcUninstallDriver: Entering");
    BOOL Result = TcUnloadDriver();
 
    if (Result != TRUE)
    {
        LOG_ERROR (L"TcUnloadDriver failed, exiting");
        bRC = FALSE;
        goto Exit;
    }
 
Exit:
 
    LOG_INFO(L"TcUninstallDriver: Exiting");
    return bRC;
}
 
 
//
// TcInitialize
//
 
BOOL bLoggingInitialized = FALSE;
 
BOOL TcInitialize  ()
{
 
    BOOL Result = TcInitializeGlobals();
    if (Result != TRUE)
    {
        LOG_ERROR (L"TcInitializeGlobals failed, exiting");
        return FALSE;
    }
 
    LOG_INFO(L"TcInitialize: Entering");
    return TRUE;
 
}
 
//
// TcUnInitialize
//
 
BOOL TcUnInitialize()
{
    if (TcCleanupSCM() == FALSE){
        LOG_ERROR (L"TcUnInitialize failed cleanup of SCM");
    }
    return TRUE;
}

Our Services

  • What our customers say about us?

© 2011-2025 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS