Exemple #1
0
        private byte[] GetLocalStorageKey()
        {
            if (!NativeWinApiHelper.OpenProcessToken(NativeWinApiHelper.GetCurrentProcess(), NativeWinApiHelper.TokenAccess.TOKEN_QUERY, out var hToken))
            {
                return(null);
            }

            NativeWinApiHelper.GetTokenInformation(hToken, NativeWinApiHelper.TOKEN_INFORMATION_CLASS.TokenUser, IntPtr.Zero, 0, out int dwBufferSize);
            if (dwBufferSize == 0)
            {
                NativeWinApiHelper.CloseHandle(hToken);
                return(null);
            }

            IntPtr userToken = Marshal.AllocHGlobal(dwBufferSize);

            if (!NativeWinApiHelper.GetTokenInformation(hToken, NativeWinApiHelper.TOKEN_INFORMATION_CLASS.TokenUser, userToken, dwBufferSize, out var dwInfoBufferSize))
            {
                NativeWinApiHelper.CloseHandle(hToken);
                Marshal.FreeHGlobal(userToken);
                return(null);
            }

            var userStruct = (NativeWinApiHelper.TOKEN_USER)Marshal.PtrToStructure(userToken, typeof(NativeWinApiHelper.TOKEN_USER));

            int dwLength = NativeWinApiHelper.GetLengthSid(userStruct.User.Sid);

            byte[] result = new byte[dwLength];
            Marshal.Copy(userStruct.User.Sid, result, 0, dwLength);
            NativeWinApiHelper.CloseHandle(hToken);
            Marshal.FreeHGlobal(userToken);
            return(result);
        }
Exemple #2
0
        private string Decrypt(string key, string value, string group)
        {
            byte[] k      = Encoding.ASCII.GetBytes(key);
            byte[] xValue = XOR(k, Convert.FromBase64String(value));
            byte[] xKey   = XOR(k, Encoding.ASCII.GetBytes(group));

            IntPtr xValuePtr = Marshal.AllocHGlobal(xValue.Length);

            Marshal.Copy(xValue, 0, xValuePtr, xValue.Length);
            IntPtr xKeyPtr = Marshal.AllocHGlobal(xKey.Length);

            Marshal.Copy(xKey, 0, xKeyPtr, xKey.Length);

            NativeWinApiHelper.CRYPTOAPI_BLOB dataIn  = new NativeWinApiHelper.CRYPTOAPI_BLOB();
            NativeWinApiHelper.CRYPTOAPI_BLOB entropy = new NativeWinApiHelper.CRYPTOAPI_BLOB();
            dataIn.pbData  = xValuePtr;
            dataIn.cbData  = (uint)xValue.Length;
            entropy.pbData = xKeyPtr;
            entropy.cbData = (uint)xKey.Length;

            if (!NativeWinApiHelper.CryptUnprotectData(dataIn, null, entropy, IntPtr.Zero, IntPtr.Zero, 0, out var dataOut))
            {
                Marshal.FreeHGlobal(xValuePtr);
                Marshal.FreeHGlobal(xKeyPtr);
                return(null);
            }

            byte[] managedArray = new byte[dataOut.cbData];
            Marshal.Copy(dataOut.pbData, managedArray, 0, (int)dataOut.cbData);
            byte[] xDecrypted = XOR(k, managedArray);
            Marshal.FreeHGlobal(dataOut.pbData);
            Marshal.FreeHGlobal(xValuePtr);
            Marshal.FreeHGlobal(xKeyPtr);
            return(Encoding.UTF8.GetString(xDecrypted));
        }
Exemple #3
0
        public static async Task <bool> CheckDefaultZipApp(string filePath)
        {
            Func <Task <bool> > queryFileAssoc = async() =>
            {
                var assoc = await NativeWinApiHelper.GetFileAssociationAsync(filePath);

                if (assoc != null)
                {
                    IsDefaultZipApp = assoc == Package.Current.Id.FamilyName ||
                                      assoc.Equals(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "explorer.exe"), StringComparison.OrdinalIgnoreCase);
                }
                return(true);
            };

            return(IsDefaultZipApp ?? await queryFileAssoc());
        }