Answered by:
UAC Automation (Success!)

Question
-
Hello, I need to automate UAC for testing purposes.
I have custom "test module", that is C# desktop application, that can automate UI via MSAA and UIA.
As I understand http://msdn.microsoft.com/en-us/library/ms742884.aspx and http://msdn.microsoft.com/en-us/library/aa905330.aspx topics UAC can be automated if following conditions are achieved:
- "User Account Control: Switch to the secure desktop when prompting for elevation" policy disabled (so UAC dialog can be found by GetDesktopWindows WinAPI function).
- "User Account Control: Only elevate UIAccess applications that are installed in secure locations" policy disabled (so test module can be started not only from System32 and Program files)
- Test module executable digitally signed by certificate (created by makecert.exe).
- This certificate added to Trusted Root Certification Authorities on test system.
- Test module manifest contain <requestedExecutionLevel level="requireAdministrator" uiAccess="true" />.
After I done all this, my test module starts with elevated previledges, can get UIA and/or MSAA tree of controls on UAC prompt dialog, but nothing happens on Click simulation or invoking buttons.
I believe, UAC can be automated, please help me)
- Edited by Anton Purin Wednesday, June 29, 2011 2:13 PM
Wednesday, June 29, 2011 1:23 PM
Answers
-
Yes, I found solution!
Here is step-by-step manual for C#:
A. Disable policies on test system.
- Open "Local Security Policy" on system.
- Go to "Security settings -> Local policies -> Security options".
- Disable "User Account Control: Switch to the secure desktop when prompting for elevation".
- Disable "User Account Control: Only elevate UIAccess applications that are installed in secure locations".
B. Add following manifest to your C# application:
<?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.v2"> <security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" uiAccess="true" /> </requestedPrivileges> </security> </trustInfo> </assembly>
And rebuild it.
C. Digitally sign your executable.
- Download: makecert.exe, pvk2pfx.exe, signtool.exe microsoft tools and place them in one folder with your executable.
- Start in cmd: makecert.exe -r -pe -sky signature -n CN=MyTestSertificateName -sv uacTest.pvk uacTest.cer
- Start in cmd (where 123 is password, that you input in first step): pvk2pfx -pvk uacTest.pvk -pi 123 -spc uacTest.cer -pfx uacTest.pfx -po 123 -f
- Start in cmd (where 123 is password, that you input in first step, and app.exe is your executable): signtool.exe sign /f uacTest.pfx /p 123 app.exe
D. Add certificate to Trusted Root Certification Authorities on test system.
- Open uacTest.cer (created on step C) in explorer.
- Follow steps of wizard, select Trusted Root Certification Authorities as target store.
E. Interact with UAC prompt with System.Windows.Forms.SendKeys class. By default in Yes|No UAC dialog No is active by default, so:
- SendKeys.SendWait("{LEFT}{ENTER}"); // For Yes response
- SendKeys.SendWait("{ENTER}"); // For No response
- Marked as answer by Anton Purin Wednesday, June 29, 2011 1:57 PM
Wednesday, June 29, 2011 1:55 PM
All replies
-
Yes, I found solution!
Here is step-by-step manual for C#:
A. Disable policies on test system.
- Open "Local Security Policy" on system.
- Go to "Security settings -> Local policies -> Security options".
- Disable "User Account Control: Switch to the secure desktop when prompting for elevation".
- Disable "User Account Control: Only elevate UIAccess applications that are installed in secure locations".
B. Add following manifest to your C# application:
<?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.v2"> <security> <requestedPrivileges> <requestedExecutionLevel level="requireAdministrator" uiAccess="true" /> </requestedPrivileges> </security> </trustInfo> </assembly>
And rebuild it.
C. Digitally sign your executable.
- Download: makecert.exe, pvk2pfx.exe, signtool.exe microsoft tools and place them in one folder with your executable.
- Start in cmd: makecert.exe -r -pe -sky signature -n CN=MyTestSertificateName -sv uacTest.pvk uacTest.cer
- Start in cmd (where 123 is password, that you input in first step): pvk2pfx -pvk uacTest.pvk -pi 123 -spc uacTest.cer -pfx uacTest.pfx -po 123 -f
- Start in cmd (where 123 is password, that you input in first step, and app.exe is your executable): signtool.exe sign /f uacTest.pfx /p 123 app.exe
D. Add certificate to Trusted Root Certification Authorities on test system.
- Open uacTest.cer (created on step C) in explorer.
- Follow steps of wizard, select Trusted Root Certification Authorities as target store.
E. Interact with UAC prompt with System.Windows.Forms.SendKeys class. By default in Yes|No UAC dialog No is active by default, so:
- SendKeys.SendWait("{LEFT}{ENTER}"); // For Yes response
- SendKeys.SendWait("{ENTER}"); // For No response
- Marked as answer by Anton Purin Wednesday, June 29, 2011 1:57 PM
Wednesday, June 29, 2011 1:55 PM -
This doesnt works for me. Done the exact same steps.
Any idea?
Tuesday, January 3, 2012 3:00 PM -
See this stackoverflow thread for a sollution
- Edited by Christoph Lindemann Thursday, January 5, 2012 8:26 AM URL
Thursday, January 5, 2012 8:26 AM