Exemplo n.º 1
0
        static bool TryGetBaseOffsetFromElfFile(string path, out int baseOffset)
        {
            baseOffset = -1;

            if (!ELFReader.TryLoad(path, out ELF <ulong> elf))
            {
                Console.WriteLine("Invalid ELF.");
                return(false);
            }
            else
            {
                var firstCodeSection = elf.GetSections <Section <ulong> >().FirstOrDefault(x => x.Type == SectionType.ProgBits);
                if (firstCodeSection == null)
                {
                    Console.WriteLine("Invalid ELF. No code (ProgBits) section found.");
                    return(false);
                }
                else
                {
                    baseOffset = ( int )(firstCodeSection.LoadAddress - firstCodeSection.Offset);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        public bool TryProcessElfFile(string filePath, out string soName, out IList <string> depends)
        {
            try
            {
                if (ELFReader.TryLoad(filePath, out var elf))
                {
                    if (elf.Class == Class.Bit32)
                    {
                        (soName, depends) = Process32BitElfFile((ELF <uint>)elf);

                        return(true);
                    }
                    else if (elf.Class == Class.Bit64)
                    {
                        (soName, depends) = Process64BitElfFile((ELF <ulong>)elf);

                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, $"Failed to process ELF file: {filePath}");
            }

            soName  = null;
            depends = null;

            return(false);
        }
Exemplo n.º 3
0
        // TODO: IAsyncEnumerable when ELF library supports it
        private IEnumerable <(string debugId, string file)> GetFiles(IEnumerable <string> files)
        {
            foreach (var file in files)
            {
                _logger.LogInformation("Processing file: {file}.", file);
                IELF?  elf = null;
                string?buildIdHex;
                try
                {
                    // TODO: find an async API
                    if (!ELFReader.TryLoad(file, out elf))
                    {
                        _logger.LogWarning("Couldn't load': {file} with ELF reader.", file);
                        continue;
                    }

                    var hasBuildId = elf.TryGetSection(".note.gnu.build-id", out var buildId);
                    if (!hasBuildId)
                    {
                        _logger.LogWarning("No Debug Id in {file}", file);
                        continue;
                    }

                    var hasUnwindingInfo  = elf.TryGetSection(".eh_frame", out _);
                    var hasDwarfDebugInfo = elf.TryGetSection(".debug_frame", out _);

                    if (!hasUnwindingInfo && !hasDwarfDebugInfo)
                    {
                        _logger.LogWarning("No unwind nor DWARF debug info in {file}", file);
                        continue;
                    }

                    _logger.LogInformation("Contains unwinding info: {hasUnwindingInfo}", hasUnwindingInfo);
                    _logger.LogInformation("Contains DWARF debug info: {hasDwarfDebugInfo}", hasDwarfDebugInfo);

                    var builder = new StringBuilder();
                    var bytes   = buildId.GetContents().Skip(16);

                    foreach (var @byte in bytes)
                    {
                        builder.Append(@byte.ToString("x2"));
                    }

                    buildIdHex = builder.ToString();
                }
                catch (Exception e)
                {
                    // You would expect TryLoad doesn't throw but that's not the case
                    _logger.LogError(e, "Failed processing file {file}.", file);
                    continue;
                }
                finally
                {
                    elf?.Dispose();
                }

                yield return(buildIdHex, file);
            }
        }
Exemplo n.º 4
0
        private async Task Collect(string path)
        {
            var files = Directory.GetFiles(path);

            _logger.LogInformation("Path {path} has {length} files to process", path, files.Length);

            foreach (var file in files)
            {
                _logger.LogInformation("Processing file: {file}.", file);
                IELF elf = null;
                try
                {
                    if (!ELFReader.TryLoad(file, out elf))
                    {
                        _logger.LogWarning("Couldn't load': {file} with ELF reader.", file);
                        continue;
                    }

                    var hasBuildId = elf.TryGetSection(".note.gnu.build-id", out var buildId);
                    if (!hasBuildId)
                    {
                        _logger.LogWarning("No Debug Id in {file}", file);
                        continue;
                    }

                    var hasUnwindingInfo  = elf.TryGetSection(".eh_frame", out _);
                    var hasDwarfDebugInfo = elf.TryGetSection(".debug_frame", out _);

                    if (!hasUnwindingInfo && !hasDwarfDebugInfo)
                    {
                        _logger.LogWarning("No unwind nor DWARF debug info in {file}", file);
                        continue;
                    }

                    await ProcessFile(file, hasUnwindingInfo, hasDwarfDebugInfo, buildId, elf);
                }
                catch (Exception e)
                {
                    // You would expect TryLoad doesn't throw but that's not the case
                    _logger.LogError(e, "Failed processing file {file}.", file);
                }
                finally
                {
                    elf?.Dispose();
                }
            }
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        internal override bool Analyze(DirectoryInfo baseDirectory, FileInfo file)
        {
            if (!base.Analyze(baseDirectory, file))
            {
                return(false);
            }
            if (!HasMagicBytes(file))
            {
                return(false);
            }

            Name         = file.Name;
            Architecture = new Architecture(OS.Linux, Cpu.All);

            IELF elfData = null;

            try
            {
                if (ELFReader.TryLoad(file.FullName, out elfData))
                {
                    if (elfData.Class == Class.NotELF || elfData.Type != FileType.Executable)
                    {
                        return(false);
                    }

                    Architecture = new Architecture(OS.Linux, GetCpu(elfData));
                }
            }
            catch (NullReferenceException)
            {}
            finally
            {
                if (elfData != null)
                {
                    elfData.Dispose();
                }
            }

            return(true);
        }
Exemplo n.º 6
0
        public XEN(string XenDump)
        {
            MemFile = XenDump;

            SupportedStatus = ELFReader.TryLoad <long>(MemFile, out Elf);
        }
Exemplo n.º 7
0
 public void ShouldNotOpenNonELFFile()
 {
     Assert.IsFalse(ELFReader.TryLoad(Utilities.GetBinary("notelf"), out var _));
 }
Exemplo n.º 8
0
        public void ShouldNotOpenNonELFFile()
        {
            IELF elf;

            Assert.IsFalse(ELFReader.TryLoad(Utilities.GetBinaryLocation("notelf"), out elf));
        }