public static string FindFileWin32(string fileName)
        {
            using (MemoryAlloc data = new MemoryAlloc(0x400))
            {
                IntPtr filePart;

                int retLength = Win32.SearchPath(null, fileName, null, data.Size / 2, data, out filePart);

                if (retLength * 2 > data.Size)
                {
                    data.ResizeNew(retLength * 2);
                    retLength = Win32.SearchPath(null, fileName, null, data.Size / 2, data, out filePart);
                }

                if (retLength == 0)
                    return null;

                return data.ReadUnicodeString(0, retLength);
            }
        }
Exemple #2
0
        public static void RefreshFileNamePrefixes()
        {
            // Just create a new dictionary to avoid having to lock the existing one.
            var newPrefixes = new Dictionary <string, string>();

            for (char c = 'A'; c <= 'Z'; c++)
            {
                using (var data = new MemoryAlloc(1024))
                {
                    int length;

                    if ((length = Win32.QueryDosDevice(c.ToString() + ":", data, data.Size / 2)) > 2)
                    {
                        newPrefixes.Add(data.ReadUnicodeString(0, length - 2), c.ToString() + ":");
                    }
                }
            }

            _fileNamePrefixes = newPrefixes;
        }
Exemple #3
0
        public static string FindFileWin32(string fileName)
        {
            using (MemoryAlloc data = new MemoryAlloc(0x400))
            {
                IntPtr filePart;

                int retLength = Win32.SearchPath(null, fileName, null, data.Size / 2, data, out filePart);

                if (retLength * 2 > data.Size)
                {
                    data.ResizeNew(retLength * 2);
                    retLength = Win32.SearchPath(null, fileName, null, data.Size / 2, data, out filePart);
                }

                if (retLength == 0)
                {
                    return(null);
                }

                return(data.ReadUnicodeString(0, retLength));
            }
        }
        public static void RefreshFileNamePrefixes()
        {
            if (_fileNamePrefixes == null)
            {
                // Just create a new dictionary to avoid having to lock the existing one.
                _fileNamePrefixes = new Dictionary<string, string>();

                for (char c = 'A'; c <= 'Z'; c++)
                {
                    using (MemoryAlloc data = new MemoryAlloc(1024))
                    {
                        int length;

                        if ((length = Win32.QueryDosDevice(c.ToString() + ":", data, data.Size/2)) > 2)
                        {
                            _fileNamePrefixes.Add(data.ReadUnicodeString(0, length - 2), c.ToString() + ":");
                        }
                    }
                }
            }
        }
        private static string GetSignerNameFromStateData(IntPtr stateData)
        {
            // Well, here's a shitload of indirection for you...

            // 1. State data -> Provider data
            IntPtr provData = Win32.WTHelperProvDataFromStateData(stateData);

            if (provData == IntPtr.Zero)
                return null;

            // 2. Provider data -> Provider signer
            IntPtr signerInfo = Win32.WTHelperGetProvSignerFromChain(provData, 0, false, 0);

            if (signerInfo == IntPtr.Zero)
                return null;

            CryptProviderSgnr sngr = (CryptProviderSgnr)Marshal.PtrToStructure(signerInfo, typeof(CryptProviderSgnr));

            if (sngr.CertChain == IntPtr.Zero)
                return null;
            if (sngr.CertChainCount == 0)
                return null;

            // 3. Provider signer -> Provider cert
            CryptProviderCert cert = (CryptProviderCert)Marshal.PtrToStructure(sngr.CertChain, typeof(CryptProviderCert));

            if (cert.Cert == IntPtr.Zero)
                return null;

            // 4. Provider cert -> Cert context
            CertContext context = (CertContext)Marshal.PtrToStructure(cert.Cert, typeof(CertContext));

            if (context.CertInfo != IntPtr.Zero)
            {
                // 5. Cert context -> Cert info
                CertInfo certInfo = (CertInfo)Marshal.PtrToStructure(context.CertInfo, typeof(CertInfo));

                unsafe
                {
                    using (MemoryAlloc buffer = new MemoryAlloc(0x200))
                    {
                        int length;

                        // 6. Cert info subject -> Subject X.500 string

                        length = Win32.CertNameToStr(
                            1,
                            new IntPtr(&certInfo.Subject),
                            3,
                            buffer,
                            buffer.Size / 2
                            );

                        if (length > buffer.Size / 2)
                        {
                            buffer.ResizeNew(length * 2);

                            length = Win32.CertNameToStr(
                                1,
                                new IntPtr(&certInfo.Subject),
                                3,
                                buffer,
                                buffer.Size / 2
                                );
                        }

                        string name = buffer.ReadUnicodeString(0);

                        // 7. Subject X.500 string -> CN or OU value

                        string value = GetX500Value(name, "CN");

                        if (string.IsNullOrEmpty(value))
                            value = GetX500Value(name, "OU");

                        return value;
                    }
                }
            }

            return null;
        }
        private static string GetReparsePointTarget(FileHandle fhandle)
        {
            using (var data = new MemoryAlloc(FileSystem.MaximumReparseDataBufferSize))
            {
                fhandle.IoControl(FileSystem.FsCtlGetReparsePoint, IntPtr.Zero, 0, data, data.Size);

                FileSystem.ReparseDataBuffer buffer = data.ReadStruct<FileSystem.ReparseDataBuffer>();

                // Make sure it is in fact a mount point.
                if (buffer.ReparseTag != (uint)IoReparseTag.MountPoint)
                    Win32.Throw(NtStatus.InvalidParameter);

                return data.ReadUnicodeString(
                    FileSystem.ReparseDataBuffer.MountPointPathBuffer + buffer.SubstituteNameOffset,
                    buffer.SubstituteNameLength / 2
                    );
            }
        }
        public static string GetDeviceName(FileHandle fhandle)
        {
            using (var data = new MemoryAlloc(600))
            {
                fhandle.IoControl(IoCtlQueryDeviceName, IntPtr.Zero, 0, data, data.Size);

                MountDevName name = data.ReadStruct<MountDevName>();

                return data.ReadUnicodeString(MountDevName.NameOffset, name.NameLength / 2);
            }
        }
