【问题标题】:Selectively disabling UAC for specific programs on Windows Programatically以编程方式选择性地为 Windows 上的特定程序禁用 UAC
【发布时间】:2013-02-17 22:12:46
【问题描述】:

堆栈和其他论坛上有许多关于禁用/绕过/抑制 UAC 的帖子问题/答案。也有解决方案。但在程序上可能不是。我只能看到一个解决方案Disabling UAC programmatically,但那里可能没有真正的编程解决方案。

是否有一个程序化的解决方案来保存用户,每次他/她运行像 wamp 这样的程序时都会提示他们,他们总是必须点击是,所以最好告诉 windows 他们的选择总是是的。我相信会有这样的

我发现Here Windows 通过 GUI 在任务计划程序中提供了此功能,因此也必须通过代码实现。

更新:我准备了一个正在运行的纯编程解决方案。看我的回答。

【问题讨论】:

  • 你为什么要绕过 UAC
  • @JakobBowyer 因为无论我在哪里部署了我的 c#、mysql 桌面应用程序(需要在系统上运行 wamp),当 wamp 启动时,用户都会对 UAC 对话感到恼火。
  • 这听起来很愚蠢。如果有人利用了你的软件,你已经为他们做了 UAC 绕过,你现在打开了一个巨大的该死的安全漏洞
  • @JakobBowyer 你太激进了,无法讨论。我在个人计算机上运行的桌面应用程序没有安全风险。不过你可能是对的。你可以投反对票,你可以劝阻其他人指导我。那是你的权利。但是你的语言和你的论点并没有说服力,就像在表现出厌恶一样。我很抱歉,但这就是我的感受。
  • 这个问题有-4,另一个stackoverflow.com/questions/682182/…有10。这两个问题都是关于禁用UAC的;其中一个有真实的答案,而另一个有意见。这是可悲的。 stackoverflow.com/faq#etiquette

标签: c# windows scheduled-tasks uac


【解决方案1】:

简要说明:创建一个新的控制台/窗口应用程序以运行任何绕过 UAC 的应用程序,按照以下指导在此应用程序中选择目标应用程序的路径,编译该程序一次,并随时运行 p>

一步一步

  1. This Codeplex link下载Microsoft.Win32.TaskScheduler.dll
  2. 制作一个 c# 应用程序(Windows 或控制台)并添加对上述 dll 的引用
  3. 将新项目(应用程序清单文件)添加到您的项目(此应用程序)
  4. 更改<requestedExecutionLevel level="asInvoker" uiAccess="false" /><requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
  5. 在您的 program.cs 文件中编写以下代码

using System;
using Microsoft.Win32.TaskScheduler;
class Program
{
   static void Main(string[] args)
   {
      TaskService ts = new TaskService();          
      TaskDefinition td = ts.NewTask();
      td.Principal.RunLevel = TaskRunLevel.Highest;
      //td.Triggers.AddNew(TaskTriggerType.Logon);          
      td.Triggers.AddNew(TaskTriggerType.Once);    // 
      string program_path = @"c:\wamp\wampmanager.exe"; // you can have it dynamic
//even of user choice giving an interface in win-form or wpf application

      td.Actions.Add(new ExecAction(program_path, null));
      ts.RootFolder.RegisterTaskDefinition("anyNamefortask", td);          
   }
}

6.现在编译并运行你的应用程序(这个应用程序)


现在您的应用程序(例如 WAMP)将运行,而不会按您所需的时间表提示任何 UAC 对话框(在我的情况下,每次登录窗口时)

来源

从:Can you turn off UAC for a single app?Selectively disabling UAC for specific programs on Windows 7 发起

基本思路来自:Make Vista launch UAC restricted programs at startup with Task Scheduler

Creating Scheduled Tasks

的基本实现

