mirror of
https://github.com/ZuneDev/ZuneModdingHelper.git
synced 2026-07-27 13:12:21 -07:00
Switched TokenManipulator to Vanara.PInvoke libraries
This commit is contained in:
@@ -3,47 +3,27 @@ using System.IO;
|
|||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Security.AccessControl;
|
using System.Security.AccessControl;
|
||||||
using System.Security.Principal;
|
using System.Security.Principal;
|
||||||
|
using Vanara.PInvoke;
|
||||||
|
|
||||||
namespace ZuneModCore.Win32
|
namespace ZuneModCore.Win32
|
||||||
{
|
{
|
||||||
public class TokenManipulator
|
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)
|
public static bool AddPrivilege(string privilege)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
bool retVal;
|
bool retVal;
|
||||||
TokPriv1Luid tp;
|
var hproc = Kernel32.GetCurrentProcess();
|
||||||
IntPtr hproc = GetCurrentProcess();
|
|
||||||
IntPtr htok = IntPtr.Zero;
|
retVal = AdvApi32.OpenProcessToken(hproc,
|
||||||
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
|
AdvApi32.TokenAccess.TOKEN_ADJUST_PRIVILEGES | AdvApi32.TokenAccess.TOKEN_QUERY, out var htok);
|
||||||
tp.Count = 1;
|
|
||||||
tp.Luid = 0;
|
retVal = AdvApi32.LookupPrivilegeValue(null, privilege, out var tpLuid);
|
||||||
tp.Attr = SE_PRIVILEGE_ENABLED;
|
AdvApi32.TOKEN_PRIVILEGES tp = new(tpLuid, AdvApi32.PrivilegeAttributes.SE_PRIVILEGE_ENABLED);
|
||||||
retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
|
|
||||||
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
|
AdvApi32.AdjustTokenPrivileges(htok, false, in tp, out _).ThrowIfFailed();
|
||||||
|
|
||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@@ -56,15 +36,16 @@ namespace ZuneModCore.Win32
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
bool retVal;
|
bool retVal;
|
||||||
TokPriv1Luid tp;
|
var hproc = Kernel32.GetCurrentProcess();
|
||||||
IntPtr hproc = GetCurrentProcess();
|
|
||||||
IntPtr htok = IntPtr.Zero;
|
retVal = AdvApi32.OpenProcessToken(hproc,
|
||||||
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
|
AdvApi32.TokenAccess.TOKEN_ADJUST_PRIVILEGES | AdvApi32.TokenAccess.TOKEN_QUERY, out var htok);
|
||||||
tp.Count = 1;
|
|
||||||
tp.Luid = 0;
|
retVal = AdvApi32.LookupPrivilegeValue(null, privilege, out var tpLuid);
|
||||||
tp.Attr = SE_PRIVILEGE_DISABLED;
|
AdvApi32.TOKEN_PRIVILEGES tp = new(tpLuid, AdvApi32.PrivilegeAttributes.SE_PRIVILEGE_DISABLED);
|
||||||
retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
|
|
||||||
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
|
AdvApi32.AdjustTokenPrivileges(htok, false, in tp, out _).ThrowIfFailed();
|
||||||
|
|
||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@@ -76,10 +57,7 @@ namespace ZuneModCore.Win32
|
|||||||
#pragma warning disable CA1416 // Validate platform compatibility
|
#pragma warning disable CA1416 // Validate platform compatibility
|
||||||
public static void TakeOwnership(FileInfo file)
|
public static void TakeOwnership(FileInfo file)
|
||||||
{
|
{
|
||||||
// Activate necessary admin privileges to make changes without NTFS perms
|
EscalateToAdmin();
|
||||||
AddPrivilege("SeRestorePrivilege"); // Necessary to set Owner Permissions
|
|
||||||
AddPrivilege("SeBackupPrivilege"); // Necessary to bypass Traverse Checking
|
|
||||||
AddPrivilege("SeTakeOwnershipPrivilege"); // Necessary to override FilePermissions
|
|
||||||
|
|
||||||
// Get access control
|
// Get access control
|
||||||
FileSecurity security = file.GetAccessControl();
|
FileSecurity security = file.GetAccessControl();
|
||||||
@@ -89,11 +67,34 @@ namespace ZuneModCore.Win32
|
|||||||
security.SetOwner(cu);
|
security.SetOwner(cu);
|
||||||
security.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.Modify, AccessControlType.Allow));
|
security.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.Modify, AccessControlType.Allow));
|
||||||
|
|
||||||
// Update the Access Control on the original WMVCORE.dll
|
// Update the Access Control on the original file
|
||||||
file.SetAccessControl(security);
|
file.SetAccessControl(security);
|
||||||
}
|
}
|
||||||
#pragma warning restore CA1416 // Validate platform compatibility
|
|
||||||
|
|
||||||
|
public static void TakeOwnership(DirectoryInfo dir)
|
||||||
|
{
|
||||||
|
EscalateToAdmin();
|
||||||
|
|
||||||
|
// Get access control
|
||||||
|
DirectorySecurity security = dir.GetAccessControl();
|
||||||
|
SecurityIdentifier? cu = WindowsIdentity.GetCurrent().User;
|
||||||
|
|
||||||
|
// Set owner to current user
|
||||||
|
security.SetOwner(cu);
|
||||||
|
security.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.Modify, AccessControlType.Allow));
|
||||||
|
|
||||||
|
// Update the Access Control on the original file
|
||||||
|
dir.SetAccessControl(security);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void TakeOwnership(FileSystemInfo info)
|
||||||
|
{
|
||||||
|
if (info is FileInfo file)
|
||||||
|
TakeOwnership(file);
|
||||||
|
else if (info is DirectoryInfo dir)
|
||||||
|
TakeOwnership(dir);
|
||||||
|
}
|
||||||
|
#pragma warning restore CA1416 // Validate platform compatibility
|
||||||
|
|
||||||
public static bool TryTakeOwnership(FileInfo file, out Exception? exception)
|
public static bool TryTakeOwnership(FileInfo file, out Exception? exception)
|
||||||
{
|
{
|
||||||
@@ -110,5 +111,13 @@ namespace ZuneModCore.Win32
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void EscalateToAdmin()
|
||||||
|
{
|
||||||
|
// Activate necessary admin privileges to make changes without NTFS perms
|
||||||
|
AddPrivilege("SeRestorePrivilege"); // Necessary to set Owner Permissions
|
||||||
|
AddPrivilege("SeBackupPrivilege"); // Necessary to bypass Traverse Checking
|
||||||
|
AddPrivilege("SeTakeOwnershipPrivilege"); // Necessary to override FilePermissions
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
<LangVersion>9</LangVersion>
|
<LangVersion>9</LangVersion>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<Platforms>AnyCPU;x64;x86</Platforms>
|
<Platforms>AnyCPU;x64;x86</Platforms>
|
||||||
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -12,6 +13,8 @@
|
|||||||
<PackageReference Include="OwlCore" Version="0.0.43" />
|
<PackageReference Include="OwlCore" Version="0.0.43" />
|
||||||
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
|
<PackageReference Include="System.IO.FileSystem.AccessControl" Version="5.0.0" />
|
||||||
<PackageReference Include="TagLibSharp" Version="2.2.0" />
|
<PackageReference Include="TagLibSharp" Version="2.2.0" />
|
||||||
|
<PackageReference Include="Vanara.PInvoke.Kernel32" Version="3.4.6" />
|
||||||
|
<PackageReference Include="Vanara.PInvoke.Security" Version="3.4.6" />
|
||||||
<PackageReference Include="Vestris.ResourceLib" Version="2.1.0" />
|
<PackageReference Include="Vestris.ResourceLib" Version="2.1.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user