Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.IO;
- using System.Windows.Forms;
- using System.Security.Principal;
- using System.Security.Permissions;
- using System.ComponentModel;
- namespace CTrModules
- {
- public class Test83
- {
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
- public extern static IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr SecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
- static extern bool SetFileShortName(IntPtr hFile, string lpShortName);
- [DllImport("advapi32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- protected static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, UInt32 Zero, IntPtr Null1, IntPtr Null2);
- [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
- [return: MarshalAs(UnmanagedType.Bool)]
- protected static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, ref LUID lpLuid);
- //Constants used for privilege adjustment
- private const string SE_RESTORE_NAME = "SeRestorePrivilege";
- private const string SE_BACKUP_NAME = "SeBackupPrivilege";
- private const UInt32 SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001;
- private const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;
- private const UInt32 SE_PRIVILEGE_REMOVED = 0x00000004;
- private const UInt32 SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000;
- protected struct TOKEN_PRIVILEGES
- {
- public UInt32 PrivilegeCount;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
- public LUID_AND_ATTRIBUTES[] Privileges;
- }
- [StructLayout(LayoutKind.Sequential)]
- protected struct LUID_AND_ATTRIBUTES
- {
- public LUID Luid;
- public UInt32 Attributes;
- }
- [StructLayout(LayoutKind.Sequential)]
- protected struct LUID
- {
- public uint LowPart;
- public int HighPart;
- }
- [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
- public void SetShortFile(string path, string newname)
- {
- int err = 0;
- const uint GENERIC_ALL = 0x10000000;
- const uint FILE_SHARE_READ = 0x00000001;
- const uint FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
- const uint OPEN_EXISTING = 3;
- const uint BACKUP_SEMANTICS = 0x02000000;
- var handle = CreateFile(path, GENERIC_ALL | FILE_FLAG_BACKUP_SEMANTICS, FILE_SHARE_READ,
- IntPtr.Zero, OPEN_EXISTING, BACKUP_SEMANTICS, IntPtr.Zero);
- // Show Error Code for last operation => OK
- err = Marshal.GetLastWin32Error();
- MessageBox.Show("Create Handle \r\n" + err.ToString() + ": " + new Win32Exception(err).Message);
- LUID LuidRestore = new LUID();
- LUID LuidBackup = new LUID();
- if (LookupPrivilegeValue(null, SE_RESTORE_NAME, ref LuidRestore) && LookupPrivilegeValue(null, SE_BACKUP_NAME, ref LuidBackup))
- {
- //Create the TokenPrivileges array to pass to AdjustTokenPrivileges
- LUID_AND_ATTRIBUTES[] LuidAndAttributes = new LUID_AND_ATTRIBUTES[2];
- LuidAndAttributes[0].Luid = LuidRestore;
- LuidAndAttributes[0].Attributes = SE_PRIVILEGE_ENABLED;
- LuidAndAttributes[1].Luid = LuidBackup;
- LuidAndAttributes[1].Attributes = SE_PRIVILEGE_ENABLED;
- TOKEN_PRIVILEGES TokenPrivileges = new TOKEN_PRIVILEGES();
- TokenPrivileges.PrivilegeCount = 2;
- TokenPrivileges.Privileges = LuidAndAttributes;
- IntPtr procHandle = WindowsIdentity.GetCurrent(TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query).Token;
- bool ret = (AdjustTokenPrivileges(procHandle, false, ref TokenPrivileges, 0, IntPtr.Zero, IntPtr.Zero));
- // Show Error Code for last operation => OK
- err = Marshal.GetLastWin32Error();
- MessageBox.Show("Set Token \r\n" + err.ToString() + ": " + new Win32Exception(err).Message);
- }
- bool suc = SetFileShortName(handle, newname);
- // ShowcError Code for last operation =>
- // Error 1314: A required privilege is not held by the client
- err = Marshal.GetLastWin32Error();
- MessageBox.Show("Set Short Name \r\n" + err.ToString() + ": " + new Win32Exception(err).Message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment