diff options
| author | Dave Wilson <[email protected]> | 2015-03-17 19:50:07 -0700 |
|---|---|---|
| committer | Dave Wilson <[email protected]> | 2015-03-17 19:50:07 -0700 |
| commit | 97cf5197cf5b882b2c689d8dc2b555f2edf8f418 (patch) | |
| tree | 46f3701832d70b420eb0fc0eb93261f9da45db3f /general/obcallback/control | |
| parent | ef1905bf1e8825bb31120dfb27e0daf3154d859a (diff) | |
Initial publish
Diffstat (limited to 'general/obcallback/control')
| -rw-r--r-- | general/obcallback/control/ObCallbackTestCtrl.vcxproj | 236 | ||||
| -rw-r--r-- | general/obcallback/control/ObCallbackTestCtrl.vcxproj.Filters | 28 | ||||
| -rw-r--r-- | general/obcallback/control/common.h | 86 | ||||
| -rw-r--r-- | general/obcallback/control/main.cpp | 320 | ||||
| -rw-r--r-- | general/obcallback/control/pch.h | 18 | ||||
| -rw-r--r-- | general/obcallback/control/pchsrc.cpp | 1 | ||||
| -rw-r--r-- | general/obcallback/control/readme.txt | 57 | ||||
| -rw-r--r-- | general/obcallback/control/utils.cpp | 727 |
8 files changed, 1473 insertions, 0 deletions
diff --git a/general/obcallback/control/ObCallbackTestCtrl.vcxproj b/general/obcallback/control/ObCallbackTestCtrl.vcxproj new file mode 100644 index 00000000..d9ea7f08 --- /dev/null +++ b/general/obcallback/control/ObCallbackTestCtrl.vcxproj @@ -0,0 +1,236 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <ProjectGuid>{8B053BEE-EA21-4D12-984B-6C93FE6D4992}</ProjectGuid> + <RootNamespace>$(MSBuildProjectName)</RootNamespace> + <Configuration Condition="'$(Configuration)' == ''">Debug</Configuration> + <Platform Condition="'$(Platform)' == ''">Win32</Platform> + <SampleGuid>{BED8786D-3B67-42D0-AC7B-D7D2F45E551E}</SampleGuid> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <TargetVersion>Windows10</TargetVersion> + <UseDebugLibraries>False</UseDebugLibraries> + <DriverTargetPlatform>Desktop</DriverTargetPlatform> + <DriverType /> + <PlatformToolset>WindowsApplicationForDrivers10.0</PlatformToolset> + <ConfigurationType>Application</ConfigurationType> + </PropertyGroup> + <PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <TargetVersion>Windows10</TargetVersion> + <UseDebugLibraries>True</UseDebugLibraries> + <DriverTargetPlatform>Desktop</DriverTargetPlatform> + <DriverType /> + <PlatformToolset>WindowsApplicationForDrivers10.0</PlatformToolset> + <ConfigurationType>Application</ConfigurationType> + </PropertyGroup> + <PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <TargetVersion>Windows10</TargetVersion> + <UseDebugLibraries>False</UseDebugLibraries> + <DriverTargetPlatform>Desktop</DriverTargetPlatform> + <DriverType /> + <PlatformToolset>WindowsApplicationForDrivers10.0</PlatformToolset> + <ConfigurationType>Application</ConfigurationType> + </PropertyGroup> + <PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <TargetVersion>Windows10</TargetVersion> + <UseDebugLibraries>True</UseDebugLibraries> + <DriverTargetPlatform>Desktop</DriverTargetPlatform> + <DriverType /> + <PlatformToolset>WindowsApplicationForDrivers10.0</PlatformToolset> + <ConfigurationType>Application</ConfigurationType> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <PropertyGroup> + <OutDir>$(IntDir)</OutDir> + </PropertyGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" /> + </ImportGroup> + <ItemGroup Label="WrappedTaskItems" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <TargetName>ObCallbackTestCtrl</TargetName> + <WIN32_WINNT_VERSION>0x0A00</WIN32_WINNT_VERSION> + <NTDDI_VERSION>0x0A000000</NTDDI_VERSION> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <TargetName>ObCallbackTestCtrl</TargetName> + <WIN32_WINNT_VERSION>0x0A00</WIN32_WINNT_VERSION> + <NTDDI_VERSION>0x0A000000</NTDDI_VERSION> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <TargetName>ObCallbackTestCtrl</TargetName> + <WIN32_WINNT_VERSION>0x0A00</WIN32_WINNT_VERSION> + <NTDDI_VERSION>0x0A000000</NTDDI_VERSION> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <TargetName>ObCallbackTestCtrl</TargetName> + <WIN32_WINNT_VERSION>0x0A00</WIN32_WINNT_VERSION> + <NTDDI_VERSION>0x0A000000</NTDDI_VERSION> + </PropertyGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <ClCompile> + <Optimization>Disabled</Optimization> + <IntrinsicFunctions>true</IntrinsicFunctions> + <WarningLevel>Level4</WarningLevel> + </ClCompile> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <ClCompile> + <Optimization>Disabled</Optimization> + <IntrinsicFunctions>true</IntrinsicFunctions> + <WarningLevel>Level4</WarningLevel> + </ClCompile> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <ClCompile> + <Optimization>Disabled</Optimization> + <IntrinsicFunctions>true</IntrinsicFunctions> + <WarningLevel>Level4</WarningLevel> + </ClCompile> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <ClCompile> + <Optimization>Disabled</Optimization> + <IntrinsicFunctions>true</IntrinsicFunctions> + <WarningLevel>Level4</WarningLevel> + </ClCompile> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <Link> + <AdditionalOptions>%(AdditionalOptions) /LARGEADDRESSAWARE</AdditionalOptions> + <AdditionalDependencies>%(AdditionalDependencies);ntdll.lib;kernel32.lib;advapi32.lib</AdditionalDependencies> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <Link> + <AdditionalOptions>%(AdditionalOptions) /LARGEADDRESSAWARE</AdditionalOptions> + <AdditionalDependencies>%(AdditionalDependencies);ntdll.lib;kernel32.lib;advapi32.lib</AdditionalDependencies> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <Link> + <AdditionalOptions>%(AdditionalOptions) /LARGEADDRESSAWARE</AdditionalOptions> + <AdditionalDependencies>%(AdditionalDependencies);ntdll.lib;kernel32.lib;advapi32.lib</AdditionalDependencies> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <Link> + <AdditionalOptions>%(AdditionalOptions) /LARGEADDRESSAWARE</AdditionalOptions> + <AdditionalDependencies>%(AdditionalDependencies);ntdll.lib;kernel32.lib;advapi32.lib</AdditionalDependencies> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <ClCompile> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + <ExceptionHandling> + </ExceptionHandling> + </ClCompile> + <Midl> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + </Midl> + <ResourceCompile> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + </ResourceCompile> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <ClCompile> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + <ExceptionHandling> + </ExceptionHandling> + </ClCompile> + <Midl> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + </Midl> + <ResourceCompile> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + </ResourceCompile> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <ClCompile> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + <ExceptionHandling> + </ExceptionHandling> + </ClCompile> + <Midl> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + </Midl> + <ResourceCompile> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + </ResourceCompile> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <ClCompile> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + <ExceptionHandling> + </ExceptionHandling> + </ClCompile> + <Midl> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + </Midl> + <ResourceCompile> + <PreprocessorDefinitions>%(PreprocessorDefinitions);UNICODE;_UNICODE</PreprocessorDefinitions> + </ResourceCompile> + </ItemDefinitionGroup> + <ItemGroup> + <ClCompile Include="main.cpp"> + <AdditionalIncludeDirectories>;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreCompiledHeaderFile>pch.h</PreCompiledHeaderFile> + <PreCompiledHeader>Use</PreCompiledHeader> + <PreCompiledHeaderOutputFile>$(IntDir)\pch.h.pch</PreCompiledHeaderOutputFile> + </ClCompile> + <ClCompile Include="pchsrc.cpp"> + <AdditionalIncludeDirectories>;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreCompiledHeaderFile>pch.h</PreCompiledHeaderFile> + <PreCompiledHeader>Create</PreCompiledHeader> + <PreCompiledHeaderOutputFile>$(IntDir)\pch.h.pch</PreCompiledHeaderOutputFile> + </ClCompile> + <ClCompile Include="utils.cpp"> + <AdditionalIncludeDirectories>;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> + <PreCompiledHeaderFile>pch.h</PreCompiledHeaderFile> + <PreCompiledHeader>Use</PreCompiledHeader> + <PreCompiledHeaderOutputFile>$(IntDir)\pch.h.pch</PreCompiledHeaderOutputFile> + </ClCompile> + </ItemGroup> + <ItemGroup> + <Inf Exclude="@(Inf)" Include="*.inf" /> + <FilesToPackage Include="$(TargetPath)" Condition="'$(ConfigurationType)'=='Driver' or '$(ConfigurationType)'=='DynamicLibrary'" /> + <FilesToPackage Include="@(Inf->'%(CopyOutput)')" Condition="'@(Inf)'!=''" /> + </ItemGroup> + <ItemGroup> + <None Exclude="@(None)" Include="*.txt;*.htm;*.html" /> + <None Exclude="@(None)" Include="*.ico;*.cur;*.bmp;*.dlg;*.rct;*.gif;*.jpg;*.jpeg;*.wav;*.jpe;*.tiff;*.tif;*.png;*.rc2" /> + <None Exclude="@(None)" Include="*.def;*.bat;*.hpj;*.asmx" /> + </ItemGroup> + <ItemGroup> + <ClInclude Exclude="@(ClInclude)" Include="*.h;*.hpp;*.hxx;*.hm;*.inl;*.xsd" /> + </ItemGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> +</Project>
\ No newline at end of file diff --git a/general/obcallback/control/ObCallbackTestCtrl.vcxproj.Filters b/general/obcallback/control/ObCallbackTestCtrl.vcxproj.Filters new file mode 100644 index 00000000..b9258c52 --- /dev/null +++ b/general/obcallback/control/ObCallbackTestCtrl.vcxproj.Filters @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="Source Files"> + <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx;*</Extensions> + <UniqueIdentifier>{5FCF0F0F-49FE-446D-9A27-490CBE156E8F}</UniqueIdentifier> + </Filter> + <Filter Include="Header Files"> + <Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions> + <UniqueIdentifier>{F9488019-A49A-430B-818A-BB8ADC3106D1}</UniqueIdentifier> + </Filter> + <Filter Include="Resource Files"> + <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms;man;xml</Extensions> + <UniqueIdentifier>{DBBA22E4-2B2D-4D16-8C9A-26B358D8752E}</UniqueIdentifier> + </Filter> + </ItemGroup> + <ItemGroup> + <ClCompile Include="main.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="pchsrc.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="utils.cpp"> + <Filter>Source Files</Filter> + </ClCompile> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/general/obcallback/control/common.h b/general/obcallback/control/common.h new file mode 100644 index 00000000..1ba366a2 --- /dev/null +++ b/general/obcallback/control/common.h @@ -0,0 +1,86 @@ + +// 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) + +#pragma once + +#pragma warning (disable: 4201) // nonstandard extension used : nameless struct/union + +#include "..\driver\shared.h" + +// +// Logging support macros. +// +// LOG_INFO +// LOG_INFO_FAILURE +// LOG_PASSED +// LOG_ERROR +// + + +#ifdef DEBUG +#define LOG_INFO(fmt, ...) \ + _tprintf(_T("%hs: ") fmt, __FUNCTION__, __VA_ARGS__);_tprintf(_T("\n")); +#define LOG_INFO_FAILURE(fmt, ...) \ + _tprintf(_T("ReportFailure %hs: ") fmt, __FUNCTION__, __VA_ARGS__);_tprintf(_T("\n")); + +#define LOG_PASSED(fmt, ...) \ + _tprintf(_T("\n!!!PASSED: %hs (%hs:%u): ") fmt, __FUNCTION__, __FILE__, __LINE__, __VA_ARGS__);_tprintf(_T("\n")); +#define LOG_ERROR(fmt, ...) \ + _tprintf(_T("\n!!!FAILED: %hs (%hs:%u): ") fmt, __FUNCTION__, __FILE__, __LINE__, __VA_ARGS__); _tprintf(_T("\n")); + +#else + +#define LOG_INFO(FormatString, ...) +#define LOG_INFO_FAILURE(FormatString, ...) + +#define LOG_PASSED(FormatString, ...) +#define LOG_ERROR(FormatString, ...) + +#endif + + +extern HANDLE TcDeviceHandle; + +BOOL TcInitialize(); +BOOL TcUnInitialize(); +BOOL TcCleanupSCM(); + +BOOL TcInstallDriver(); + +BOOL TcUninstallDriver(); + +BOOL TcRemoveProtection (); + +BOOL TcProcessName ( + _In_ int argc, + _In_reads_(argc) LPCWSTR argv[], + _In_ ULONG ulOperation +); + +BOOL TcUnprotectCallback (); + +BOOL TcProcessNameCallback ( + _In_reads_(NAME_SIZE+1) PCWSTR pnametoprotect, + _In_ ULONG ulOperation +); + +// +// Utility functions +// + +BOOL TcInitializeGlobals(); +BOOL TcLoadDriver(); +BOOL TcUnloadDriver(); + +BOOL TcCreateService(); +BOOL TcDeleteService(); +BOOL TcStartService(); +BOOL TcStopService(); + +BOOL TcOpenDevice(); +BOOL TcCloseDevice(); + diff --git a/general/obcallback/control/main.cpp b/general/obcallback/control/main.cpp new file mode 100644 index 00000000..e5a36224 --- /dev/null +++ b/general/obcallback/control/main.cpp @@ -0,0 +1,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 + (http://www.microsoft.com/opensource/licenses.mspx) + + +--*/ + +#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; +} diff --git a/general/obcallback/control/pch.h b/general/obcallback/control/pch.h new file mode 100644 index 00000000..d165c04f --- /dev/null +++ b/general/obcallback/control/pch.h @@ -0,0 +1,18 @@ + +// 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) + +#pragma once + +//#include <nt.h> +//#include <ntrtl.h> +//#include <nturtl.h> +#include <windows.h> +#include <stdlib.h> +#include <tchar.h> +#include <strsafe.h> +#include <winioctl.h> + diff --git a/general/obcallback/control/pchsrc.cpp b/general/obcallback/control/pchsrc.cpp new file mode 100644 index 00000000..17305716 --- /dev/null +++ b/general/obcallback/control/pchsrc.cpp @@ -0,0 +1 @@ +#include "pch.h"
\ No newline at end of file diff --git a/general/obcallback/control/readme.txt b/general/obcallback/control/readme.txt new file mode 100644 index 00000000..694696ad --- /dev/null +++ b/general/obcallback/control/readme.txt @@ -0,0 +1,57 @@ +The sample code exercises both PsSetCreateProcessNotifyRoutineEx() and ObRegisterCallbacks(). +These routines were introduced in Vista SP1 and are present in Windows7. They are available in both 32bit OS and 64bit OS. +The first example uses ObRegisterCallbacks() and a callback to restrict requested access rights during a open process action. +The second example uses PsSetCreateProcessNotifyRoutineEx() to reject a process creation by examining the command line. + +The code once compiled produces two files: ObCallbackTest.sys and ObCallbackTestCtrl.exe + +It is important to change the names of the binaries in the sample code to be unique for your own use. +#define TD_DRIVER_NAME L"ObCallbackTest" +#define TD_DRIVER_NAME_WITH_EXT L"ObCallbackTest.sys" + +#define TD_NT_DEVICE_NAME L"\\Device\\ObCallbackTest" +#define TD_DOS_DEVICES_LINK_NAME L"\\DosDevices\\ObCallbackTest" +#define TD_WIN32_DEVICE_NAME L"\\\\.\\ObCallbackTest" + + + + +For running the code you can use (run as administrator): + + C:\> obcallbacktest.exe -? (for command line help) + C:\> obcallbacktest.exe -install (installs the kernel driver) + C:\> obcallbacktest.exe -name notepad (specifies that the string �notepad� will be watched as a protected executable) + + (now you can start up �notepad.exe�) + C:\> notepad + + (locate the process ID of notepad.exe) + C:\> tlist + + (attempt to kill off the notepad.exe with a PID of 2329) + C:\> kill �f 2329 + process notepad.exe (2329) � �Untitled � Notepad� could not be killed + + (remove the protections on the notepad process) + C:\> obcallbacktest.exe -deprotect + + (attempt to kill off the process � which will succeed) + C:\> kill �f 2329 + + (uninstall the kernel driver) + C:\> obcallbacktest.exe -uninstall + +Another sample test you can run is to prevent a process from being created + + C:\> obcallbacktest.exe -install (installs the kernel driver) + C:\> obcallbacktest.exe -reject notepad (specifies that the string �notepad� will be watched and prevented from starting as a process) + + (now you can start up �notepad.exe�) + C:\> notepad + Access is denied. + + +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). + +May 2009 + diff --git a/general/obcallback/control/utils.cpp b/general/obcallback/control/utils.cpp new file mode 100644 index 00000000..3c1cb617 --- /dev/null +++ b/general/obcallback/control/utils.cpp @@ -0,0 +1,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; +} + |
