Exemplo n.º 1
0
        internal IKeyImpl ReleaseKeyImpl()
        {
            IKeyImpl result = keyImpl_;

            keyImpl_ = null;
            return(result);
        }
        public ucDataConnectionEditor(IBaseNode node)
        {
            InitializeComponent();

            baseNode      = node;
            lblTitle.Text = node.Name;

            string UserAppFolder = ConfigurationRepository.IConfiguration.GetConfigurationValue("UserAppDataPath", "");

            rootPath = string.Format("{0}{1}-{2}-config.xml", UserAppFolder,
                                     node.Parent.Name.ToLower(),
                                     node.Name.ToLower());

            conf.AddConfigurationSource(
                new IConfigurationSourceImpl("UserAppDataPath", Constants.ConfigurationSource, rootPath));

            string keyId = string.Format("{0}-{1}", node.Parent.Name.ToLower(), node.Name.ToLower());
            IKey   key   = new IKeyImpl(keyId);

            key.keyBytes = KeyUtils.GetBytes(node.Reference.Replace("-", ""));
            conf.IKms.AddKey(key);

            IConfigurationItem item = new IConfigurationItemImpl("secret", "", Constants.ConfigurationSource);

            item.StorageKey = keyId;
            conf.AddConfigurationItem(item);

            string plain = item.GetValue("");

            log.Debug(plain);
        }
Exemplo n.º 3
0
 private bool TryOpenOrCreateDiffKey(KeyIdentity existingBase, KeyIdentity identity,
                                     KeySecurity samDesired, out IKeyImpl openedImpl)
 {
     if (TryOpen(existingBase, KeyDisposition.DIFF_HIVE, identity, samDesired, out openedImpl))
     {
         return(true);
     }
     // If some modification operation is requested and offreg key
     // does not exist, but it is in one of readonly hives - creating it
     if (samDesired.IsOnlyForRead() ||
         !KeyExistsInROHives(identity))
     {
         return(false);
     }
     try
     {
         openedImpl = Create(identity,
                             null, (int)Win32Api.RegOption.REG_OPTION_NON_VOLATILE, IntPtr.Zero,
                             IntPtr.Zero);
         return(true);
     }
     catch (FileNotFoundException)
     {
         return(false);
     }
 }
Exemplo n.º 4
0
        // existingBase is passed for performance optimization
        // TODO: encapsulate existingBase in a class and extract methods
        // with it a first argument there
        private bool TryOpenImpl(KeyIdentity existingBase, KeyDisposition disposition,
                                 KeyIdentity identity, KeySecurity samDesired, out IKeyImpl openedImpl)
        {
            openedImpl = null;
            if (disposition != KeyDisposition.DIFF_HIVE)
            {
                if (diffHive_.IsMarkedAsDeleted(existingBase, identity))
                {
                    return(false);
                }
            }
            IKeyImplOpenFactory factory = null;

            switch (disposition)
            {
            case KeyDisposition.WINDOWS_REG:
                factory = windowsKeyFactory_;
                break;

            case KeyDisposition.BASE_HIVE:
                factory = baseHive_;
                break;

            case KeyDisposition.DIFF_HIVE:
                factory = diffHive_;
                break;
            }
            if (factory == null)
            {
                // In case if specified hive is not used (currently used
                // only for null baseHive_)
                return(false);
            }
            return(factory.TryOpen(identity, samDesired, out openedImpl));
        }
Exemplo n.º 5
0
        protected void InitializeKMS()
        {
            log.Debug("Creating system key");
            NetworkInterface nic = null;

            foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (adapter.Name == "LAN-verbinding")
                {
                    nic = adapter;
                    break;
                }
            }

            if (nic != null)
            {
                IKey key = new IKeyImpl("system");
                if (key.KeyValueFromGuid(nic.Id))
                {
                    Kms.AddKey(key);
                }

                log.Debug("System key created");
                return;
            }

            log.Debug("System key not created");
        }
Exemplo n.º 6
0
        public bool TryOpen(KeyIdentity identity, KeySecurity samDesired, out IKeyImpl openedImpl)
        {
            WindowsKey windowsKey;
            bool       result = WindowsKey.TryOpen(identity, samDesired, out windowsKey);

            openedImpl = windowsKey;
            return(result);
        }
Exemplo n.º 7
0
        public bool TryOpen(KeyIdentity identity, KeySecurity samDesired, out IKeyImpl openedImpl)
        {
            OffRegKey offregKey;
            bool      result = OffRegKey.TryOpen(this, hive_, identity, out offregKey);

            openedImpl = offregKey;
            return(result);
        }
Exemplo n.º 8
0
 internal VirtualKey(VirtualKeyFactory opener, KeyIdentity identity,
                     IKeyImpl baseKeyImpl, DataAlterer alterer)
 {
     opener_   = opener;
     identity_ = identity;
     baseImpl_ = baseKeyImpl;
     alterer_  = alterer;
     ReinitializeCache();
 }
Exemplo n.º 9
0
 // returns false if the key does not exist in the given disposition,
 // on other errors throws exception.
 private bool TryOpen(KeyIdentity existingBase, KeyDisposition disposition, KeyIdentity identity,
                      KeySecurity samDesired, out IKeyImpl openedImpl)
 {
     if (!TryOpenImpl(existingBase, disposition, identity, samDesired, out openedImpl))
     {
         return(false);
     }
     baseKeyManager_.BaseKeyCopied(identity.BaseKey);
     openedImpl = new KeyImplDecoratorHookingClose(openedImpl,
                                                   () => baseKeyManager_.BaseKeyClosed(identity.BaseKey));
     return(true);
 }
Exemplo n.º 10
0
 protected /*virtual*/ override void DisposeUnmanaged()
 {
     if (keyImpl_ != null)
     {
         // Ignoring errors such as closing a predefined key
         try
         {
             keyImpl_.Close();
         }
         catch { }
         keyImpl_ = null;
     }
 }
Exemplo n.º 11
0
        private bool TryGetSubKey(KeyDisposition disposition, string lpSubKey,
                                  KeySecurity samDesired, Win32Api.RegWow64Options wowOptions, out KeyImplHolder subKeyHolder)
        {
            KeyIdentity subKeyIdentity = new KeyIdentity(identity_, wowOptions, lpSubKey);
            IKeyImpl    keyImpl        = cachedKeyImpls_.TryGet(subKeyIdentity, disposition, samDesired);

            if (keyImpl != null)
            {
                subKeyHolder = new NotOwningKeyImplHolder(keyImpl);
                return(true);
            }
            KeyImplHolder newOne;

            if (!opener_.TryOpenHolder(identity_, disposition, subKeyIdentity, samDesired, out newOne))
            {
                subKeyHolder = null;
                return(false);
            }
            subKeyHolder = cachedKeyImpls_.Add(subKeyIdentity, disposition, newOne);
            return(true);
        }
Exemplo n.º 12
0
        internal VirtualKey CreateKey(KeyIdentity identity, string lpClass,
                                      Win32Api.RegOption dwOptions, IntPtr lpSecurityAttributes)
        {
            // Security attributes and security descriptor are not the same
            IntPtr securityDescriptor = IntPtr.Zero;

            if (lpSecurityAttributes != IntPtr.Zero)
            {
                Win32Api.SECURITY_ATTRIBUTES securityAttributes =
                    (Win32Api.SECURITY_ATTRIBUTES)Marshal.PtrToStructure(
                        lpSecurityAttributes, typeof(Win32Api.SECURITY_ATTRIBUTES));
                if (securityAttributes.nLength >=
                    Marshal.SizeOf(typeof(UInt32)) + Marshal.SizeOf(typeof(IntPtr)))
                {
                    securityDescriptor = securityAttributes.lpSecurityDescriptor;
                }
            }
            IKeyImpl registryKey = Create(identity, lpClass, dwOptions,
                                          securityDescriptor, IntPtr.Zero);

            return(new VirtualKey(this, identity, registryKey, alterer_));
        }
Exemplo n.º 13
0
 internal bool TryAlterData(KeyIdentity key, string valueName,
                            IKeyImpl keyImpl, IntPtr pdwType, Data dst, DataTransformer.Operation operation)
 {
     // TODO: this is a specific check that
     // rewrite is done only for values in sandboxie reghive which is BASE_HIVE
     // and if values are copied, DIFF_HIVE is also supported
     // It should be put outside this generalized class
     if (keyImpl.GetDisposition() == KeyDisposition.WINDOWS_REG)
     {
         return(Win32Exception.CheckIfFoundAndNoError(operation(pdwType, dst)));
     }
     return(DataTransformer.TryAlterData(pdwType, dst, operation,
                                         (result, type) =>
     {
         DebugLogger.WriteLine(@"AlterData {0} {1}\{2} ({3}) Result: {4}",
                               keyImpl.GetDisposition(), key.ToString(), valueName, type, (int)result);
         return result == Win32Api.Error.ERROR_SUCCESS &&
         DataTransformer.IsStringType(type);
     },
                                         (type, pSrcData, cbSrcData) =>
                                         DataTransformer.TransformStringValue(
                                             type, pSrcData, cbSrcData, dst, str => CallStrAlterers(key, valueName, str),
                                             HookBarrier.IsLastInjectedFuncAnsi ? StringFormat.Ansi : StringFormat.Unicode)));
 }
