2011-03-29 6 views
1

Notre application doit communiquer avec un autre programme via une interface COM. L'interface ne fonctionnera pas si l'autre programme est démarré avec "Exécuter en tant qu'administrateur". Souhaiterait détecter si cet autre processus est dans cet état et avertir l'utilisateur. Des idées?Détecter si un autre processus est démarré en tant que "Exécuter en tant qu'administrateur"

Recherche de langages .NET (C# ou VB.NET).

TIA

+1

Votre problème semble être pourquoi COM ne franchit pas le pont entre les applications fonctionnant aux différents niveaux, pas comment le détecter. Avez-vous correctement sécurisé le canal COM? –

+0

COM rend impossible de découvrir quel processus héberge le serveur. Très bien conçu, il permet toutes sortes de trucs d'hébergement. Vous devrez utiliser des connaissances secrètes que vous avez sur le processus. –

Répondre

1

Vous pouvez essayer quelque chose comme ceci:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.InteropServices; 
using System.Diagnostics; 
using System.Security.Principal; 
using System.Reflection; 

namespace WindowsFormsApplication2 
{ 

    public class ProcessHelper 
    { 
     [DllImport("advapi32.dll", SetLastError = true)] 
     private static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle); 

     [DllImport("kernel32.dll", SetLastError = true)] 
     private static extern bool CloseHandle(IntPtr hObject); 

     private const int STANDARD_RIGHTS_REQUIRED  = 0xF0000; 
     private const int TOKEN_ASSIGN_PRIMARY   =0x1; 
     private const int TOKEN_DUPLICATE     = 0x2; 
     private const int TOKEN_IMPERSONATE    = 0x4; 
     private const int TOKEN_QUERY      = 0x8; 
     private const int TOKEN_QUERY_SOURCE    = 0x10; 
     private const int TOKEN_ADJUST_GROUPS   = 0x40; 
     private const int TOKEN_ADJUST_PRIVILEGES  = 0x20; 
     private const int TOKEN_ADJUST_SESSIONID   = 0x100; 
     private const int TOKEN_ADJUST_DEFAULT   = 0x80; 
     private const int TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_QUERY_SOURCE | TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_SESSIONID | TOKEN_ADJUST_DEFAULT); 

     public static bool IsProcessOwnerAdmin(string processName) 
     { 
      Process proc = Process.GetProcessesByName(processName)[0]; 

      IntPtr ph = IntPtr.Zero; 

      OpenProcessToken(proc.Handle, TOKEN_ALL_ACCESS, out ph); 

      WindowsIdentity iden = new WindowsIdentity(ph); 

      bool result = false; 

      foreach (IdentityReference role in iden.Groups) 
      { 
       if (role.IsValidTargetType(typeof(SecurityIdentifier))) 
       { 
        SecurityIdentifier sid = role as SecurityIdentifier; 

        if (sid.IsWellKnown(WellKnownSidType.AccountAdministratorSid) || sid.IsWellKnown(WellKnownSidType.BuiltinAdministratorsSid)) 
        { 
         result = true; 
         break; 
        } 
       } 
      } 

      CloseHandle(ph); 

      return result; 
     } 
    } 

    static class Program 
    { 
     [STAThread] 
     static void Main() 
     { 
      bool isAdmin = ProcessHelper.IsProcessOwnerAdmin("outlook"); 
     } 
    } 
} 

Cela pourrait aussi être une bonne chose d'avoir: Well-known security identifiers in Windows operating systems

Ce devrait être un bon point de départ :-)

+0

Merci pour le point de départ. Il fonctionne accepter quand le processus est démarré en utilisant RUN AS. Je reçois l'accès refusé sur le jeton OpenProcess. Étant donné qu'il est exécuté par l'administrateur intégré, l'utilisateur actuel (l'utilisateur admin) n'a pas de privilèges d'accès. Je peux le pirater et dire quand je reçois un accès refusé, je sais qu'il est en cours d'exécution. Si vous avez d'autres suggestions apprécierait un solutino plus élégant. –

+0

J'ai changé l'échantillon de code, essayez avec celui-ci. – HABJAN

+0

Toujours le même accès refusé. BTW, j'ai ajouté const int STANDARD_RIGHTS_REQUIRED = 0x000F0000; –

Questions connexes