Example #1
0
        public static uint ApiRegSetKeySecurity(RegistryHive hive,
                                                string _sObjectname,
                                                IntPtr pSecurityDescriptor)
        {
            uint iRet = 0;

            Logger.Log(string.Format("RegistryInteropWrapperWindows.ApiRegSetKeySecurity() is called", Logger.LogLevel.Verbose));

            IntPtr hKey = (IntPtr)0, phSubKey = (IntPtr)0; IntPtr hProv = (IntPtr)0;

            if ((RegistryInteropWindows.RegConnectRegistry(RegistryInteropWrapperWindows.sHostName, hive, out hKey)) == 0)
            {
                try
                {
                    iRet = (uint)RegistryInteropWindows.RegOpenKeyEx(
                        hKey,
                        _sObjectname,
                        0,
                        (uint)(RegistryApi.RegSAM.Execute),
                        out phSubKey);

                    iRet = RegistryInteropWindows.RegSetKeySecurity(phSubKey,
                                                                    SecurityDescriptorApi.SECURITY_INFORMATION.DACL_SECURITY_INFORMATION |
                                                                    SecurityDescriptorApi.SECURITY_INFORMATION.PROTECTED_DACL_SECURITY_INFORMATION |
                                                                    SecurityDescriptorApi.SECURITY_INFORMATION.UNPROTECTED_DACL_SECURITY_INFORMATION,
                                                                    //SecurityDescriptorApi.SECURITY_INFORMATION.SACL_SECURITY_INFORMATION, //Commented this since the Api is returning the Access denied error code=5
                                                                    pSecurityDescriptor);
                }
                catch (Exception ex) { Logger.LogException("RegistryInteropWrapperWindows.ApiRegSetKeySecurity()", ex); }
                finally
                {
                    if ((int)phSubKey > 0)
                    {
                        // Attempt to dispose of key
                        RegistryInteropWindows.RegCloseKey(phSubKey);
                    }

                    if ((int)hKey > 0)
                    {
                        // Attempt to dispose of hive
                        RegistryInteropWindows.RegCloseKey(hKey);
                    }

                    if ((int)pSecurityDescriptor > 0)
                    {
                        // Attempt to dispose of hive
                        SecurityDescriptorApi.CloseHandle(pSecurityDescriptor);
                    }
                }
            }

            return(iRet);
        }
Example #2
0
        public static object RegGetValue(RegistryHive hive, string key, string sValue, out int type)
        {
            type = 0;
            if (string.IsNullOrEmpty(sHostName))
            {
                return(null);
            }

            const int ErrorMoreDataIsAvailable = 234;

            byte[] buffer = null;
            int    size = 0;
            IntPtr hKey = (IntPtr)0, hSubKey = (IntPtr)0, data = (IntPtr)0;

            if ((RegistryInteropWindows.RegConnectRegistry(RegistryInteropWrapperWindows.sHostName, hive, out hKey)) == 0)
            {
                try
                {
                    if ((RegistryInteropWindows.RegOpenKey(hKey, key, out hSubKey)) == 0)
                    {
                        buffer = new byte[512];
                        size   = buffer.Length;
                        if ((RegistryInteropWindows.RegQueryValueEx(hSubKey, sValue, 0, out type, buffer, ref size)) == ErrorMoreDataIsAvailable)
                        {
                            // Resize buffer and perform query again
                            Array.Resize <byte>(ref buffer, size);
                            size = buffer.Length;
                            RegistryInteropWindows.RegQueryValueEx(hSubKey, sValue, 0, out type, buffer, ref size);
                        }
                    }
                }
                finally
                {
                    if ((int)hSubKey > 0)
                    {
                        // Attempt to dispose of key
                        RegistryInteropWindows.RegCloseKey(hSubKey);
                    }

                    if ((int)hKey > 0)
                    {
                        // Attempt to dispose of hive
                        RegistryInteropWindows.RegCloseKey(hKey);
                    }
                }
            }

            return(buffer);
        }
Example #3
0
        public static int RegSetValue(RegistryHive hive, string key, string sValue, object data)
        {
            int    ret = -1;
            int    cData;
            IntPtr hKey = (IntPtr)0, phSubKey = (IntPtr)0;

            if ((RegistryInteropWindows.RegConnectRegistry(RegistryInteropWrapperWindows.sHostName, hive, out hKey)) == 0)
            {
                try
                {
                    if ((RegistryInteropWindows.RegOpenKey(hKey, key, out phSubKey)) == 0)
                    {
                        byte[] buffer = data as byte[];
                        cData = buffer.Length;
                        ret   = RegistryInteropWindows.RegSetValueEx(phSubKey, sValue, 0,
                                                                     RegistryValueKind.Unknown, buffer, cData);
                    }
                }
                finally
                {
                    if ((int)phSubKey > 0)
                    {
                        // Attempt to dispose of key
                        RegistryInteropWindows.RegCloseKey(phSubKey);
                    }

                    if ((int)hKey > 0)
                    {
                        // Attempt to dispose of hive
                        RegistryInteropWindows.RegCloseKey(hKey);
                    }
                }

                return(ret);
            }

            return(ret);
        }
