Подтвердить что ты не робот

Как определить, является ли DLL управляемой сборкой или встроенной (не допустить загрузку родной DLL)?

Исходное название: Как я могу предотвратить загрузку родной DLL из приложения .NET?

Фон:

Приложение My С# включает в себя плагин и общий плагин-загрузчик.

Плагин-загрузчик перечисляет каталог приложения для идентификации плагинов-плагинов (по существу он ищет *.dll в это время).

Внутри того же каталога приложений есть родная (Windows, non.net) dll, которая, косвенно, зависит от одной из плагинов плагина.

Плагин-загрузчик слепо предполагает, что native.dll - это сборка .NET Assembly, просто потому, что он проверяет только расширение файла. Когда он пытается загрузить собственную DLL, генерируется исключение:

"Не удалось загрузить файл или сборку" native.dll "или одну из его зависимостей. Ожидается, что модуль будет содержать манифест сборки.

Я в основном создаю диагностический отчет, если загрузка плагинов не удалась, поэтому я стараюсь, чтобы этот журнал не заполнялся сообщениями о невозможности загрузки родной DLL (чего я даже не хочу пытаться).

Вопрос:

Есть ли какой-нибудь вызов .NET API, который я могу использовать, чтобы определить, является ли бинарный файл сборкой .NET, чтобы я вообще не пытался загружать собственную DLL?

Возможно, более продолжительный срок я переведу мои плагины в подкаталог, но пока я просто хочу, чтобы работа вокруг не включала жесткое кодирование имени native.dll внутри моего загрузчика плагинов.

Я предполагаю, что я ищу какой-то статический API-интерфейс Assembly.IsManaged(), который я упустил. Предположительно, такой API не существует?

4b9b3361

Ответ 1

