libclamav/pe.c
e60810eb
 /*
d6bc83e4
  *  Copyright (C) 2004 - 2005 Tomasz Kojm <tkojm@clamav.net>
e60810eb
  *
3daea9cf
  *  With additions from aCaB <acab@clamav.net>
  *
e60810eb
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  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
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <time.h>
 
 #include "cltypes.h"
 #include "clamav.h"
 #include "others.h"
5ede41fa
 #include "pe.h"
f2eb2372
 #include "upx.h"
5ede41fa
 #include "petite.h"
3c5b57d4
 #include "fsg.h"
5ede41fa
 #include "scanners.h"
3daea9cf
 #include "rebuildpe.h"
d6bc83e4
 #include "str.h"
e60810eb
 
 #define IMAGE_DOS_SIGNATURE	    0x5a4d	    /* MZ */
3d9cb459
 #define IMAGE_DOS_SIGNATURE_OLD	    0x4d5a          /* ZM */
e60810eb
 #define IMAGE_NT_SIGNATURE	    0x00004550
 #define IMAGE_OPTIONAL_SIGNATURE    0x010b
 
ac4e01f9
 #define DETECT_BROKEN		    (options & CL_SCAN_BLOCKBROKEN)
d99b1840
 #define BLOCKMAX                    (options & CL_SCAN_BLOCKMAX)
f8355d13
 
3d9cb459
 #define UPX_NRV2B "\x11\xdb\x11\xc9\x01\xdb\x75\x07\x8b\x1e\x83\xee\xfc\x11\xdb\x11\xc9\x11\xc9\x75\x20\x41\x01\xdb"
 #define UPX_NRV2D "\x83\xf0\xff\x74\x78\xd1\xf8\x89\xc5\xeb\x0b\x01\xdb\x75\x07\x8b\x1e\x83\xee\xfc\x11\xdb\x11\xc9"
 #define UPX_NRV2E "\xeb\x52\x31\xc9\x83\xe8\x03\x72\x11\xc1\xe0\x08\x8a\x06\x46\x83\xf0\xff\x74\x75\xd1\xf8\x89\xc5"
f2eb2372
 
c2484690
 #if WORDS_BIGENDIAN == 0
 #define EC16(v)	(v)
 #define EC32(v) (v)
 #else
 static inline uint16_t EC16(uint16_t v)
 {
     return ((v >> 8) + (v << 8));
 }
 
 static inline uint32_t EC32(uint32_t v)
 {
     return ((v >> 24) | ((v & 0x00FF0000) >> 8) | ((v & 0x0000FF00) << 8) | (v << 24));
 }
 #endif
 
 extern short cli_leavetemps_flag;
 
f0635204
 struct offset_list {
     uint32_t offset;
     struct offset_list *next;
 };
 
a8b53539
 static uint32_t cli_rawaddr(uint32_t rva, struct pe_image_section_hdr *shp, uint16_t nos, unsigned int *err)
e3886d61
 {
 	int i, found = 0;
 
 
     for(i = 0; i < nos; i++) {
c2484690
 	if(EC32(shp[i].VirtualAddress) <= rva && EC32(shp[i].VirtualAddress) + EC32(shp[i].SizeOfRawData) > rva) {
e3886d61
 	    found = 1;
 	    break;
 	}
     }
 
     if(!found) {
a8b53539
 	*err = 1;
 	return 0;
e3886d61
     }
 
a8b53539
     *err = 0;
c2484690
     return rva - EC32(shp[i].VirtualAddress) + EC32(shp[i].PointerToRawData);
e3886d61
 }
 
a8b53539
 /*
e3886d61
 static int cli_ddump(int desc, int offset, int size, const char *file)
cea6b75d
 {
 	int pos, ndesc, bread, sum = 0;
 	char buff[FILEBUFF];
 
 
     cli_dbgmsg("in ddump()\n");
 
     if((pos = lseek(desc, 0, SEEK_CUR)) == -1) {
 	cli_dbgmsg("Invalid descriptor\n");
c2484690
 	return -1;
cea6b75d
     }
 
     if(lseek(desc, offset, SEEK_SET) == -1) {
 	cli_dbgmsg("lseek() failed\n");
 	lseek(desc, pos, SEEK_SET);
c2484690
 	return -1;
cea6b75d
     }
 
     if((ndesc = open(file, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU)) < 0) {
 	cli_dbgmsg("Can't create file %s\n", file);
 	lseek(desc, pos, SEEK_SET);
c2484690
 	return -1;
cea6b75d
     }
 
     while((bread = read(desc, buff, FILEBUFF)) > 0) {
 	if(sum + bread >= size) {
 	    if(write(ndesc, buff, size - sum) == -1) {
 		cli_dbgmsg("Can't write to file\n");
 		lseek(desc, pos, SEEK_SET);
 		close(ndesc);
 		unlink(file);
c2484690
 		return -1;
cea6b75d
 	    }
 	    break;
 	} else {
 	    if(write(ndesc, buff, bread) == -1) {
 		cli_dbgmsg("Can't write to file\n");
 		lseek(desc, pos, SEEK_SET);
 		close(ndesc);
 		unlink(file);
c2484690
 		return -1;
cea6b75d
 	    }
 	}
 	sum += bread;
     }
 
     close(ndesc);
     lseek(desc, pos, SEEK_SET);
     return 0;
 }
a8b53539
 */
cea6b75d
 
d99b1840
 int cli_scanpe(int desc, const char **virname, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, unsigned int arec, unsigned int mrec)
