Guest User

Set Short Filename

a guest
Sep 3rd, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. using System.Security.Principal;
  8. using System.Security.Permissions;
  9. using System.ComponentModel;
  10.  
  11. namespace CTrModules
  12. {
  13.     public class Test83
  14.     {
  15.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
  16.         public extern static IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr SecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
  17.  
  18.         [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  19.         static extern bool SetFileShortName(IntPtr hFile, string lpShortName);
  20.  
  21.         [DllImport("advapi32.dll", SetLastError = true)]
  22.         [return: MarshalAs(UnmanagedType.Bool)]
  23.         protected static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, UInt32 Zero, IntPtr Null1, IntPtr Null2);
  24.  
  25.         [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  26.         [return: MarshalAs(UnmanagedType.Bool)]
  27.         protected static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, ref LUID lpLuid);
  28.  
  29.         //Constants used for privilege adjustment
  30.         private const string SE_RESTORE_NAME = "SeRestorePrivilege";
  31.         private const string SE_BACKUP_NAME = "SeBackupPrivilege";
  32.         private const UInt32 SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001;
  33.         private const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;
  34.         private const UInt32 SE_PRIVILEGE_REMOVED = 0x00000004;
  35.         private const UInt32 SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000;
  36.  
  37.         protected struct TOKEN_PRIVILEGES
  38.         {
  39.             public UInt32 PrivilegeCount;
  40.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
  41.             public LUID_AND_ATTRIBUTES[] Privileges;
  42.         }
  43.  
  44.         [StructLayout(LayoutKind.Sequential)]
  45.         protected struct LUID_AND_ATTRIBUTES
  46.         {
  47.             public LUID Luid;
  48.             public UInt32 Attributes;
  49.         }
  50.  
  51.         [StructLayout(LayoutKind.Sequential)]
  52.         protected struct LUID
  53.         {
  54.             public uint LowPart;
  55.             public int HighPart;
  56.         }
  57.  
  58.         [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
  59.         public void SetShortFile(string path, string newname)
  60.         {
  61.             int err = 0;
  62.             const uint GENERIC_ALL = 0x10000000;
  63.             const uint FILE_SHARE_READ = 0x00000001;
  64.             const uint FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;
  65.  
  66.             const uint OPEN_EXISTING = 3;
  67.             const uint BACKUP_SEMANTICS = 0x02000000;
  68.  
  69.             var handle = CreateFile(path, GENERIC_ALL | FILE_FLAG_BACKUP_SEMANTICS, FILE_SHARE_READ,
  70.             IntPtr.Zero, OPEN_EXISTING, BACKUP_SEMANTICS, IntPtr.Zero);
  71.  
  72.             // Show Error Code for last operation => OK
  73.             err = Marshal.GetLastWin32Error();
  74.             MessageBox.Show("Create Handle \r\n" + err.ToString() + ": " + new Win32Exception(err).Message);
  75.  
  76.             LUID LuidRestore = new LUID();
  77.             LUID LuidBackup = new LUID();
  78.             if (LookupPrivilegeValue(null, SE_RESTORE_NAME, ref LuidRestore) && LookupPrivilegeValue(null, SE_BACKUP_NAME, ref LuidBackup))
  79.             {
  80.                 //Create the TokenPrivileges array to pass to AdjustTokenPrivileges
  81.                 LUID_AND_ATTRIBUTES[] LuidAndAttributes = new LUID_AND_ATTRIBUTES[2];
  82.                 LuidAndAttributes[0].Luid = LuidRestore;
  83.                 LuidAndAttributes[0].Attributes = SE_PRIVILEGE_ENABLED;
  84.                 LuidAndAttributes[1].Luid = LuidBackup;
  85.                 LuidAndAttributes[1].Attributes = SE_PRIVILEGE_ENABLED;
  86.  
  87.                 TOKEN_PRIVILEGES TokenPrivileges = new TOKEN_PRIVILEGES();
  88.                 TokenPrivileges.PrivilegeCount = 2;
  89.                 TokenPrivileges.Privileges = LuidAndAttributes;
  90.  
  91.                 IntPtr procHandle = WindowsIdentity.GetCurrent(TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query).Token;
  92.  
  93.                 bool ret = (AdjustTokenPrivileges(procHandle, false, ref TokenPrivileges, 0, IntPtr.Zero, IntPtr.Zero));
  94.  
  95.                 // Show Error Code for last operation => OK
  96.                 err = Marshal.GetLastWin32Error();
  97.                 MessageBox.Show("Set Token \r\n" + err.ToString() + ": " + new Win32Exception(err).Message);
  98.             }
  99.  
  100.             bool suc = SetFileShortName(handle, newname);
  101.  
  102.             // ShowcError Code for last operation =>
  103.             // Error 1314: A required privilege is not held by the client
  104.             err = Marshal.GetLastWin32Error();
  105.             MessageBox.Show("Set Short Name \r\n" + err.ToString() + ": " + new Win32Exception(err).Message);
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment