├── .gitignore ├── FileNameInformation.cpp ├── FileNameInformation.h ├── LICENSE ├── README.md ├── SymlinkProtect.cpp ├── SymlinkProtect.inf ├── SymlinkProtect.png ├── SymlinkProtect.rc ├── SymlinkProtect.sln ├── SymlinkProtect.vcxproj └── SymlinkProtect.vcxproj.filters /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /FileNameInformation.cpp: -------------------------------------------------------------------------------- 1 | #include "FileNameInformation.h" 2 | 3 | FilterFileNameInformation::FilterFileNameInformation( 4 | PFLT_CALLBACK_DATA data, 5 | FileNameOptions options 6 | ) 7 | { 8 | auto status = FltGetFileNameInformation(data, (FLT_FILE_NAME_OPTIONS)options, &_info); 9 | if (!NT_SUCCESS(status)) 10 | { 11 | _info = nullptr; 12 | } 13 | } 14 | 15 | FilterFileNameInformation::~FilterFileNameInformation() 16 | { 17 | if (_info) 18 | { 19 | FltReleaseFileNameInformation(_info); 20 | } 21 | } 22 | 23 | NTSTATUS 24 | FilterFileNameInformation::Parse() 25 | { 26 | return FltParseFileNameInformation(_info); 27 | } 28 | -------------------------------------------------------------------------------- /FileNameInformation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | enum class FileNameOptions 6 | { 7 | Normalized = FLT_FILE_NAME_NORMALIZED, 8 | Opened = FLT_FILE_NAME_OPENED, 9 | Short = FLT_FILE_NAME_SHORT, 10 | 11 | QueryDefault = FLT_FILE_NAME_QUERY_DEFAULT, 12 | QueryCacheOnly = FLT_FILE_NAME_QUERY_CACHE_ONLY, 13 | QueryFileSystemOnly = FLT_FILE_NAME_QUERY_FILESYSTEM_ONLY, 14 | 15 | RequestFromCurrentProvider = FLT_FILE_NAME_REQUEST_FROM_CURRENT_PROVIDER, 16 | DoNotCache = FLT_FILE_NAME_DO_NOT_CACHE, 17 | AllowQueryOnReparse = FLT_FILE_NAME_ALLOW_QUERY_ON_REPARSE 18 | }; 19 | 20 | DEFINE_ENUM_FLAG_OPERATORS(FileNameOptions); 21 | 22 | struct FilterFileNameInformation 23 | { 24 | FilterFileNameInformation(PFLT_CALLBACK_DATA data, FileNameOptions options = FileNameOptions::QueryDefault | FileNameOptions::Normalized); 25 | ~FilterFileNameInformation(); 26 | 27 | operator bool() const 28 | { 29 | return _info != nullptr; 30 | } 31 | 32 | PFLT_FILE_NAME_INFORMATION Get() const 33 | { 34 | return _info; 35 | } 36 | 37 | operator PFLT_FILE_NAME_INFORMATION() const 38 | { 39 | return Get(); 40 | } 41 | 42 | PFLT_FILE_NAME_INFORMATION operator->() 43 | { 44 | return _info; 45 | } 46 | 47 | NTSTATUS Parse(); 48 | 49 | private: 50 | PFLT_FILE_NAME_INFORMATION _info; 51 | }; 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Gabor Seljan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SymlinkProtect 2 | 3 | File system minifilter driver for Windows written in C++ to block symbolic link attacks. It monitors user-mode applications and blocks malicious attempts to set a reparse point on a directory creating a mount point to some suspicious targets like `\RPC Control`. See [here](https://www.seljan.hu/posts/how-to-mitigate-symbolic-link-attacks-on-windows/) for more information about the inner workings of the driver. 4 | 5 | ## Usage 6 | 7 | 1. [Download](https://github.com/sgabe/SymlinkProtect/releases) the latest release or compile the driver. 8 | 2. Right-click on the *SymlinkProtect.inf* file and click on **Install**. 9 | 3. Open an elevated command prompt and enable test signing: \ 10 | `bcdedit /set testsigning on` 11 | 4. After reboot, open an elevated command prompt again. 12 | 5. Load the driver with `fltmc.exe` with the *load* option: \ 13 | `fltmc load symlinkprotect` 14 | 6. Unload the driver with `fltmc.exe` with the *unload* option: \ 15 | `fltmc unload symlinkprotect` 16 | 17 | ## Demo 18 | 19 | ![SymlinkProtect in action](SymlinkProtect.png) 20 | -------------------------------------------------------------------------------- /SymlinkProtect.cpp: -------------------------------------------------------------------------------- 1 | /*++ 2 | 3 | Module Name: 4 | 5 | SymlinkProtect.cpp 6 | 7 | Abstract: 8 | 9 | This is the main module of the SymlinkProtect miniFilter driver 10 | 11 | Environment: 12 | 13 | Kernel mode 14 | 15 | --*/ 16 | 17 | #include 18 | #include 19 | #include "FileNameInformation.h" 20 | 21 | #pragma prefast(disable:__WARNING_ENCODE_MEMBER_FUNCTION_POINTER, "Not valid for kernel mode drivers") 22 | 23 | PFLT_FILTER gFilterHandle; 24 | ULONG_PTR OperationStatusCtx = 1; 25 | 26 | #define PTDBG_TRACE_ROUTINES 0x00000001 27 | #define PTDBG_TRACE_OPERATION_STATUS 0x00000002 28 | 29 | ULONG gTraceFlags = 0; 30 | 31 | #define DRIVER_CONTEXT_TAG 'xcsP' 32 | #define DRIVER_TAG 'sP' 33 | 34 | bool IsSymlinkAllowed(_In_ WCHAR* targetName); 35 | 36 | #define PT_DBG_PRINT( _dbgLevel, _string ) \ 37 | (FlagOn(gTraceFlags,(_dbgLevel)) ? \ 38 | DbgPrint _string : \ 39 | ((int)0)) 40 | 41 | /************************************************************************* 42 | Prototypes 43 | *************************************************************************/ 44 | 45 | EXTERN_C_START 46 | 47 | DRIVER_INITIALIZE DriverEntry; 48 | 49 | NTSTATUS 50 | DriverEntry( 51 | _In_ PDRIVER_OBJECT DriverObject, 52 | _In_ PUNICODE_STRING RegistryPath 53 | ); 54 | 55 | NTSTATUS 56 | SymlinkProtectUnload( 57 | _In_ FLT_FILTER_UNLOAD_FLAGS Flags 58 | ); 59 | 60 | FLT_PREOP_CALLBACK_STATUS 61 | SymlinkProtectPreFSControl( 62 | _Inout_ PFLT_CALLBACK_DATA Data, 63 | _In_ PCFLT_RELATED_OBJECTS FltObjects, 64 | _Flt_CompletionContext_Outptr_ PVOID* CompletionContext 65 | ); 66 | 67 | EXTERN_C_END 68 | 69 | // 70 | // Assign text sections for each routine 71 | // 72 | 73 | #ifdef ALLOC_PRAGMA 74 | #pragma alloc_text(INIT, DriverEntry) 75 | #pragma alloc_text(PAGE, SymlinkProtectUnload) 76 | #endif 77 | 78 | // 79 | // Operation registration 80 | // 81 | 82 | CONST FLT_OPERATION_REGISTRATION Callbacks[] = { 83 | { IRP_MJ_FILE_SYSTEM_CONTROL, 0, SymlinkProtectPreFSControl, nullptr }, 84 | { IRP_MJ_OPERATION_END } 85 | }; 86 | 87 | // 88 | // This defines what we want to filter with FltMgr 89 | // 90 | 91 | CONST FLT_REGISTRATION FilterRegistration = { 92 | sizeof(FLT_REGISTRATION), // Size 93 | FLT_REGISTRATION_VERSION, // Version 94 | 0, // Flags 95 | nullptr, // Context 96 | Callbacks, 97 | SymlinkProtectUnload, 98 | nullptr, // InstanceSetup 99 | nullptr, // InstanceQueryTeardown 100 | nullptr, // InstanceTeardownStart 101 | nullptr, // InstanceTeardownComplete, 102 | nullptr, // GenerateFileName 103 | nullptr, // GenerateDestinationFileName 104 | nullptr // NormalizeNameComponent 105 | }; 106 | 107 | /************************************************************************* 108 | Helper functions 109 | *************************************************************************/ 110 | 111 | bool 112 | IsSymlinkAllowed( 113 | _In_ WCHAR* targetName 114 | ) 115 | { 116 | auto allowSymlink = true; 117 | 118 | WCHAR dest[512] = { 0 }; 119 | wcsncpy_s(dest, targetName, _TRUNCATE); 120 | _wcslwr(dest); 121 | 122 | if (wcsstr(dest, L"\\rpc control") != nullptr || 123 | wcsstr(dest, L"\\basenamedobjects") != nullptr || 124 | wcsstr(dest, L"\\appcontainernamedobjects") != nullptr || 125 | wcsstr(dest, L":\\program") != nullptr || 126 | wcsstr(dest, L":\\windows") != nullptr) 127 | { 128 | allowSymlink = false; 129 | } 130 | 131 | return allowSymlink; 132 | } 133 | 134 | /************************************************************************* 135 | MiniFilter initialization and unload routines 136 | *************************************************************************/ 137 | 138 | NTSTATUS 139 | DriverEntry( 140 | _In_ PDRIVER_OBJECT DriverObject, 141 | _In_ PUNICODE_STRING RegistryPath 142 | ) 143 | { 144 | NTSTATUS status; 145 | 146 | UNREFERENCED_PARAMETER(RegistryPath); 147 | 148 | PT_DBG_PRINT(PTDBG_TRACE_ROUTINES, 149 | ("SymlinkProtect!DriverEntry: Entered\n")); 150 | 151 | status = FltRegisterFilter(DriverObject, &FilterRegistration, &gFilterHandle); 152 | if (NT_SUCCESS(status)) 153 | { 154 | status = FltStartFiltering(gFilterHandle); 155 | if (!NT_SUCCESS(status)) 156 | { 157 | FltUnregisterFilter(gFilterHandle); 158 | } 159 | } 160 | 161 | return status; 162 | } 163 | 164 | NTSTATUS 165 | SymlinkProtectUnload( 166 | _In_ FLT_FILTER_UNLOAD_FLAGS Flags 167 | ) 168 | { 169 | UNREFERENCED_PARAMETER(Flags); 170 | 171 | PAGED_CODE(); 172 | 173 | PT_DBG_PRINT(PTDBG_TRACE_ROUTINES, 174 | ("SymlinkProtect!SymlinkProtectUnload: Entered\n")); 175 | 176 | FltUnregisterFilter(gFilterHandle); 177 | 178 | return STATUS_SUCCESS; 179 | } 180 | 181 | /************************************************************************* 182 | MiniFilter callback routines 183 | *************************************************************************/ 184 | 185 | FLT_PREOP_CALLBACK_STATUS 186 | SymlinkProtectPreFSControl ( 187 | _Inout_ PFLT_CALLBACK_DATA Data, 188 | _In_ PCFLT_RELATED_OBJECTS FltObjects, 189 | _Flt_CompletionContext_Outptr_ PVOID *CompletionContext) 190 | { 191 | UNREFERENCED_PARAMETER(FltObjects); 192 | UNREFERENCED_PARAMETER(CompletionContext); 193 | 194 | PT_DBG_PRINT(PTDBG_TRACE_ROUTINES, 195 | ("SymlinkProtect!SymlinkProtectPreFSControl: Entered\n")); 196 | 197 | if (Data->RequestorMode == KernelMode || 198 | Data->Iopb->Parameters.DeviceIoControl.Buffered.IoControlCode != FSCTL_SET_REPARSE_POINT) 199 | { 200 | return FLT_PREOP_SUCCESS_NO_CALLBACK; 201 | } 202 | 203 | ULONG maxSize = 32767 * sizeof(WCHAR); 204 | auto& params = Data->Iopb->Parameters.DeviceIoControl; 205 | auto* reparseBuffer = (REPARSE_DATA_BUFFER*)params.Buffered.SystemBuffer; 206 | 207 | if (reparseBuffer->ReparseTag != IO_REPARSE_TAG_MOUNT_POINT || 208 | reparseBuffer->MountPointReparseBuffer.SubstituteNameLength > maxSize) 209 | { 210 | return FLT_PREOP_SUCCESS_NO_CALLBACK; 211 | } 212 | 213 | auto targetName = (WCHAR*)ExAllocatePoolWithTag(PagedPool, maxSize + sizeof(WCHAR), DRIVER_TAG); 214 | if (targetName == nullptr) 215 | { 216 | return FLT_PREOP_SUCCESS_NO_CALLBACK; 217 | } 218 | 219 | __try 220 | { 221 | RtlZeroMemory(targetName, maxSize + sizeof(WCHAR)); 222 | auto offset = reparseBuffer->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR); 223 | auto count = reparseBuffer->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR); 224 | wcsncpy_s(targetName, 1 + maxSize / sizeof(WCHAR), &reparseBuffer->MountPointReparseBuffer.PathBuffer[offset], count); 225 | 226 | if (!IsSymlinkAllowed(targetName)) 227 | { 228 | FilterFileNameInformation linkNameInfo(Data); 229 | if (linkNameInfo && NT_SUCCESS(linkNameInfo.Parse())) 230 | { 231 | KdPrint(("[SymlinkProtect] Junction prevented for %wZ <<===>> %ws\n", &linkNameInfo->Name, targetName)); 232 | } 233 | 234 | Data->IoStatus.Status = STATUS_INVALID_PARAMETER; 235 | Data->IoStatus.Information = 0; 236 | 237 | return FLT_PREOP_COMPLETE; 238 | } 239 | } 240 | __finally 241 | { 242 | ExFreePool(targetName); 243 | } 244 | 245 | return FLT_PREOP_SUCCESS_NO_CALLBACK; 246 | } 247 | -------------------------------------------------------------------------------- /SymlinkProtect.inf: -------------------------------------------------------------------------------- 1 | ;;; 2 | ;;; SymlinkProtect 3 | ;;; 4 | 5 | [Version] 6 | Signature = "$Windows NT$" 7 | Class = "ActivityMonitor" 8 | ClassGuid = {b86dff51-a31e-4bac-b3cf-e8cfe75c9fc2} 9 | Provider = %ManufacturerName% 10 | DriverVer = 11 | CatalogFile = SymlinkProtect.cat 12 | PnpLockDown = 1 13 | 14 | [DestinationDirs] 15 | DefaultDestDir = 12 16 | MiniFilter.DriverFiles = 12 ;%windir%\system32\drivers 17 | 18 | ;; 19 | ;; Default install sections 20 | ;; 21 | 22 | [DefaultInstall] 23 | OptionDesc = %ServiceDescription% 24 | CopyFiles = MiniFilter.DriverFiles 25 | 26 | [DefaultInstall.Services] 27 | AddService = %ServiceName%,,MiniFilter.Service 28 | 29 | ;; 30 | ;; Default uninstall sections 31 | ;; 32 | 33 | [DefaultUninstall] 34 | DelFiles = MiniFilter.DriverFiles 35 | 36 | [DefaultUninstall.Services] 37 | DelService = %ServiceName%,0x200 ;Ensure service is stopped before deleting 38 | 39 | ; 40 | ; Services Section 41 | ; 42 | 43 | [MiniFilter.Service] 44 | DisplayName = %ServiceName% 45 | Description = %ServiceDescription% 46 | ServiceBinary = %12%\%DriverName%.sys ;%windir%\system32\drivers\ 47 | Dependencies = "FltMgr" 48 | ServiceType = 2 ;SERVICE_FILE_SYSTEM_DRIVER 49 | StartType = 3 ;SERVICE_DEMAND_START 50 | ErrorControl = 1 ;SERVICE_ERROR_NORMAL 51 | LoadOrderGroup = "FSFilter Activity Monitor" 52 | AddReg = MiniFilter.AddRegistry 53 | 54 | ; 55 | ; Registry Modifications 56 | ; 57 | 58 | [MiniFilter.AddRegistry] 59 | HKR,,"DebugFlags",0x00010001 ,0x0 60 | HKR,,"SupportedFeatures",0x00010001,0x3 61 | HKR,"Instances","DefaultInstance",0x00000000,%DefaultInstance% 62 | HKR,"Instances\"%Instance1.Name%,"Altitude",0x00000000,%Instance1.Altitude% 63 | HKR,"Instances\"%Instance1.Name%,"Flags",0x00010001,%Instance1.Flags% 64 | 65 | ; 66 | ; Copy Files 67 | ; 68 | 69 | [MiniFilter.DriverFiles] 70 | %DriverName%.sys 71 | 72 | [SourceDisksFiles] 73 | SymlinkProtect.sys = 1,, 74 | 75 | [SourceDisksNames] 76 | 1 = %DiskId1%,,, 77 | 78 | ;; 79 | ;; String Section 80 | ;; 81 | 82 | [Strings] 83 | ManufacturerName = "Gabor Seljan" 84 | ServiceDescription = "SymlinkProtect Mini-Filter Driver" 85 | ServiceName = "SymlinkProtect" 86 | DriverName = "SymlinkProtect" 87 | DiskId1 = "SymlinkProtect Device Installation Disk" 88 | 89 | ;Instances specific information. 90 | DefaultInstance = "SymlinkProtect Instance" 91 | Instance1.Name = "SymlinkProtect Instance" 92 | Instance1.Altitude = "362600" 93 | Instance1.Flags = 0x0 ; Allow all attachments 94 | -------------------------------------------------------------------------------- /SymlinkProtect.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sgabe/SymlinkProtect/a638bb4fafe9a036a3dd596d65040f5c2c0c3705/SymlinkProtect.png -------------------------------------------------------------------------------- /SymlinkProtect.rc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #define VER_FILETYPE VFT_DRV 6 | #define VER_FILESUBTYPE VFT2_DRV_SYSTEM 7 | #define VER_FILEDESCRIPTION_STR "SymlinkProtect Filter Driver" 8 | #define VER_INTERNALNAME_STR "SymlinkProtect.sys" 9 | 10 | #include "common.ver" 11 | -------------------------------------------------------------------------------- /SymlinkProtect.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30717.126 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SymlinkProtect", "SymlinkProtect.vcxproj", "{F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|ARM = Debug|ARM 11 | Debug|ARM64 = Debug|ARM64 12 | Debug|x64 = Debug|x64 13 | Debug|x86 = Debug|x86 14 | Release|ARM = Release|ARM 15 | Release|ARM64 = Release|ARM64 16 | Release|x64 = Release|x64 17 | Release|x86 = Release|x86 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|ARM.ActiveCfg = Debug|ARM 21 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|ARM.Build.0 = Debug|ARM 22 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|ARM.Deploy.0 = Debug|ARM 23 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|ARM64.ActiveCfg = Debug|ARM64 24 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|ARM64.Build.0 = Debug|ARM64 25 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|ARM64.Deploy.0 = Debug|ARM64 26 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|x64.ActiveCfg = Debug|x64 27 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|x64.Build.0 = Debug|x64 28 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|x64.Deploy.0 = Debug|x64 29 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|x86.ActiveCfg = Debug|Win32 30 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|x86.Build.0 = Debug|Win32 31 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Debug|x86.Deploy.0 = Debug|Win32 32 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|ARM.ActiveCfg = Release|ARM 33 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|ARM.Build.0 = Release|ARM 34 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|ARM.Deploy.0 = Release|ARM 35 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|ARM64.ActiveCfg = Release|ARM64 36 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|ARM64.Build.0 = Release|ARM64 37 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|ARM64.Deploy.0 = Release|ARM64 38 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|x64.ActiveCfg = Release|x64 39 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|x64.Build.0 = Release|x64 40 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|x64.Deploy.0 = Release|x64 41 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|x86.ActiveCfg = Release|Win32 42 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|x86.Build.0 = Release|Win32 43 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF}.Release|x86.Deploy.0 = Release|Win32 44 | EndGlobalSection 45 | GlobalSection(SolutionProperties) = preSolution 46 | HideSolutionNode = FALSE 47 | EndGlobalSection 48 | GlobalSection(ExtensibilityGlobals) = postSolution 49 | SolutionGuid = {51DA186B-8948-46B0-B71F-3955CC0E0DA9} 50 | EndGlobalSection 51 | EndGlobal 52 | -------------------------------------------------------------------------------- /SymlinkProtect.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | Debug 22 | ARM 23 | 24 | 25 | Release 26 | ARM 27 | 28 | 29 | Debug 30 | ARM64 31 | 32 | 33 | Release 34 | ARM64 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | {F5BA0C90-E7A9-449D-9FA4-CA2951404DCF} 45 | {f2f62967-0815-4fd7-9b86-6eedcac766eb} 46 | v4.5 47 | 12.0 48 | Debug 49 | Win32 50 | SymlinkProtect 51 | SymlinkProtect 52 | 53 | 54 | 55 | Windows10 56 | true 57 | WindowsKernelModeDriver10.0 58 | Driver 59 | WDM 60 | false 61 | 62 | 63 | Windows10 64 | false 65 | WindowsKernelModeDriver10.0 66 | Driver 67 | WDM 68 | false 69 | 70 | 71 | Windows10 72 | true 73 | WindowsKernelModeDriver10.0 74 | Driver 75 | WDM 76 | Spectre 77 | 78 | 79 | Windows10 80 | false 81 | WindowsKernelModeDriver10.0 82 | Driver 83 | WDM 84 | Spectre 85 | 86 | 87 | Windows10 88 | true 89 | WindowsKernelModeDriver10.0 90 | Driver 91 | WDM 92 | 93 | 94 | Windows10 95 | false 96 | WindowsKernelModeDriver10.0 97 | Driver 98 | WDM 99 | 100 | 101 | Windows10 102 | true 103 | WindowsKernelModeDriver10.0 104 | Driver 105 | WDM 106 | 107 | 108 | Windows10 109 | false 110 | WindowsKernelModeDriver10.0 111 | Driver 112 | WDM 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | DbgengKernelDebugger 124 | 125 | 126 | DbgengKernelDebugger 127 | 128 | 129 | DbgengKernelDebugger 130 | true 131 | 132 | 133 | DbgengKernelDebugger 134 | true 135 | 136 | 137 | DbgengKernelDebugger 138 | 139 | 140 | DbgengKernelDebugger 141 | 142 | 143 | DbgengKernelDebugger 144 | 145 | 146 | DbgengKernelDebugger 147 | 148 | 149 | 150 | fltmgr.lib;%(AdditionalDependencies) 151 | 152 | 153 | 154 | 155 | fltmgr.lib;%(AdditionalDependencies) 156 | 157 | 158 | 159 | 160 | fltmgr.lib;%(AdditionalDependencies) 161 | 162 | 163 | 164 | 165 | fltmgr.lib;%(AdditionalDependencies) 166 | 167 | 168 | 169 | 170 | fltmgr.lib;%(AdditionalDependencies) 171 | 172 | 173 | 174 | 175 | fltmgr.lib;%(AdditionalDependencies) 176 | 177 | 178 | 179 | 180 | fltmgr.lib;%(AdditionalDependencies) 181 | 182 | 183 | 184 | 185 | fltmgr.lib;%(AdditionalDependencies) 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | -------------------------------------------------------------------------------- /SymlinkProtect.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | {8E41214B-6785-4CFE-B992-037D68949A14} 18 | inf;inv;inx;mof;mc; 19 | 20 | 21 | 22 | 23 | Source Files 24 | 25 | 26 | Source Files 27 | 28 | 29 | 30 | 31 | Header Files 32 | 33 | 34 | 35 | 36 | Driver Files 37 | 38 | 39 | 40 | 41 | Resource Files 42 | 43 | 44 | 45 | --------------------------------------------------------------------------------