Skip to content

Commit

Permalink
CreateFile Hook Feature Added
Browse files Browse the repository at this point in the history
  • Loading branch information
marcussacana committed May 21, 2019
1 parent 4c17a6e commit 9eeef4d
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 1 deletion.
9 changes: 9 additions & 0 deletions SRL/FileWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,15 @@ private static void LoadConfig() {
Log("Custom Directory Loaded", true);
}

if (HookSettings.CreateFile) {
if (!HookCreateFile)
InstallCreateFileHooks();
HookCreateFile = true;
Log("CreateFile Hook Enabled", true);
}
else if (HookGlyphOutline)
Warning("CreateFile Hook Settings Changed - Restart Required");

if (HookSettings.GetGlyphOutline) {
if (!HookGlyphOutline)
InstallGlyphHooks();
Expand Down
126 changes: 126 additions & 0 deletions SRL/Hook/FileHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using System.IO;
using System.Runtime.InteropServices;

namespace SRL
{
static partial class StringReloader
{
static void InstallCreateFileHooks()
{
dGetFileAttrA = new GetFileAttributesADelegate(GetFileAttributes);
dGetFileAttrW = new GetFileAttributesWDelegate(GetFileAttributes);
dGetFileAttrExA = new GetFileAttributesExADelegate(GetFileAttributesEx);
dGetFileAttrExW = new GetFileAttributesExWDelegate(GetFileAttributesEx);
dCreateFileA = new CreateFileADelegate(CreateFile);
dCreateFileW = new CreateFileWDelegate(CreateFile);

hCreateFileA = new FxHook("kernel32.dll", "CreateFileA", dCreateFileA);
hCreateFileW = new FxHook("kernel32.dll", "CreateFileW", dCreateFileW);
hGetFileAttrA = new FxHook("kernel32.dll", "GetFileAttributesA", dGetFileAttrA);
hGetFileAttrW = new FxHook("kernel32.dll", "GetFileAttributesW", dGetFileAttrW);
hGetFileAttrExA = new FxHook("kernel32.dll", "GetFileAttributesExA", dGetFileAttrExA);
hGetFileAttrExW = new FxHook("kernel32.dll", "GetFileAttributesExW", dGetFileAttrExW);

hCreateFileA.Install();
hCreateFileW.Install();
hGetFileAttrA.Install();
hGetFileAttrW.Install();
hGetFileAttrExA.Install();
hGetFileAttrExW.Install();

Base = BaseDir.TrimEnd('\\', '/');
if (Base == AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\', '/'))
Base += "\\Patch";

Base += '\\';

Log("CreateFile Hook Path: {0}", true, Base);
}

static bool GetFileAttributesEx(string FileName, IntPtr fInfoLevelId, IntPtr lpFileInformation)
{
FileName = ParsePath(FileName);

return GetFileAttributesExW(FileName, fInfoLevelId, lpFileInformation);
}

static uint GetFileAttributes(string FileName)
{
FileName = ParsePath(FileName);

return GetFileAttributesW(FileName);
}
static IntPtr CreateFile(string FileName, IntPtr Access, IntPtr Share, IntPtr Security, IntPtr Mode, IntPtr Flags, IntPtr TemplateFile)
{
FileName = ParsePath(FileName);

return CreateFileW(FileName, Access, Share, Security, Mode, Flags, TemplateFile);
}

static string ParsePath(string Path)
{

string PatchPath = Base + System.IO.Path.GetFileName(Path);

if (ValidPath(PatchPath))
{
#if DEBUG
Log("File Path Converted from:\n{0}\nto:\n{1}", true, Path, PatchPath);
#endif
return PatchPath;
}

return Path;
}

static string Base;

static bool ValidPath(string Path) => GetFileAttributesW(Path) != INVALID_FILE_ATTRIBUTES;

const uint INVALID_FILE_ATTRIBUTES = 0xFFFFFFFF;

static GetFileAttributesADelegate dGetFileAttrA;
static GetFileAttributesWDelegate dGetFileAttrW;
static GetFileAttributesExADelegate dGetFileAttrExA;
static GetFileAttributesExWDelegate dGetFileAttrExW;
static CreateFileADelegate dCreateFileA;
static CreateFileWDelegate dCreateFileW;

static FxHook hGetFileAttrA;
static FxHook hGetFileAttrW;
static FxHook hGetFileAttrExA;
static FxHook hGetFileAttrExW;
static FxHook hCreateFileA;
static FxHook hCreateFileW;

[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Ansi, SetLastError = true)]
delegate uint GetFileAttributesADelegate([MarshalAs(UnmanagedType.LPStr)] string Filepath);

[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)]
delegate uint GetFileAttributesWDelegate([MarshalAs(UnmanagedType.LPWStr)] string Filepath);

[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Ansi, SetLastError = true)]
delegate bool GetFileAttributesExADelegate([MarshalAs(UnmanagedType.LPStr)] string Filepath, IntPtr fInfoLevelId, IntPtr lpFileInformation);

[UnmanagedFunctionPointer(CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)]
delegate bool GetFileAttributesExWDelegate([MarshalAs(UnmanagedType.LPWStr)] string Filepath, IntPtr fInfoLevelId, IntPtr lpFileInformation);

[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Ansi, SetLastError = true)]
delegate IntPtr CreateFileADelegate([MarshalAs(UnmanagedType.LPStr)] string filename, IntPtr Access, IntPtr Share, IntPtr Security, IntPtr Mode, IntPtr Flags, IntPtr templateFile);

[UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
delegate IntPtr CreateFileWDelegate([MarshalAs(UnmanagedType.LPWStr)] string filename, IntPtr Access, IntPtr Share, IntPtr Security, IntPtr Mode, IntPtr Flags, IntPtr templateFile);


[DllImport("kernelbase.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool GetFileAttributesExW(string lpFileName, IntPtr fInfoLevelId, IntPtr lpFileInformation);

[DllImport("kernelbase.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)]
static extern uint GetFileAttributesW(string lpFileName);

[DllImport("kernelbase.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode, SetLastError = true)]
static extern IntPtr CreateFileW(string FileName, IntPtr Access, IntPtr Share, IntPtr Security, IntPtr Mode, IntPtr Flags, IntPtr templateFile);

}
}
File renamed without changes.
3 changes: 2 additions & 1 deletion SRL/SRL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
<Compile Include="Compiler.cs" />
<Compile Include="DNVM.cs" />
<Compile Include="FileWorker.cs" />
<Compile Include="Hook\GenericHook.cs" />
<Compile Include="Hook\FileHook.cs" />
<Compile Include="Hook\TextHook.cs" />
<Compile Include="Hook\HookFX.cs" />
<Compile Include="Hook\IntroInjector.cs" />
<Compile Include="Ini.cs" />
Expand Down
1 change: 1 addition & 0 deletions SRL/SRL.ini
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ MoveWindow=false


[Hook]
CreateFile=false
UndoChars=true
MultiByteToWideChar=false
SetWindowText=false
Expand Down
2 changes: 2 additions & 0 deletions SRL/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ internal struct HookSettings {
[FieldParmaters(Name = "SendMessage", DefaultValue = false)]
public bool SendMessage;
#endif
[FieldParmaters(Name = "CreateFile", DefaultValue = false)]
public bool CreateFile;
[FieldParmaters(Name = "MultiByteToWideChar", DefaultValue = false)]
public bool MultiByteToWideChar;
[FieldParmaters(Name = "SetWindowText", DefaultValue = false)]
Expand Down
1 change: 1 addition & 0 deletions SRL/Variables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ struct Range {
static bool HookCreateWindowEx;
static bool HookSendMessage;
#endif
static bool HookCreateFile;
static bool HookMultiByteToWideChar;
static bool HookSetWindowText;
static bool HookGlyphOutline;
Expand Down

0 comments on commit 9eeef4d

Please sign in to comment.