Exemple #8
0
        private static string GetSignerNameFromStateData(IntPtr stateData)
        {
            // Well, here's a shitload of indirection for you...

            // 1. State data -> Provider data
            IntPtr provData = Win32.WTHelperProvDataFromStateData(stateData);

            if (provData == IntPtr.Zero)
            {
                return(null);
            }

            // 2. Provider data -> Provider signer
            IntPtr signerInfo = Win32.WTHelperGetProvSignerFromChain(provData, 0, false, 0);

            if (signerInfo == IntPtr.Zero)
            {
                return(null);
            }

            CryptProviderSgnr sngr = (CryptProviderSgnr)Marshal.PtrToStructure(signerInfo, typeof(CryptProviderSgnr));

            if (sngr.CertChain == IntPtr.Zero)
            {
                return(null);
            }
            if (sngr.CertChainCount == 0)
            {
                return(null);
            }

            // 3. Provider signer -> Provider cert
            CryptProviderCert cert = (CryptProviderCert)Marshal.PtrToStructure(sngr.CertChain, typeof(CryptProviderCert));

            if (cert.Cert == IntPtr.Zero)
            {
                return(null);
            }

            // 4. Provider cert -> Cert context
            CertContext context = (CertContext)Marshal.PtrToStructure(cert.Cert, typeof(CertContext));

            if (context.CertInfo != IntPtr.Zero)
            {
                // 5. Cert context -> Cert info
                CertInfo certInfo = (CertInfo)Marshal.PtrToStructure(context.CertInfo, typeof(CertInfo));

                unsafe
                {
                    using (MemoryAlloc buffer = new MemoryAlloc(0x200))
                    {
                        int length;

                        // 6. Cert info subject -> Subject X.500 string

                        length = Win32.CertNameToStr(
                            1,
                            new IntPtr(&certInfo.Subject),
                            3,
                            buffer,
                            buffer.Size / 2
                            );

                        if (length > buffer.Size / 2)
                        {
                            buffer.ResizeNew(length * 2);

                            length = Win32.CertNameToStr(
                                1,
                                new IntPtr(&certInfo.Subject),
                                3,
                                buffer,
                                buffer.Size / 2
                                );
                        }

                        string name = buffer.ReadUnicodeString(0);

                        // 7. Subject X.500 string -> CN or OU value

                        string value = GetX500Value(name, "CN");

                        if (string.IsNullOrEmpty(value))
                        {
                            value = GetX500Value(name, "OU");
                        }

                        return(value);
                    }
                }
            }

            return(null);
        }