libclamav/yc.c
822930fc
 /*
c442ca9c
  *  Copyright (C) 2013-2019 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  *  Copyright (C) 2007-2013 Sourcefire, Inc.
2023340a
  *
  *  Authors: Ivan Zlatev
822930fc
  *
  *  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.
822930fc
  *
  *  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.
822930fc
  */
 
 /* Decrypts files, protected by Y0da Cryptor 1.3 */
 
 /* aCaB:
  * 13/01/2006 - merged standalone unpacker into libclamav
  * 14/01/2006 - major rewrite and bugfix
  */
 
 #include <string.h>
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
b2e7c931
 
60d8d2c3
 #include "clamav.h"
822930fc
 #include "pe.h"
 #include "others.h"
fc83da82
 #include "yc.h"
822930fc
 
75282b5c
 #define EC16(x) le16_to_host(x) /* Convert little endian to host */
822930fc
 
3d6d7a7d
 #define DO_HEURISTIC 1
 
 static int yc_bounds_check(cli_ctx *ctx, char *base, unsigned int filesize, char *offset, unsigned int bound)
 {
       if ((unsigned int)((offset+bound)-base) > filesize) {
99225735
           cli_dbgmsg("yC: Bounds check assertion.\n");
3d6d7a7d
 #if DO_HEURISTIC
           cli_append_virus(ctx, "Heuristics.BoundsCheck");
 #endif
           return 1;
       }
 
       return 0;
 }
 
 
e0264472
 /* ========================================================================== */
 /* "Emulates" the poly decryptors */
 
3d6d7a7d
 static int yc_poly_emulator(cli_ctx *ctx, char *base, unsigned int filesize, char* decryptor_offset, char* code, unsigned int ecx, uint32_t max_emu)
