DESKTOP

Windows 7 : ADDING UAC SUPPORT TO YOUR APPLICATION (part 1) - Creating a Manifest - Creating the Application Code

1/27/2014 1:35:12 AM

The .NET Framework is a little behind the curve when it comes to the UAC. This is surprising considering how hard Microsoft is pushing people to rely on the UAC as a security measure. Currently, you can't request privilege elevation in the .NET Framework for a thread or other useful work element. In fact, you have only two choices:

  • Elevate the application to administrator level as a whole using a manifest.

  • Call an external application element and elevate it as part of the calling process.

The following sections describe how to create an application that provides UAC support within the limits that the .NET Framework currently allows. In many cases, you'll have to make some decisions on how to break your application into pieces using the current strategies. Microsoft will probably make it possible eventually to perform in-place privilege elevations or allow some method of creating a thread that will support privilege elevation.

1. Creating a Manifest

The first technique for working with the UAC is to elevate your entire application to administrator level. You should use this approach only when the application is designed exclusively for administrator use, won't see any use outside the network, and is extremely focused — performing just one or two small tasks. Using this technique to elevate the privileges of an application that should normally run at a lower level will leave huge security holes in your system. The following sections describe how to work with this kind of application.

1.1. Defining the Manifest Application

The technique shown in this section will work with any .NET application. However, the example relies on a Windows Forms template application that has a single button, btnAudit, for testing purposes. The example doesn't require any additional references, but you do need to add the following using statements:

using System.Security.AccessControl;
using System.Security.Principal;
using System.IO;

1.2. Adding the Manifest

The <requestedExecutionLevel> tag accepts two inputs. The first, level, describes what type of execution you need. When working with the UAC, you always request requireAdministrator. However, there are situations where using highestAvailable may work for specific needs. Microsoft terms these scenarios as mixed mode, where a standard user and administrator work with the application. An article at http://msdn.microsoft.com/library/bb756929.aspx describes how these settings react when users have certain settings enabled on their systems.

The second setting, uiAccess, gives the application access to other windows on the desktop. Normally, the application can't drive output to other windows. Microsoft recommends setting this value to false normally. The only exception that Microsoft makes is for applications with accessibility (special needs) features. For example, an application might need to send output to an on-screen keyboard. There are probably other situations where you'll need to set this value to true, but doing so opens a potential security hole. Updating a simple status indicator probably isn't a good use for this feature — the security risk outweighs the benefit of the update.

1.3. Creating the Application Code

The example application adds or removes auditing from a test file, Test.TXT, that appears in the output folder. In this case, the application adds or removes auditing for the write right of the BUILTIN\Users account. The event fires when the user fails to write to the file. This application relies on a simple toggle mechanism to perform its task after detecting the audit state of the file. Listing 1 shows the code you need for this example.

Example 1. Performing an audit of actions on a file or directory
private void btnAudit_Click(object sender, EventArgs e)
{
// Create a file security object for the target file.
FileSecurity FS = File.GetAccessControl(
Application.StartupPath + @"\Temp.TXT",
AccessControlSections.Audit);

// Create a new rule.
FileSystemAuditRule Rule = new FileSystemAuditRule(
new NTAccount(@"BUILTIN\Users"),
FileSystemRights.Write,
AuditFlags.Failure);
// Obtain a list of the existing rules.
AuthorizationRuleCollection AuditRules =
FS.GetAuditRules(true, true,
typeof(NTAccount));

// Check for the existence of the rule in the collection.
Boolean FoundIt = false;
foreach (FileSystemAuditRule AR in AuditRules)

// Look for the rule.
if ((AR.IdentityReference == Rule.IdentityReference) &&
(AR.FileSystemRights.HasFlag(FileSystemRights.Write)) &&
(AR.AuditFlags.HasFlag(AuditFlags.Failure)))
{
// Set FoundIt appropriately.
FoundIt = true;

// Exit the loop.
break;
}

// Add or remove the rule as appropriate.
if (FoundIt)

// Remove the rule from the file security object.
FS.RemoveAuditRule(Rule);

else


// Add the rule to the file security object.
FS.AddAuditRule(Rule);

// Save the rule to the file.
File.SetAccessControl(
Application.StartupPath + @"\Temp.TXT", FS);

// Display a success message.
MessageBox.Show("Change Succeeded!");
}

Other  
  •  Parallels Desktop 9 For Mac - The Best Of Both Worlds
  •  Windows Server 2008 and Windows Vista : Benefits of Group Policy Preferences (part 2) - Working with Any Organizational Unit Design
  •  Windows Server 2008 and Windows Vista : Benefits of Group Policy Preferences (part 1) - User-Friendly Interface
  •  Windows Server 2008 and Windows Vista : Creating Custom ADMX and ADML Files (part 4) - Using ADMX File Language
  •  Windows Server 2008 and Windows Vista : Creating Custom ADMX and ADML Files (part 3) - Core ADMX File Concepts
  •  Windows Server 2008 and Windows Vista : Creating Custom ADMX and ADML Files (part 2) - Core ADMX File Concepts
  •  Windows Server 2008 and Windows Vista : Creating Custom ADMX and ADML Files (part 1) - ADMX Schema , ADMX File Structure , ADML File Structure
  •  Windows 7 : Custom Libraries and Saved Searches (part 2) - Using Saved Searches
  •  Windows 7 : Custom Libraries and Saved Searches (part 1) - Creating Custom Libraries
  •  Windows 7 : Visualization and Organization - How to Make the Windows Shell Work for You - The Organizational Advantage of Libraries
  •  
    Top 10
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
    OPEL MERIVA : Making a grand entrance
    FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
    BMW 650i COUPE : Sexy retooling of BMW's 6-series
    BMW 120d; M135i - Finely tuned
    PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
    PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS