Skip to content

Commit 3e5b9d5

Browse files
committed
Add project files.
1 parent c46012d commit 3e5b9d5

File tree

4 files changed

+355
-0
lines changed

4 files changed

+355
-0
lines changed

SilentLoad.sln

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.11.35327.3
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SilentLoad", "SilentLoad\SilentLoad.vcxproj", "{A5D2F9C8-0DC2-4A2D-B72F-2FD5FF740043}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{A5D2F9C8-0DC2-4A2D-B72F-2FD5FF740043}.Debug|x64.ActiveCfg = Debug|x64
17+
{A5D2F9C8-0DC2-4A2D-B72F-2FD5FF740043}.Debug|x64.Build.0 = Debug|x64
18+
{A5D2F9C8-0DC2-4A2D-B72F-2FD5FF740043}.Debug|x86.ActiveCfg = Debug|Win32
19+
{A5D2F9C8-0DC2-4A2D-B72F-2FD5FF740043}.Debug|x86.Build.0 = Debug|Win32
20+
{A5D2F9C8-0DC2-4A2D-B72F-2FD5FF740043}.Release|x64.ActiveCfg = Release|x64
21+
{A5D2F9C8-0DC2-4A2D-B72F-2FD5FF740043}.Release|x64.Build.0 = Release|x64
22+
{A5D2F9C8-0DC2-4A2D-B72F-2FD5FF740043}.Release|x86.ActiveCfg = Release|Win32
23+
{A5D2F9C8-0DC2-4A2D-B72F-2FD5FF740043}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {FB2054AB-CC9E-4B15-B438-B4E89375DCD1}
30+
EndGlobalSection
31+
EndGlobal

SilentLoad/SilentLoad.vcxproj

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>17.0</VCProjectVersion>
23+
<Keyword>Win32Proj</Keyword>
24+
<ProjectGuid>{a5d2f9c8-0dc2-4a2d-b72f-2fd5ff740043}</ProjectGuid>
25+
<RootNamespace>SilentLoad</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v143</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v143</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v143</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v143</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<ClCompile>
75+
<WarningLevel>Level3</WarningLevel>
76+
<SDLCheck>true</SDLCheck>
77+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
78+
<ConformanceMode>true</ConformanceMode>
79+
</ClCompile>
80+
<Link>
81+
<SubSystem>Console</SubSystem>
82+
<GenerateDebugInformation>true</GenerateDebugInformation>
83+
<AdditionalDependencies>ntdll.lib;%(AdditionalDependencies)</AdditionalDependencies>
84+
</Link>
85+
</ItemDefinitionGroup>
86+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
87+
<ClCompile>
88+
<WarningLevel>Level3</WarningLevel>
89+
<FunctionLevelLinking>true</FunctionLevelLinking>
90+
<IntrinsicFunctions>true</IntrinsicFunctions>
91+
<SDLCheck>true</SDLCheck>
92+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
93+
<ConformanceMode>true</ConformanceMode>
94+
</ClCompile>
95+
<Link>
96+
<SubSystem>Console</SubSystem>
97+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
98+
<OptimizeReferences>true</OptimizeReferences>
99+
<GenerateDebugInformation>true</GenerateDebugInformation>
100+
<AdditionalDependencies>ntdll.lib;%(AdditionalDependencies)</AdditionalDependencies>
101+
</Link>
102+
</ItemDefinitionGroup>
103+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
104+
<ClCompile>
105+
<WarningLevel>Level3</WarningLevel>
106+
<SDLCheck>true</SDLCheck>
107+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
108+
<ConformanceMode>true</ConformanceMode>
109+
<LanguageStandard_C>stdc17</LanguageStandard_C>
110+
</ClCompile>
111+
<Link>
112+
<SubSystem>Console</SubSystem>
113+
<GenerateDebugInformation>true</GenerateDebugInformation>
114+
<AdditionalDependencies>ntdll.lib;%(AdditionalDependencies)</AdditionalDependencies>
115+
</Link>
116+
</ItemDefinitionGroup>
117+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
118+
<ClCompile>
119+
<WarningLevel>Level3</WarningLevel>
120+
<FunctionLevelLinking>true</FunctionLevelLinking>
121+
<IntrinsicFunctions>true</IntrinsicFunctions>
122+
<SDLCheck>true</SDLCheck>
123+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
124+
<ConformanceMode>true</ConformanceMode>
125+
</ClCompile>
126+
<Link>
127+
<SubSystem>Console</SubSystem>
128+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
129+
<OptimizeReferences>true</OptimizeReferences>
130+
<GenerateDebugInformation>true</GenerateDebugInformation>
131+
<AdditionalDependencies>ntdll.lib;%(AdditionalDependencies)</AdditionalDependencies>
132+
</Link>
133+
</ItemDefinitionGroup>
134+
<ItemGroup>
135+
<ClCompile Include="main.cpp" />
136+
</ItemGroup>
137+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
138+
<ImportGroup Label="ExtensionTargets">
139+
</ImportGroup>
140+
</Project>

