Monday, November 29, 2010

Enumerating Registry Entries in ASP.NET C# WMI

This snippet allows you to enumerate registry entries. Not only can you enumerate on local connections but remote machines as well. Most samples on the internet i've seen have just that limitation. Asides from  being in VB and VBS, it's also problematic when enumerating keys and subkeys on remote machines. In this particular example taken from my MobileExec 2011 program, this code successfully enumerates the esoteric registry key entailing windows uninstall. You can ideally identify the add/remove programs for any given remote computer! Here is a screenshot of my app above as well. Enjoy!



   if (words[0].ToString().ToLower() == "software")
                {
                    //MessageBox("Software");
                    ConnectionOptions co = new ConnectionOptions();
                    co.Username = txtUsername.Text;
                    co.Password = txtPassword.Text;
                    co.Impersonation = ImpersonationLevel.Impersonate;


                    co.EnablePrivileges = true;
                    co.Authentication = AuthenticationLevel.PacketPrivacy;
                  
                    System.Net.IPHostEntry h =
                System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
                    string IPAddress = h.AddressList.GetValue(0).ToString();
                    string lm = System.Net.Dns.GetHostName().ToString();




                    if (txtUsername.Text == "" || txtPassword.Text == ""
                 || txtServer.Text == "127.0.0.1"
                 || txtServer.Text.ToLower() == "localhost"
                 || txtServer.Text.ToLower() == lm.ToLower()
                 || txtServer.Text.ToLower() == IPAddress.ToLower())
                    {
                        co.Username = null;
                        co.Password = null;
                    }
                    string temp1 = txtServer.Text;
                   
                
                    Label1.ForeColor = Color.Black;
                    Label1.Text = "Sorry! Software could not be found!";


                    this.Label1.Style["text-align"] = "left";
                    
                    Label1.Text = "List of Installed Software for <b>" +
                          temp1 + 
                          "</b><br />--------------------------------------------------------<br />";


                    try
                    {
                        ManagementScope myScope = new ManagementScope("\\\\" + temp1 + "\\root\\default", co);
                        ManagementPath mypath = new ManagementPath("StdRegProv");
                         ManagementClass wmiRegistry = new ManagementClass(myScope, mypath, null);


                        const uint HKEY_LOCAL_MACHINE = unchecked((uint)0x80000002);
                        //ManagementClass wmiRegistry = new ManagementClass("root/default",
                        //"StdRegProv",null);
                        string keyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
                        object[] methodArgs = new object[] { HKEY_LOCAL_MACHINE, keyPath, null };
                        uint returnValue = (uint)wmiRegistry.InvokeMethod("EnumKey", methodArgs);
                        Console.WriteLine("Executing EnumKey() returns: {0}", returnValue);
                        if (null != methodArgs[2])
                        {
                            string[] subKeys = methodArgs[2] as String[];
                            if (subKeys == null) return;
                            ManagementBaseObject inParam =
                            wmiRegistry.GetMethodParameters("GetStringValue");
                            inParam["hDefKey"] = HKEY_LOCAL_MACHINE;
                            string keyName = "";


                            foreach (string subKey in subKeys)
                            {
                                //Display application name
                                keyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" +
                                subKey;
                                keyName = "DisplayName";
                                inParam["sSubKeyName"] = keyPath;
                                inParam["sValueName"] = keyName;
                                ManagementBaseObject outParam =
                                wmiRegistry.InvokeMethod("GetStringValue", inParam, null);


                                if ((uint)outParam["ReturnValue"] == 0)
                                    Label1.Text += outParam["sValue"].ToString() + "<br />";
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox("Software: " + ex.Message);


                    }
                          
                    
                }
                //end of software
             

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...