Fixed permissions for "Fix Video Sync"

This commit is contained in:
Yoshi Askharoun
2021-05-26 11:32:53 -05:00
parent 454bc8f07f
commit 415f9c1b45
3 changed files with 108 additions and 11 deletions
+32 -9
View File
@@ -2,7 +2,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Threading.Tasks;
using ZuneModCore.Win32;
namespace ZuneModCore.Mods
{
@@ -21,34 +24,54 @@ namespace ZuneModCore.Mods
public override AbstractUIElementGroup? OptionsUI => null;
#pragma warning disable CA1416 // Validate platform compatibility
public override Task<string?> Apply()
{
try
{
// Make a backup of the file
File.Copy(WMVCORE_PATH, Path.Combine(StorageDirectory, "WMVCORE.original.dll"), true);
// NOTE: This is quite dangerous to do blindly, so let's not do this
// Kill processes that are using WMVCORE.dll
//foreach (System.Diagnostics.Process proc in FileUtil.WhoIsLocking(WMVCORE_PATH))
//{
// proc.Kill();
//}
// Get the working WMVCORE.dll
string sourcePath = Path.Combine(
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)!, "Resources\\WMVCORE.dll");
// Get the original WMVCORE.dll
FileInfo wmvDllInfo = new(WMVCORE_PATH);
try
{
// Activate necessary admin privileges to make changes without NTFS perms
TokenManipulator.AddPrivilege("SeRestorePrivilege"); // Necessary to set Owner Permissions
TokenManipulator.AddPrivilege("SeBackupPrivilege"); // Necessary to bypass Traverse Checking
TokenManipulator.AddPrivilege("SeTakeOwnershipPrivilege"); // Necessary to override FilePermissions
// Get access control
FileSecurity security = wmvDllInfo.GetAccessControl();
SecurityIdentifier? cu = WindowsIdentity.GetCurrent().User;
if (cu == null)
return Task.FromResult<string?>("Failed to set permissions on WMVCORE.dll, current user was null");
// Set owner to current user
security.SetOwner(cu);
security.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.Modify, AccessControlType.Allow));
// Update the Access Control on the original WMVCORE.dll
wmvDllInfo.SetAccessControl(security);
// Copy the pre-Anniversary Update WMVCORE.dll
File.Copy(sourcePath, WMVCORE_PATH, true);
return Task.FromResult<string?>(null);
}
catch (IOException)
{
return Task.FromResult<string?>($"Unable to replace '{wmvDllInfo.FullName}'. Verify that Zune and Windows Media Player are not running and try again.");
}
catch (Exception ex)
{
return Task.FromResult(ex.Message)!;
}
}
#pragma warning restore CA1416 // Validate platform compatibility
public override Task<string?> Reset()
{
+73
View File
@@ -0,0 +1,73 @@
using System;
using System.Runtime.InteropServices;
namespace ZuneModCore.Win32
{
public class TokenManipulator
{
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr
phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name,
ref long pluid);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
internal const int SE_PRIVILEGE_DISABLED = 0x00000000;
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public static bool AddPrivilege(string privilege)
{
try
{
bool retVal;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
return retVal;
}
catch
{
throw;
}
}
public static bool RemovePrivilege(string privilege)
{
try
{
bool retVal;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_DISABLED;
retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
return retVal;
}
catch
{
throw;
}
}
}
}
+1
View File
@@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="OwlCore" Version="0.0.2" />
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
</ItemGroup>
<ItemGroup>