Les Snippets

Connexion

Ajuster les privileges par token pour appeler des fonctions systeme

Niveau requis pour utiliser/comprendre cette source : 2 ( Initié )
Créé le 17/07/2009 00:12:35 et initié par f0xi [Liste]
Date de mise à jour : 07/10/2009 00:32:27
Vue : 5430
Catégorie(s) : API, Sécurité, Trucs & Astuces, Système, Divers
Langages dispo pour ce code :
- Delphi 5
- C# 2.x, C# 3.x



Langage : Delphi 5
Date ajout : 17/07/2009
Posté par f0xi [Liste]
  function BeginAdjustPrivilege(const PrivilegeName: string; var ATokenHandle: THandle): boolean;
  var
    TokPrivilege: TTokenPrivileges;
    SizeOfTokPrivilege : Cardinal;
  begin
    Result := OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, ATokenHandle);
    if not result then
      exit;
    SizeOfTokPrivilege := SizeOf(TokPrivilege);
    FillChar(TokPrivilege, SizeOfTokPrivilege, 0);
    TokPrivilege.PrivilegeCount := 1;
    result := LookupPrivilegeValue(nil, PWideChar(PrivilegeName), TokPrivilege.Privileges[0].Luid);
    TokPrivilege.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
    result := AdjustTokenPrivileges(ATokenHandle, False, TokPrivilege, SizeOfTokPrivilege, nil, PDWORD(nil)^);
  end;

  function EndAdjustPrivilege(var ATokenHandle: THandle): boolean;
  begin
    result := CloseHandle(ATokenHandle);
  end;


exemple d'utilisation :
var ATH : THandle;
begin
  if not BeginAdjustPrivilege('SeSystemtimePrivilege', ATH) then
    exit;
  try
    { ... }
  finally
    EndAdjustPrivilege(ATH);
  end;
end;

Langage : C# 2.x , C# 3.x
Date ajout : 07/10/2009
Posté par Willi [Liste]
DateMAJ : 07/10/2009
public static class NativeMethods
{
        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, out LUID lpLuid);
        [DllImport("advapi32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool AdjustTokenPrivileges(IntPtr TokenHandle,
           [MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, Int32 Zero, IntPtr Null1, IntPtr Null2);
        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
        internal static extern bool OpenProcessToken(IntPtr ProcessHandler, int acc, ref IntPtr ProcessToken);
        [DllImport("advapi32.dll", SetLastError = true)]
        internal static extern bool LookupPrivilegeValue(string Host, string Name, ref long Luid);
        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
        internal static extern bool AdjustTokenPrivileges(IntPtr ProcessToken, bool disall,
        ref LUID Token, int Len, IntPtr prev, IntPtr ReLen);
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern bool CloseHandle(IntPtr hObject);
}
[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct LUID
{
    public int Count;
    public long Luid;
    public int Attr;
}
[StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
    public LUID Luid;
    public UInt32 Attributes;
}
public struct TOKEN_PRIVILEGES
{
    private const Int32 ANYSIZE_ARRAY = 1;
    public UInt32 PrivilegeCount;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = ANYSIZE_ARRAY)]
    public LUID_AND_ATTRIBUTES[] Privileges;
}
public static class Privileges
{
    const int TOKEN_QUERY = 0x8;
    const int TOKEN_ADJUST_PRIVILEGES = 0x20;
    const int SE_PRIVILEGE_ENABLED = 2;

    public static bool SetPrivilege(IntPtr hProcess, string seName, bool enable)
    {
        bool bok = false;
        IntPtr hToken = IntPtr.Zero;
        TOKEN_PRIVILEGES tp = new TOKEN_PRIVILEGES(); ;
        LUID luid;
        if (!NativeMethods.LookupPrivilegeValue(string.Empty, seName, out luid))
            bok = false;
        else
        {
            tp.PrivilegeCount = 1;
            tp.Privileges = new LUID_AND_ATTRIBUTES[1];
            if (enable)
                tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
            else
                tp.Privileges[0].Attributes = 0;
            if (NativeMethods.OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref hToken))
            {
                if (!NativeMethods.AdjustTokenPrivileges(hToken, false, ref tp,
                    Marshal.SizeOf(typeof(TOKEN_PRIVILEGES)), IntPtr.Zero, IntPtr.Zero))
                    bok = false;
                else
                    bok = true;
            }
            else
                bok = false;
        }
        if (hToken != IntPtr.Zero)
            NativeMethods.CloseHandle(hToken);
        return bok;
    }
}
Remarque :
Ajouter la directive
using System.Runtime.InteropServices

Exemple d'utilisation:
Privileges.SetPrivilege(MyProcess,"SeSystemtimePrivilege",true);

Privileges.SetPrivilege(MyProcess,"SeSystemtimePrivilege",false);



Codes sources en rapport avec : Openprocesstoken, Token_adjust_privileges, Lookupprivilegevalue, Adjusttokenprivileges, Privileges

{Visual Basic, VB6, VB.NET, VB 2005} GESTION DES PRIVILEGES DES PROCESSUS
Voici un ptit module pour lister et modifier les privileges des processus... j'ai mis un exemple ...

{Visual Basic, VB6, VB.NET, VB 2005} CLASSE SUR LES PROCESSUS (ET LEURS MODLES) ==> PERMET D'AVOIR LES INFOS, LISTER, KILLER, CHANGER LA PRIORITÉ, SUSPENDRE...
Cette source est une classe rapide (parce que c'est beaucoup plus propre ainsi) qui permet de faire ...