📜 ⬆️ ⬇️

UAC, let's be friends!

UAC technology is not a superfluous component of the latest Windows OS security, and users come to this thought while fighting with malware and viruses. Programmers, in turn, should correctly approach the writing of applications and take into account the presence of such a "circumstance".

image

There are a lot of articles on Habré and in general on the topic “How to disable UAC”, “How to bypass UAC”, etc. But why disconnect, because this function is useful? Why bypass, we are not intruders?
')
Need to be friends!

Below I will tell you how to do this in your application.

Manifesto


To begin with, we will consider the simplest and ugliest, in my opinion, option - editing the manifest. What is he bad? That is suitable only for those applications that always need to have administrator privileges. How will it look like? When launching your application, a user will receive a familiar window in which he will need to confirm permission for the program to perform actions with administrator privileges. And so every time you start the program. In principle, the option is acceptable for programs that run infrequently and in one instance.

Immediately I must say that in autostart (not sure that by all means, but at least through the registry) such applications cannot be placed. Windows will simply slam them at the start without showing any UAC window. Maybe in this case it makes sense to use the technology of Windows services.

So the implementation (taken from here )

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
            <requestedPrivileges>
                <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
            </requestedPrivileges>
        </security>
    </trustInfo>
</assembly>    

:

. , , , , , .



, , ( UAC). , . (, , C#):

public static bool IsAdmin()
{
	System.Security.Principal.WindowsIdentity id = System.Security.Principal.WindowsIdentity.GetCurrent();
	System.Security.Principal.WindowsPrincipal p = new System.Security.Principal.WindowsPrincipal(id);
	
	return p.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);            
}


, . ? , - , . . :

public static void RunAsAdmin(string aFileName, string anArguments)
{
	System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo();
  
	processInfo.FileName = aFileName;
	processInfo.Arguments = anArguments;
	processInfo.UseShellExecute = true;
	processInfo.Verb = "runas"; //   
            
	System.Diagnostics.Process.Start(processInfo);
}

? :
1. . , . WinAPI .NET, , . DHCP Client, ,
sc.exe start dhcp
, .

2. . , WinAPI , . , . , , ..

2. . , . . , Windows , - .

3. . , , . . - , . , StackOverflow , , .

4. . , : . ( IsAdmin()) , , , . (IsAdmin() true) .

, . UAC, «» .

, - . .


UAC .



, link-label' , UAC. , , , , .

WinForms- .

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(HandleRef hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

public static void SetButtonShield(Button btn, bool showShield)
{
    // BCM_SETSHIELD = 0x0000160C
	btn.FlatStyle = FlatStyle.System;
    SendMessage(new HandleRef(btn, btn.Handle), 0x160C, IntPtr.Zero, showShield ? new IntPtr(1) : IntPtr.Zero);
}

, WinForms WPF, . , ImageSource - , .

System.Drawing.Icon img = System.Drawing.SystemIcons.Shield;

System.Drawing.Bitmap bitmap = img.ToBitmap();
IntPtr hBitmap = bitmap.GetHbitmap();

ImageSource wpfBitmap =
	 System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
		  hBitmap, IntPtr.Zero, Int32Rect.Empty,
		  BitmapSizeOptions.FromEmptyOptions());


, .
, ( MessageBox). , . , , . .



, .



, , UAC. github.

, .

Source: https://habr.com/ru/post/185264/


All Articles