SilentLoad/SilentLoad.vcxproj.filters

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="main.cpp">
19+
<Filter>Source Files</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>

SilentLoad/main.cpp

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#include <Windows.h>
2+
#include <stdio.h>
3+
#include <winternl.h>
4+
#include <strsafe.h>
5+
6+
#define SERVICE_NAME L"SilentLoad"
7+
#define DRIVER_PATH L"\\??\\C:\\Windows\\System32\\drivers\\SilentLoad.sys"
8+
9+
typedef NTSTATUS(NTAPI* _NtLoadDriver)(PUNICODE_STRING DriverServiceName);
10+
11+
static _NtLoadDriver NtLoadDriver = NULL;
12+
13+
static bool GrantPrivilege(LPCTSTR privilege)
14+
{
15+
HANDLE Token;
16+
TOKEN_PRIVILEGES TokenPrivileges;
17+
LUID Luid;
18+
19+
if (!LookupPrivilegeValueW(NULL, SE_LOAD_DRIVER_NAME, &Luid))
20+
return false;
21+
22+
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &Token))
23+
return false;
24+
25+
TokenPrivileges = { 0 };
26+
TokenPrivileges.PrivilegeCount = 1;
27+
TokenPrivileges.Privileges[0].Luid = Luid;
28+
TokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
29+
30+
if (!AdjustTokenPrivileges(Token, FALSE, &TokenPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL)
31+
|| GetLastError() == ERROR_NOT_ALL_ASSIGNED)
32+
{
33+
CloseHandle(Token);
34+
return false;
35+
}
36+
37+
CloseHandle(Token);
38+
39+
return true;
40+
}
41+
42+
static _NtLoadDriver ResolveNtLoadDriver()
43+
{
44+
HMODULE Handle;
45+
46+
Handle = GetModuleHandleW(L"ntdll.dll");
47+
if (!Handle)
48+
return NULL;
49+
50+
return (_NtLoadDriver)GetProcAddress(Handle, "NtLoadDriver");
51+
}
52+
53+
static bool AddService(LPCWSTR ServiceName, LPCWSTR DriverPath)
54+
{
55+
HKEY ServicesKey;
56+
DWORD ImagePathLength;
57+
DWORD ServiceType;
58+
DWORD ServiceStartType;
59+
DWORD ServiceErrorControl;
60+
DWORD ServiceNameLength;
61+
62+
if (!NT_SUCCESS(RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Services", 0, KEY_ALL_ACCESS, &ServicesKey)))
63+
goto Error;
64+
65+
if (!NT_SUCCESS(RegCreateKeyW(ServicesKey, ServiceName, &ServicesKey)))
66+
goto Error;
67+
68+
ImagePathLength = (wcslen(DriverPath) + 1) * sizeof(WCHAR);
69+
if (!NT_SUCCESS(RegSetValueExW(ServicesKey, L"ImagePath", 0, REG_EXPAND_SZ, (LPBYTE)DriverPath, ImagePathLength)))
70+
goto Error;
71+
72+
ServiceType = SERVICE_KERNEL_DRIVER;
73+
if (!NT_SUCCESS(RegSetValueExW(ServicesKey, L"Type", 0, REG_DWORD, (LPBYTE)&ServiceType, sizeof(ServiceType))))
74+
goto Error;
75+
76+
ServiceStartType = SERVICE_DEMAND_START;
77+
if (!NT_SUCCESS(RegSetValueExW(ServicesKey, L"Start", 0, REG_DWORD, (LPBYTE)&ServiceStartType, sizeof(ServiceStartType))))
78+
goto Error;
79+
80+
ServiceErrorControl = SERVICE_ERROR_NORMAL;
81+
if (!NT_SUCCESS(RegSetValueExW(ServicesKey, L"ErrorControl", 0, REG_DWORD, (LPBYTE)&ServiceErrorControl, sizeof(ServiceErrorControl))))
82+
goto Error;
83+
84+
ServiceNameLength = (wcslen(ServiceName) + 1) * sizeof(WCHAR);
85+
if (!NT_SUCCESS(RegSetValueExW(ServicesKey, L"DisplayName", 0, REG_SZ, (LPBYTE)ServiceName, ServiceNameLength)))
86+
goto Error;
87+
88+
RegCloseKey(ServicesKey);
89+
90+
return true;
91+
92+
Error:
93+
RegCloseKey(ServicesKey);
94+
95+
return false;
96+
}
97+
98+
static bool RemoveService(LPCWSTR ServiceName)
99+
{
100+
WCHAR RegistryPath[MAX_PATH];
101+
102+
StringCchPrintfW(RegistryPath, ARRAYSIZE(RegistryPath), L"SYSTEM\\CurrentControlSet\\Services\\%s", ServiceName);
103+
104+
return NT_SUCCESS(RegDeleteTreeW(HKEY_LOCAL_MACHINE, RegistryPath));
105+
}
106+
107+
static bool LoadDriver(LPCWSTR ServiceName)
108+
{
109+
UNICODE_STRING DriverServiceName;
110+
WCHAR Buffer[MAX_PATH];
111+
NTSTATUS Status;
112+
113+
StringCchPrintfW(Buffer, ARRAYSIZE(Buffer), L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\%s", ServiceName);
114+
RtlInitUnicodeString(&DriverServiceName, Buffer);
115+
116+
Status = NtLoadDriver(&DriverServiceName);
117+
if (Status == 0xC000010E)
118+
{
119+
printf("Driver already loaded\n");
120+
return true;
121+
}
122+
123+
return NT_SUCCESS(Status);
124+
125+
}
126+
127+
int main(int argc, char* argv[])
128+
{
129+
if (!GrantPrivilege(SE_LOAD_DRIVER_NAME))
130+
{
131+
printf("Failed to grant privilege\n");
132+
goto End;
133+
}
134+
135+
NtLoadDriver = ResolveNtLoadDriver();
136+
if (!NtLoadDriver)
137+
{
138+
printf("Failed to resolve NtLoadDriver\n");
139+
goto End;
140+
}
141+
142+
printf("NtLoadDriver: 0x%p\n", NtLoadDriver);
143+
144+
if (!AddService(SERVICE_NAME, DRIVER_PATH))
145+
{
146+
printf("Failed to add service\n");
147+
goto End;
148+
}
149+
150+
if (!LoadDriver(SERVICE_NAME))
151+
{
152+
printf("Failed to load driver\n");
153+
goto End;
154+
}
155+
156+
157+
End:
158+
if (!RemoveService(SERVICE_NAME))
159+
printf("Failed to remove service\n");
160+
161+
return 0;
162+
}

0 commit comments

Comments
 (0)