822930fc
 {
 
   /* 
      This is the instruction set of the poly code.
      Numbers stand for example only.
 
      2C 05            SUB AL,5
      2AC1             SUB AL,CL
      34 10            XOR AL,10
      32C1             XOR AL,CL
      FEC8             DEC AL
      04 10            ADD AL,10
      02C1             ADD AL,CL
      C0C0 06          ROL AL,6
      C0C8 05          ROR AL,5
      D2C8             ROR AL,CL
      D2C0             ROL AL,CL
 
   */
   unsigned char al;
   unsigned char cl = ecx & 0xff;
abee2cb8
   unsigned int j,i;
9baa46aa
   unsigned int max_jmp_loop = 100000000;
822930fc
 
e8e37462
   for(i=0;i<ecx&&i<max_emu;i++) /* Byte looper - Decrypts every byte and write it back */
822930fc
     {
3d6d7a7d
         if (yc_bounds_check(ctx, base, filesize, code, i)) {
771c2309
             return 2;
3d6d7a7d
         }
822930fc
       al = code[i];
 
e0264472
       for(j=0;j<0x30;j++)   /* Poly Decryptor "Emulator" */
822930fc
 	{
3d6d7a7d
         if (yc_bounds_check(ctx, base, filesize, decryptor_offset, j)) {
771c2309
             return 2;
3d6d7a7d
         }
 
822930fc
 	  switch(decryptor_offset[j])
 	    {
 
e0264472
 	    case '\xEB':	/* JMP short */
822930fc
 	      j++;
3d6d7a7d
             if (yc_bounds_check(ctx, base, filesize, decryptor_offset, j)) {
771c2309
                 return 2;
3d6d7a7d
             }
58e7f60a
 	      if (!max_jmp_loop)
 	          return 2;
 	      max_jmp_loop--;
822930fc
 	      j = j + decryptor_offset[j];
 	      break;
 
e0264472
 	    case '\xFE':	/* DEC  AL */
822930fc
 	      al--;
 	      j++;
 	      break;
 
e0264472
 	    case '\x2A':	/* SUB AL,CL */
822930fc
 	      al = al - cl;
 	      j++;
 	      break;
 
e0264472
 	    case '\x02':	/* ADD AL,CL */
822930fc
 	      al = al + cl;
 	      j++;
 	      break
 		;
e0264472
 	    case '\x32':	/* XOR AL,CL */
822930fc
 	      al = al ^ cl;
 	      j++;
 	      break;
 	      ;
e0264472
 	    case '\x04':	/* ADD AL,num */
822930fc
 	      j++;
3d6d7a7d
             if (yc_bounds_check(ctx, base, filesize, decryptor_offset, j)) {
771c2309
                 return 2;
3d6d7a7d
             }
822930fc
 	      al = al + decryptor_offset[j];
 	      break;
 	      ;
e0264472
 	    case '\x34':	/* XOR AL,num */
822930fc
 	      j++;
3d6d7a7d
             if (yc_bounds_check(ctx, base, filesize, decryptor_offset, j)) {
771c2309
                 return 2;
3d6d7a7d
             }
822930fc
 	      al = al ^ decryptor_offset[j];
 	      break;
 
e0264472
 	    case '\x2C':	/* SUB AL,num */
822930fc
 	      j++;
3d6d7a7d
             if (yc_bounds_check(ctx, base, filesize, decryptor_offset, j)) {
771c2309
                 return 2;
3d6d7a7d
             }
822930fc
 	      al = al - decryptor_offset[j];
 	      break;
 
 			
 	    case '\xC0':
 	      j++;
3d6d7a7d
             if (yc_bounds_check(ctx, base, filesize, decryptor_offset, j)) {
771c2309
                 return 2;
3d6d7a7d
             }
e0264472
 	      if(decryptor_offset[j]=='\xC0') /* ROL AL,num */
822930fc
 		{
 		  j++;
3d6d7a7d
             if (yc_bounds_check(ctx, base, filesize, decryptor_offset, j)) {
771c2309
                 return 2;
3d6d7a7d
             }
adc98193
 		  CLI_ROL(al,decryptor_offset[j]);
822930fc
 		}
e0264472
 	      else			/* ROR AL,num */
822930fc
 		{
 		  j++;
3d6d7a7d
             if (yc_bounds_check(ctx, base, filesize, decryptor_offset, j)) {
771c2309
                 return 2;
3d6d7a7d
             }
adc98193
 		  CLI_ROR(al,decryptor_offset[j]);
822930fc
 		}
 	      break;
 
 	    case '\xD2':
 	      j++;
3d6d7a7d
             if (yc_bounds_check(ctx, base, filesize, decryptor_offset, j)) {
771c2309
                 return 2;
3d6d7a7d
             }
e0264472
 	      if(decryptor_offset[j]=='\xC8') /* ROR AL,CL */
822930fc
 		{
 		  j++;
adc98193
 		  CLI_ROR(al,cl);
822930fc
 		}
e0264472
 	      else			/* ROL AL,CL */
822930fc
 		{
 		  j++;
adc98193
 		  CLI_ROL(al,cl);
822930fc
 		}
 	      break;
 
 	    case '\x90':
 	    case '\xf8':
 	    case '\xf9':
 	      break;
 
 	    default:
3d6d7a7d
             if (yc_bounds_check(ctx, base, filesize, decryptor_offset, j)) {
771c2309
                 return 2;
3d6d7a7d
             }
822930fc
 	      cli_dbgmsg("yC: Unhandled opcode %x\n", (unsigned char)decryptor_offset[j]);
5150394a
 	      return 1;
822930fc
 	    }
 	}
       cl--;
3d6d7a7d
             if (yc_bounds_check(ctx, base, filesize, code, i))
771c2309
                 return 2;
822930fc
       code[i] = al;
     }
   return 0;
 
 }
 
 
e0264472
 /* ========================================================================== */
 /* Main routine which calls all others */
 
