2004-05-11 23:30:57 +00:00
|
|
|
/*
|
2018-03-05 16:34:35 -05:00
|
|
|
* Copyright (C) 2015, 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
|
2008-04-02 15:24:51 +00:00
|
|
|
* Copyright (C) 2007-2008 Sourcefire, Inc.
|
2004-05-11 23:30:57 +00:00
|
|
|
*
|
2008-04-02 15:24:51 +00:00
|
|
|
* Authors: Alberto Wu, Tomasz Kojm
|
2018-03-05 16:34:35 -05:00
|
|
|
*
|
|
|
|
* Acknowledgements: The header structures were based upon a PE format
|
|
|
|
* analysis by B. Luevelsmeyer.
|
2004-08-02 17:09:06 +00:00
|
|
|
*
|
2004-05-11 23:30:57 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2008-04-02 15:24:51 +00:00
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
2004-05-11 23:30:57 +00:00
|
|
|
*
|
|
|
|
* 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
|
2006-04-09 19:59:28 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
2004-05-11 23:30:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __PE_H
|
|
|
|
#define __PE_H
|
|
|
|
|
|
|
|
#include "clamav.h"
|
2005-09-18 23:19:28 +00:00
|
|
|
#include "execs.h"
|
2006-02-15 00:41:40 +00:00
|
|
|
#include "others.h"
|
2007-01-08 21:43:03 +00:00
|
|
|
#include "cltypes.h"
|
2009-09-01 13:49:36 +02:00
|
|
|
#include "fmap.h"
|
2009-12-04 16:45:48 +02:00
|
|
|
#include "bcfeatures.h"
|
2009-11-24 14:53:15 +02:00
|
|
|
/** @file */
|
2010-09-02 18:04:00 +03:00
|
|
|
/** Header for this PE file
|
|
|
|
\group_pe */
|
2004-08-02 17:09:06 +00:00
|
|
|
struct pe_image_file_hdr {
|
2009-11-24 14:53:15 +02:00
|
|
|
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 */
|
2004-08-02 17:09:06 +00:00
|
|
|
uint16_t Characteristics;
|
|
|
|
};
|
|
|
|
|
2010-09-02 18:04:00 +03:00
|
|
|
/** PE data directory header
|
|
|
|
\group_pe */
|
2004-08-02 17:09:06 +00:00
|
|
|
struct pe_image_data_dir {
|
|
|
|
uint32_t VirtualAddress;
|
|
|
|
uint32_t Size;
|
|
|
|
};
|
|
|
|
|
2010-09-02 18:04:00 +03:00
|
|
|
/** 32-bit PE optional header
|
|
|
|
\group_pe */
|
2005-11-26 22:39:01 +00:00
|
|
|
struct pe_image_optional_hdr32 {
|
2004-08-02 17:09:06 +00:00
|
|
|
uint16_t Magic;
|
2009-11-24 14:53:15 +02:00
|
|
|
uint8_t MajorLinkerVersion; /**< unreliable */
|
|
|
|
uint8_t MinorLinkerVersion; /**< unreliable */
|
|
|
|
uint32_t SizeOfCode; /**< unreliable */
|
|
|
|
uint32_t SizeOfInitializedData; /**< unreliable */
|
|
|
|
uint32_t SizeOfUninitializedData; /**< unreliable */
|
2004-08-02 17:09:06 +00:00
|
|
|
uint32_t AddressOfEntryPoint;
|
|
|
|
uint32_t BaseOfCode;
|
|
|
|
uint32_t BaseOfData;
|
2009-11-24 14:53:15 +02:00
|
|
|
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 */
|
2010-05-07 09:57:30 +03:00
|
|
|
uint16_t MajorImageVersion; /**< unreliable */
|
|
|
|
uint16_t MinorImageVersion; /**< unreliable */
|
2004-08-02 17:09:06 +00:00
|
|
|
uint16_t MajorSubsystemVersion;
|
|
|
|
uint16_t MinorSubsystemVersion;
|
2010-05-07 09:57:30 +03:00
|
|
|
uint32_t Win32VersionValue; /*< ? */
|
2004-08-02 17:09:06 +00:00
|
|
|
uint32_t SizeOfImage;
|
|
|
|
uint32_t SizeOfHeaders;
|
2009-11-24 14:53:15 +02:00
|
|
|
uint32_t CheckSum; /**< NT drivers only */
|
2004-08-02 17:09:06 +00:00
|
|
|
uint16_t Subsystem;
|
|
|
|
uint16_t DllCharacteristics;
|
|
|
|
uint32_t SizeOfStackReserve;
|
|
|
|
uint32_t SizeOfStackCommit;
|
|
|
|
uint32_t SizeOfHeapReserve;
|
|
|
|
uint32_t SizeOfHeapCommit;
|
2010-05-07 09:57:30 +03:00
|
|
|
uint32_t LoaderFlags; /*< ? */
|
2009-11-24 14:53:15 +02:00
|
|
|
uint32_t NumberOfRvaAndSizes; /**< unreliable */
|
2005-11-26 22:39:01 +00:00
|
|
|
struct pe_image_data_dir DataDirectory[16];
|
|
|
|
};
|
|
|
|
|
2010-09-02 18:04:00 +03:00
|
|
|
/** PE 64-bit optional header
|
|
|
|
\group_pe */
|
2005-11-26 22:39:01 +00:00
|
|
|
struct pe_image_optional_hdr64 {
|
|
|
|
uint16_t Magic;
|
2009-11-24 14:53:15 +02:00
|
|
|
uint8_t MajorLinkerVersion; /**< unreliable */
|
|
|
|
uint8_t MinorLinkerVersion; /**< unreliable */
|
|
|
|
uint32_t SizeOfCode; /**< unreliable */
|
|
|
|
uint32_t SizeOfInitializedData; /**< unreliable */
|
|
|
|
uint32_t SizeOfUninitializedData; /**< unreliable */
|
2005-11-26 22:39:01 +00:00
|
|
|
uint32_t AddressOfEntryPoint;
|
|
|
|
uint32_t BaseOfCode;
|
2009-11-24 14:53:15 +02:00
|
|
|
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 */
|
2005-11-26 22:39:01 +00:00
|
|
|
uint16_t MajorSubsystemVersion;
|
|
|
|
uint16_t MinorSubsystemVersion;
|
|
|
|
uint32_t Win32VersionValue; /* ? */
|
|
|
|
uint32_t SizeOfImage;
|
|
|
|
uint32_t SizeOfHeaders;
|
2009-11-24 14:53:15 +02:00
|
|
|
uint32_t CheckSum; /**< NT drivers only */
|
2005-11-26 22:39:01 +00:00
|
|
|
uint16_t Subsystem;
|
|
|
|
uint16_t DllCharacteristics;
|
|
|
|
uint64_t SizeOfStackReserve;
|
|
|
|
uint64_t SizeOfStackCommit;
|
|
|
|
uint64_t SizeOfHeapReserve;
|
|
|
|
uint64_t SizeOfHeapCommit;
|
|
|
|
uint32_t LoaderFlags; /* ? */
|
2009-11-24 14:53:15 +02:00
|
|
|
uint32_t NumberOfRvaAndSizes; /**< unreliable */
|
2004-08-02 17:09:06 +00:00
|
|
|
struct pe_image_data_dir DataDirectory[16];
|
|
|
|
};
|
|
|
|
|
2010-09-02 18:04:00 +03:00
|
|
|
/** PE section header
|
|
|
|
\group_pe */
|
2004-08-02 17:09:06 +00:00
|
|
|
struct pe_image_section_hdr {
|
2009-11-24 14:53:15 +02:00
|
|
|
uint8_t Name[8]; /**< may not end with NULL */
|
2004-08-02 17:09:06 +00:00
|
|
|
/*
|
|
|
|
union {
|
|
|
|
uint32_t PhysicalAddress;
|
|
|
|
uint32_t VirtualSize;
|
|
|
|
} AddrSize;
|
|
|
|
*/
|
|
|
|
uint32_t VirtualSize;
|
|
|
|
uint32_t VirtualAddress;
|
2009-11-24 14:53:15 +02:00
|
|
|
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 */
|
2004-08-02 17:09:06 +00:00
|
|
|
uint32_t Characteristics;
|
|
|
|
};
|
|
|
|
|
2018-08-27 22:53:23 -04:00
|
|
|
#define WIN_CERT_REV_2 0x0200
|
|
|
|
#define WIN_CERT_TYPE_PKCS7 0x0002
|
|
|
|
|
|
|
|
/** PE authenticode data header
|
|
|
|
\group_pe */
|
|
|
|
struct pe_certificate_hdr {
|
|
|
|
uint32_t length; /** length of the certificate data, including the header */
|
|
|
|
uint16_t revision;
|
|
|
|
uint16_t type;
|
|
|
|
};
|
|
|
|
|
2010-09-02 18:04:00 +03:00
|
|
|
/** Data for the bytecode PE hook
|
|
|
|
\group_pe */
|
2009-10-02 17:33:11 +03:00
|
|
|
struct cli_pe_hook_data {
|
2010-02-12 16:47:44 +02:00
|
|
|
uint32_t offset;
|
2010-05-07 09:57:30 +03:00
|
|
|
uint32_t ep; /**< EntryPoint as file offset */
|
|
|
|
uint16_t nsections;/**< Number of sections */
|
2010-03-24 10:41:11 +02:00
|
|
|
uint16_t dummy; /* align */
|
2010-05-07 09:57:30 +03:00
|
|
|
struct pe_image_file_hdr file_hdr;/**< Header for this PE file */
|
|
|
|
struct pe_image_optional_hdr32 opt32; /**< 32-bit PE optional header */
|
2010-03-24 10:41:11 +02:00
|
|
|
uint32_t dummy2; /* align */
|
2010-05-07 09:57:30 +03:00
|
|
|
struct pe_image_optional_hdr64 opt64;/**< 64-bit PE optional header */
|
|
|
|
struct pe_image_data_dir dirs[16]; /**< PE data directory header */
|
2010-02-12 16:47:44 +02:00
|
|
|
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 */
|
2009-10-02 17:33:11 +03:00
|
|
|
};
|
|
|
|
|
2010-07-30 15:54:15 +02:00
|
|
|
int cli_scanpe(cli_ctx *ctx);
|
2004-05-11 23:30:57 +00:00
|
|
|
|
2014-01-21 11:30:27 -05:00
|
|
|
#define CL_CHECKFP_PE_FLAG_NONE 0x00000000
|
|
|
|
#define CL_CHECKFP_PE_FLAG_STATS 0x00000001
|
|
|
|
#define CL_CHECKFP_PE_FLAG_AUTHENTICODE 0x00000002
|
|
|
|
|
2016-06-29 18:21:39 -04:00
|
|
|
enum {
|
|
|
|
CL_GENHASH_PE_CLASS_SECTION,
|
|
|
|
CL_GENHASH_PE_CLASS_IMPTBL,
|
|
|
|
/* place new class types above this line */
|
|
|
|
CL_GENHASH_PE_CLASS_LAST
|
|
|
|
};
|
|
|
|
|
2009-10-02 18:09:31 +02:00
|
|
|
int cli_peheader(fmap_t *map, struct cli_exe_info *peinfo);
|
2018-09-14 14:39:47 -04:00
|
|
|
cl_error_t cli_checkfp_pe(cli_ctx *ctx, stats_section_t *hashes, uint32_t flags);
|
2016-06-29 18:21:39 -04:00
|
|
|
int cli_genhash_pe(cli_ctx *ctx, unsigned int class, int type);
|
2004-08-27 00:19:44 +00:00
|
|
|
|
2010-02-15 18:12:02 +02:00
|
|
|
uint32_t cli_rawaddr(uint32_t, const struct cli_exe_section *, uint16_t, unsigned int *, size_t, uint32_t);
|
2010-01-04 14:56:04 +01:00
|
|
|
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 *);
|
2009-11-11 03:58:22 +01:00
|
|
|
|
2004-05-11 23:30:57 +00:00
|
|
|
#endif
|