diff --git a/Debugger.vcxproj b/Debugger.vcxproj index 6c235d2..7d54a5b 100644 --- a/Debugger.vcxproj +++ b/Debugger.vcxproj @@ -94,7 +94,7 @@ MachineX86 true true - kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;comctl32.lib;dbghelp.lib;%(AdditionalDependencies) + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;comctl32.lib;dbghelp.lib;ntdll.lib;%(AdditionalDependencies) %(AdditionalLibraryDirectories) @@ -140,7 +140,7 @@ MachineX86 true true - kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;comctl32.lib;dbghelp.lib;%(AdditionalDependencies) + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;comctl32.lib;dbghelp.lib;ntdll.lib;%(AdditionalDependencies) %(AdditionalLibraryDirectories) @@ -150,6 +150,7 @@ + @@ -158,6 +159,7 @@ + diff --git a/FileNamePattern.cpp b/FileNamePattern.cpp new file mode 100644 index 0000000..a079ce5 --- /dev/null +++ b/FileNamePattern.cpp @@ -0,0 +1,113 @@ +#include "FileNamePattern.h" + +#include "Handle.h" +#include "Support.h" + +#include +#include + +#include + +// The Windows SDK omits these ntdll declarations. +extern "C" NTSYSAPI BOOLEAN NTAPI RtlIsNameInExpression( + PUNICODE_STRING Expression, PUNICODE_STRING Name, BOOLEAN IgnoreCase, + PWCH UpcaseTable); +extern "C" NTSYSAPI NTSTATUS NTAPI RtlUpcaseUnicodeString( + PUNICODE_STRING DestinationString, PCUNICODE_STRING SourceString, + BOOLEAN AllocateDestinationString); + +namespace +{ + std::wstring ToWide(char const* text) + { + // Match FindFirstFileA's code page. + auto const count = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, + text, -1, nullptr, 0); + if (!count) + { + throw_lasterror_or(ERROR_NO_UNICODE_TRANSLATION, text); + } + + std::wstring result(count, L'\0'); + if (!MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, text, -1, + result.data(), count)) + { + throw_lasterror_or(ERROR_NO_UNICODE_TRANSLATION, text); + } + result.pop_back(); + return result; + } + + UNICODE_STRING AsUnicodeString(std::wstring const& text) + { + constexpr auto MaxLength = std::numeric_limits::max(); + if (text.size() > MaxLength / sizeof(wchar_t)) + { + throw_lasterror(ERROR_FILENAME_EXCED_RANGE, {}); + } + auto const length = static_cast(text.size() * sizeof(wchar_t)); + return { length, length, const_cast(text.data()) }; + } +} + +FileNamePattern::FileNamePattern(char const* pathPattern) +{ + auto const path = ToWide(pathPattern); + std::wstring_view pattern = path; + auto const separator = pattern.find_last_of(L"\\/:"); + if (separator != std::wstring_view::npos) + { + pattern.remove_prefix(separator + 1); + } + + // Normalize trailing dots/spaces except in extended paths. + // A trailing '*.' selects extensionless names. + bool extensionless = false; + if (!path.starts_with(L"\\\\?\\")) + { + auto const endedInDot = pattern.ends_with(L'.'); + auto const last = pattern.find_last_not_of(L" ."); + pattern = pattern.substr(0, last == std::wstring_view::npos ? 0 : last + 1); + extensionless = endedInDot && pattern.ends_with(L'*'); + } + + // Use FindFirstFile's DOS_STAR (<), DOS_QM (>), and DOS_DOT (") translation. + expression.reserve(pattern.size()); + for (size_t i = 0; i < pattern.size(); ++i) + { + auto const ch = pattern[i]; + auto const next = i + 1 < pattern.size() ? pattern[i + 1] : L'\0'; + if (ch == L'*' && (next == L'.' || (extensionless && i + 1 == pattern.size()))) + { + expression.push_back(L'<'); + } + else if (ch == L'?') + { + expression.push_back(L'>'); + } + else if (ch == L'.' && (next == L'*' || next == L'?')) + { + expression.push_back(L'"'); + } + else + { + expression.push_back(ch); + } + } + + // IgnoreCase requires an uppercase expression. + auto source = AsUnicodeString(expression); + auto destination = source; + if (RtlUpcaseUnicodeString(&destination, &source, FALSE) < 0) + { + throw_lasterror(ERROR_INVALID_NAME, pathPattern); + } +} + +bool FileNamePattern::Matches(char const* fileName) const +{ + auto const name = ToWide(fileName); + auto patternString = AsUnicodeString(expression); + auto nameString = AsUnicodeString(name); + return RtlIsNameInExpression(&patternString, &nameString, TRUE, nullptr) != FALSE; +} diff --git a/FileNamePattern.h b/FileNamePattern.h new file mode 100644 index 0000000..a466fcc --- /dev/null +++ b/FileNamePattern.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +// Matches Win32 search patterns against long filenames. +class FileNamePattern +{ +public: + explicit FileNamePattern(char const* pathPattern); + + bool Matches(char const* fileName) const; + +private: + std::wstring expression; +}; diff --git a/README.md b/README.md index e04959a..2765532 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ syringe.exe game.exe --args="-CD. -SPAWN" ### DLL Injection Behavior -By default, Syringe injects **all compatible DLLs** found in the directory. +By default, Syringe injects **all compatible DLLs** found in the directory whose long filenames end in `.dll` (case insensitive). Renaming a DLL to `.dlli`, `.dllbak`, or another extension excludes it from automatic discovery, even when its Windows 8.3 short name still ends in `.DLL`. To load only specific DLLs, use one or more instances of: @@ -52,7 +52,7 @@ Example: syringe.exe game.exe -i=ExtensionA.dll -i=ExtensionB.dll ``` -When at least one `-i=` option is present, **only the specified DLLs** are injected. +When at least one `-i=` option is present, **only the specified DLLs** are injected. Selections can use exact filenames or Win32 wildcard patterns, including other filename extensions. Every selection is checked against the long filename, so `-i=*.dll` also excludes `.dlli` and `.dll+` files that only match through an 8.3 alias. Use their long names or a matching pattern to select them explicitly. ### Debugger Detach and Process Lifetime diff --git a/SyringeDebugger.cpp b/SyringeDebugger.cpp index 61eca49..8fe1774 100644 --- a/SyringeDebugger.cpp +++ b/SyringeDebugger.cpp @@ -2,6 +2,7 @@ #include "CRC32.h" #include "FindFile.h" +#include "FileNamePattern.h" #include "Handle.h" #include "Log.h" #include "Support.h" @@ -1410,10 +1411,17 @@ void SyringeDebugger::FindDLLs() { Log::WriteLine(__FUNCTION__ ": Searching for DLLs matching \"%s\"...", dll.c_str()); + FileNamePattern const pattern(dll.c_str()); for (auto file = FindFile(dll.c_str()); file; ++file) { std::string_view const fn(file->cFileName); + // Reject matches that only come from an 8.3 alias. + if (!pattern.Matches(file->cFileName)) + { + continue; + } + // Log::WriteLine( // __FUNCTION__ ": Potential DLL: \"%.*s\"", printable(fn)); diff --git a/Tests.vcxproj b/Tests.vcxproj index 798aabd..b4449ce 100644 --- a/Tests.vcxproj +++ b/Tests.vcxproj @@ -73,7 +73,7 @@ MachineX86 false false - kernel32.lib;user32.lib;gdi32.lib;advapi32.lib;shell32.lib;dbghelp.lib;%(AdditionalDependencies) + kernel32.lib;user32.lib;gdi32.lib;advapi32.lib;shell32.lib;dbghelp.lib;ntdll.lib;%(AdditionalDependencies) true @@ -102,7 +102,7 @@ MachineX86 true true - kernel32.lib;user32.lib;gdi32.lib;advapi32.lib;shell32.lib;dbghelp.lib;%(AdditionalDependencies) + kernel32.lib;user32.lib;gdi32.lib;advapi32.lib;shell32.lib;dbghelp.lib;ntdll.lib;%(AdditionalDependencies) true @@ -111,6 +111,7 @@ +