libclamav/pe.h
6307ca15
 /*
2023340a
  *  Copyright (C) 2007-2008 Sourcefire, Inc.
6307ca15
  *
2023340a
  *  Authors: Alberto Wu, Tomasz Kojm
85dd8460
  *
6307ca15
  *  This program is free software; you can redistribute it and/or modify
2023340a
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
6307ca15
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
6307ca15
  */
 
 #ifndef __PE_H
 #define __PE_H
 
 #include "clamav.h"
01302683
 #include "execs.h"
3c91998b
 #include "others.h"
355450c9
 #include "cltypes.h"
048d7677
 #include "fmap.h"
34da9ae4
 #include "bcfeatures.h"
b8656613
 /** @file */
4abbeb3a
 /** Header for this PE file
   \group_pe */
85dd8460
 struct pe_image_file_hdr {
b8656613
     uint32_t Magic;  /**< PE magic header: PE\\0\\0 */
     uint16_t Machine;/**< CPU this executable runs on, see libclamav/pe.c for possible values */
     uint16_t NumberOfSections;/**< Number of sections in this executable */
     uint32_t TimeDateStamp;   /**< Unreliable */
     uint32_t PointerToSymbolTable;	    /**< debug */
     uint32_t NumberOfSymbols;		    /**< debug */
     uint16_t SizeOfOptionalHeader;	    /**< == 224 */
85dd8460
     uint16_t Characteristics;
 };
 
4abbeb3a
 /** PE data directory header
   \group_pe */
85dd8460
 struct pe_image_data_dir {
     uint32_t VirtualAddress;
     uint32_t Size;
 };
 
4abbeb3a
 /** 32-bit PE optional header
   \group_pe */
667a4b35
 struct pe_image_optional_hdr32 {
85dd8460
     uint16_t Magic;
b8656613
     uint8_t  MajorLinkerVersion;		    /**< unreliable */
     uint8_t  MinorLinkerVersion;		    /**< unreliable */
     uint32_t SizeOfCode;			    /**< unreliable */
     uint32_t SizeOfInitializedData;		    /**< unreliable */
     uint32_t SizeOfUninitializedData;		    /**< unreliable */
85dd8460
     uint32_t AddressOfEntryPoint;
     uint32_t BaseOfCode;
     uint32_t BaseOfData;
b8656613
     uint32_t ImageBase;				    /**< multiple of 64 KB */
     uint32_t SectionAlignment;			    /**< usually 32 or 4096 */
     uint32_t FileAlignment;			    /**< usually 32 or 512 */
     uint16_t MajorOperatingSystemVersion;	    /**< not used */
     uint16_t MinorOperatingSystemVersion;	    /**< not used */
1c4683ac
     uint16_t MajorImageVersion;			    /**< unreliable */
     uint16_t MinorImageVersion;			    /**< unreliable */
85dd8460
     uint16_t MajorSubsystemVersion;
     uint16_t MinorSubsystemVersion;
1c4683ac
     uint32_t Win32VersionValue;			    /*< ? */
85dd8460
     uint32_t SizeOfImage;
     uint32_t SizeOfHeaders;
b8656613
     uint32_t CheckSum;				    /**< NT drivers only */
85dd8460
     uint16_t Subsystem;
     uint16_t DllCharacteristics;
     uint32_t SizeOfStackReserve;
     uint32_t SizeOfStackCommit;
     uint32_t SizeOfHeapReserve;
     uint32_t SizeOfHeapCommit;
1c4683ac
     uint32_t LoaderFlags;			    /*< ? */
b8656613
     uint32_t NumberOfRvaAndSizes;		    /**< unreliable */
667a4b35
     struct pe_image_data_dir DataDirectory[16];
 };
 
4abbeb3a
 /** PE 64-bit optional header
   \group_pe */