Ответ, цитируемый lubos hasko, хорош, но он не работает для 64-битных сборок. Здесь исправленная версия (вдохновленная http://apichange.codeplex.com/SourceControl/changeset/view/76c98b8c7311#ApiChange.Api/src/Introspection/CorFlagsReader.cs)

public static bool IsManagedAssembly(string fileName)
{
    using (Stream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
    using (BinaryReader binaryReader = new BinaryReader(fileStream))
    {
        if (fileStream.Length < 64)
        {
            return false;
        }

        //PE Header starts @ 0x3C (60). Its a 4 byte header.
        fileStream.Position = 0x3C;
        uint peHeaderPointer = binaryReader.ReadUInt32();
        if (peHeaderPointer == 0)
        {
            peHeaderPointer = 0x80;
        }

        // Ensure there is at least enough room for the following structures:
        //     24 byte PE Signature & Header
        //     28 byte Standard Fields         (24 bytes for PE32+)
        //     68 byte NT Fields               (88 bytes for PE32+)
        // >= 128 byte Data Dictionary Table
        if (peHeaderPointer > fileStream.Length - 256)
        {
            return false;
        }

        // Check the PE signature.  Should equal 'PE\0\0'.
        fileStream.Position = peHeaderPointer;
        uint peHeaderSignature = binaryReader.ReadUInt32();
        if (peHeaderSignature != 0x00004550)
        {
            return false;
        }

        // skip over the PEHeader fields
        fileStream.Position += 20;

        const ushort PE32 = 0x10b;
        const ushort PE32Plus = 0x20b;

        // Read PE magic number from Standard Fields to determine format.
        var peFormat = binaryReader.ReadUInt16();
        if (peFormat != PE32 && peFormat != PE32Plus)
        {
            return false;
        }

        // Read the 15th Data Dictionary RVA field which contains the CLI header RVA.
        // When this is non-zero then the file contains CLI data otherwise not.
        ushort dataDictionaryStart = (ushort)(peHeaderPointer + (peFormat == PE32 ? 232 : 248));
        fileStream.Position = dataDictionaryStart;

        uint cliHeaderRva = binaryReader.ReadUInt32();
        if (cliHeaderRva == 0)
        {
            return false;
        }

        return true;
    }
}

Пропущенный фрагмент должен был зависеть от словаря данных начинаться по-разному в зависимости от того, являемся ли мы PE32 или PE32Plus:

    // Read PE magic number from Standard Fields to determine format.
    var peFormat = binaryReader.ReadUInt16();
    if (peFormat != PE32 && peFormat != PE32Plus)
    {
        return false;
    }

    // Read the 15th Data Dictionary RVA field which contains the CLI header RVA.
    // When this is non-zero then the file contains CLI data otherwise not.
    ushort dataDictionaryStart = (ushort)(peHeaderPointer + (peFormat == PE32 ? 232 : 248));

Ответ 2

Как определить, является ли файл сборкой .NET или нет?

public static bool IsManagedAssembly(string fileName)
{
    uint peHeader;
    uint peHeaderSignature;
    ushort machine;
    ushort sections;
    uint timestamp;
    uint pSymbolTable;
    uint noOfSymbol;
    ushort optionalHeaderSize;
    ushort characteristics;
    ushort dataDictionaryStart;
    uint[] dataDictionaryRVA = new uint[16];
    uint[] dataDictionarySize = new uint[16];

    Stream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    BinaryReader reader = new BinaryReader(fs);

    //PE Header starts @ 0x3C (60). Its a 4 byte header.
    fs.Position = 0x3C;
    peHeader = reader.ReadUInt32();

    //Moving to PE Header start location...
    fs.Position = peHeader;
    peHeaderSignature = reader.ReadUInt32();

    //We can also show all these value, but we will be       
    //limiting to the CLI header test.
    machine = reader.ReadUInt16();
    sections = reader.ReadUInt16();
    timestamp = reader.ReadUInt32();
    pSymbolTable = reader.ReadUInt32();
    noOfSymbol = reader.ReadUInt32();
    optionalHeaderSize = reader.ReadUInt16();
    characteristics = reader.ReadUInt16();

    // Now we are at the end of the PE Header and from here, the PE Optional Headers starts... To go directly to the datadictionary, we'll increase the stream’s current position to with 96 (0x60). 96 because, 28 for Standard fields 68 for NT-specific fields From here DataDictionary starts...and its of total 128 bytes. DataDictionay has 16 directories in total, doing simple maths 128/16 = 8. So each directory is of 8 bytes. In this 8 bytes, 4 bytes is of RVA and 4 bytes of Size. btw, the 15th directory consist of CLR header! if its 0, its not a CLR file :)
    dataDictionaryStart = Convert.ToUInt16(Convert.ToUInt16(fs.Position) + 0x60);
    fs.Position = dataDictionaryStart;
    for (int i = 0; i < 15; i++)
    {
        dataDictionaryRVA[i] = reader.ReadUInt32();
        dataDictionarySize[i] = reader.ReadUInt32();
    }
    fs.Close();

    if (dataDictionaryRVA[14] == 0) return false;
    else return true;
}

Ответ 3

Как было предложено orip, вы захотите обернуть его в блок try {} catch {}, в частности, вы хотите следить за BadImageFormatException

foreach (string aDll in dllCollection) 
{
  try 
  {
     Assembly anAssembly = Assembly.LoadFrom(aDll);
  }
  catch (BadImageFormatException ex)
  {
    //Handle this here
  }
  catch (Exception ex)
  {
    //Other exceptions (i/o, security etc.)
   }
}

Ознакомьтесь с другими исключениями здесь http://msdn.microsoft.com/en-us/library/1009fa28.aspx

Ответ 4

Я боюсь, что единственный реальный способ сделать это - позвонить System.Reflection.AssemblyName.GetAssemblyName, передав полный путь к файлу, который вы хотите проверить. Это попытается вытащить имя из манифеста, не загружая полную сборку в домен. Если файл является управляемой ассемблером, он возвращает имя сборки как строку, иначе он выкинет BadImageFormatException, который вы можете поймать и проигнорировать, прежде чем пропустить сборку и перейти на другие плагины.

Ответ 5

Использование исключения BadImageFormatException - плохой путь, например. если ваше приложение нацелено на .NET 3.5, оно не будет распознавать, пусть говорят сборки, собранные против .NET Core, хотя сборка управляется.

Итак, я думаю, что разбор PE-заголовка намного лучше.

Ответ 6

Вы всегда можете обернуть загрузку DLL блоком try/except...

Ответ 7

Продолжая ответ Кирилла, я перевел его на VB, немного настроил логику Boolean для удобства чтения и превратил ее в метод расширения для System.IO.FileInfo. Надеюсь, это может помочь кому-то.

Public Module FileSystem
  <Extension>
  Public Function IsManagedAssembly(File As FileInfo) As Boolean
    Dim _
      uHeaderSignature,
      uHeaderPointer As UInteger

    Dim _
      uFormat,
      u64,
      u32 As UShort

    u64 = &H20B
    u32 = &H10B

    IsManagedAssembly = False

    If File.Exists AndAlso File.Length.IsAtLeast(64) Then
      Using oStream As New FileStream(File.FullName, FileMode.Open, FileAccess.Read)
        Using oReader As New BinaryReader(oStream)
          'PE Header starts @ 0x3C (60). Its a 4 byte header.
          oStream.Position = &H3C
          uHeaderPointer = oReader.ReadUInt32

          If uHeaderPointer = 0 Then
            uHeaderPointer = &H80
          End If

          ' Ensure there is at least enough room for the following structures:
          '     24 byte PE Signature & Header
          '     28 byte Standard Fields         (24 bytes for PE32+)
          '     68 byte NT Fields               (88 bytes for PE32+)
          ' >= 128 byte Data Dictionary Table
          If uHeaderPointer < oStream.Length - 257 Then
            ' Check the PE signature.  Should equal 'PE\0\0'.
            oStream.Position = uHeaderPointer
            uHeaderSignature = oReader.ReadUInt32

            If uHeaderSignature = &H4550 Then
              ' skip over the PEHeader fields
              oStream.Position += 20

              ' Read PE magic number from Standard Fields to determine format.
              uFormat = oReader.ReadUInt16

              If uFormat = u32 OrElse uFormat = u64 Then
                ' Read the 15th Data Dictionary RVA field which contains the CLI header RVA.
                ' When this is non-zero then the file contains CLI data, otherwise not.
                Select Case uFormat
                  Case u32 : oStream.Position = uHeaderPointer + &HE8
                  Case u64 : oStream.Position = uHeaderPointer + &HF8
                End Select

                IsManagedAssembly = oReader.ReadUInt32 > 0
              End If
            End If
          End If
        End Using
      End Using
    End If
  End Function
End Module