Exemple #1
0
        static void Main(string[] args)
        {
            string pdbPath = @"pdb";
            string dwarfPath = @"dwarf";

            Dictionary<string, Type> pdbTypes = new Dictionary<string, Type>();
            Dictionary<string, Type> dwarfTypes = new Dictionary<string, Type>();

            foreach (Type type in PdbParser.Parse(File.ReadLines(pdbPath)))
            {
                if (pdbTypes.ContainsKey(type.FullName))
                {
                    if (!pdbTypes[type.FullName].Alternates.ContainsKey(type.ToString()))
                    {
                        pdbTypes[type.FullName].Alternates[type.ToString()] = type;
                    }
                }
                else
                {
                    pdbTypes[type.FullName] = type;
                    pdbTypes[type.FullName].Alternates[type.ToString()] = type;
                }
            }

            foreach (Type type in DwarfParser.Parse(File.ReadLines(dwarfPath)))
            {
                if (dwarfTypes.ContainsKey(type.FullName))
                {
                    if (!dwarfTypes[type.FullName].Alternates.ContainsKey(type.ToString()))
                    {
                        dwarfTypes[type.FullName].Alternates[type.ToString()] = type;
                    }
                }
                else
                {
                    dwarfTypes[type.FullName] = type;
                    dwarfTypes[type.FullName].Alternates[type.ToString()] = type;
                }
            }

            foreach (Type type in dwarfTypes.Values.OrderBy(x => x.FullName))
            {
                foreach (string alt in type.Alternates.Keys.OrderBy(x => x))
                {
                    // Console.WriteLine(alt);
                }

                if (pdbTypes.ContainsKey(type.FullName) && ! pdbTypes[type.FullName].Alternates.ContainsKey(type.ToString()))
                {
                    Console.WriteLine($"Type Mismatch: {type.FullName}\n{type}\n{type.SourceLine}");
                    foreach (string pdbType in pdbTypes[type.FullName].Alternates.Keys)
                    {
                        Console.WriteLine(pdbType);
                    }
                }
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            string pdbPath   = @"pdb";
            string dwarfPath = @"dwarf";

            Dictionary <string, Type> pdbTypes   = new Dictionary <string, Type>();
            Dictionary <string, Type> dwarfTypes = new Dictionary <string, Type>();

            foreach (Type type in PdbParser.Parse(File.ReadLines(pdbPath)))
            {
                if (pdbTypes.ContainsKey(type.FullName))
                {
                    if (!pdbTypes[type.FullName].Alternates.ContainsKey(type.ToString()))
                    {
                        pdbTypes[type.FullName].Alternates[type.ToString()] = type;
                    }
                }
                else
                {
                    pdbTypes[type.FullName] = type;
                    pdbTypes[type.FullName].Alternates[type.ToString()] = type;
                }
            }

            foreach (Type type in DwarfParser.Parse(File.ReadLines(dwarfPath)))
            {
                if (dwarfTypes.ContainsKey(type.FullName))
                {
                    if (!dwarfTypes[type.FullName].Alternates.ContainsKey(type.ToString()))
                    {
                        dwarfTypes[type.FullName].Alternates[type.ToString()] = type;
                    }
                }
                else
                {
                    dwarfTypes[type.FullName] = type;
                    dwarfTypes[type.FullName].Alternates[type.ToString()] = type;
                }
            }

            foreach (Type type in dwarfTypes.Values.OrderBy(x => x.FullName))
            {
                if (pdbTypes.ContainsKey(type.FullName))
                {
                    CompareTypes(type, pdbTypes[type.FullName]);
                }
            }
        }
Exemple #3
0
        private static void SrvSrcFileExtractor(string file)
        {
            string targetPath =
                Utility.CleanupPath(Path.GetDirectoryName(file));

            targetPath += System.IO.Path.GetFileName(file) + ".txt";

            try
            {
                using (PdbParser tempParser =
                           new PdbParser(file))
                {
                    MemoryStream resultStream =
                        tempParser.GetStreamBeginsWithBytesArrayASCII(SRCSRVLocator);

                    if (resultStream != null)
                    {
                        Console.WriteLine("Writing File To:" + targetPath);
                        using (System.IO.FileStream outputStream = new System.IO.FileStream(targetPath, FileMode.Create))
                        {
                            resultStream.WriteTo(outputStream);
                            outputStream.Flush();
                            resultStream.Dispose();
                        }
                    }
                }
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (InvalidDataException ex)
            {
                Console.WriteLine("Not a Pdb File");
            }
            catch (System.IO.IOException ioException)
            {
                Console.WriteLine("An IO Exception Occurred While Extracting The Pdb File:" + ioException.Message);
            }
        }
        /// <summary>
        /// Provides the functionality to map a DLL from disk into a process
        /// </summary>
        public LibraryMapper(Process process, string dllFilePath, MappingFlags mappingFlags = MappingFlags.None)
        {
            if (process is null || process.HasExited)
            {
                throw new ArgumentException("The provided process is not currently running");
            }

            if (string.IsNullOrWhiteSpace(dllFilePath) || !File.Exists(dllFilePath))
            {
                throw new ArgumentException("The provided DLL file path did not point to a valid file");
            }

            EnterDebugMode();

            _dllBlock = File.ReadAllBytes(dllFilePath);

            _mappingFlags = mappingFlags;

            _pdbParser = new PdbParser(ResolveNtdllFilePath(process), "LdrpInvertedFunctionTable");

            _peImage = new PeImage(_dllBlock.ToArray());

            _processManager = new ProcessManager(process);
        }
        /// <summary>
        /// Provides the functionality to map a DLL from memory into a process
        /// </summary>
        public LibraryMapper(Process process, Memory <byte> dllBlock, MappingFlags mappingFlags = MappingFlags.None)
        {
            if (process is null || process.HasExited)
            {
                throw new ArgumentException("The provided process is not currently running");
            }

            if (dllBlock.IsEmpty)
            {
                throw new ArgumentException("The provided DLL buffer was empty");
            }

            EnterDebugMode();

            _dllBlock = dllBlock.ToArray();

            _mappingFlags = mappingFlags;

            _pdbParser = new PdbParser(ResolveNtdllFilePath(process), "LdrpInvertedFunctionTable");

            _peImage = new PeImage(dllBlock);

            _processManager = new ProcessManager(process);
        }
        int Main(string pdbPath, string dwarfPath)
        {
            Dictionary <string, Type> pdbTypes   = new Dictionary <string, Type>();
            Dictionary <string, Type> dwarfTypes = new Dictionary <string, Type>();

            foreach (Type type in PdbParser.Parse(File.ReadLines(pdbPath)))
            {
                if (pdbTypes.ContainsKey(type.FullName))
                {
                    if (!pdbTypes[type.FullName].Alternates.ContainsKey(type.ToString()))
                    {
                        pdbTypes[type.FullName].Alternates[type.ToString()] = type;
                    }
                }
                else
                {
                    pdbTypes[type.FullName] = type;
                    pdbTypes[type.FullName].Alternates[type.ToString()] = type;
                }
            }

            Console.WriteLine($"PDB unique types : {pdbTypes.Keys.Count}");

            foreach (Type type in DwarfParser.Parse(File.ReadLines(dwarfPath)))
            {
                if (dwarfTypes.ContainsKey(type.FullName))
                {
                    if (!dwarfTypes[type.FullName].Alternates.ContainsKey(type.ToString()))
                    {
                        dwarfTypes[type.FullName].Alternates[type.ToString()] = type;
                    }
                }
                else
                {
                    dwarfTypes[type.FullName] = type;
                    dwarfTypes[type.FullName].Alternates[type.ToString()] = type;
                }
            }

            Console.WriteLine($"Dwarf unique types : {dwarfTypes.Keys.Count}");

            foreach (Type type in dwarfTypes.Values.OrderBy(x => x.FullName))
            {
                if (pdbTypes.ContainsKey(type.FullName))
                {
                    CompareTypes(type, pdbTypes[type.FullName]);
                }
                else
                {
                    // Console.WriteLine($"dwarf unique type : {type.FullName}");
                    dwarfUnique++;
                }
            }

            foreach (Type type in pdbTypes.Values.OrderBy(x => x.FullName))
            {
                if (!dwarfTypes.ContainsKey(type.FullName))
                {
                    // Console.WriteLine($"PDB unique type : {type.FullName}");
                    pdbUnique++;
                }
            }

            Console.WriteLine($"Matches: {matched}");
            Console.WriteLine($"Ignored: {ignored}");
            Console.WriteLine($"Mismatched: {mismatched}");
            Console.WriteLine($"DwarfUnique: {dwarfUnique}");
            Console.WriteLine($"PdbUnique: {pdbUnique}");

            return(mismatched + (matched == 0 ? 1 : 0));
        }
        private async Task <IEnumerable <BaseParsedIndexedBinary> > IndexBinaryContentsAsync(PackageArchiveReader package, IEnumerable <NuGetFramework> supportedFrameworks, bool symbolPackage, CancellationToken cancellationToken)
        {
            var files = (await package.GetFilesAsync(cancellationToken).ConfigureAwait(false))
                        .Select(x => new { name = x, ext = Path.GetExtension(x) });

            var hasSymbols = files.Any(x => string.Equals(x.ext, ".pdb", StringComparison.InvariantCultureIgnoreCase));

            if (!symbolPackage && hasSymbols)
            {
                return(null);
            }
            else if (symbolPackage && !hasSymbols)
            {
                return(null);
            }

            var skipFx = supportedFrameworks.Contains(NuGetFramework.AnyFramework);
            var bins   = !symbolPackage
                ? files.Where(x => string.Equals(x.ext, ".dll", StringComparison.InvariantCultureIgnoreCase) ||
                              string.Equals(x.ext, ".exe", StringComparison.InvariantCultureIgnoreCase))
                : files.Where(x => string.Equals(x.ext, ".pdb", StringComparison.InvariantCultureIgnoreCase));

            var binaries = new List <BaseParsedIndexedBinary>();

            foreach (var bin in bins)
            {
                var entry     = bin.name;
                var name      = Path.GetFileName(entry);
                var ext       = bin.ext;
                var location  = Path.GetDirectoryName(entry);
                var parent    = Path.GetFileName(location);
                var hash      = default(string);
                var fx        = string.Equals(parent, "lib", StringComparison.InvariantCultureIgnoreCase) || skipFx ? NuGetFramework.AnyFramework : NuGetFramework.ParseFolder(parent);
                var isSymbols = string.Equals(ext, ".pdb", StringComparison.InvariantCultureIgnoreCase);

                if (!supportedFrameworks.Contains(fx))
                {
                    continue;
                }

                var ze = package.GetEntry(entry);
                using (var ms = new MemoryStream())
                {
                    using (var zstream = ze.Open())
                        await zstream.CopyToAsync(ms).ConfigureAwait(false);
                    ms.Position = 0;

                    if (!isSymbols)
                    {
                        using (var sha256 = SHA256.Create())
                        {
                            var hashBin = sha256.ComputeHash(ms);
                            hash = string.Create(256 / 8 * 2, hashBin, StringifyHash);
                        }
                        ms.Position = 0;

                        using (var pereader = new PEReader(ms))
                        {
                            var symbolIds = default(IEnumerable <SymbolIdentifier>);

                            var debugdir = pereader.ReadDebugDirectory();
                            if (debugdir != null && debugdir.Length > 0 && debugdir.Any(x => x.Type == DebugDirectoryEntryType.Reproducible /* == Deterministic (0x10) */))
                            {
                                symbolIds = debugdir.Where(x => x.Type == DebugDirectoryEntryType.CodeView)
                                            .Select(x => new
                                {
                                    data = pereader.ReadCodeViewDebugDirectoryData(x),
                                    kind = x.MinorVersion == PortablePdbEntryMinorVersion ? SymbolKind.Portable : SymbolKind.Full
                                })
                                            .Select(x => new SymbolIdentifier(x.data.Guid, x.kind == SymbolKind.Portable ? PortablePdbAge : x.data.Age, x.kind));
                            }

                            binaries.Add(new ParsedIndexedBinaryExecutable
                            {
                                Entry             = entry,
                                Name              = name,
                                Extension         = ext,
                                Location          = location,
                                Parent            = parent,
                                Sha256            = hash,
                                Length            = ze.Length,
                                Framework         = fx,
                                SymbolIdentifiers = symbolIds.ToList() // Otherwise it tries to enumerate over disposed objects
                            });
                        }
                    }
                    else
                    {
                        var magic = ReadMagic(ms);
                        if (magic == PortablePdbMagic)
                        {
                            // Read portable PDB
                            using (var metadata = MetadataReaderProvider.FromPortablePdbStream(ms, MetadataStreamOptions.LeaveOpen))
                            {
                                var reader    = metadata.GetMetadataReader();
                                var dbgHeader = reader.DebugMetadataHeader;
                                if (dbgHeader != null)
                                {
                                    binaries.Add(new ParsedIndexedBinarySymbols
                                    {
                                        Entry      = entry,
                                        Name       = name,
                                        Extension  = ext,
                                        Location   = location,
                                        Parent     = parent,
                                        Framework  = fx,
                                        Identifier = new BlobContentId(dbgHeader.Id).Guid,
                                        Age        = PortablePdbAge,
                                        Kind       = SymbolKind.Portable
                                    });
                                }
                            }
                        }
                        else
                        {
                            // Read full PDB
                            using (var msf = new MsfParser(ms, leaveOpen: true))
                                using (var pdb = new PdbParser(msf, leaveOpen: true))
                                {
                                    if (pdb.TryGetMetadata(out var pdbmeta))
                                    {
                                        binaries.Add(new ParsedIndexedBinarySymbols
                                        {
                                            Entry      = entry,
                                            Name       = name,
                                            Extension  = ext,
                                            Location   = location,
                                            Parent     = parent,
                                            Framework  = fx,
                                            Identifier = pdbmeta.Identifier,
                                            Age        = pdbmeta.Age,
                                            Kind       = SymbolKind.Full
                                        });
                                    }
                                }
                        }
                    }
                }
            }

            return(binaries);

            void StringifyHash(Span <char> buffer, byte[] state)
            {
                for (var i = state.Length - 1; i >= 0; i--)
                {
                    state[i].TryFormat(buffer.Slice(i * 2), out _, "x2", CultureInfo.InvariantCulture);
                }
            }

            uint ReadMagic(MemoryStream ms)
            {
                Span <byte> magic = stackalloc byte[4];

                ms.Read(magic);
                ms.Position = 0;

                return(BinaryPrimitives.ReadUInt32BigEndian(magic));
            }
        }