libclamav/yc.c
dfad2d4d
 /*
  *  Copyright (C) 2005 Ivan Zlatev <pumqara@gmail.com>
  *
  *  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
30738099
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
dfad2d4d
  */
 
 /* 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
 
 #include "cltypes.h"
 #include "pe.h"
 #include "others.h"
 
70ea6092
 #define EC32(x) le32_to_host(x) /* Convert little endian to host */
 #define EC16(x) le16_to_host(x) /* Convert little endian to host */
dfad2d4d
 
764dab55
 /* ========================================================================== */
 /* "Emulates" the poly decryptors */
 
dfad2d4d
 static int yc_poly_emulator(char* decryptor_offset, char* code, unsigned int ecx)
 {
 
   /* 
      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;
051cf29f
   unsigned int j,i;
dfad2d4d
 
051cf29f
   for(i=0;i<ecx;i++) /* Byte looper - Decrypts every byte and write it back */
dfad2d4d
     {
       al = code[i];
 
764dab55
       for(j=0;j<0x30;j++)   /* Poly Decryptor "Emulator" */
dfad2d4d
 	{
 	  switch(decryptor_offset[j])
 	    {
 
764dab55
 	    case '\xEB':	/* JMP short */
dfad2d4d
 	      j++;
 	      j = j + decryptor_offset[j];
 	      break;
 
764dab55
 	    case '\xFE':	/* DEC  AL */
dfad2d4d
 	      al--;
 	      j++;
 	      break;
 
764dab55
 	    case '\x2A':	/* SUB AL,CL */
dfad2d4d
 	      al = al - cl;
 	      j++;
 	      break;
 
764dab55
 	    case '\x02':	/* ADD AL,CL */
dfad2d4d
 	      al = al + cl;
 	      j++;
 	      break
 		;
764dab55
 	    case '\x32':	/* XOR AL,CL */
dfad2d4d
 	      al = al ^ cl;
 	      j++;
 	      break;
 	      ;
764dab55
 	    case '\x04':	/* ADD AL,num */
dfad2d4d
 	      j++;
 	      al = al + decryptor_offset[j];
 	      break;
 	      ;
764dab55
 	    case '\x34':	/* XOR AL,num */
dfad2d4d
 	      j++;
 	      al = al ^ decryptor_offset[j];
 	      break;
 
764dab55
 	    case '\x2C':	/* SUB AL,num */
dfad2d4d
 	      j++;
 	      al = al - decryptor_offset[j];
 	      break;
 
 			
 	    case '\xC0':
 	      j++;
764dab55
 	      if(decryptor_offset[j]=='\xC0') /* ROL AL,num */
dfad2d4d
 		{
 		  j++;
688dc728
 		  ROL(al,decryptor_offset[j]);
dfad2d4d
 		}
764dab55
 	      else			/* ROR AL,num */
dfad2d4d
 		{
 		  j++;
 		  ROR(al,decryptor_offset[j]);
 		}
 	      break;
 
 	    case '\xD2':
 	      j++;
764dab55
 	      if(decryptor_offset[j]=='\xC8') /* ROR AL,CL */
dfad2d4d
 		{
 		  j++;
 		  ROR(al,cl);
 		}
764dab55
 	      else			/* ROL AL,CL */
dfad2d4d
 		{
 		  j++;
 		  ROL(al,cl);
 		}
 	      break;
 
 	    case '\x90':
 	    case '\xf8':
 	    case '\xf9':
 	      break;
 
 	    default:
 	      cli_dbgmsg("yC: Unhandled opcode %x\n", (unsigned char)decryptor_offset[j]);
688dc728
 	      return 1;
dfad2d4d
 	    }
 	}
       cl--;
       code[i] = al;
     }
   return 0;
 
 }
 
 
764dab55
 /* ========================================================================== */
 /* Main routine which calls all others */
 
dfad2d4d
 int yc_decrypt(char *fbuf, unsigned int filesize, struct pe_image_section_hdr *sections, unsigned int sectcount, uint32_t peoffset, int desc)
 {
   uint32_t ycsect = EC32(sections[sectcount].PointerToRawData);
688dc728
   unsigned int i;
dfad2d4d
   struct pe_image_file_hdr *pe = (struct pe_image_file_hdr*) (fbuf + peoffset);
 
   /* 
 
   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
   Lenght to decrypt - ECX = 0xB97
 
   */
   cli_dbgmsg("yC: decrypting decryptor on sect %d\n", sectcount); 
   if (yc_poly_emulator(fbuf + ycsect + 0x93, fbuf + ycsect + 0xc6 ,0xB97))
     return 1;
   filesize-=EC32(sections[sectcount].SizeOfRawData);
 
   /* 
 
   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
   Lenght to decrypt - ECX = Raw Size of Section
 
   */
 
 
764dab55
   /* Loop through all sections and decrypt them... */
051cf29f
   for(i=0;i<sectcount;i++)
dfad2d4d
     {
       uint32_t name = (uint32_t) cli_readint32((char *)sections[i].Name);
764dab55
       if ( name == 0x63727372 || /* rsrc */
 	   name == 0x7273722E || /* .rsr */
 	   name == 0x6F6C6572 || /* relo */
 	   name == 0x6C65722E || /* .rel */
 	   name == 0x6164652E || /* .eda */
 	   name == 0x6164722E || /* .rda */
 	   name == 0x6164692E || /* .ida */
 	   name == 0x736C742E || /* .tls */
e2b5770b
 	   ((name&0xffff) == 0x4379) || /* yC */
dfad2d4d
 	   EC32(sections[i].PointerToRawData) == 0 ||
 	   EC32(sections[i].SizeOfRawData) == 0 ) continue;
       cli_dbgmsg("yC: decrypting sect%d\n",i); 
       if (yc_poly_emulator(fbuf + ycsect + 0x457, fbuf + EC32(sections[i].PointerToRawData), EC32(sections[i].SizeOfRawData)))
 	return 1;
     }
 
764dab55
   /* Remove yC section */
   pe->NumberOfSections=EC16(pe->NumberOfSections)-1;
dfad2d4d
 
764dab55
   /* Remove IMPORT_DIRECTORY information */
dfad2d4d
   memset((char *)pe + sizeof(struct pe_image_file_hdr) + 0x68, 0, 8);
 
764dab55
   /* OEP resolving */
   /* OEP = DWORD PTR [ Start of yC section+ A0F] */
dfad2d4d
   cli_writeint32((char *)pe + sizeof(struct pe_image_file_hdr) + 16, cli_readint32(fbuf + ycsect + 0xa0f));
 
764dab55
   /* Fix SizeOfImage */
dfad2d4d
   cli_writeint32((char *)pe + sizeof(struct pe_image_file_hdr) + 0x38, cli_readint32((char *)pe + sizeof(struct pe_image_file_hdr) + 0x38) - EC32(sections[sectcount].VirtualSize));
 
   if (cli_writen(desc, fbuf, filesize)==-1) {
764dab55
     cli_dbgmsg("yC: Cannot write unpacked file\n");
dfad2d4d
     return 1;
   }
   return 0;
 }