Exemplo n.º 14
0
//        private string debugCreationCallStack_;

        internal KeyImplHolder(IKeyImpl keyImpl)
        {
            keyImpl_ = keyImpl;
//            debugCreationCallStack_ = Environment.StackTrace;
        }
Exemplo n.º 15
0
        private DoesntExistOrMarkedAsDeletedState DoesntExistOrMarkedAsDeleted(KeyIdentity existingBase, KeyIdentity identity,
                                                                               IntPtr allocatedpData, IntPtr allocatedpcbData)
        {
            string[] subkeys = identity.SplitPath();
            int      existingLevels, nonRemovableLevels;

            CalcNonRemovableLevels(existingBase, identity, out existingLevels, out nonRemovableLevels);
            if (subkeys.Length <= nonRemovableLevels)
            {
                return(DoesntExistOrMarkedAsDeletedState.Exists);
            }

            for (int level = existingLevels; level < subkeys.Length; level++)
            {
                KeyIdentity   curLevelIdentity = KeyIdentity.Build(identity.BaseKey, subkeys, level);
                KeyImplHolder holder;
                lock (this)
                {
                    IKeyImpl keyImpl = null;

                    if (level <= nonRemovableLevels)
                    {
                        keyImpl = cacheForDeletedMarks_.TryGet(curLevelIdentity, KeyDisposition.DIFF_HIVE, cacheSecurity_);
                    }
                    if (keyImpl != null)
                    {
                        holder = new NotOwningKeyImplHolder(keyImpl);
                    }
                    else
                    {
                        OffregLib.OffregKey   key;
                        OffregLib.Win32Result result;
                        if (!hive_.Root.TryOpenSubKey(OffRegKey.GetMainOffRegPath(curLevelIdentity), out key, out result))
                        {
                            return(DoesntExistOrMarkedAsDeletedState.DoesntExist);
                        }
                        holder = new KeyImplHolder(new OffRegKey(this, key, curLevelIdentity));
                        if (level <= nonRemovableLevels)
                        {
                            // Adding to cache only the lowest, non removable level
                            holder = cacheForDeletedMarks_.Add(
                                curLevelIdentity, KeyDisposition.DIFF_HIVE,
                                holder);
                        }
                    }
                }
                using (holder)
                {
                    Marshal.WriteInt32(allocatedpcbData, Marshal.SizeOf(typeof(int)));
                    if (holder.GetKeyImpl().TryQueryValue(
                            DELETED_PREFIX + subkeys[level],
                            IntPtr.Zero, IntPtr.Zero, allocatedpData, allocatedpcbData))
                    {
                        if (Marshal.ReadInt32(allocatedpcbData) == Marshal.SizeOf(typeof(int)) &&
                            Marshal.ReadInt32(allocatedpData) != 0)
                        {
                            // There is a special value marking key as deleted
                            return(DoesntExistOrMarkedAsDeletedState.MarkedAsDeleted);
                        }
                    }
                }
            }
            return(DoesntExistOrMarkedAsDeletedState.Exists);
        }
Exemplo n.º 16
0
 internal KeyImplDecoratorHookingClose(IKeyImpl decorated, Callback callback)
 {
     decorated_ = decorated;
     callback_  = callback;
 }
Exemplo n.º 17
0
 private bool TryAlterData(string subKey, string value, IKeyImpl keyImpl,
                           IntPtr pdwType, Data dst, DataTransformer.Operation operation)
 {
     return(alterer_.TryAlterData(new KeyIdentity(identity_, Win32Api.RegWow64Options.None, subKey),
                                  value, keyImpl, pdwType, dst, operation));
 }
Exemplo n.º 18
0
 internal NotOwningKeyImplHolder(IKeyImpl keyImpl)
     : base(keyImpl)
 {
 }