667a4b35
 struct pe_image_optional_hdr64 {
     uint16_t Magic;
b8656613
     uint8_t  MajorLinkerVersion;		    /**< unreliable */
     uint8_t  MinorLinkerVersion;		    /**< unreliable */
     uint32_t SizeOfCode;			    /**< unreliable */
     uint32_t SizeOfInitializedData;		    /**< unreliable */
     uint32_t SizeOfUninitializedData;		    /**< unreliable */
667a4b35
     uint32_t AddressOfEntryPoint;
     uint32_t BaseOfCode;
b8656613
     uint64_t ImageBase;				    /**< multiple of 64 KB */
     uint32_t SectionAlignment;			    /**< usually 32 or 4096 */
     uint32_t FileAlignment;			    /**< usually 32 or 512 */
     uint16_t MajorOperatingSystemVersion;	    /**< not used */
     uint16_t MinorOperatingSystemVersion;	    /**< not used */
     uint16_t MajorImageVersion;			    /**< unreliable */
     uint16_t MinorImageVersion;			    /**< unreliable */
667a4b35
     uint16_t MajorSubsystemVersion;
     uint16_t MinorSubsystemVersion;
     uint32_t Win32VersionValue;			    /* ? */
     uint32_t SizeOfImage;
     uint32_t SizeOfHeaders;
b8656613
     uint32_t CheckSum;				    /**< NT drivers only */
667a4b35
     uint16_t Subsystem;
     uint16_t DllCharacteristics;
     uint64_t SizeOfStackReserve;
     uint64_t SizeOfStackCommit;
     uint64_t SizeOfHeapReserve;
     uint64_t SizeOfHeapCommit;
     uint32_t LoaderFlags;			    /* ? */
b8656613
     uint32_t NumberOfRvaAndSizes;		    /**< unreliable */
85dd8460
     struct pe_image_data_dir DataDirectory[16];
 };
 
4abbeb3a
 /** PE section header
   \group_pe */
85dd8460
 struct pe_image_section_hdr {
b8656613
     uint8_t Name[8];			    /**< may not end with NULL */
85dd8460
     /*
     union {
 	uint32_t PhysicalAddress;
 	uint32_t VirtualSize;
     } AddrSize;
     */
     uint32_t VirtualSize;
     uint32_t VirtualAddress;
b8656613
     uint32_t SizeOfRawData;		    /**< multiple of FileAlignment */
     uint32_t PointerToRawData;		    /**< offset to the section's data */
     uint32_t PointerToRelocations;	    /**< object files only */
     uint32_t PointerToLinenumbers;	    /**< object files only */
     uint16_t NumberOfRelocations;	    /**< object files only */
     uint16_t NumberOfLinenumbers;	    /**< object files only */
85dd8460
     uint32_t Characteristics;
 };
 
4abbeb3a
 /** Data for the bytecode PE hook
   \group_pe */
ab636570
 struct cli_pe_hook_data {
236fb136
   uint32_t offset;
1c4683ac
   uint32_t ep; /**< EntryPoint as file offset */
   uint16_t nsections;/**< Number of sections */
50829fbf
   uint16_t dummy; /* align */
1c4683ac
   struct pe_image_file_hdr file_hdr;/**< Header for this PE file */
   struct pe_image_optional_hdr32 opt32; /**< 32-bit PE optional header */
50829fbf
   uint32_t dummy2; /* align */
1c4683ac
   struct pe_image_optional_hdr64 opt64;/**< 64-bit PE optional header */
   struct pe_image_data_dir dirs[16]; /**< PE data directory header */
236fb136
   uint32_t e_lfanew;/**< address of new exe header */
   uint32_t overlays;/**< number of overlays */
   int32_t overlays_sz;/**< size of overlays */
   uint32_t hdr_size;/**< internally needed by rawaddr */
ab636570
 };
 
453d8180
 int cli_scanpe(cli_ctx *ctx);
6307ca15
 
49cc1e3c
 int cli_peheader(fmap_t *map, struct cli_exe_info *peinfo);
cdbf8c8e
 
c80f26a2
 uint32_t cli_rawaddr(uint32_t, const struct cli_exe_section *, uint16_t, unsigned int *, size_t, uint32_t);
d2ba6f98
 void findres(uint32_t, uint32_t, uint32_t, fmap_t *map, struct cli_exe_section *, uint16_t, uint32_t, int (*)(void *, uint32_t, uint32_t, uint32_t, uint32_t), void *);
235464bb
 
6307ca15
 #endif