最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Check if a standard Windows user is launching an application with Run as Administrator option using Windows service - Stack

programmeradmin2浏览0评论

I've a custom Windows service (written in C#) which basically I'm using for whitelisting or blacklisting Windows applications. I don't want to use any existing Windows functionality or any commercial solution. I want to check if a standard user is trying to launch a Windows application with the Run as Administrator option from the context menu.

For monitoring a launched application, I'm using ManagementEventWatcher() which is subscribing to __InstanceCreationEvent:

public class ProcessLaunchMonitorService : ServiceBase
{
public ProcessLaunchMonitorService()
{
    this.ServiceName = "testservice";
}

protected override void OnStart(string[] args)
{
    string query = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'";
    // Setup the watcher
    _watcher = new ManagementEventWatcher(query);
    _watcher.EventArrived += new EventArrivedEventHandler(OnProcessStarted);
    _watcher.Start();
}

private void OnProcessStarted(object sender, EventArrivedEventArgs e)
{
    // Get the process name
    var process = (ManagementBaseObject)e.NewEvent["TargetInstance"];
    string processName = process["Name"].ToString();
    int processId = Convert.ToInt32(process["ProcessId"]);
    var launchedProcess = Process.GetProcessById(processId); 
// want to check if this launchedProcess was launched with Run as Administrator option
}
}

Current result: This code is fully executable without any error. This code is actually missing the part for detecting if the user used the Run as Administrator option while launching the application.

Expected result: How can I check if the user used the Run as Administrator option while launching the application (launchedProcess)?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论