3d6d7a7d
 int yc_decrypt(cli_ctx *ctx, char *fbuf, unsigned int filesize, struct cli_exe_section *sections, unsigned int sectcount, uint32_t peoffset, int desc, uint32_t ecx,int16_t offset) {
d0b31fa3
   uint32_t ycsect = sections[sectcount].raw+offset;
5150394a
   unsigned int i;
822930fc
   struct pe_image_file_hdr *pe = (struct pe_image_file_hdr*) (fbuf + peoffset);
fcd9ab4b
   char *sname = (char *)pe + EC16(pe->SizeOfOptionalHeader) + 0x18;
e8e37462
   uint32_t max_emu;
478c3732
   unsigned int ofilesize = filesize;
822930fc
   /* 
 
   First layer (decryptor of the section decryptor) in last section 
 
   Start offset for analyze: Start of yC Section + 0x93
   End offset for analyze: Start of yC Section + 0xC3
d0b31fa3
   Length to decrypt - ECX = 0xB97
822930fc
 
   */
d0b31fa3
   cli_dbgmsg("yC: offset: %x, length: %x\n", offset, ecx);
   cli_dbgmsg("yC: decrypting decryptor on sect %d\n", sectcount);
771c2309
   switch (yc_poly_emulator(ctx, fbuf, filesize, fbuf + ycsect + 0x93, fbuf + ycsect + 0xc6, ecx, ecx)) {
   case 2:
       return CL_VIRUS;
   case 1:
       return CL_EUNPACK;
   }
de800f2a
   filesize-=sections[sectcount].ursz;
822930fc
 
   /* 
 
   Second layer (decryptor of the sections) in last section 
 
   Start offset for analyze: Start of yC Section + 0x457
   End offset for analyze: Start of yC Section + 0x487
e8e37462
   Length to decrypt - ECX = Raw Size of Section
822930fc
 
   */
 
 
e0264472
   /* Loop through all sections and decrypt them... */
e8e37462
   for(i=0;i<sectcount;i++) {
     uint32_t name = (uint32_t) cli_readint32(sname+i*0x28);
     if (!sections[i].raw ||
 	!sections[i].rsz ||
 	name == 0x63727372 || /* rsrc */
 	name == 0x7273722E || /* .rsr */
 	name == 0x6F6C6572 || /* relo */
 	name == 0x6C65722E || /* .rel */
 	name == 0x6164652E || /* .eda */
 	name == 0x6164722E || /* .rda */
 	name == 0x6164692E || /* .ida */
 	name == 0x736C742E || /* .tls */
 	(name&0xffff) == 0x4379  /* yC */
de800f2a
 	) continue;
e8e37462
     cli_dbgmsg("yC: decrypting sect%d\n",i);
     max_emu = filesize - sections[i].raw;
     if (max_emu > filesize) {
       cli_dbgmsg("yC: bad emulation length limit %u\n", max_emu);
       return 1;
822930fc
     }
478c3732
     switch (yc_poly_emulator(ctx, fbuf, ofilesize, fbuf + ycsect + (offset == -0x18 ? 0x3ea : 0x457), 
e8e37462
 			 fbuf + sections[i].raw, 
 			 sections[i].ursz, 
771c2309
 			 max_emu)) {
     case 2:
         return CL_VIRUS;
     case 1:
         return CL_EUNPACK;
     }
e8e37462
   }
822930fc
 
e0264472
   /* Remove yC section */
a0b31dc3
   pe->NumberOfSections=EC16(sectcount);
822930fc
 
e0264472
   /* Remove IMPORT_DIRECTORY information */
822930fc
   memset((char *)pe + sizeof(struct pe_image_file_hdr) + 0x68, 0, 8);
 
e0264472
   /* OEP resolving */
   /* OEP = DWORD PTR [ Start of yC section+ A0F] */
822930fc
   cli_writeint32((char *)pe + sizeof(struct pe_image_file_hdr) + 16, cli_readint32(fbuf + ycsect + 0xa0f));
 
e0264472
   /* Fix SizeOfImage */
de800f2a
   cli_writeint32((char *)pe + sizeof(struct pe_image_file_hdr) + 0x38, cli_readint32((char *)pe + sizeof(struct pe_image_file_hdr) + 0x38) - sections[sectcount].vsz);
822930fc
 
   if (cli_writen(desc, fbuf, filesize)==-1) {
e0264472
     cli_dbgmsg("yC: Cannot write unpacked file\n");
771c2309
     return CL_EUNPACK;
822930fc
   }
771c2309
   return CL_SUCCESS;
822930fc
 }