Remotely start applications with WMI & bypass Code Access Security
This article assumes that the reader has an understanding of how to make queries to WMI, has administrative rights on the remote machine & can create a windows service. I am not going to contain any code but I will try to include links to sites that can provide help.
Reasoning (the story)
I needed to install a network monitoring agent on all of our servers. To do this I was using WMI to start a .exe that started and monitored the installation. My application had to check environment variables. Which then caused it to fail because of Code Access Security, even though I had full administrative rights. You can change the systems security policy to allows your program to run if you are allowed, but I figured that it would be more work to get all of the security settings changed. So I came up with a new plan to install a windows service through the Win32_Service Namespace in WMI. So I change the application to a sindows service and it could read the environment variables just fine. Then I removed the windows service through the same WMI Namespace. (I plan on making it a client/server where the server installs, monitors, and uninstalls the client.)
The Steps (super simplified)
- Write a windows service. (don't forget the installer)
Use this service to install applications, or start executables. Also, if you leave this on the remote machine for any period of time you greatley consider the security of it.- Creatinga windows service:
http://msdn.microsoft.com/en-us/library/aa984464(VS.71).aspx - The Installer:
http://www.montgomerysoftware.com/CreatingWindowsServiceInCSharp.aspx
- Creatinga windows service:
- Remotley install/remove the service. (I choose WMI)
- WMI Win32_Service Class:
http://msdn.microsoft.com/en-us/library/aa394418(VS.85).aspx - WMI Win32_Service.Create() method:
http://msdn.microsoft.com/en-us/library/aa389390.aspx - How to execute a WMI method:
http://msdn.microsoft.com/en-us/library/ms257364(VS.80).aspx
- WMI Win32_Service Class:
Thats about it, after your service starts on the machine it can do just about anything it wants, read system variables, start other applications, install programs...
Thanks for reading, please leave a comment to let everyone know how this helped you out.