e60810eb
 {
 	uint16_t e_magic; /* DOS signature ("MZ") */
c2484690
 	uint16_t nsections;
e60810eb
 	uint32_t e_lfanew; /* address of new exe header */
e3886d61
 	uint32_t ep; /* entry point (raw) */
f0635204
 	uint8_t polipos = 0;
f91f55e0
 	time_t timestamp;
e60810eb
 	struct pe_image_file_hdr file_hdr;
 	struct pe_image_optional_hdr optional_hdr;
e3886d61
 	struct pe_image_section_hdr *section_hdr;
e60810eb
 	struct stat sb;
289ed7c4
 	char sname[9], buff[4096], *tempfile;
f0635204
 	unsigned char *ubuff;
 	ssize_t bytes;
b76bf2c4
 	unsigned int i, found, upx_success = 0, min = 0, max = 0, err, broken = 0;
 	unsigned int ssize = 0, dsize = 0, dll = 0;
d99b1840
 	int (*upxfn)(char *, uint32_t , char *, uint32_t *, uint32_t, uint32_t, uint32_t) = NULL;
0d32af73
 	char *src = NULL, *dest = NULL;
f0635204
 	int ndesc, ret = CL_CLEAN;
e60810eb
 
 
     if(read(desc, &e_magic, sizeof(e_magic)) != sizeof(e_magic)) {
cea6b75d
 	cli_dbgmsg("Can't read DOS signature\n");
1f576bce
 	return CL_CLEAN;
e60810eb
     }
 
3d9cb459
     if(EC16(e_magic) != IMAGE_DOS_SIGNATURE && EC16(e_magic) != IMAGE_DOS_SIGNATURE_OLD) {
e60810eb
 	cli_dbgmsg("Invalid DOS signature\n");
c2484690
 	return CL_CLEAN;
e60810eb
     }
 
     lseek(desc, 58, SEEK_CUR); /* skip to the end of the DOS header */
 
     if(read(desc, &e_lfanew, sizeof(e_lfanew)) != sizeof(e_lfanew)) {
cea6b75d
 	cli_dbgmsg("Can't read new header address\n");
1f576bce
 	/* truncated header? */
 	if(DETECT_BROKEN) {
 	    if(virname)
 		*virname = "Broken.Executable";
 	    return CL_VIRUS;
 	}
 	return CL_CLEAN;
e60810eb
     }
 
c2484690
     e_lfanew = EC32(e_lfanew);
e60810eb
     cli_dbgmsg("e_lfanew == %d\n", e_lfanew);
     if(!e_lfanew) {
 	cli_dbgmsg("Not a PE file\n");
c2484690
 	return CL_CLEAN;
e60810eb
     }
0b2b8b9b
 
     if(lseek(desc, e_lfanew, SEEK_SET) < 0) {
 	/* probably not a PE file */
 	cli_dbgmsg("Can't lseek to e_lfanew\n");
 	return CL_CLEAN;
     }
e60810eb
 
     if(read(desc, &file_hdr, sizeof(struct pe_image_file_hdr)) != sizeof(struct pe_image_file_hdr)) {
0b2b8b9b
 	/* bad information in e_lfanew - probably not a PE file */
e60810eb
 	cli_dbgmsg("Can't read file header\n");
0b2b8b9b
 	return CL_CLEAN;
e60810eb
     }
 
c2484690
     if(EC32(file_hdr.Magic) != IMAGE_NT_SIGNATURE) {
cea6b75d
 	cli_dbgmsg("Invalid PE signature (probably NE file)\n");
c2484690
 	return CL_CLEAN;
e60810eb
     }
 
b76bf2c4
     if(EC16(file_hdr.Characteristics) & 0x2000) {
 	cli_dbgmsg("File type: DLL\n");
 	dll = 1;
     } else if(EC16(file_hdr.Characteristics) & 0x01) {
 	cli_dbgmsg("File type: Executable\n");
     }
 
c2484690
     switch(EC16(file_hdr.Machine)) {
9c8806fb
 	case 0x0:
 	    cli_dbgmsg("Machine type: Unknown\n");
e60810eb
 	case 0x14c:
cea6b75d
 	    cli_dbgmsg("Machine type: 80386\n");
e60810eb
 	    break;
9c8806fb
 	case 0x14d:
cea6b75d
 	    cli_dbgmsg("Machine type: 80486\n");
e60810eb
 	    break;
9c8806fb
 	case 0x14e:
cea6b75d
 	    cli_dbgmsg("Machine type: 80586\n");
e60810eb
 	    break;
9c8806fb
 	case 0x160:
 	    cli_dbgmsg("Machine type: R30000 (big-endian)\n");
 	    break;
e60810eb
 	case 0x162:
cea6b75d
 	    cli_dbgmsg("Machine type: R3000\n");
e60810eb
 	    break;
 	case 0x166:
cea6b75d
 	    cli_dbgmsg("Machine type: R4000\n");
e60810eb
 	    break;
 	case 0x168:
cea6b75d
 	    cli_dbgmsg("Machine type: R10000\n");
e60810eb
 	    break;
 	case 0x184:
cea6b75d
 	    cli_dbgmsg("Machine type: DEC Alpha AXP\n");
e60810eb
 	    break;
da034cc1
 	case 0x284:
 	    cli_dbgmsg("Machine type: DEC Alpha AXP 64bit\n");
 	    break;
e60810eb
 	case 0x1f0:
cea6b75d
 	    cli_dbgmsg("Machine type: PowerPC\n");
e60810eb
 	    break;
da034cc1
 	case 0x200:
 	    cli_dbgmsg("Machine type: IA64\n");
 	    break;
 	case 0x268:
 	    cli_dbgmsg("Machine type: M68k\n");
 	    break;
 	case 0x266:
 	    cli_dbgmsg("Machine type: MIPS16\n");
 	    break;
 	case 0x366:
 	    cli_dbgmsg("Machine type: MIPS+FPU\n");
 	    break;
 	case 0x466:
 	    cli_dbgmsg("Machine type: MIPS16+FPU\n");
 	    break;
 	case 0x1a2:
 	    cli_dbgmsg("Machine type: Hitachi SH3\n");
 	    break;
9c8806fb
 	case 0x1a3:
 	    cli_dbgmsg("Machine type: Hitachi SH3-DSP\n");
 	    break;
 	case 0x1a4:
 	    cli_dbgmsg("Machine type: Hitachi SH3-E\n");
 	    break;
da034cc1
 	case 0x1a6:
 	    cli_dbgmsg("Machine type: Hitachi SH4\n");
 	    break;
9c8806fb
 	case 0x1a8:
 	    cli_dbgmsg("Machine type: Hitachi SH5\n");
 	    break;
da034cc1
 	case 0x1c0:
 	    cli_dbgmsg("Machine type: ARM\n");
 	    break;
 	case 0x1c2:
 	    cli_dbgmsg("Machine type: THUMB\n");
 	    break;
9c8806fb
 	case 0x1d3:
 	    cli_dbgmsg("Machine type: AM33\n");
 	    break;
 	case 0x520:
 	    cli_dbgmsg("Machine type: Infineon TriCore\n");
 	    break;
 	case 0xcef:
 	    cli_dbgmsg("Machine type: CEF\n");
 	    break;
 	case 0xebc:
 	    cli_dbgmsg("Machine type: EFI Byte Code\n");
 	    break;
 	case 0x9041:
 	    cli_dbgmsg("Machine type: M32R\n");
 	    break;
 	case 0xc0ee:
 	    cli_dbgmsg("Machine type: CEE\n");
 	    break;
 	case 0x8664:
 	    cli_dbgmsg("Machine type: AMD64\n");
 	    break;
e60810eb
 	default:
da034cc1
 	    cli_warnmsg("Unknown machine type in PE header (0x%x)\n", EC16(file_hdr.Machine));
e60810eb
     }
 
c2484690
     nsections = EC16(file_hdr.NumberOfSections);
e82a5185
     if(nsections < 1 || nsections > 99) {
f2284195
 	if(DETECT_BROKEN) {
 	    if(virname)
 		*virname = "Broken.Executable";
 	    return CL_VIRUS;
 	}
e82a5185
 	if(nsections)
 	    cli_warnmsg("PE file contains %d sections\n", nsections);
 	else
 	    cli_warnmsg("PE file contains no sections\n");
f2284195
 	return CL_CLEAN;
     }
c2484690
     cli_dbgmsg("NumberOfSections: %d\n", nsections);
 
f91f55e0
     timestamp = (time_t) EC32(file_hdr.TimeDateStamp);
     cli_dbgmsg("TimeDateStamp: %s", ctime(&timestamp));
e60810eb
 
c2484690
     cli_dbgmsg("SizeOfOptionalHeader: %d\n", EC16(file_hdr.SizeOfOptionalHeader));
e60810eb
 
c2484690
     if(EC16(file_hdr.SizeOfOptionalHeader) != sizeof(struct pe_image_optional_hdr)) {
d99b1840
 	/* Support for PE32+ binaries available in CVS */
c2484690
 	return CL_CLEAN;
e60810eb
     }
 
     if(read(desc, &optional_hdr, sizeof(struct pe_image_optional_hdr)) != sizeof(struct pe_image_optional_hdr)) {
 	cli_dbgmsg("Can't optional file header\n");
f8355d13
 	if(DETECT_BROKEN) {
 	    if(virname)
 		*virname = "Broken.Executable";
 	    return CL_VIRUS;
 	}
 	return CL_CLEAN;
e60810eb
     }
 
     cli_dbgmsg("MajorLinkerVersion: %d\n", optional_hdr.MajorLinkerVersion);
     cli_dbgmsg("MinorLinkerVersion: %d\n", optional_hdr.MinorLinkerVersion);
c2484690
     cli_dbgmsg("SizeOfCode: %d\n", EC32(optional_hdr.SizeOfCode));
     cli_dbgmsg("SizeOfInitializedData: %d\n", EC32(optional_hdr.SizeOfInitializedData));
     cli_dbgmsg("SizeOfUninitializedData: %d\n", EC32(optional_hdr.SizeOfUninitializedData));
     cli_dbgmsg("AddressOfEntryPoint: 0x%x\n", EC32(optional_hdr.AddressOfEntryPoint));
     cli_dbgmsg("SectionAlignment: %d\n", EC32(optional_hdr.SectionAlignment));
     cli_dbgmsg("FileAlignment: %d\n", EC32(optional_hdr.FileAlignment));
     cli_dbgmsg("MajorSubsystemVersion: %d\n", EC16(optional_hdr.MajorSubsystemVersion));
     cli_dbgmsg("MinorSubsystemVersion: %d\n", EC16(optional_hdr.MinorSubsystemVersion));
     cli_dbgmsg("SizeOfImage: %d\n", EC32(optional_hdr.SizeOfImage));
     cli_dbgmsg("SizeOfHeaders: %d\n", EC32(optional_hdr.SizeOfHeaders));
 
     switch(EC16(optional_hdr.Subsystem)) {
04268c03
 	case 0:
 	    cli_dbgmsg("Subsystem: Unknown\n");
 	    break;
e60810eb
 	case 1:
cea6b75d
 	    cli_dbgmsg("Subsystem: Native (a driver ?)\n");
e60810eb
 	    break;
 	case 2:
cea6b75d
 	    cli_dbgmsg("Subsystem: Win32 GUI\n");
e60810eb
 	    break;
 	case 3:
cea6b75d
 	    cli_dbgmsg("Subsystem: Win32 console\n");
e60810eb
 	    break;
 	case 5:
cea6b75d
 	    cli_dbgmsg("Subsystem: OS/2 console\n");
e60810eb
 	    break;
 	case 7:
cea6b75d
 	    cli_dbgmsg("Subsystem: POSIX console\n");
e60810eb
 	    break;
da034cc1
 	case 8:
 	    cli_dbgmsg("Subsystem: Native Win9x driver\n");
 	    break;
 	case 9:
 	    cli_dbgmsg("Subsystem: WinCE GUI\n");
 	    break;
 	case 10:
 	    cli_dbgmsg("Subsystem: EFI application\n");
 	    break;
 	case 11:
 	    cli_dbgmsg("Subsystem: EFI driver\n");
 	    break;
 	case 12:
 	    cli_dbgmsg("Subsystem: EFI runtime driver\n");
 	    break;
e60810eb
 	default:
04268c03
 	    cli_warnmsg("Unknown subsystem in PE header (0x%x)\n", EC16(optional_hdr.Subsystem));
e60810eb
     }
 
c2484690
     cli_dbgmsg("NumberOfRvaAndSizes: %d\n", EC32(optional_hdr.NumberOfRvaAndSizes));
5ede41fa
     cli_dbgmsg("------------------------------------\n");
e60810eb
 
e263999b
     if(fstat(desc, &sb) == -1) {
 	cli_dbgmsg("fstat failed\n");
 	return CL_EIO;
     }
 
c2484690
     section_hdr = (struct pe_image_section_hdr *) cli_calloc(nsections, sizeof(struct pe_image_section_hdr));
e3886d61
 
     if(!section_hdr) {
 	cli_dbgmsg("Can't allocate memory for section headers\n");
 	return CL_EMEM;
     }
 
c2484690
     for(i = 0; i < nsections; i++) {
e60810eb
 
e3886d61
 	if(read(desc, &section_hdr[i], sizeof(struct pe_image_section_hdr)) != sizeof(struct pe_image_section_hdr)) {
e60810eb
 	    cli_dbgmsg("Can't read section header\n");
55ab2cea
 	    cli_dbgmsg("Possibly broken PE file\n");
e3886d61
 	    free(section_hdr);
f8355d13
 	    if(DETECT_BROKEN) {
 		if(virname)
 		    *virname = "Broken.Executable";
 		return CL_VIRUS;
 	    }
f2eb2372
 	    return CL_CLEAN;
e60810eb
 	}
 
e3886d61
 	strncpy(sname, section_hdr[i].Name, 8);
e60810eb
 	sname[8] = 0;
8ce7d5f5
 	cli_dbgmsg("Section %d\n", i);
e60810eb
 	cli_dbgmsg("Section name: %s\n", sname);
c2484690
 	cli_dbgmsg("VirtualSize: %d\n", EC32(section_hdr[i].VirtualSize));
 	cli_dbgmsg("VirtualAddress: 0x%x\n", EC32(section_hdr[i].VirtualAddress));
 	cli_dbgmsg("SizeOfRawData: %d\n", EC32(section_hdr[i].SizeOfRawData));
 	cli_dbgmsg("PointerToRawData: 0x%x (%d)\n", EC32(section_hdr[i].PointerToRawData), EC32(section_hdr[i].PointerToRawData));
e60810eb
 
c2484690
 	if(EC32(section_hdr[i].Characteristics) & 0x20) {
cea6b75d
 	    cli_dbgmsg("Section contains executable code\n");
 
c2484690
 	    if(EC32(section_hdr[i].VirtualSize) < EC32(section_hdr[i].SizeOfRawData)) {
cea6b75d
 		cli_dbgmsg("Section contains free space\n");
 		/*
 		cli_dbgmsg("Dumping %d bytes\n", section_hdr.SizeOfRawData - section_hdr.VirtualSize);
f91f55e0
 		ddump(desc, section_hdr.PointerToRawData + section_hdr.VirtualSize, section_hdr.SizeOfRawData - section_hdr.VirtualSize, cli_gentemp(NULL));
cea6b75d
 		*/
 
 	    }
 	}
e60810eb
 
c2484690
 	if(EC32(section_hdr[i].Characteristics) & 0x20000000)
cea6b75d
 	    cli_dbgmsg("Section's memory is executable\n");
b76bf2c4
 
 	if(EC32(section_hdr[i].Characteristics) & 0x80000000)
 	    cli_dbgmsg("Section's memory is writeable\n");
 
5ede41fa
 	cli_dbgmsg("------------------------------------\n");
cea6b75d
 
a8b53539
 	if(EC32(section_hdr[i].PointerToRawData) + EC32(section_hdr[i].SizeOfRawData) > (unsigned long int) sb.st_size) {
f8355d13
 	    cli_dbgmsg("Possibly broken PE file - Section %d out of file (Offset@ %d, Rsize %d, Total filesize %d)\n", i, EC32(section_hdr[i].PointerToRawData), EC32(section_hdr[i].SizeOfRawData), sb.st_size);
 	    if(DETECT_BROKEN) {
 		if(virname)
 		    *virname = "Broken.Executable";
b76bf2c4
 		free(section_hdr);
f8355d13
 		return CL_VIRUS;
 	    }
b76bf2c4
 	    broken = 1;
f8355d13
 	}
 
5ede41fa
 	if(!i) {
 	    min = EC32(section_hdr[i].VirtualAddress);
 	    max = EC32(section_hdr[i].VirtualAddress) + EC32(section_hdr[i].SizeOfRawData);
 	} else {
 	    if(EC32(section_hdr[i].VirtualAddress) < min)
 		min = EC32(section_hdr[i].VirtualAddress);
cea6b75d
 
5ede41fa
 	    if(EC32(section_hdr[i].VirtualAddress) + EC32(section_hdr[i].SizeOfRawData) > max)
 		max = EC32(section_hdr[i].VirtualAddress) + EC32(section_hdr[i].SizeOfRawData);
cea6b75d
 	}
e60810eb
 
f0635204
 	if(!strlen(sname)) {
 	    if(EC32(section_hdr[i].VirtualSize) > 40000 && EC32(section_hdr[i].VirtualSize) < 70000) {
 		if(EC32(section_hdr[i].Characteristics) == 0xe0000060)
 		    polipos = i;
 	    }
 	}
e60810eb
     }
 
a8b53539
     if((ep = EC32(optional_hdr.AddressOfEntryPoint)) >= min && !(ep = cli_rawaddr(EC32(optional_hdr.AddressOfEntryPoint), section_hdr, nsections, &err)) && err) {
5ede41fa
 	cli_dbgmsg("Possibly broken PE file\n");
e3886d61
 	free(section_hdr);
f8355d13
 	if(DETECT_BROKEN) {
 	    if(virname)
 		*virname = "Broken.Executable";
 	    return CL_VIRUS;
 	}
f2eb2372
 	return CL_CLEAN;
e60810eb
     }
 
f2eb2372
     cli_dbgmsg("EntryPoint offset: 0x%x (%d)\n", ep, ep);
 
d6bc83e4
     /* Attempt to detect some popular polymorphic viruses */
 
     /* W32.Parite.B */
b76bf2c4
     if(!dll && ep == EC32(section_hdr[nsections - 1].PointerToRawData)) {
3d4cb247
 	lseek(desc, ep, SEEK_SET);
d6bc83e4
 	if(read(desc, buff, 4096) == 4096) {
f58ad3be
 		const char *pt = cli_memstr(buff, 4040, "\x47\x65\x74\x50\x72\x6f\x63\x41\x64\x64\x72\x65\x73\x73\x00", 15);
d6bc83e4
 	    if(pt) {
 		    uint32_t dw1, dw2;
 
 		pt += 15;
 		if(((dw1 = cli_readint32(pt)) ^ (dw2 = cli_readint32(pt + 4))) == 0x505a4f && ((dw1 = cli_readint32(pt + 8)) ^ (dw2 = cli_readint32(pt + 12))) == 0xffffb && ((dw1 = cli_readint32(pt + 16)) ^ (dw2 = cli_readint32(pt + 20))) == 0xb8) {
 		    *virname = "W32.Parite.B";
98454000
 		    free(section_hdr);
d6bc83e4
 		    return CL_VIRUS;
 		}
 	    }
 	}
     }
 
b76bf2c4
     /* W32.Magistr.A/B */
     if(!dll && (EC32(section_hdr[nsections - 1].Characteristics) & 0x80000000)) {
 	    uint32_t rsize, vsize;
 
 	rsize = EC32(section_hdr[nsections - 1].SizeOfRawData);
 	vsize = EC32(section_hdr[nsections - 1].VirtualSize);
 
 	if(rsize >= 0x612c && vsize >= 0x612c && ((vsize & 0xff) == 0xec)) {
 		int bw = rsize < 0x7000 ? rsize : 0x7000;
 
 	    lseek(desc, EC32(section_hdr[nsections - 1].PointerToRawData) + rsize - bw, SEEK_SET);
 	    if(read(desc, buff, 4096) == 4096) {
 		if(cli_memstr(buff, 4091, "\xe8\x2c\x61\x00\x00", 5)) {
 		    *virname = "W32.Magistr.A";
 		    free(section_hdr);
 		    return CL_VIRUS;
 		} 
 	    }
 
 	} else if(rsize >= 0x7000 && vsize >= 0x7000 && ((vsize & 0xff) == 0xed)) {
 		int bw = rsize < 0x8000 ? rsize : 0x8000;
 
 	    lseek(desc, EC32(section_hdr[nsections - 1].PointerToRawData) + rsize - bw, SEEK_SET);
 	    if(read(desc, buff, 4096) == 4096) {
 		if(cli_memstr(buff, 4091, "\xe8\x04\x72\x00\x00", 5)) {
 		    *virname = "W32.Magistr.B";
 		    free(section_hdr);
 		    return CL_VIRUS;
 		} 
 	    }
 	}
     }
 
f0635204
     /* W32.Polipos.A */
    if(polipos && !dll && nsections > 2 && nsections < 13 && e_lfanew <= 0x800 && (EC16(optional_hdr.Subsystem) == 2 || EC16(optional_hdr.Subsystem) == 3) && EC16(file_hdr.Machine) == 0x14c && optional_hdr.SizeOfStackReserve >= 0x80000) {
 	    uint32_t remaining = EC32(section_hdr[0].SizeOfRawData);
 	    uint32_t chunk = sizeof(buff);
 	    uint32_t val, shift, raddr, curroff, total = 0;
 	    const char *jpt;
 	    struct offset_list *offlist = NULL, *offnode;
 
 
 	cli_dbgmsg("Detected W32.Polipos.A characteristics\n");
 
 	if(remaining < chunk)
 	    chunk = remaining;
 
 	lseek(desc, EC32(section_hdr[0].PointerToRawData), SEEK_SET);
 	while((bytes = cli_readn(desc, buff, chunk)) > 0) {
 	    shift = 0;
 	    while(bytes - 5 > shift) {
 		jpt = buff + shift;
 		if(*jpt != '\xe9' && *jpt != '\xe8') {
 		    shift++;
 		    continue;
 		}
 		val = cli_readint32(jpt + 1);
 		val += 5 + EC32(section_hdr[0].VirtualAddress) + total + shift;
 		raddr = cli_rawaddr(val, section_hdr, nsections, &err);
 
 		if(!err && (raddr >= EC32(section_hdr[polipos].PointerToRawData) && raddr < EC32(section_hdr[polipos].PointerToRawData) + EC32(section_hdr[polipos].SizeOfRawData)) && (!offlist || (raddr != offlist->offset))) {
 		    offnode = (struct offset_list *) cli_malloc(sizeof(struct offset_list));
 		    if(!offnode) {
 			free(section_hdr);
 			while(offlist) {
 			    offnode = offlist;
 			    offlist = offlist->next;
 			    free(offnode);
 			}
 			return CL_EMEM;
 		    }
 		    offnode->offset = raddr;
 		    offnode->next = offlist;
 		    offlist = offnode;
 		}
 
 		shift++;
 	    }
 
 	    if(remaining < chunk) {
 		chunk = remaining;
 	    } else {
 		remaining -= bytes;
 		if(remaining < chunk) {
 		    chunk = remaining;
 		}
 	    }
 
 	    if(!remaining)
 		break;
 
 	    total += bytes;
 	}
 
 	offnode = offlist;
 	while(offnode) {
 	    cli_dbgmsg("Polipos: Checking offset 0x%x (%u) - ", offnode->offset, offnode->offset);
 	    lseek(desc, offnode->offset, SEEK_SET);
 	    if(cli_readn(desc, buff, 9) == 9) {
 		ubuff = (unsigned char *) buff;
 		if(ubuff[0] == 0x55 && ubuff[1] == 0x8b && ubuff[2] == 0xec &&
 		   ((ubuff[3] == 0x83 && ubuff[4] == 0xec && ubuff[6] == 0x60) ||  ubuff[3] == 0x60 ||
 		    (ubuff[3] == 0x81 && ubuff[4] == 0xec && ubuff[7] == 0x00 && ubuff[8] == 0x00))) {
 		    ret = CL_VIRUS;
 		    *virname = "W32.Polipos.A";
 		    break;
 		}
 	    }
 	    offnode = offnode->next;
 	}
 
 	while(offlist) {
 	    offnode = offlist;
 	    offlist = offlist->next;
 	    free(offnode);
 	}
 
 	if(ret == CL_VIRUS) {
 	    free(section_hdr);
 	    return CL_VIRUS;
 	}
     }
 
 
9c8806fb
     if(broken) {
 	free(section_hdr);
b76bf2c4
 	return CL_CLEAN;
9c8806fb
     }
b76bf2c4
 
3c5b57d4
     /* UPX & FSG support */
c2484690
 
     /* try to find the first section with physical size == 0 */
     found = 0;
a8b53539
     for(i = 0; i < (unsigned int) nsections - 1; i++) {
3c5b57d4
 	if(!section_hdr[i].SizeOfRawData && section_hdr[i].VirtualSize && section_hdr[i + 1].SizeOfRawData && section_hdr[i + 1].VirtualSize) {
c2484690
 	    found = 1;
3c5b57d4
 	    cli_dbgmsg("UPX/FSG: empty section found - assuming compression\n");
c2484690
 	    break;
 	}
     }
 
     if(found) {
 
3c5b57d4
 	/* Check EP for UPX vs. FSG */
 	if(lseek(desc, ep, SEEK_SET) == -1) {
 	    cli_dbgmsg("UPX/FSG: lseek() failed\n");
9c3920b9
 	    free(section_hdr);
3c5b57d4
 	    return CL_EIO;
5115a5eb
 	}
 
3daea9cf
         if(read(desc, buff, 168) != 168) {
6bd80fe3
 	    cli_dbgmsg("UPX/FSG: Can't read 168 bytes at 0x%x (%d)\n", ep, ep);
b5666f50
 	    cli_dbgmsg("UPX/FSG: Broken or not UPX/FSG compressed file\n");
3c5b57d4
             free(section_hdr);
b5666f50
 	    return CL_CLEAN;
fbbf7cd2
 	}
 
3daea9cf
 	if(buff[0] == '\x87' && buff[1] == '\x25') {
f2eb2372
 
3daea9cf
 	    /* FSG v2.0 support - thanks to aCaB ! */
f2eb2372
 
3c5b57d4
 	    ssize = EC32(section_hdr[i + 1].SizeOfRawData);
 	    dsize = EC32(section_hdr[i].VirtualSize);
f2eb2372
 
3c5b57d4
 	    while(found) {
 		    uint32_t newesi, newedi, newebx, newedx;
5115a5eb
 
3c5b57d4
 		if(limits && limits->maxfilesize && (ssize > limits->maxfilesize || dsize > limits->maxfilesize)) {
e82a5185
 		    cli_dbgmsg("FSG: Sizes exceeded (ssize: %u, dsize: %u, max: %lu)\n", ssize, dsize , limits->maxfilesize);
3c5b57d4
 		    free(section_hdr);
d99b1840
 		    if(BLOCKMAX) {
 			*virname = "PE.FSG.ExceededFileSize";
 		         return CL_VIRUS;
 		    } else {
 		         return CL_CLEAN;
 		    }
3c5b57d4
 		}
5115a5eb
 
3c5b57d4
 		if(ssize <= 0x19 || dsize <= ssize) {
 		    cli_dbgmsg("FSG: Size mismatch (ssize: %d, dsize: %d)\n", ssize, dsize);
 		    free(section_hdr);
 		    return CL_CLEAN;
 		}
 
 		if((newedx = cli_readint32(buff + 2) - EC32(optional_hdr.ImageBase)) < EC32(section_hdr[i + 1].VirtualAddress) || newedx >= EC32(section_hdr[i + 1].VirtualAddress) + EC32(section_hdr[i + 1].SizeOfRawData) - 4) {
 		    cli_dbgmsg("FSG: xchg out of bounds (%x), giving up\n", newedx);
 		    break;
 		}
 
 		if((src = (char *) cli_malloc(ssize)) == NULL) {
 		    free(section_hdr);
 		    return CL_EMEM;
 		}
 
 		lseek(desc, EC32(section_hdr[i + 1].PointerToRawData), SEEK_SET);
a8b53539
 		if((unsigned int) read(desc, src, ssize) != ssize) {
3c5b57d4
 		    cli_dbgmsg("Can't read raw data of section %d\n", i);
 		    free(section_hdr);
 		    free(src);
 		    return CL_EIO;
 		}
 
a8b53539
 		if(newedx < EC32(section_hdr[i + 1].VirtualAddress) || ((dest = src + newedx - EC32(section_hdr[i + 1].VirtualAddress)) < src && dest >= src + EC32(section_hdr[i + 1].VirtualAddress) + EC32(section_hdr[i + 1].SizeOfRawData) - 4)) {
3c5b57d4
 		    cli_dbgmsg("FSG: New ESP out of bounds\n");
 		    free(src);
 		    break;
 		}
 
 		if((newedx = cli_readint32(dest) - EC32(optional_hdr.ImageBase)) <= EC32(section_hdr[i + 1].VirtualAddress) || newedx >= EC32(section_hdr[i + 1].VirtualAddress) + EC32(section_hdr[i + 1].SizeOfRawData) - 4) {
 		    cli_dbgmsg("FSG: New ESP (%x) is wrong\n", newedx);
 		    free(src);
 		    break;
 		}
  
 		if((dest = src + newedx - EC32(section_hdr[i + 1].VirtualAddress)) < src || dest >= src + EC32(section_hdr[i + 1].VirtualAddress) + EC32(section_hdr[i + 1].SizeOfRawData) - 32) {
 		    cli_dbgmsg("FSG: New stack out of bounds\n");
 		    free(src);
 		    break;
 		}
 
 		newedi = cli_readint32(dest) - EC32(optional_hdr.ImageBase);
 		newesi = cli_readint32(dest + 4) - EC32(optional_hdr.ImageBase);
 		newebx = cli_readint32(dest + 16) - EC32(optional_hdr.ImageBase);
 		newedx = cli_readint32(dest + 20);
 
 		if(newedi != EC32(section_hdr[i].VirtualAddress)) {
 		    cli_dbgmsg("FSG: Bad destination buffer (edi is %x should be %x)\n", newedi, EC32(section_hdr[i].VirtualAddress));
 		    free(src);
 		    break;
 		}
 
 		if(newesi < EC32(section_hdr[i + 1].VirtualAddress) || newesi >= EC32(section_hdr[i + 1].VirtualAddress) + EC32(section_hdr[i + 1].SizeOfRawData)) {
 		    cli_dbgmsg("FSG: Source buffer out of section bounds\n");
 		    free(src);
 		    break;
 		}
 
 		if(newebx < EC32(section_hdr[i + 1].VirtualAddress) || newebx >= EC32(section_hdr[i + 1].VirtualAddress) + EC32(section_hdr[i + 1].SizeOfRawData) - 16) {
 		    cli_dbgmsg("FSG: Array of functions out of bounds\n");
 		    free(src);
 		    break;
 		}
 
d99b1840
 		newedx=cli_readint32(newebx + 12 - EC32(section_hdr[i + 1].VirtualAddress) + src) - EC32(optional_hdr.ImageBase);
 		cli_dbgmsg("FSG: found old EP @%x\n",newedx);
3c5b57d4
 
 		if((dest = (char *) cli_calloc(dsize, sizeof(char))) == NULL) {
 		    free(section_hdr);
 		    free(src);
 		    return CL_EMEM;
 		}
 
d99b1840
 		tempfile = cli_gentemp(NULL);
 		if((ndesc = open(tempfile, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU)) < 0) {
 		  cli_dbgmsg("FSG: Can't create file %s\n", tempfile);
 		  free(tempfile);
 		  free(section_hdr);
 		  free(src);
 		  free(dest);
 		  return CL_EIO;
                 }
 
 		switch (unfsg_200(newesi - EC32(section_hdr[i + 1].VirtualAddress) + src, dest, ssize + EC32(section_hdr[i + 1].VirtualAddress) - newesi, dsize, newedi, EC32(optional_hdr.ImageBase), newedx, ndesc)) {
 		case 1: /* Everything OK */
 		  cli_dbgmsg("FSG: Unpacked and rebuilt executable saved in %s\n", tempfile);
 		  free(src);
 		  free(dest);
 		  fsync(ndesc);
 		  lseek(ndesc, 0, SEEK_SET);
 
 		  cli_dbgmsg("***** Scanning rebuilt PE file *****\n");
 		  if(cli_magic_scandesc(ndesc, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
 		    free(section_hdr);
 		    close(ndesc);
 		    if(!cli_leavetemps_flag)
 		      unlink(tempfile);
 		    free(tempfile);
 		    return CL_VIRUS;
 		  }
 		  close(ndesc);
 		  if(!cli_leavetemps_flag)
 		    unlink(tempfile);
 		  free(tempfile);
 		  free(section_hdr);
 		  return CL_CLEAN;
 
 		case 0: /* We've got an unpacked buffer, no exe though */
 		  cli_dbgmsg("FSG: Successfully decompressed\n");
 		  close(ndesc);
 		  free(tempfile);
 		  found = 0;
 		  upx_success = 1;
 		  break; /* Go and scan the buffer! */
 
 		default: /* Everything gone wrong */
 		  cli_dbgmsg("FSG: Unpacking failed\n");
 		  close(ndesc);
 		  free(tempfile);
 		  free(src);
 		  free(dest);
 		  break;
3c5b57d4
 		}
 
d99b1840
 		break; /* were done with 2 */
5115a5eb
 	    }
 	}
 
3daea9cf
  	if(found && buff[0] == '\xbe' && cli_readint32(buff + 1) - EC32(optional_hdr.ImageBase) < min) {
 
 	    /* FSG support - v. 1.33 (thx trog for the many samples) */
 
 	    ssize = EC32(section_hdr[i + 1].SizeOfRawData);
 	    dsize = EC32(section_hdr[i].VirtualSize);
 
 	    while(found) {
6bd80fe3
 	            int gp, t, sectcnt = 0;
3daea9cf
 		    char *support;
 		    uint32_t newesi, newedi, newebx, oldep;
 		    struct SECTION *sections;
 
 
 		if(limits && limits->maxfilesize && (ssize > limits->maxfilesize || dsize > limits->maxfilesize)) {
e82a5185
 		    cli_dbgmsg("FSG: Sizes exceeded (ssize: %u, dsize: %u, max: %lu)\n", ssize, dsize, limits->maxfilesize);
3daea9cf
 		    free(section_hdr);
d99b1840
 		    if(BLOCKMAX) {
 			*virname = "PE.FSG.ExceededFileSize";
 		         return CL_VIRUS;
 		    } else {
 		         return CL_CLEAN;
 		    }
3daea9cf
 		}
 
 		if(ssize <= 0x19 || dsize <= ssize) {
 		    cli_dbgmsg("FSG: Size mismatch (ssize: %d, dsize: %d)\n", ssize, dsize);
 		    free(section_hdr);
 		    return CL_CLEAN;
 		}
 
a8b53539
 		if((gp = cli_readint32(buff + 1) - EC32(optional_hdr.ImageBase)) >= (int) EC32(section_hdr[i + 1].PointerToRawData) || gp < 0) {
0d32af73
 		    cli_dbgmsg("FSG: Support data out of padding area (vaddr: %d)\n", EC32(section_hdr[i].VirtualAddress));
3daea9cf
 		    break;
 		}
 
6bd80fe3
 		lseek(desc, gp, SEEK_SET);
 		gp = EC32(section_hdr[i + 1].PointerToRawData) - gp;
 
a8b53539
 		if(limits && limits->maxfilesize && (unsigned int) gp > limits->maxfilesize) {
3daea9cf
 		    cli_dbgmsg("FSG: Buffer size exceeded (size: %d, max: %lu)\n", gp, limits->maxfilesize);
 		    free(section_hdr);
d99b1840
 		    if(BLOCKMAX) {
 			*virname = "PE.FSG.ExceededFileSize";
 		         return CL_VIRUS;
 		    } else {
 		         return CL_CLEAN;
 		    }
3daea9cf
 		}
 
 		if((support = (char *) cli_malloc(gp)) == NULL) {
 		    free(section_hdr);
 		    return CL_EMEM;
 		}
 
 		if(read(desc, support, gp) != gp) {
 		    cli_dbgmsg("Can't read %d bytes from padding area\n", gp); 
 		    free(section_hdr);
 		    free(support);
 		    return CL_EIO;
 		}
 
 		newebx = cli_readint32(support) - EC32(optional_hdr.ImageBase); /* Unused */
 		newedi = cli_readint32(support + 4) - EC32(optional_hdr.ImageBase); /* 1st dest */
 		newesi = cli_readint32(support + 8) - EC32(optional_hdr.ImageBase); /* Source */
 
9c8806fb
 		if(newesi < EC32(section_hdr[i + 1].VirtualAddress) || newesi >= EC32(section_hdr[i + 1].VirtualAddress) + EC32(section_hdr[i + 1].SizeOfRawData)) {
3daea9cf
 		    cli_dbgmsg("FSG: Source buffer out of section bounds\n");
 		    free(support);
 		    break;
 		}
 
 		if(newedi != EC32(section_hdr[i].VirtualAddress)) {
 		    cli_dbgmsg("FSG: Bad destination (is %x should be %x)\n", newedi, EC32(section_hdr[i].VirtualAddress));
 		    free(support);
 		    break;
 		}
 
 		/* Counting original sections */
 		for(t = 12; t < gp - 4; t += 4) {
 			uint32_t rva = cli_readint32(support+t);
 
 		    if(!rva)
 			break;
 
 		    rva -= EC32(optional_hdr.ImageBase)+1;
 		    sectcnt++;
 
 		    if(rva % 0x1000)
 			/* FIXME: really need to bother? */
 			cli_dbgmsg("FSG: Original section %d is misaligned\n", sectcnt);
 
 		    if(rva < EC32(section_hdr[i].VirtualAddress) || rva >= EC32(section_hdr[i].VirtualAddress)+EC32(section_hdr[i].VirtualSize)) {
 			cli_dbgmsg("FSG: Original section %d is out of bounds\n", sectcnt);
 			break;
 		    }
 		}
 
d8f0a45c
 		if(t >= gp - 4 || cli_readint32(support + t)) {
3daea9cf
 		    free(support);
 		    break;
 		}
 
 		if((sections = (struct SECTION *) cli_malloc((sectcnt + 1) * sizeof(struct SECTION))) == NULL) {
 		    free(section_hdr);
 		    free(support);
 		    return CL_EMEM;
 		}
 
 		sections[0].rva = newedi;
 		for(t = 1; t <= sectcnt; t++)
 		    sections[t].rva = cli_readint32(support + 8 + t * 4) - 1 -EC32(optional_hdr.ImageBase);
 
 		free(support);
 
 		if((src = (char *) cli_malloc(ssize)) == NULL) {
 		    free(section_hdr);
 		    free(sections);
 		    return CL_EMEM;
 		}
 
 		lseek(desc, EC32(section_hdr[i + 1].PointerToRawData), SEEK_SET);
a8b53539
 		if((unsigned int) read(desc, src, ssize) != ssize) {
3daea9cf
 		    cli_dbgmsg("Can't read raw data of section %d\n", i);
 		    free(section_hdr);
 		    free(sections);
 		    free(src);
 		    return CL_EIO;
 		}
 
 		if((dest = (char *) cli_calloc(dsize, sizeof(char))) == NULL) {
 		    free(section_hdr);
 		    free(src);
 		    free(sections);
 		    return CL_EMEM;
 		}
 
 		oldep = EC32(optional_hdr.AddressOfEntryPoint) + 161 + 6 + cli_readint32(buff+163);
 		cli_dbgmsg("FSG: found old EP @%x\n", oldep);
 
 		tempfile = cli_gentemp(NULL);
 		if((ndesc = open(tempfile, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU)) < 0) {
 		    cli_dbgmsg("FSG: Can't create file %s\n", tempfile);
 		    free(tempfile);
 		    free(section_hdr);
 		    free(src);
 		    free(dest);
 		    free(sections);
 		    return CL_EIO;
 		}
 
 		switch(unfsg_133(src + newesi - EC32(section_hdr[i + 1].VirtualAddress), dest, ssize + EC32(section_hdr[i + 1].VirtualAddress) - newesi, dsize, sections, sectcnt, EC32(optional_hdr.ImageBase), oldep, ndesc)) {
 		    case 1: /* Everything OK */
 			cli_dbgmsg("FSG: Unpacked and rebuilt executable saved in %s\n", tempfile);
 			free(src);
 			free(dest);
 			free(sections);
 			fsync(ndesc);
 			lseek(ndesc, 0, SEEK_SET);
 
7c00689d
 			cli_dbgmsg("***** Scanning rebuilt PE file *****\n");
3daea9cf
 			if(cli_magic_scandesc(ndesc, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
 			    free(section_hdr);
 			    close(ndesc);
 			    if(!cli_leavetemps_flag)
 				unlink(tempfile);
 			    free(tempfile);
 			    return CL_VIRUS;
 			}
 
 			close(ndesc);
 			if(!cli_leavetemps_flag)
 			    unlink(tempfile);
 			free(tempfile);
 			free(section_hdr);
 			return CL_CLEAN;
 
 		    case 0: /* We've got an unpacked buffer, no exe though */
 			cli_dbgmsg("FSG: FSG: Successfully decompressed\n");
 			close(ndesc);
 			unlink(tempfile);
 			free(tempfile);
 			free(sections);
 			found = 0;
 			upx_success = 1;
 			break; /* Go and scan the buffer! */
 
 		    default: /* Everything gone wrong */
 			cli_dbgmsg("FSG: Unpacking failed\n");
 			close(ndesc);
 			unlink(tempfile); // It's empty anyway
 			free(tempfile);
 			free(src);
 			free(dest);
 			free(sections);
 			break;
 		}
 
 		break; /* were done with 1.33 */
 	    }
 	}
 
6bd80fe3
 	/* FIXME: easy 2 hack */
  	if(found && buff[0] == '\xbb' && cli_readint32(buff + 1) - EC32(optional_hdr.ImageBase) < min && buff[5] == '\xbf' && buff[10] == '\xbe') {
 
 	    /* FSG support - v. 1.31 */
 
 	    ssize = EC32(section_hdr[i + 1].SizeOfRawData);
 	    dsize = EC32(section_hdr[i].VirtualSize);
 
 	    while(found) {
 		    int gp = cli_readint32(buff+1) - EC32(optional_hdr.ImageBase), t, sectcnt = 0;
 		    char *support;
 		    uint32_t newesi = cli_readint32(buff+11) - EC32(optional_hdr.ImageBase);
 		    uint32_t newedi = cli_readint32(buff+6) - EC32(optional_hdr.ImageBase);
 		    uint32_t oldep = EC32(optional_hdr.AddressOfEntryPoint);
 		    struct SECTION *sections;
 
 	        if (oldep <= EC32(section_hdr[i + 1].VirtualAddress) || oldep > EC32(section_hdr[i + 1].VirtualAddress)+EC32(section_hdr[i + 1].SizeOfRawData) - 0xe0) {
 		  cli_dbgmsg("FSG: EP not in section %d\n", i+1);
 		  break;
 		}
 		oldep -= EC32(section_hdr[i + 1].VirtualAddress);
 
9c8806fb
 		if(newesi < EC32(section_hdr[i + 1].VirtualAddress) || newesi >= EC32(section_hdr[i + 1].VirtualAddress) + EC32(section_hdr[i + 1].SizeOfRawData)) {
6bd80fe3
 		    cli_dbgmsg("FSG: Source buffer out of section bounds\n");
 		    break;
 		}
 
 		if(newedi != EC32(section_hdr[i].VirtualAddress)) {
 		    cli_dbgmsg("FSG: Bad destination (is %x should be %x)\n", newedi, EC32(section_hdr[i].VirtualAddress));
 		    break;
 		}
 
 		if(limits && limits->maxfilesize && (ssize > limits->maxfilesize || dsize > limits->maxfilesize)) {
e82a5185
 		    cli_dbgmsg("FSG: Sizes exceeded (ssize: %u, dsize: %u, max: %lu)\n", ssize, dsize, limits->maxfilesize);
6bd80fe3
 		    free(section_hdr);
d99b1840
 		    if(BLOCKMAX) {
 			*virname = "PE.FSG.ExceededFileSize";
 		         return CL_VIRUS;
 		    } else {
 		         return CL_CLEAN;
 		    }
6bd80fe3
 		}
 
 		if(ssize <= 0x19 || dsize <= ssize) {
 		    cli_dbgmsg("FSG: Size mismatch (ssize: %d, dsize: %d)\n", ssize, dsize);
 		    free(section_hdr);
 		    return CL_CLEAN;
 		}
 
a8b53539
 		if(gp >= (int) EC32(section_hdr[i + 1].PointerToRawData) || gp < 0) {
6bd80fe3
 		    cli_dbgmsg("FSG: Support data out of padding area (newedi: %d, vaddr: %d)\n", newedi, EC32(section_hdr[i].VirtualAddress));
 		    break;
 		}
 
 		lseek(desc, gp, SEEK_SET);
 		gp = EC32(section_hdr[i + 1].PointerToRawData) - gp;
 
a8b53539
 		if(limits && limits->maxfilesize && (unsigned int) gp > limits->maxfilesize) {
6bd80fe3
 		    cli_dbgmsg("FSG: Buffer size exceeded (size: %d, max: %lu)\n", gp, limits->maxfilesize);
 		    free(section_hdr);
d99b1840
 		    if(BLOCKMAX) {
 			*virname = "PE.FSG.ExceededFileSize";
 		         return CL_VIRUS;
 		    } else {
 		         return CL_CLEAN;
 		    }
6bd80fe3
 		}
 
 		if((support = (char *) cli_malloc(gp)) == NULL) {
 		    free(section_hdr);
 		    return CL_EMEM;
 		}
 
 		if(read(desc, support, gp) != gp) {
 		    cli_dbgmsg("Can't read %d bytes from padding area\n", gp); 
 		    free(section_hdr);
 		    free(support);
 		    return CL_EIO;
 		}
 
 		/* Counting original sections */
 		for(t = 0; t < gp - 2; t += 2) {
 		  uint32_t rva = support[t]+256*support[t+1];
 		  
 		  if (rva == 2 || rva == 1)
 		    break;
 
 		  rva = ((rva-2)<<12) - EC32(optional_hdr.ImageBase);
 		  sectcnt++;
 
 		  if(rva < EC32(section_hdr[i].VirtualAddress) || rva >= EC32(section_hdr[i].VirtualAddress)+EC32(section_hdr[i].VirtualSize)) {
 		    cli_dbgmsg("FSG: Original section %d is out of bounds\n", sectcnt);
 		    break;
 		  }
 		}
 
d6bc83e4
 		if(t >= gp-10 || cli_readint32(support + t + 6) != 2) {
6bd80fe3
 		    free(support);
 		    break;
 		}
 
 		if((sections = (struct SECTION *) cli_malloc((sectcnt + 1) * sizeof(struct SECTION))) == NULL) {
 		    free(section_hdr);
 		    free(support);
 		    return CL_EMEM;
 		}
 
 		sections[0].rva = newedi;
 		for(t = 0; t <= sectcnt - 1; t++) {
 		  sections[t+1].rva = (((support[t*2]+256*support[t*2+1])-2)<<12)-EC32(optional_hdr.ImageBase);
 		}
 
 		free(support);
 
 		if((src = (char *) cli_malloc(ssize)) == NULL) {
 		    free(section_hdr);
 		    free(sections);
 		    return CL_EMEM;
 		}
 
 		lseek(desc, EC32(section_hdr[i + 1].PointerToRawData), SEEK_SET);
a8b53539
 		if((unsigned int) read(desc, src, ssize) != ssize) {
6bd80fe3
 		    cli_dbgmsg("Can't read raw data of section %d\n", i);
 		    free(section_hdr);
 		    free(sections);
 		    free(src);
 		    return CL_EIO;
 		}
 
 		if((dest = (char *) cli_calloc(dsize, sizeof(char))) == NULL) {
 		    free(section_hdr);
 		    free(src);
 		    free(sections);
 		    return CL_EMEM;
 		}
 
 		/* Better not increasing buff size any further, let's go the hard way */
920406d9
 		gp = 0xda + 6*(buff[16]=='\xe8');
 		oldep = EC32(optional_hdr.AddressOfEntryPoint) + gp + 6 + cli_readint32(src+gp+2+oldep);
6bd80fe3
 		cli_dbgmsg("FSG: found old EP @%x\n", oldep);
 
 		tempfile = cli_gentemp(NULL);
 		if((ndesc = open(tempfile, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU)) < 0) {
 		    cli_dbgmsg("FSG: Can't create file %s\n", tempfile);
 		    free(tempfile);
 		    free(section_hdr);
 		    free(src);
 		    free(dest);
 		    free(sections);
 		    return CL_EIO;
 		}
 
 		switch(unfsg_133(src + newesi - EC32(section_hdr[i + 1].VirtualAddress), dest, ssize + EC32(section_hdr[i + 1].VirtualAddress) - newesi, dsize, sections, sectcnt, EC32(optional_hdr.ImageBase), oldep, ndesc)) {
 		    case 1: /* Everything OK */
 			cli_dbgmsg("FSG: Unpacked and rebuilt executable saved in %s\n", tempfile);
 			free(src);
 			free(dest);
 			free(sections);
 			fsync(ndesc);
 			lseek(ndesc, 0, SEEK_SET);
 
7c00689d
 			cli_dbgmsg("***** Scanning rebuilt PE file *****\n");
6bd80fe3
 			if(cli_magic_scandesc(ndesc, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
 			    free(section_hdr);
 			    close(ndesc);
 			    if(!cli_leavetemps_flag)
 				unlink(tempfile);
 			    free(tempfile);
 			    return CL_VIRUS;
 			}
 
 			close(ndesc);
 			if(!cli_leavetemps_flag)
 			    unlink(tempfile);
 			free(tempfile);
 			free(section_hdr);
 			return CL_CLEAN;
 
 		    case 0: /* We've got an unpacked buffer, no exe though */
 			cli_dbgmsg("FSG: FSG: Successfully decompressed\n");
 			close(ndesc);
 			unlink(tempfile);
 			free(tempfile);
 			free(sections);
 			found = 0;
 			upx_success = 1;
d99b1840
 			break; /* Go and scan the decompressed data */
6bd80fe3
 
 		    default: /* Everything gone wrong */
 			cli_dbgmsg("FSG: Unpacking failed\n");
 			close(ndesc);
 			unlink(tempfile); // It's empty anyway
 			free(tempfile);
 			free(src);
 			free(dest);
 			free(sections);
 			break;
 		}
 
 		break; /* were done with 1.31 */
 	    }
 	}
 
 
3c5b57d4
 	if(found) {
fbbf7cd2
 
3c5b57d4
 	    /* UPX support */
fbbf7cd2
 
3c5b57d4
 	    strncpy(sname, section_hdr[i].Name, 8);
 	    sname[8] = 0;
 	    cli_dbgmsg("UPX: Section %d name: %s\n", i, sname);
 	    strncpy(sname, section_hdr[i + 1].Name, 8);
 	    sname[8] = 0;
 	    cli_dbgmsg("UPX: Section %d name: %s\n", i + 1, sname);
 
 	    if(strncmp(section_hdr[i].Name, "UPX0", 4) || strncmp(section_hdr[i + 1].Name, "UPX1", 4))
 		cli_dbgmsg("UPX: Possibly hacked UPX section headers\n");
 
 	    /* we assume (i + 1) is UPX1 */
 	    ssize = EC32(section_hdr[i + 1].SizeOfRawData);
 	    dsize = EC32(section_hdr[i].VirtualSize) + EC32(section_hdr[i + 1].VirtualSize);
 
 	    if(limits && limits->maxfilesize && (ssize > limits->maxfilesize || dsize > limits->maxfilesize)) {
e82a5185
 		cli_dbgmsg("UPX: Sizes exceeded (ssize: %u, dsize: %u, max: %lu)\n", ssize, dsize , limits->maxfilesize);
3c5b57d4
 		free(section_hdr);
d99b1840
 		if(BLOCKMAX) {
 		    *virname = "PE.UPX.ExceededFileSize";
 		    return CL_VIRUS;
 		} else {
 		    return CL_CLEAN;
 		}
f2eb2372
 	    }
fbbf7cd2
 
3c5b57d4
 	    if(ssize <= 0x19 || dsize <= ssize) { /* FIXME: What are reasonable values? */
 		cli_dbgmsg("UPX: Size mismatch (ssize: %d, dsize: %d)\n", ssize, dsize);
 		free(section_hdr);
 		return CL_CLEAN;
 	    }
f2eb2372
 
3c5b57d4
 	    /* FIXME: use file operations in case of big files */
 	    if((src = (char *) cli_malloc(ssize)) == NULL) {
 		free(section_hdr);
 		return CL_EMEM;
f2eb2372
 	    }
 
e82a5185
 	    if(dsize > CLI_MAX_ALLOCATION) {
 		cli_errmsg("UPX: Too big value of dsize\n");
 		free(section_hdr);
 		free(src);
 		return CL_EMEM;
 	    }
 
7c00689d
 	    if((dest = (char *) cli_calloc(dsize + 1024 + nsections * 40, sizeof(char))) == NULL) {
3c5b57d4
 		free(section_hdr);
 		free(src);
 		return CL_EMEM;
f2eb2372
 	    }
 
3c5b57d4
 	    lseek(desc, EC32(section_hdr[i + 1].PointerToRawData), SEEK_SET);
a8b53539
 	    if((unsigned int) read(desc, src, ssize) != ssize) {
3c5b57d4
 		cli_dbgmsg("Can't read raw data of section %d\n", i);
 		free(section_hdr);
 		free(src);
 		free(dest);
 		return CL_EIO;
 	    }
 
 	    /* try to detect UPX code */
 
 	    if(lseek(desc, ep, SEEK_SET) == -1) {
 		cli_dbgmsg("lseek() failed\n");
 		free(section_hdr);
 		free(src);
 		free(dest);
 		return CL_EIO;
 	    }
 
 	    if(read(desc, buff, 126) != 126) { /* i.e. 0x69 + 13 + 8 */
 		cli_dbgmsg("UPX: Can't read 126 bytes at 0x%x (%d)\n", ep, ep);
b5666f50
 		cli_dbgmsg("UPX/FSG: Broken or not UPX/FSG compressed file\n");
3c5b57d4
 		free(section_hdr);
 		free(src);
 		free(dest);
b5666f50
 		return CL_CLEAN;
f2eb2372
 	    } else {
3c5b57d4
 		if(cli_memstr(UPX_NRV2B, 24, buff + 0x69, 13) || cli_memstr(UPX_NRV2B, 24, buff + 0x69 + 8, 13)) {
 		    cli_dbgmsg("UPX: Looks like a NRV2B decompression routine\n");
 		    upxfn = upx_inflate2b;
 		} else if(cli_memstr(UPX_NRV2D, 24, buff + 0x69, 13) || cli_memstr(UPX_NRV2D, 24, buff + 0x69 + 8, 13)) {
 		    cli_dbgmsg("UPX: Looks like a NRV2D decompression routine\n");
 		    upxfn = upx_inflate2d;
 		} else if(cli_memstr(UPX_NRV2E, 24, buff + 0x69, 13) || cli_memstr(UPX_NRV2E, 24, buff + 0x69 + 8, 13)) {
 		    cli_dbgmsg("UPX: Looks like a NRV2E decompression routine\n");
 		    upxfn = upx_inflate2e;
 		}
 	    }
 
 	    if(upxfn) {
 		    int skew = cli_readint32(buff + 2) - EC32(optional_hdr.ImageBase) - EC32(section_hdr[i + 1].VirtualAddress);
 
d6bc83e4
 		if(buff[1] != '\xbe' || skew <= 0 || skew > 0xfff) { /* FIXME: legit skews?? */
3c5b57d4
 		    skew = 0; 
7c00689d
 		    if(upxfn(src, ssize, dest, &dsize, EC32(section_hdr[i].VirtualAddress), EC32(section_hdr[i + 1].VirtualAddress), EC32(optional_hdr.AddressOfEntryPoint)) >= 0)
3c5b57d4
 			upx_success = 1;
 
 		} else {
 		    cli_dbgmsg("UPX: UPX1 seems skewed by %d bytes\n", skew);
7c00689d
                     if(upxfn(src + skew, ssize - skew, dest, &dsize, EC32(section_hdr[i].VirtualAddress), EC32(section_hdr[i + 1].VirtualAddress), EC32(optional_hdr.AddressOfEntryPoint)-skew) >= 0 || upxfn(src, ssize, dest, &dsize, EC32(section_hdr[i].VirtualAddress), EC32(section_hdr[i + 1].VirtualAddress), EC32(optional_hdr.AddressOfEntryPoint)) >= 0)
3c5b57d4
 			upx_success = 1;
 		}
 
 		if(upx_success)
 		    cli_dbgmsg("UPX: Successfully decompressed\n");
 		else
 		    cli_dbgmsg("UPX: Prefered decompressor failed\n");
 	    }
 
 	    if(!upx_success && upxfn != upx_inflate2b) {
7c00689d
 		if(upx_inflate2b(src, ssize, dest, &dsize, EC32(section_hdr[i].VirtualAddress), EC32(section_hdr[i + 1].VirtualAddress), EC32(optional_hdr.AddressOfEntryPoint)) == -1 && upx_inflate2b(src + 0x15, ssize - 0x15, dest, &dsize, EC32(section_hdr[i].VirtualAddress), EC32(section_hdr[i + 1].VirtualAddress), EC32(optional_hdr.AddressOfEntryPoint) - 0x15) == -1) {
 
3c5b57d4
 		    cli_dbgmsg("UPX: NRV2B decompressor failed\n");
 		} else {
 		    upx_success = 1;
 		    cli_dbgmsg("UPX: Successfully decompressed with NRV2B\n");
 		}
 	    }
 
 	    if(!upx_success && upxfn != upx_inflate2d) {
7c00689d
 		if(upx_inflate2d(src, ssize, dest, &dsize, EC32(section_hdr[i].VirtualAddress), EC32(section_hdr[i + 1].VirtualAddress), EC32(optional_hdr.AddressOfEntryPoint)) == -1 && upx_inflate2d(src + 0x15, ssize - 0x15, dest, &dsize, EC32(section_hdr[i].VirtualAddress), EC32(section_hdr[i + 1].VirtualAddress), EC32(optional_hdr.AddressOfEntryPoint) - 0x15) == -1) {
 
3c5b57d4
 		    cli_dbgmsg("UPX: NRV2D decompressor failed\n");
 		} else {
 		    upx_success = 1;
 		    cli_dbgmsg("UPX: Successfully decompressed with NRV2D\n");
 		}
 	    }
 
 	    if(!upx_success && upxfn != upx_inflate2e) {
7c00689d
 		if(upx_inflate2e(src, ssize, dest, &dsize, EC32(section_hdr[i].VirtualAddress), EC32(section_hdr[i + 1].VirtualAddress), EC32(optional_hdr.AddressOfEntryPoint)) == -1 && upx_inflate2e(src + 0x15, ssize - 0x15, dest, &dsize, EC32(section_hdr[i].VirtualAddress), EC32(section_hdr[i + 1].VirtualAddress), EC32(optional_hdr.AddressOfEntryPoint) - 0x15) == -1) {
3c5b57d4
 		    cli_dbgmsg("UPX: NRV2E decompressor failed\n");
 		} else {
 		    upx_success = 1;
 		    cli_dbgmsg("UPX: Successfully decompressed with NRV2E\n");
 		}
 	    }
 
 	    if(!upx_success) {
 		cli_dbgmsg("UPX: All decompressors failed\n");
 		free(src);
 		free(dest);
c2484690
 	    }
 	}
f2eb2372
 
3c5b57d4
 	if(upx_success) {
7c00689d
 	    free(src);
 	    free(section_hdr);
f2eb2372
 
7c00689d
 	    tempfile = cli_gentemp(NULL);
 	    if((ndesc = open(tempfile, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU)) < 0) {
 		cli_dbgmsg("UPX/FSG: Can't create file %s\n", tempfile);
 		free(tempfile);
 		free(dest);
 		return CL_EIO;
 	    }
f2eb2372
 
a8b53539
 	    if((unsigned int) write(ndesc, dest, dsize) != dsize) {
7c00689d
 		cli_dbgmsg("UPX/FSG: Can't write %d bytes\n", dsize);
 		free(tempfile);
 		free(dest);
f2eb2372
 		close(ndesc);
7c00689d
 		return CL_EIO;
 	    }
 
 	    free(dest);
 	    fsync(ndesc);
 	    lseek(ndesc, 0, SEEK_SET);
 
 	    if(cli_leavetemps_flag)
3c5b57d4
 		cli_dbgmsg("UPX/FSG: Decompressed data saved in %s\n", tempfile);
7c00689d
 
c840fa41
 	    cli_dbgmsg("***** Scanning decompressed data *****\n");
7c00689d
 	    if((ret = cli_magic_scandesc(ndesc, virname, scanned, root, limits, options, arec, mrec)) == CL_VIRUS) {
 		close(ndesc);
 		if(!cli_leavetemps_flag)
 		    unlink(tempfile);
f2eb2372
 		free(tempfile);
7c00689d
 		return CL_VIRUS;
f2eb2372
 	    }
 
7c00689d
 	    close(ndesc);
 	    if(!cli_leavetemps_flag)
 		unlink(tempfile);
 	    free(tempfile);
65ab8d96
 	    return ret;
3c5b57d4
 	}
f2eb2372
     }
e3886d61
 
5ede41fa
     /* Petite */
 
     found = 2;
 
     lseek(desc, ep, SEEK_SET);
f8355d13
     if(read(desc, buff, 200) != 200) {
3daea9cf
 	cli_dbgmsg("Can't read 200 bytes\n");
f8355d13
 	free(section_hdr);
5ede41fa
 	return CL_EIO;
f8355d13
     }
5ede41fa
 
a8b53539
     if(buff[0] != '\xb8' || (uint32_t) cli_readint32(buff + 1) != EC32(section_hdr[nsections - 1].VirtualAddress) + EC32(optional_hdr.ImageBase)) {
 	if(nsections < 2 || buff[0] != '\xb8' || (uint32_t) cli_readint32(buff + 1) != EC32(section_hdr[nsections - 2].VirtualAddress) + EC32(optional_hdr.ImageBase))
5ede41fa
 	    found = 0;
 	else
 	    found = 1;
     }
 
     if(found) {
 	cli_dbgmsg("Petite: v2.%d compression detected\n", found);
 
 	if(cli_readint32(buff + 0x80) == 0x163c988d) {
 	    cli_dbgmsg("Petite: level zero compression is not supported yet\n");
 	} else {
 	    dsize = max - min;
 
 	    if(limits && limits->maxfilesize && dsize > limits->maxfilesize) {
e82a5185
 		cli_dbgmsg("Petite: Size exceeded (dsize: %u, max: %lu)\n", dsize, limits->maxfilesize);
5ede41fa
 		free(section_hdr);
d99b1840
 		if(BLOCKMAX) {
 		    *virname = "PE.Petite.ExceededFileSize";
 		    return CL_VIRUS;
 		} else {
 		    return CL_CLEAN;
 		}
5ede41fa
 	    }
 
 	    if((dest = (char *) cli_calloc(dsize, sizeof(char))) == NULL) {
 		cli_dbgmsg("Petite: Can't allocate %d bytes\n", dsize);
 		free(section_hdr);
 		return CL_EMEM;
 	    }
 
 	    for(i = 0 ; i < nsections; i++) {
65ab8d96
 		if(section_hdr[i].SizeOfRawData) {
a8b53539
 			uint32_t offset = cli_rawaddr(EC32(section_hdr[i].VirtualAddress), section_hdr, nsections, &err);
65ab8d96
 
a8b53539
 		    if(err || lseek(desc, offset, SEEK_SET) == -1 || (unsigned int) read(desc, dest + EC32(section_hdr[i].VirtualAddress) - min, EC32(section_hdr[i].SizeOfRawData)) != EC32(section_hdr[i].SizeOfRawData)) {
65ab8d96
 			free(section_hdr);
 			free(dest);
 			return CL_EIO;
 		    }
 		}
5ede41fa
 	    }
 
 	    tempfile = cli_gentemp(NULL);
 	    if((ndesc = open(tempfile, O_RDWR|O_CREAT|O_TRUNC, S_IRWXU)) < 0) {
 		cli_dbgmsg("Petite: Can't create file %s\n", tempfile);
 		free(tempfile);
 		free(section_hdr);
 		free(dest);
 		return CL_EIO;
 	    }
 
65ab8d96
 	    /* aCaB: Fixed to allow petite v2.1 unpacking (last section is a ghost) */
5ede41fa
 	    switch(petite_inflate2x_1to9(dest, min, max - min, section_hdr,
65ab8d96
 		    nsections - (found == 1 ? 1 : 0), EC32(optional_hdr.ImageBase),
d414d959
 		    EC32(optional_hdr.AddressOfEntryPoint), ndesc,
5ede41fa
 		    found, EC32(optional_hdr.DataDirectory[2].VirtualAddress),
 		    EC32(optional_hdr.DataDirectory[2].Size))) {
 		case 1:
 		    cli_dbgmsg("Petite: Unpacked and rebuilt executable saved in %s\n", tempfile);
7c00689d
 		    cli_dbgmsg("***** Scanning rebuilt PE file *****\n");
5ede41fa
 		    break;
 
 		case 0:
 		    cli_dbgmsg("Petite: Unpacked data saved in %s\n", tempfile);
 		    break;
 
 		default:
 		    cli_dbgmsg("Petite: Unpacking failed\n");
 	    }
 
 	    free(dest);
 	    fsync(ndesc);
 	    lseek(ndesc, 0, SEEK_SET);
 
 	    if(cli_magic_scandesc(ndesc, virname, scanned, root, limits, options, arec, mrec) == CL_VIRUS) {
 		free(section_hdr);
 		close(ndesc);
 		if(!cli_leavetemps_flag) {
 		    unlink(tempfile);
 		    free(tempfile);
 		} else {
 		    free(tempfile);
 		}
 		return CL_VIRUS;
 	    }
 
 	    close(ndesc);
 
 	    if(!cli_leavetemps_flag) {
 		unlink(tempfile);
 		free(tempfile);
 	    } else {
 		free(tempfile);
 	    }
 	}
     }
 
e60810eb
     /* to be continued ... */
 
e3886d61
     free(section_hdr);
f2eb2372
     return CL_CLEAN;
e60810eb
 }
e263999b
 
856d9c84
 int cli_peheader(int desc, struct cli_pe_info *peinfo)
e263999b
 {
 	uint16_t e_magic; /* DOS signature ("MZ") */
 	uint32_t e_lfanew; /* address of new exe header */
c840fa41
 	uint32_t min, max;
e263999b
 	struct pe_image_file_hdr file_hdr;
 	struct pe_image_optional_hdr optional_hdr;
 	struct pe_image_section_hdr *section_hdr;
 	struct stat sb;
 	int i;
a8b53539
 	unsigned int err;
e263999b
 
 
     cli_dbgmsg("in cli_peheader\n");
 
     if(read(desc, &e_magic, sizeof(e_magic)) != sizeof(e_magic)) {
 	cli_dbgmsg("Can't read DOS signature\n");
 	return -1;
     }
 
     if(EC16(e_magic) != IMAGE_DOS_SIGNATURE && EC16(e_magic) != IMAGE_DOS_SIGNATURE_OLD) {
 	cli_dbgmsg("Invalid DOS signature\n");
 	return -1;
     }
 
     lseek(desc, 58, SEEK_CUR); /* skip to the end of the DOS header */
 
     if(read(desc, &e_lfanew, sizeof(e_lfanew)) != sizeof(e_lfanew)) {
 	cli_dbgmsg("Can't read new header address\n");
 	/* truncated header? */
 	return -1;
     }
 
     e_lfanew = EC32(e_lfanew);
     if(!e_lfanew) {
 	cli_dbgmsg("Not a PE file\n");
 	return -1;
     }
 
     if(lseek(desc, e_lfanew, SEEK_SET) < 0) {
 	/* probably not a PE file */
 	cli_dbgmsg("Can't lseek to e_lfanew\n");
 	return -1;
     }
 
     if(read(desc, &file_hdr, sizeof(struct pe_image_file_hdr)) != sizeof(struct pe_image_file_hdr)) {
 	/* bad information in e_lfanew - probably not a PE file */
 	cli_dbgmsg("Can't read file header\n");
 	return -1;
     }
 
     if(EC32(file_hdr.Magic) != IMAGE_NT_SIGNATURE) {
 	cli_dbgmsg("Invalid PE signature (probably NE file)\n");
 	return -1;
     }
 
     if(EC16(file_hdr.SizeOfOptionalHeader) != sizeof(struct pe_image_optional_hdr)) {
 	return -1;
     }
 
856d9c84
     peinfo->nsections = EC16(file_hdr.NumberOfSections);
e263999b
 
     if(read(desc, &optional_hdr, sizeof(struct pe_image_optional_hdr)) != sizeof(struct pe_image_optional_hdr)) {
 	cli_dbgmsg("Can't optional file header\n");
 	return -1;
     }
 
856d9c84
     peinfo->section = (struct SECTION *) cli_calloc(peinfo->nsections, sizeof(struct SECTION));
e263999b
 
856d9c84
     if(!peinfo->section) {
e263999b
 	cli_dbgmsg("Can't allocate memory for section headers\n");
 	return -1;
     }
 
     if(fstat(desc, &sb) == -1) {
 	cli_dbgmsg("fstat failed\n");
856d9c84
 	free(peinfo->section);
e263999b
 	return -1;
     }
 
856d9c84
     section_hdr = (struct pe_image_section_hdr *) cli_calloc(peinfo->nsections, sizeof(struct pe_image_section_hdr));
e263999b
 
     if(!section_hdr) {
 	cli_dbgmsg("Can't allocate memory for section headers\n");
856d9c84
 	free(peinfo->section);
e263999b
 	return -1;
     }
 
856d9c84
     for(i = 0; i < peinfo->nsections; i++) {
e263999b
 
 	if(read(desc, &section_hdr[i], sizeof(struct pe_image_section_hdr)) != sizeof(struct pe_image_section_hdr)) {
 	    cli_dbgmsg("Can't read section header\n");
 	    cli_dbgmsg("Possibly broken PE file\n");
 	    free(section_hdr);
856d9c84
 	    free(peinfo->section);
e263999b
 	    return -1;
 	}
 
856d9c84
 	peinfo->section[i].rva = EC32(section_hdr[i].VirtualAddress);
 	peinfo->section[i].vsz = EC32(section_hdr[i].VirtualSize);
 	peinfo->section[i].raw = EC32(section_hdr[i].PointerToRawData);
 	peinfo->section[i].rsz = EC32(section_hdr[i].SizeOfRawData);
e263999b
 
c840fa41
 	if(!i) {
 	    min = EC32(section_hdr[i].VirtualAddress);
 	    max = EC32(section_hdr[i].VirtualAddress) + EC32(section_hdr[i].SizeOfRawData);
 	} else {
 	    if(EC32(section_hdr[i].VirtualAddress) < min)
 		min = EC32(section_hdr[i].VirtualAddress);
 
 	    if(EC32(section_hdr[i].VirtualAddress) + EC32(section_hdr[i].SizeOfRawData) > max)
 		max = EC32(section_hdr[i].VirtualAddress) + EC32(section_hdr[i].SizeOfRawData);
 	}
     }
a8b53539
 
c840fa41
     if((peinfo->ep = EC32(optional_hdr.AddressOfEntryPoint)) >= min && !(peinfo->ep = cli_rawaddr(EC32(optional_hdr.AddressOfEntryPoint), section_hdr, peinfo->nsections, &err)) && err) {
e263999b
 	cli_dbgmsg("Possibly broken PE file\n");
 	free(section_hdr);
856d9c84
 	free(peinfo->section);
e263999b
 	return -1;
     }
 
     free(section_hdr);
     return 0;
 }