【讨论】:

    【解决方案2】:

    正确的方法不是忽略用户访问控制 (UAC),而是在这些参数内进行测试。这样您就不会破坏安全性,而是在其范围内工作。

    通过禁用安全性,您将面临被利用的风险。据提供多项安全测试的 Secuna 称,小公司、懒惰的开发者应用程序以及公然无视安全性都是被关注的应用程序。

    这意味着您的应用程序可能会在某个时候成为受害者。

    我会采取的方法是在 UAC 中进行测试。确保存在适当的权限来执行您的任务,这样它就不会一直以提升的权限运行。一个例子可能是:

    class Elevated_Rights
    {
        // Token Bool:
        private bool _level = false;
    
        #region Constructor:
        protected Elevated_Rights()
        {
               // Invoke Method On Creation:
               Elevate();
         }
         #endregion
         public void Elevate()
         {
               // Get Identity:
               WindowsIdentity user = WindowsIdentity.GetCurrent();
    
               // Set Principal
               WindowsPrincipal role = new WindowsPrincipal(user);
    
               #region Test Operating System for UAC:
               if (Environment.OSVersion.Platform != PlatformID.Win32NT ||            Environment.OSVersion.Version.Major < 6)
                {
                     // False:
                     _level = false;
                 }
                 #endregion
                 else
                 {
                        #region Test Identity Not Null:
                        if (user == null)
                        {
                            // False:
                            _level = false;
                        }
                        #endregion
                        else
                        {
                            #region Ensure Security Role:
                            if (!(role.IsInRole(WindowsBuiltInRole.Administrator)))
                            {
                                // False:
                                _level = false;
                            }
                            else
                            {
                                // True:
                                _level = true;
                            }
                            #endregion
                 } 
          }
    } 
    

    类似的东西可以让您针对 UAC 进行测试,然后执行任务。我不太确定您为什么要禁用 UAC,但这是我的方法。

    希望对您有所帮助。

    【讨论】:

    • 感谢您的回答,它不依赖于任务计划程序,但目前尚不清楚如何在主应用程序中使用此类!
    【解决方案3】:

    如果您想绕过作为标准用户运行所获得的保护,那么更好的解决方案是更改文件夹和注册表项的权限,以便允许所有用户修改您的应用程序的文件夹。

    GrantAllUsersFullControlToFileOrFolder("C:\Program Files\Grobtastic");
    

    使用以下伪代码实现:

    void  GrantAllUsersFullControlToFileOrFolder(String path)
    {
        PACL oldDACL;
        PACL newDACL;    
        PSECURITY_DESCRIPTOR sd;
    
        //Get the current DALC (Discretionary Access Control List) and Security Descriptor
        GetNamedSecurityInfo(path, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, 
              nil, nil, ref oldDACL, nil, ref sd);
    
        //Create an SID for the "Users" group
        PSID usersSid = StringToSid("S-1-5-32-545");
    
        // Initialize an EXPLICIT_ACCESS structure for the new Access Control Entry (ACE)
        EXPLICIT_ACCESS ea;
        ZeroMemory(@ea, SizeOf(EXPLICIT_ACCESS));
        ea.grfAccessPermissions  = GENERIC_ALL;
        ea.grfAccessMode         = GRANT_ACCESS;
        ea.grfInheritance        = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
        ea.Trustee.TrusteeForm   = TRUSTEE_IS_SID;
        ea.Trustee.TrusteeType   = TRUSTEE_IS_GROUP;
        ea.Trustee.ptstrName     = PChar(usersSID);
    
        // Create a new ACL that merges the new ACE into the existing ACL.
        // SetEntriesInAcl takes care of adding the ACE in the correct order in the list
        SetEntriesInAcl(1, @ea, oldDACL, ref newDACL); //use LocalFree to free returned newDACL
    
        //Attach the new ACL as the object's new DACL
        SetNamedSecurityInfo(path, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION,
              nil, nil, newDACL, nil);
    
        LocalFree(HLOCAL(sd));
        LocalFree(HLOCAL(newDACL));
        FreeSid(usersSID);
    }
    

    即使在禁用 UAC 的情况下也可以使用(即用户是标准用户,并且没有方便的方式让他们提升权限)。它也适用于没有 UAC 便利功能的 Windows XP,您必须快速切换用户才能以管理员身份运行。

    然后您将可执行文件显示为运行 asInvoker,因为您不需要管理权限。


    问问自己:

    在 Windows XP 上我会做什么?
    在禁用 UAC 的 Windows 7 上我会做什么?

    如果他们是标准用户,您的程序会死机吗?

    【讨论】:

      猜你喜欢
      • 2010-10-15
      • 2014-12-02
      • 2012-05-14
      • 2021-07-20
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      相关资源
      最近更新 更多