PE Header auslesen

    • Allgemein

    Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von Myrax.

      PE Header auslesen

      Hallo,

      ich habe einen evtl seltenen Quellcode gefunden, mit dem man die Header einer PE Datei auslesen kann:
      DOS-, NT- und Sectionheader.

      Ich habe den Code nur auf das Auslesen der Header gekürzt, Credits gehen an w!cKed

      VB.NET-Quellcode

      1. 'all crediz to w!cKed
      2. Imports System.IO
      3. Imports System.Runtime.InteropServices
      4. Class PE_Header
      5. #Region "Structures"
      6. <StructLayout(LayoutKind.Sequential)> _
      7. Private Structure IMAGE_DOS_HEADER
      8. Public e_magic As UInt16
      9. Public e_cblp As UInt16
      10. Public e_cp As UInt16
      11. Public e_crlc As UInt16
      12. Public e_cparhdr As UInt16
      13. Public e_minalloc As UInt16
      14. Public e_maxalloc As UInt16
      15. Public e_ss As UInt16
      16. Public e_sp As UInt16
      17. Public e_csum As UInt16
      18. Public e_ip As UInt16
      19. Public e_cs As UInt16
      20. Public e_lfarlc As UInt16
      21. Public e_ovno As UInt16
      22. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=4)> _
      23. Public e_res1 As UInt16()
      24. Public e_oemid As UInt16
      25. Public e_oeminfo As UInt16
      26. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=10)> _
      27. Public e_res2 As UInt16()
      28. Public e_lfanew As Int32
      29. End Structure
      30. <StructLayout(LayoutKind.Sequential)> _
      31. Private Structure IMAGE_FILE_HEADER
      32. Public Machine As UInt16
      33. Public NumberOfSections As UInt16
      34. Public TimeDateStamp As UInt32
      35. Public PointerToSymbolTable As UInt32
      36. Public NumberOfSymbols As UInt32
      37. Public SizeOfOptionalHeader As UInt16
      38. Public Characteristics As UInt16
      39. End Structure
      40. <StructLayout(LayoutKind.Sequential)> _
      41. Private Structure IMAGE_DATA_DIRECTORY
      42. Public VirtualAddress As UInt32
      43. Public Size As UInt32
      44. End Structure
      45. <StructLayout(LayoutKind.Sequential)> _
      46. Private Structure IMAGE_OPTIONAL_HEADER32
      47. Public Magic As UInt16
      48. Public MajorLinkerVersion As [Byte]
      49. Public MinorLinkerVersion As [Byte]
      50. Public SizeOfCode As UInt32
      51. Public SizeOfInitializedData As UInt32
      52. Public SizeOfUninitializedData As UInt32
      53. Public AddressOfEntryPoint As UInt32
      54. Public BaseOfCode As UInt32
      55. Public BaseOfData As UInt32
      56. Public ImageBase As UInt32
      57. Public SectionAlignment As UInt32
      58. Public FileAlignment As UInt32
      59. Public MajorOperatingSystemVersion As UInt16
      60. Public MinorOperatingSystemVersion As UInt16
      61. Public MajorImageVersion As UInt16
      62. Public MinorImageVersion As UInt16
      63. Public MajorSubsystemVersion As UInt16
      64. Public MinorSubsystemVersion As UInt16
      65. Public Win32VersionValue As UInt32
      66. Public SizeOfImage As UInt32
      67. Public SizeOfHeaders As UInt32
      68. Public CheckSum As UInt32
      69. Public Subsystem As UInt16
      70. Public DllCharacteristics As UInt16
      71. Public SizeOfStackReserve As UInt32
      72. Public SizeOfStackCommit As UInt32
      73. Public SizeOfHeapReserve As UInt32
      74. Public SizeOfHeapCommit As UInt32
      75. Public LoaderFlags As UInt32
      76. Public NumberOfRvaAndSizes As UInt32
      77. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=16)> _
      78. Public DataDirectory As IMAGE_DATA_DIRECTORY()
      79. End Structure
      80. <StructLayout(LayoutKind.Sequential)> _
      81. Private Structure IMAGE_NT_HEADERS
      82. Public Signature As UInt32
      83. Public FileHeader As IMAGE_FILE_HEADER
      84. Public OptionalHeader As IMAGE_OPTIONAL_HEADER32
      85. End Structure
      86. <StructLayout(LayoutKind.Sequential)> _
      87. Private Structure IMAGE_SECTION_HEADER
      88. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> _
      89. Public Name As Byte()
      90. Public VirtualSize As UIntPtr
      91. Public VirtualAddress As UInteger
      92. Public SizeOfRawData As UInteger
      93. Public PointerToRawData As UInteger
      94. Public PointerToRelocations As UInteger
      95. Public PointerToLinenumbers As UInteger
      96. Public NumberOfRelocations As Short
      97. Public NumberOfLinenumbers As Short
      98. Public Characteristics As UInteger
      99. End Structure
      100. #End Region
      101. Public Function Get_PE_Header(ByVal sFilePath As String) As Boolean
      102. Dim DHD As New IMAGE_DOS_HEADER()
      103. Dim NHD As New IMAGE_NT_HEADERS()
      104. Dim SHD As New IMAGE_SECTION_HEADER()
      105. Dim iPointer As Integer = 0
      106. Dim lLastSectPos As Long = 0
      107. Dim lSize As Long = 0
      108. Dim lAlign As Long = 0
      109. Dim lDataSize As Long = 0
      110. Dim fBytes As Byte() = New Byte(-1) {}
      111. Try
      112. Dim bReader As New BinaryReader(New FileStream(sFilePath, FileMode.Open, FileAccess.Read))
      113. fBytes = bReader.ReadBytes(CInt(bReader.BaseStream.Length))
      114. bReader.Close()
      115. Catch
      116. End Try
      117. If fBytes.Length <= 0 Then
      118. Return False
      119. End If
      120. Dim gHandle As GCHandle = GCHandle.Alloc(fBytes, GCHandleType.Pinned)
      121. iPointer = gHandle.AddrOfPinnedObject().ToInt32()
      122. 'IMAGE_DOS_HEADER
      123. DHD = CType(Marshal.PtrToStructure(New IntPtr(iPointer), GetType(IMAGE_DOS_HEADER)), IMAGE_DOS_HEADER)
      124. 'IMAGE_NT_HEADERS
      125. NHD = CType(Marshal.PtrToStructure(New IntPtr(iPointer + DHD.e_lfanew), GetType(IMAGE_NT_HEADERS)), IMAGE_NT_HEADERS)
      126. If NHD.Signature <> 17744 OrElse DHD.e_magic <> 23117 Then
      127. Return False
      128. End If
      129. lLastSectPos = DHD.e_lfanew + Marshal.SizeOf(New IMAGE_NT_HEADERS()) + (NHD.FileHeader.NumberOfSections - 1) * Marshal.SizeOf(New IMAGE_SECTION_HEADER())
      130. 'IMAGE_SECTION_HEADER
      131. SHD = CType(Marshal.PtrToStructure(New IntPtr(iPointer + lLastSectPos), GetType(IMAGE_SECTION_HEADER)), IMAGE_SECTION_HEADER)
      132. Return True
      133. End Function
      134. End Class
      Für ein Mindestmaß an Rechtschreibung, Interpunktion und Majuskeln!
      Wenn ich jetzt mal dumm fragen dürfte... wozu braucht man dass jetzt genau?
      Wird da lediglich nur überprüft ob das File ausführbar ist oder nicht?

      MFG
      GOOGLE ist mein Meister :)
      Naja, ist schon sehr speziell, aber hier ein Beispiel:
      du kannst mit dem IMAGE_NT_HEADER, den IMAGE_FILE_HEADER und somit die Machine-Flag auslesen. Damit kannst du herausfinden, ob die Datei für 64bit kompiliert ist (0x8664) oder für x386 (0x14c).
      Für ein Mindestmaß an Rechtschreibung, Interpunktion und Majuskeln!