Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 22nd, 2010 | Syntax: None | Size: 33.68 KB | Hits: 143 | Expires: Never
Copy text to clipboard
  1. /* ObCrypter v1.2 for p0wnbox.com by OBLiQUE */
  2.  
  3. /* WARNING!!!: compile it only with g++
  4.  * use MinGW if you have Window$
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/stat.h>
  10.  
  11. #if defined(__unix__) || defined(__APPLE__)
  12. #define c '/'
  13. #else
  14. #define c '\\'
  15. #endif
  16.  
  17. /* arguments */
  18. #define FLAG_V 0x01 // verbose mode
  19. #define FLAG_W 0x02 // write to path
  20. #define FLAG_F 0x04 // force to overwrite
  21. #define FLAG_E 0x08 // write the whole file
  22.  
  23. // usage
  24. char usage[]="\n  ObCrypter v1.2 for p0wnbox.com by OBLiQUE\n\n"
  25.                 "  usage: obCrypter [options] file\n\n"
  26.                 "\t-w [file]\tchoose an output file\n"
  27.                 "\t-f\t\tforce to overwrite the output file\n"
  28.                 "\t-v\t\tverbose mode - this is slower than the quiet mode\n"
  29.                 "\t-e\t\twrite the whole file - EOF support\n\n"
  30.                 "  example: obCrypter -v -w output_file.exe input_file.exe\n\n";
  31.  
  32. unsigned int argflags=0;
  33. enum sort_t {byVirtualOffset, byRawOffset}; // sorting types
  34.  
  35. class Section {
  36. public:
  37.         Section();
  38.         Section(Section *_section);
  39.         char sname[9]; // section name
  40.         unsigned int voffset; // VirtualOffset
  41.         unsigned int vsize; // VirtualSize
  42.         unsigned int roffset; // RawOffset
  43.         unsigned int rsize; // RawSize
  44.         unsigned int flags; // Characteristics
  45.         unsigned int *psort; // this points to voffset or roffset
  46.         Section *next; // next section
  47. };
  48.  
  49. class Info {
  50. public:
  51.         Info(sort_t _sort);
  52.         ~Info();
  53.         int insert(Section *_section); // add section to the linked-list
  54.         int readinfo(); // read file info
  55.         int writefile(); // write crypted file
  56.         int crypt(char *_tocrypt,char *_crypted);
  57.         int resort(sort_t _sort); // resort the linked-list
  58. private:
  59.         unsigned int oep; // original EntryPoint
  60.         unsigned int imagebase; // ImageBase
  61.         unsigned int nos; // NumberOfSectios
  62.         unsigned int size; // tocrypt size
  63.         unsigned int pe_start; // PE-header starting point
  64.         unsigned int import_rva; // ImportTable RVA
  65.         unsigned int import_size; // ImportTable size
  66.         sort_t sort; // sort type
  67.         FILE *tocrypt; // file to crypt
  68.         FILE *crypted; // crypted file
  69.         Section *head; // top of my linked-list
  70.         Section *tail; // tail of my linked-list
  71. };
  72.  
  73. int main(int argc,char *argv[])
  74. {
  75.         if (argc<2) {
  76.                 printf("%s", usage);
  77.                 return 1;
  78.         }
  79.  
  80.         unsigned int i,j,n=0;
  81.         char *path_tocrypt=NULL, *path_crypted=NULL;
  82.  
  83.         for (i=1;i<argc;i++)
  84.                 if (argv[i][0]=='-') {
  85.                         for (j=1;argv[i][j]!='\0';j++)
  86.                                 switch (argv[i][j]) {
  87.                                         case 'v':
  88.                                                 argflags|=FLAG_V;
  89.                                                 break;
  90.                                         case 'w':
  91.                                                 argflags|=FLAG_W;
  92.                                                 if (argc-1==i+1) {
  93.                                                         printf("%s", usage);
  94.                                                         return 1;
  95.                                                 } else path_crypted=argv[i+1];
  96.                                                 break;
  97.                                         case 'f':
  98.                                                 argflags|=FLAG_F;
  99.                                                 break;
  100.                                         case 'e':
  101.                                                 argflags|=FLAG_E;
  102.                                                 break;
  103.                                         default:
  104.                                                 fprintf(stderr,"Illegal argument: %c\n",argv[i][j]);
  105.                                                 printf("%s", usage);
  106.                                                 return 1;
  107.                                 }
  108.                 } else if (argc-1==i)
  109.                         path_tocrypt=argv[i];
  110.                 else n++;
  111.  
  112.         if (argflags & FLAG_W) n--;
  113.         if (!path_tocrypt || n) {
  114.                 printf("%s", usage);
  115.                 return 1;
  116.         }
  117.  
  118.         Info fileinfo(byVirtualOffset);
  119.  
  120.         if (fileinfo.crypt(path_tocrypt,path_crypted)) {
  121.                 fprintf(stderr,"\nExiting ...\n");
  122.                 return 1;
  123.         }
  124.         return 0;
  125. }
  126.  
  127. Section::Section()
  128. {
  129.         sname[0]='\0';
  130.         voffset=0;
  131.         vsize=0;
  132.         roffset=0;
  133.         rsize=0;
  134.         flags=0;
  135.         psort=NULL;
  136.         next=NULL;
  137. }
  138.  
  139. Section::Section(Section *_section)
  140. {
  141.         snprintf(sname,sizeof(sname),"%s",_section->sname);
  142.         voffset=_section->voffset;
  143.         vsize=_section->vsize;
  144.         roffset=_section->roffset;
  145.         rsize=_section->rsize;
  146.         flags=_section->flags;
  147.         psort=_section->psort;
  148.         next=_section->next;
  149. }
  150.  
  151. Info::Info(sort_t _sort)
  152. {
  153.         size=0;
  154.         oep=0;
  155.         imagebase=0;
  156.         nos=0;
  157.         import_rva=0;
  158.         import_size=0;
  159.         sort=_sort;
  160.         head=NULL;
  161.         tail=NULL;
  162.         tocrypt=NULL;
  163.         crypted=NULL;
  164. }
  165.  
  166. Info::~Info()
  167. {
  168.         Section *tmp;
  169.         while (head!=NULL) {
  170.                 tmp=head;
  171.                 head=head->next;
  172.                 delete tmp;
  173.         }
  174.         tail=head;
  175.         if (tocrypt) fclose(tocrypt);
  176.         if (crypted) fclose(crypted);
  177. }
  178.  
  179. int Info::crypt(char *_tocrypt, char *_crypted)
  180. {
  181.         struct stat flst;
  182.         char path_tocrypt[FILENAME_MAX], path_crypted[FILENAME_MAX];
  183.  
  184.         if (argflags & FLAG_V) printf("ObCrypter v1.2 for p0wnbox.com by OBLiQUE\n");
  185.         if (_crypted) {
  186.                 stat(_crypted,&flst);
  187.                 if (S_ISDIR(flst.st_mode)) {
  188.                         if (_crypted[strlen(_crypted)-1]==c)
  189.                                 _crypted[strlen(_crypted)-1]='\0';
  190.                         snprintf(path_crypted,FILENAME_MAX,"%s%c%s",_crypted,c,"crypted.exe");
  191.                 } else snprintf(path_crypted,FILENAME_MAX,"%s",_crypted);
  192.         } else snprintf(path_crypted,FILENAME_MAX,"crypted.exe");
  193.         snprintf(path_tocrypt,FILENAME_MAX,"%s",_tocrypt);
  194.         if (!strncmp(path_crypted,path_tocrypt,FILENAME_MAX)) {
  195.                 fprintf(stderr,"ERROR! The filename should be different\n");
  196.                 return 1;
  197.         }
  198.         if (!(tocrypt=fopen(path_tocrypt,"rb"))) {
  199.                 fprintf(stderr,"ERROR!: Can't open the file\n");
  200.                 return 1;
  201.         }
  202.         if (readinfo()) return 1;
  203.         if (argflags & FLAG_V) printf("Output file: %s\n\n",path_crypted);
  204.         if (!stat(path_crypted,&flst) && (!(argflags & FLAG_F))) {
  205.                 char input[3];
  206.                 do {
  207.                         printf("File exist! Do you want to overwrite? [Y/N]: ");
  208.                         scanf("%2s%*[^\n]",input);
  209.                 } while (input[1]!='\0' || (input[0]!='Y' && input[0]!='y' && input[0]!='n' && input[0]!='N'));
  210.                 if (input[0]=='N' || input[0]=='n') return 1;
  211.                 argflags|=FLAG_F;
  212.                 if (argflags & FLAG_V) printf("\n");
  213.         }
  214.         if (!(crypted=fopen(path_crypted,"wb"))) {
  215.                 fprintf(stderr,"ERROR!: Can't write the file\n");
  216.                 return 1;
  217.         }
  218.         writefile();
  219.         return 0;
  220. }
  221.  
  222. int Info::insert(Section *_section) // instert in sorted linked-listed
  223. {
  224.         Section *new_section = new Section(_section);
  225.  
  226.         new_section->next=NULL;
  227.         if (sort==byVirtualOffset) new_section->psort=&new_section->voffset;
  228.         else if (sort==byRawOffset) new_section->psort=&new_section->roffset;
  229.         if (head==NULL) tail=head=new_section;
  230.         else if (*head->psort>*new_section->psort) {
  231.                 new_section->next=head;
  232.                 head=new_section;
  233.         } else {
  234.                 Section *previous=head, *current=head->next;
  235.                 while (current!=NULL) {
  236.                         if (*current->psort>*new_section->psort)
  237.                                 break;
  238.                         previous=current;
  239.                         current=current->next;
  240.                 }
  241.                 new_section->next=current;
  242.                 previous->next=new_section;
  243.                 if (new_section->next==NULL) tail=new_section;
  244.         }
  245.         return 0;
  246. }
  247.  
  248. int Info::resort(sort_t _sort)
  249. {
  250.         Info *new_info = new Info(_sort); // create new linked-list
  251.         Section *tmp=head;
  252.  
  253.         while (tmp!=NULL) { // insert the sections to the new linked-list
  254.                 new_info->insert(tmp);
  255.                 tmp=tmp->next;
  256.         }
  257.         while (head!=NULL) { // delete the sections from the old linked-list
  258.                 tmp=head;
  259.                 head=head->next;
  260.                 delete tmp;
  261.         }
  262.         head=new_info->head; // change the pointer to the new head
  263.         tail=new_info->tail; // change the pointer to the new tail
  264.         new_info->tail=NULL; // i make it NULL to prevent deleting the new sorted list
  265.         new_info->head=NULL;
  266.         delete new_info; // delete the new linked-list
  267.         return 0;
  268. }
  269.  
  270. int Info::readinfo()
  271. {
  272.         unsigned int tmp;
  273.         Section *current;
  274.  
  275.         fseek(tocrypt,0,SEEK_END);
  276.         size=ftell(tocrypt);
  277.         fseek(tocrypt,0x3c,SEEK_SET);
  278.         fread(&tmp,4,1,tocrypt);
  279.         fseek(tocrypt,tmp,SEEK_SET);
  280.         pe_start=ftell(tocrypt);
  281.         fread(&tmp,4,1,tocrypt);
  282.         if (tmp!=0x00004550) {
  283.                 fprintf(stderr,"ERROR!: Not a PE-Format file\n");
  284.                 return 1;
  285.         }
  286.         if (argflags & FLAG_V) printf("\nFile Info\n\n");
  287.         fseek(tocrypt,2,SEEK_CUR);
  288.         fread(&nos,2,1,tocrypt);
  289.         if (argflags & FLAG_V) printf("NumberOfSections ... %#.4x\n",nos);
  290.         fseek(tocrypt,0x20,SEEK_CUR);
  291.         fread(&oep,4,1,tocrypt);
  292.         if (argflags & FLAG_V) printf("Original EntryPoint ... %#.8x\n",oep);
  293.         fseek(tocrypt,8,SEEK_CUR);
  294.         fread(&imagebase,4,1,tocrypt);
  295.         if (argflags & FLAG_V) printf("ImageBase ... %#.8x\n",imagebase);
  296.         fseek(tocrypt,0x48,SEEK_CUR);
  297.         fread(&import_rva,4,1,tocrypt);
  298.         if (argflags & FLAG_V) printf("ImportTable RVA ... %#.8x\n",import_rva);
  299.         fread(&import_size,4,1,tocrypt);
  300.         if (argflags & FLAG_V) printf("ImportTable Size ... %#.8x\n",import_size);
  301.         fseek(tocrypt,0x70,SEEK_CUR);
  302.         if (argflags & FLAG_V) printf("\nSections Info\n\n");
  303.         tmp=0;
  304.         if (argflags & FLAG_V) {
  305.                 printf("+----------+-------------+------------+------------+------------+------------+\n");
  306.                 printf("|   Name   | VirtualSize | Vir.Offset |  RawSize   | RawOffset  |   Flags    |\n");
  307.                 printf("+----------+-------------+------------+------------+------------+------------+\n");
  308.         }
  309.  
  310.         while(tmp<nos) {
  311.                 int flag=0;
  312.                 Section *new_section = new Section();
  313.  
  314.                 fread(new_section->sname,8,1,tocrypt);
  315.                 if (argflags & FLAG_V) printf("| %-8s |",new_section->sname);
  316.                 fread(&new_section->vsize,4,1,tocrypt);
  317.                 if (argflags & FLAG_V) printf(" %#-11.8x |",new_section->vsize);
  318.                 fread(&new_section->voffset,4,1,tocrypt);
  319.                 if (argflags & FLAG_V) printf(" %#-10.8x |",new_section->voffset);
  320.                 fread(&new_section->rsize,4,1,tocrypt);
  321.                 if (argflags & FLAG_V) printf(" %#-10.8x |",new_section->rsize);
  322.                 fread(&new_section->roffset,4,1,tocrypt);
  323.                 if (argflags & FLAG_V) printf(" %#-10.8x |",new_section->roffset);
  324.                 fseek(tocrypt,0xc,SEEK_CUR);
  325.                 fread(&new_section->flags,4,1,tocrypt);
  326.                 if (argflags & FLAG_V) printf(" %#-10.8x |\n",new_section->flags);
  327.                 if (!strcmp(new_section->sname,".idata")) // rename the .idata
  328.                         snprintf(new_section->sname,sizeof(new_section->sname),".import");
  329.                 insert(new_section);
  330.                 delete new_section;
  331.                 tmp++;
  332.         }
  333.  
  334.         if (argflags & FLAG_V) printf("+----------+-------------+------------+------------+------------+------------+\n");
  335.  
  336.         tmp=0;
  337.         current=head;
  338.         while (current!=NULL) {
  339.                 if (current->roffset==0x0 || current->rsize==0x0) {
  340.                         current=current->next;
  341.                         continue;
  342.                 }
  343.                 if (current->roffset>pe_start) {
  344.                         if (!tmp) tmp=current->roffset;
  345.                         else if (current->roffset<tmp) tmp=current->roffset;
  346.                 }
  347.                 current=current->next;
  348.         }
  349.         if (((nos+2)*0x28+pe_start+0xf8)>=tmp) {
  350.                 fprintf(stderr,"ERROR!: No space for new sections\n");
  351.                 return 1;
  352.         }
  353.  
  354.         for (tmp=0; tmp<2; tmp++) {
  355.                 Section  *new_section = new Section();
  356.                 current=head;
  357.                 while (current!=NULL) {
  358.                         new_section->voffset=(new_section->voffset<current->voffset+current->vsize)?current->voffset+current->vsize:new_section->voffset;
  359.                         new_section->roffset=(new_section->roffset<current->roffset+current->rsize)?current->roffset+current->rsize:new_section->roffset;
  360.                         current=current->next;
  361.                 }
  362.                 new_section->voffset+=(0x1000-new_section->voffset%0x1000)%0x1000;
  363.                 if (tmp==0) { // add new .idata section
  364.                         snprintf(new_section->sname,sizeof(new_section->sname),".idata");
  365.                         new_section->vsize=0x200;
  366.                         new_section->rsize=0x200;
  367.                         new_section->flags=0xc0000040;
  368.                 } else if (tmp==1) { // add .Ob section
  369.                         snprintf(new_section->sname,sizeof(new_section->sname),".Ob");
  370.                         new_section->vsize=0x92+0x11*nos+0x88; // stub size
  371.                         new_section->vsize+=(0x200-new_section->vsize%0x200)%0x200;
  372.                         new_section->rsize=0x92+0x11*nos+0x88;
  373.                         new_section->rsize+=(0x200-new_section->rsize%0x200)%0x200;
  374.                         new_section->flags=0xe00000e0;
  375.                 }
  376.                 insert(new_section);
  377.                 delete new_section;
  378.                 nos++;
  379.         }
  380.         if (argflags & FLAG_V) printf("\n");
  381.  
  382.         return 0;
  383. }
  384.  
  385. int Info::writefile()
  386. {
  387.         char c_tmp;
  388.         unsigned int i_tmp, i_tmp2;
  389.         Section *current, *s_tmp;
  390.         int xornum, flag;
  391.         char msg[]="\rWriting the crypted file [%u/%u bytes]";
  392.  
  393.         fseek(tocrypt,0,SEEK_SET);
  394.         if (argflags & FLAG_V) printf(msg,ftell(tocrypt),size);
  395.         i_tmp=pe_start+0xf8;
  396.         while (ftell(tocrypt)<i_tmp)
  397.                 if (ftell(tocrypt)==pe_start+0x6) {
  398.                         fwrite(&nos,2,1,crypted);
  399.                         fseek(tocrypt,2,SEEK_CUR);
  400.                 } else if (ftell(tocrypt)==pe_start+0x28) {
  401.                         fwrite(&tail->voffset,4,1,crypted);
  402.                         fseek(tocrypt,4,SEEK_CUR);
  403.                 } else if (ftell(tocrypt)==pe_start+0x50) {
  404.                         i_tmp2=tail->voffset+tail->vsize;
  405.                         fwrite(&i_tmp2,4,1,crypted);
  406.                         fseek(tocrypt,4,SEEK_CUR);
  407.                 } else if (ftell(tocrypt)==pe_start+0x80) {
  408.                         i_tmp2=tail->voffset-0x1000;
  409.                         fwrite(&i_tmp2,4,1,crypted);
  410.                         fwrite("\x63\x00\x00\x00",4,1,crypted);
  411.                         fseek(tocrypt,8,SEEK_CUR);
  412.                 } else {
  413.                         fread(&c_tmp,1,1,tocrypt);
  414.                         fwrite(&c_tmp,1,1,crypted);
  415.                 }
  416.         current=head;
  417.         while (current!=NULL) {
  418.                 fwrite(current->sname,8,1,crypted);
  419.                 fwrite(&current->vsize,4,1,crypted);
  420.                 fwrite(&current->voffset,4,1,crypted);
  421.                 fwrite(&current->rsize,4,1,crypted);
  422.                 if ((argflags & FLAG_E) && (!strcmp(current->sname,".Ob") || !strcmp(current->sname,".idata"))) {
  423.                         i_tmp=size;
  424.                         i_tmp+=(0x1000-i_tmp%0x1000)%0x1000;
  425.                         if (!strcmp(current->sname,".Ob"))
  426.                                 i_tmp+=0x200;
  427.                         fwrite(&i_tmp,4,1,crypted);
  428.                 } else fwrite(&current->roffset,4,1,crypted);
  429.                 fseek(tocrypt,ftell(crypted),SEEK_SET);
  430.                 for (i_tmp=0;i_tmp<0xc;i_tmp++) {
  431.                         fread(&c_tmp,1,1,tocrypt);
  432.                         fwrite(&c_tmp,1,1,crypted);
  433.                 }
  434.                 i_tmp=current->flags | 0x80000000; // make the section writable
  435.                 fwrite(&i_tmp,4,1,crypted);
  436.                 current=current->next;
  437.         }
  438.         fseek(tocrypt,ftell(crypted),SEEK_SET);
  439.         resort(byRawOffset);
  440.         current=head;
  441.         while (current->roffset==0x0 || current->rsize==0x0)
  442.                 current=current->next;
  443.         while (ftell(tocrypt)<current->roffset) { // write to the file util the start of the first section
  444.                 fread(&c_tmp,1,1,tocrypt);
  445.                 fwrite(&c_tmp,1,1,crypted);
  446.         }
  447.         /* crypter */
  448.         while (current!=NULL) {
  449.                 if (current->roffset==0x0 || current->rsize==0x0) {
  450.                         current=current->next;
  451.                         continue;
  452.                 }
  453.                 if (strncmp(current->sname,".idata",8)) {
  454.                         fseek(tocrypt,current->roffset,SEEK_SET);
  455.                         s_tmp=current->next;
  456.                         while (s_tmp!=NULL && (s_tmp->roffset==0x0 || s_tmp->rsize==0x0))
  457.                                 s_tmp=s_tmp->next;
  458.  
  459.                         if ((!strcmp(current->sname,".rsrc") ||
  460.                                 !strcmp(current->sname,".rdata")) &&
  461.                                 (import_rva>current->voffset &&
  462.                                 import_rva<(current->voffset+current->vsize))) //if import table is in .rsrc or .rdata
  463.                                         i_tmp=(import_rva-current->voffset)+current->roffset;
  464.                         else
  465.                         crypt:  i_tmp=s_tmp->roffset;
  466.  
  467.                         xornum=0xa0;
  468.                         while (ftell(tocrypt)<i_tmp) {
  469.                                 fread(&c_tmp,1,1,tocrypt);
  470.                                 if ((strcmp(current->sname,".rsrc") &&
  471.                                         strcmp(current->sname,".rdata")) || flag) { // crypt all sections except
  472.                                         xornum++;
  473.                                         if (xornum==0xff) xornum=0xa1;
  474.                                         if (c_tmp!='\x0' && c_tmp!=(char)xornum) c_tmp^=xornum;
  475.                                 }
  476.                                 fwrite(&c_tmp,1,1,crypted);
  477.                                 if ((argflags & FLAG_V) && !(ftell(tocrypt)%0xffff)) {
  478.                                         printf(msg,ftell(tocrypt),size);
  479.                                         fflush(stdout);
  480.                                 }
  481.                         }
  482.                         flag=0;
  483.                         if (i_tmp<s_tmp->roffset) {
  484.                                 flag=1;
  485.                                 goto crypt; // crypt the import table
  486.                         }
  487.                 } else {
  488.  
  489.                         if (argflags & FLAG_E) { // write the whole file
  490.                                 current->roffset=size;
  491.                                 current->roffset+=(0x1000-current->roffset%0x1000)%0x1000;
  492.                                 while (ftell(tocrypt)<size) {
  493.                                         fread(&c_tmp,1,1,tocrypt);
  494.                                         fwrite(&c_tmp,1,1,crypted);
  495.                                         if ((argflags & FLAG_V) && !(ftell(tocrypt)%0xffff)) {
  496.                                                 printf(msg,ftell(tocrypt),size);
  497.                                                 fflush(stdout);
  498.                                         }
  499.                                 }
  500.                                 while(ftell(crypted)<current->roffset)
  501.                                         fwrite("\x0",1,1,crypted);
  502.                         }
  503.  
  504.                         /* new Import Table section */
  505.                         fwrite("\x0\x0\x0\x0",4,1,crypted); // OriginalFirstThunk
  506.                         fwrite("\x0\x0\x0\x0",4,1,crypted); // TimeDateStamp
  507.                         fwrite("\x0\x0\x0\x0",4,1,crypted); // ForwarderChain
  508.                         i_tmp=current->voffset+0x28;
  509.                         fwrite(&i_tmp,4,1,crypted); // Name
  510.                         i_tmp=current->voffset+0x35;
  511.                         fwrite(&i_tmp,4,1,crypted); // FirstThunk
  512.                         for (i_tmp=0;i_tmp<5;i_tmp++)
  513.                                 fwrite("\x0\x0\x0\x0",4,1,crypted); // add one null descriptor
  514.                         fwrite("KERNEL32.DLL\x0",13,1,crypted);
  515.                         i_tmp=current->voffset+0x41;
  516.                         fwrite(&i_tmp,4,1,crypted); // pointer to the LoadLibraryA
  517.                         i_tmp=current->voffset+0x4F;
  518.                         fwrite(&i_tmp,4,1,crypted); // pointer to the GetProcAddress
  519.                         fwrite("\x0\x0\x0\x0",4,1,crypted); // end of API pointers
  520.                         fwrite("\x0\x0",2,1,crypted); // Hint
  521.                         fwrite("LoadLibraryA",12,1,crypted);
  522.                         fwrite("\x0\x0",2,1,crypted); // Hint
  523.                         fwrite("GetProcAddress\x0\x0\x0\x0",18,1,crypted);
  524.                         while (ftell(crypted)<current->roffset+current->rsize)
  525.                                 fwrite("\x0",1,1,crypted);
  526.                         current=current->next;
  527.  
  528.                         /* stub section */
  529.                         /* ;stub in opcodes and assembly
  530.                          *
  531.                          * ;section information
  532.                          *
  533.                          * B9 xxxxxxxx      MOV ECX, [start of section]
  534.                          * BB xxxxxxxx      MOV EBX, [end of section]
  535.                          * BA xxxxxxxx      MOV EDX, [address for the next section information ]
  536.                          *
  537.                          * ;decrypter
  538.                          *
  539.                          * B0 A0            MOV AL,0A0
  540.                          * FEC0             INC AL
  541.                          * 3C FF            CMP AL,0FF ;to eax will be 0xA1 until 0xFE
  542.                          * 74 F8            JZ SHORT [address of the 4th mov]
  543.                          * 36:8039 00       CMP BYTE PTR SS:[ECX],0
  544.                          * 74 08            JZ SHORT [sto inc ecx]
  545.                          * 36:38 01         CMP BYTE PTR SS:[ECX],AL
  546.                          * 74 03            JZ SHORT [sto inc ecx]
  547.                          * 363001           XOR BYTE PTR SS:[ECX],AL
  548.                          * 41               INC ECX
  549.                          * 3BCB             CMP ECX,EDX
  550.                          * 75 E7            JNZ SHORT [address of the 1st inc]
  551.                          * B8 xxxxxxxx      MOV EAX, [address of the 4th mov]
  552.                          * FF E2            JMP EDX
  553.                          *
  554.                          * ;next section information...this will repeated for all the crypted sections
  555.                          *
  556.                          * B9 xxxxxxxx      MOV ECX, [start of section]
  557.                          * BB xxxxxxxx      MOV EBX, [end of section]
  558.                          * BA xxxxxxxx      MOV EDX, [address for the next section information ]
  559.                          * FF E0            JMP EAX ;back to the decrypter
  560.                          *
  561.                          * ;old ImportTable loader
  562.                          *
  563.                          * 55               PUSH EBP
  564.                          * 8BEC             MOV EBP,ESP
  565.                          * 83EC 14          SUB ESP,0C ;create memory for 3 dword variables
  566.                          * BB xxxxxxxx      MOV EBX,[imagebase + import_rva]
  567.                          * 36:8B53 0C       MOV EDX,DWORD PTR SS:[EBX+C] ;get the name of dll
  568.                          * 8955 FC          MOV DWORD PTR SS:[EBP-4],EDX
  569.                          * 8145 FC xxxxxxxx ADD DWORD PTR SS:[EBP-4],[imagebase]
  570.                          * 36:8B53 10       MOV EDX,DWORD PTR SS:[EBX+10] ;get the FirstThunk
  571.                          * 8955 F8          MOV DWORD PTR SS:[EBP-8], EDX
  572.                          * 8145 F8 xxxxxxxx ADD DWORD PTR SS:[EBP-8], [imagebase]
  573.                          * FF75 FC          PUSH DWORD PTR SS:[EBP-4] ;push the arg1 (dll name)
  574.                          * 36:FF15 xxxxxxxx CALL DWORD PTR SS:[imagebase+voffset+0x35] ;call LoadLibrayA
  575.                          * 8945 F4          MOV DWORD PTR SS:[EBP-C], EAX ;save the result
  576.                          * 8B4D F8          MOV ECX,DWORD PTR SS:[EBP-8]
  577.                          * 36:8B11          MOV EDX,DWORD PTR SS:[ECX] ;get the API name
  578.                          * F7C2 00000080    TEST EDX,80000000
  579.                          * 74 08            JZ SHORT
  580.                          * 81E2 FFFFFF7F    AND EDX,7FFFFFFF
  581.                          * EB 06            JMP SHORT
  582.                          * 81C2 xxxxxxxx    ADD EDX,[imagebase + 2]
  583.                          * 51               PUSH ECX ;save ecx in the stack
  584.                          * 52               PUSH EDX ;push arg2 (API name)
  585.                          * FF75 F4          PUSH DWORD PTR SS:[EBP-C] ;push arg1 (first result)
  586.                          * 36:FF15 xxxxxxxx CALL DWORD PTR SS:[imagebase+voffset+0x39] ;call GetProcAddress
  587.                          * 59               POP ECX; restore ecx
  588.                          * 36:8901          MOV DWORD PTR SS:[ECX],EAX ;save the IAT pointer
  589.                          * 83C1 04          ADD ECX,4
  590.                          * 36:8339 00       CMP DWORD PTR SS:[ECX],0
  591.                          * 75 CE            JNZ SHORT ;loop back for the next API
  592.                          * 83C3 14          ADD EBX,14 ;next descriptor
  593.                          * 36:837B 10 00    CMP DWORD PTR SS:[EBX+10],0
  594.                          * 75 98            JNZ SHORT ;loop back for the next dll
  595.                          * 8BE5             MOV ESP,EBP ;clear the memory
  596.                          * 5D               POP EBP
  597.                          *
  598.                          * B8 xxxxxxxx      MOV EAX,[orignal entry point + imagebase]
  599.                          * FF E0            JMP EAX
  600.                          */
  601.                         if (argflags & FLAG_V) {
  602.                                 printf(msg,ftell(tocrypt),size);
  603.                                 if (ftell(tocrypt)<size)
  604.                                         printf("\n%lu useless bytes have been cleaned ...",size-ftell(tocrypt));
  605.                                 printf("\nWriting the stub ...\n");
  606.                         }
  607.  
  608.                         /* stub */
  609.                         s_tmp=head;
  610.                         int i_edx;
  611.                         fwrite("\xB9",1,1,crypted);
  612.                         while (s_tmp!=NULL && (s_tmp->roffset==0x0 || s_tmp->rsize==0x0))
  613.                                 s_tmp=s_tmp->next;
  614.                         i_tmp=s_tmp->voffset+imagebase;
  615.                         fwrite(&i_tmp,4,1,crypted);
  616.                         fwrite("\xBB",1,1,crypted);
  617.                         i_tmp=s_tmp->voffset+s_tmp->vsize+imagebase;
  618.                         fwrite(&i_tmp,4,1,crypted);
  619.                         fwrite("\xBA",1,1,crypted);
  620.                         i_edx=imagebase+current->voffset+0x31;
  621.                         fwrite(&i_edx,4,1,crypted);
  622.                         fwrite("\xB0\xA0",2,1,crypted);
  623.                         fwrite("\xFE\xC0",2,1,crypted);
  624.                         fwrite("\x3C\xFF",2,1,crypted);
  625.                         fwrite("\x74\xF8",2,1,crypted);
  626.                         fwrite("\x36\x80\x39\x00",4,1,crypted);
  627.                         fwrite("\x74\x08",2,1,crypted);
  628.                         fwrite("\x36\x38\x01",3,1,crypted);
  629.                         fwrite("\x74\x03",2,1,crypted);
  630.                         fwrite("\x36\x30\x01",3,1,crypted);
  631.                         fwrite("\x41",1,1,crypted);
  632.                         fwrite("\x3B\xCB",2,1,crypted);
  633.                         fwrite("\x75\xE7",2,1,crypted);
  634.                         fwrite("\xB8",1,1,crypted);
  635.                         i_tmp=imagebase+current->voffset+0xf;
  636.                         fwrite(&i_tmp,4,1,crypted);
  637.                         fwrite("\xFF\xE2",2,1,crypted);
  638.                         s_tmp=s_tmp->next;
  639.                         while (strncmp(s_tmp->sname,".idata",8)) {
  640.                                 flag=0;
  641.                                 if (s_tmp->roffset==0x0 || s_tmp->voffset==0x0) {
  642.                                         s_tmp=s_tmp->next;
  643.                                         continue;
  644.                                 }
  645.                                 if (!strcmp(s_tmp->sname,".rsrc") ||
  646.                                         !strcmp(s_tmp->sname,".rdata")) {
  647.                                         if (import_rva>s_tmp->voffset &&
  648.                                                 import_rva<(s_tmp->voffset+s_tmp->vsize))
  649.                                                         flag=1;
  650.                                         else {
  651.                                                 s_tmp=s_tmp->next;
  652.                                                 continue;
  653.                                         }
  654.                                 }
  655.                                 fwrite("\xB9",1,1,crypted);
  656.                                 if (flag) i_tmp=imagebase+import_rva;
  657.                                 else i_tmp=imagebase+s_tmp->voffset;
  658.                                 fwrite(&i_tmp,4,1,crypted);
  659.                                 fwrite("\xBB",1,1,crypted);
  660.                                 i_tmp=imagebase+s_tmp->voffset+s_tmp->vsize;
  661.                                 fwrite(&i_tmp,4,1,crypted);
  662.                                 fwrite("\xBA",1,1,crypted);
  663.                                 i_edx+=0x11;
  664.                                 fwrite(&i_edx,4,1,crypted);
  665.                                 fwrite("\xFF\xE0",2,1,crypted);
  666.                                 s_tmp=s_tmp->next;
  667.                         }
  668.  
  669.                         /* old ImportTable loader */
  670.                         fwrite("\x55",1,1,crypted);
  671.                         fwrite("\x8B\xEC",2,1,crypted);
  672.                         fwrite("\x83\xEC\x0C",3,1,crypted);
  673.                         fwrite("\xBB",1,1,crypted);
  674.                         i_tmp=imagebase+import_rva;
  675.                         fwrite(&i_tmp,4,1,crypted);
  676.                         fwrite("\x36\x8B\x53\x0C",4,1,crypted);
  677.                         fwrite("\x89\x55\xFC",3,1,crypted);
  678.                         fwrite("\x81\x45\xFC",3,1,crypted);
  679.                         fwrite(&imagebase,4,1,crypted);
  680.                         fwrite("\x36\x8B\x53\x10",4,1,crypted);
  681.                         fwrite("\x89\x55\xF8",3,1,crypted);
  682.                         fwrite("\x81\x45\xF8",3,1,crypted);
  683.                         fwrite(&imagebase,4,1,crypted);
  684.                         fwrite("\xFF\x75\xFC",3,1,crypted);
  685.                         fwrite("\x36\xFF\x15",3,1,crypted);
  686.                         i_tmp=imagebase+s_tmp->voffset+0x35;
  687.                         fwrite(&i_tmp,4,1,crypted);
  688.                         fwrite("\x89\x45\xF4",3,1,crypted);
  689.                         fwrite("\x8B\x4D\xF8",3,1,crypted);
  690.                         fwrite("\x36\x8B\x11",3,1,crypted);
  691.                         fwrite("\xF7\xC2\x00\x00\x00\x80",6,1,crypted);
  692.                         fwrite("\x74\x08",2,1,crypted);
  693.                         fwrite("\x81\xE2\xFF\xFF\xFF\x7F",6,1,crypted);
  694.                         fwrite("\xEB\x06",2,1,crypted);
  695.                         fwrite("\x81\xC2",2,1,crypted);
  696.                         i_tmp=imagebase+2;
  697.                         fwrite(&i_tmp,4,1,crypted);
  698.                         fwrite("\x51",1,1,crypted);
  699.                         fwrite("\x52",1,1,crypted);
  700.                         fwrite("\xFF\x75\xF4",3,1,crypted);
  701.                         fwrite("\x36\xFF\x15",3,1,crypted);
  702.                         i_tmp=imagebase+s_tmp->voffset+0x39;
  703.                         fwrite(&i_tmp,4,1,crypted);
  704.                         fwrite("\x59",1,1,crypted);
  705.                         fwrite("\x36\x89\x01",3,1,crypted);
  706.                         fwrite("\x83\xC1\x04",3,1,crypted);
  707.                         fwrite("\x36\x83\x39\x00",4,1,crypted);
  708.                         fwrite("\x75\xCE",2,1,crypted);
  709.                         fwrite("\x83\xC3\x14",3,1,crypted);
  710.                         fwrite("\x36\x83\x7B\x10\x00",5,1,crypted);
  711.                         fwrite("\x75\x98",2,1,crypted);
  712.                         fwrite("\x8B\xE5",2,1,crypted);
  713.                         fwrite("\x5D",1,1,crypted);
  714.                         /* jmp to OEP */
  715.                         fwrite("\xB8",1,1,crypted);
  716.                         i_tmp=imagebase+oep;
  717.                         fwrite(&i_tmp,4,1,crypted);
  718.                         fwrite("\xFF\xE0",2,1,crypted);
  719.                 }
  720.                 while (ftell(crypted)<current->roffset+current->rsize)
  721.                         fwrite("\x0",1,1,crypted);
  722.                 current=current->next;
  723.         }
  724.         if (argflags & FLAG_V) printf("Done!\n");
  725.         return 0;
  726. }