Example #4
0
        public static bool HandleClose(IntPtr tokenHandle)
        {
            if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows)
            {
                if (impersonatedUser != null)
                {
                    impersonatedUser.Undo();
                }

                if (tokenHandle != IntPtr.Zero)
                {
                    bool iResult = RegistryInteropWindows.CloseHandle(tokenHandle);
                    if (!iResult)
                    {
                        tokenHandle = IntPtr.Zero;
                        int ret = Marshal.GetLastWin32Error();
                        Logger.Log("RegistryInteropWrapperWindows.RegLogonUser ret = {0}" + ret.ToString());
                    }
                    return(iResult);
                }
            }

            return(true);
        }
Example #5
0
        public static IntPtr ApiRegGetKeySecurity(RegistryHive hive, string _sObjectname)
        {
            uint iRet = 0;

            Logger.Log(string.Format("RegistryInteropWrapperWindows.ApiRegGetKeySecurity(_sObjectname = {0})", _sObjectname), Logger.LogLevel.Verbose);

            IntPtr hKey = (IntPtr)0, phSubKey = (IntPtr)0; IntPtr hProv = (IntPtr)0;
            IntPtr pSecurityDescriptor    = IntPtr.Zero;
            IntPtr pProcessHandle         = IntPtr.Zero;
            ulong  lpcbSecurityDescriptor = 0;

            if ((RegistryInteropWindows.RegConnectRegistry(RegistryInteropWrapperWindows.sHostName, hive, out hKey)) == 0)
            {
                try
                {
                    iRet = SecurityDescriptorWrapper.ApiGetCurrentProcessHandle(
                        SecurityDescriptorApi.TOKEN_ALL_ACCESS,
                        out pProcessHandle);

                    iRet = (uint)RegistryInteropWindows.RegOpenKeyEx(
                        hKey,
                        _sObjectname,
                        0,
                        (uint)(RegistryApi.RegSAM.AllAccess),
                        out phSubKey);
                    SecurityDescriptorWrapper.ApiGetHandleToCSP(_sObjectname, out hProv);

                    if ((iRet) == 0)
                    {
                        iRet = RegistryInteropWindows.RegGetKeySecurity(phSubKey,
                                                                        SecurityDescriptorApi.SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION |
                                                                        SecurityDescriptorApi.SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION |
                                                                        SecurityDescriptorApi.SECURITY_INFORMATION.DACL_SECURITY_INFORMATION,
                                                                        //SecurityDescriptorApi.SECURITY_INFORMATION.SACL_SECURITY_INFORMATION, //Commented this since the Api is returning the Access denied error code=5
                                                                        IntPtr.Zero,
                                                                        ref lpcbSecurityDescriptor);

                        if (iRet == (uint)122) //Insufficient buffer
                        {
                            pSecurityDescriptor = Marshal.AllocHGlobal((int)lpcbSecurityDescriptor);
                            iRet = RegistryInteropWindows.RegGetKeySecurity(phSubKey,
                                                                            SecurityDescriptorApi.SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION |
                                                                            SecurityDescriptorApi.SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION |
                                                                            SecurityDescriptorApi.SECURITY_INFORMATION.DACL_SECURITY_INFORMATION,
                                                                            //SecurityDescriptorApi.SECURITY_INFORMATION.SACL_SECURITY_INFORMATION,
                                                                            pSecurityDescriptor,
                                                                            ref lpcbSecurityDescriptor);
                        }
                        SecurityDescriptor.objectType = SecurityDescriptorApi.SE_OBJECT_TYPE.SE_REGISTRY_KEY;
                        if (iRet != 0)
                        {
                            Logger.Log(string.Format("RegistryInteropWrapperWindows.ApiRegGetKeySecurity returns error code; " + iRet), Logger.LogLevel.Verbose);
                            return(IntPtr.Zero);
                        }
                    }
                }
                catch (Exception ex) { Logger.LogException("RegistryInteropWrapperWindows.ApiRegGetKeySecurity()", ex); }
                finally
                {
                    if ((int)phSubKey > 0)
                    {
                        // Attempt to dispose of key
                        RegistryInteropWindows.RegCloseKey(phSubKey);
                    }

                    if ((int)hKey > 0)
                    {
                        // Attempt to dispose of hive
                        RegistryInteropWindows.RegCloseKey(hKey);
                    }
                }
            }

            return(pSecurityDescriptor);
        }