Advertisement
Guest User

PE Injector

a guest
Jun 1st, 2015
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. void SetFlags(IMAGE_SECTION_HEADER* pSectHeader) {
  2.     pSectHeader->Characteristics = 2684354592;
  3. }
  4.  
  5. void CompleteSection(IMAGE_NT_HEADERS *pHeader, IMAGE_SECTION_HEADER *pLastSectHeader, LPBYTE pBase, TCHAR *FileName) {
  6.     unsigned long FileAlignment = pHeader->OptionalHeader.FileAlignment;
  7.     unsigned long SectionAlignment = pHeader->OptionalHeader.SectionAlignment;
  8.  
  9.     IMAGE_SECTION_HEADER* pFirstSectHeader = IMAGE_FIRST_SECTION(pHeader);
  10.     unsigned long StartPoint = pFirstSectHeader->VirtualAddress;
  11.     unsigned long FinishPoint = pLastSectHeader->VirtualAddress;
  12.    
  13.     unsigned long RVAAddress = pHeader->OptionalHeader.AddressOfEntryPoint;
  14.     unsigned long TempEntryPoint = pHeader->OptionalHeader.BaseOfCode;
  15.  
  16.     printf("\nInit Address...\nStart Address = %d\nFinish Adress = %d\nNumber of Characters = %d\n",
  17.         StartPoint, FinishPoint, FinishPoint - StartPoint);
  18.     printf("Start Writing Encoder Data ");
  19.     FILE *BinaryGoal = fopen(FileName, "ab+");
  20.    
  21.     if (!BinaryGoal) {
  22.         printf("Error! File is not opened (BinaryFile)!\n");
  23.         return;
  24.     }
  25.  
  26.     fseek(BinaryGoal, pLastSectHeader->SizeOfRawData + pLastSectHeader->PointerToRawData, SEEK_SET);
  27.  
  28.     struct output *StartInHex = GetASMValue(StartPoint);
  29.     struct output *FinishInHex = GetASMValue(FinishPoint);
  30.     struct output *NumberOfData = GetASMValue(FinishPoint - StartPoint);
  31.     struct output *EntryPoint = GetASMValue(TempEntryPoint);
  32.    
  33.     int data[] = {
  34.         0xB8,/* mov eax + 4 byte*/
  35.         0xBB, /*mov ebx + 4 byte*/
  36.         0x66, 0xB9,/*mov cx + 2 byte*/
  37.         0x8A, 0x28,/*mov,ch,eax*/
  38.         0x80, 0xF5, 0x02, /*xor ch, 2*/
  39.         0x88, 0x28, /*mov [eax], ch*/
  40.         0x40, /*inc eax*/
  41.         0xE2, 0xF6, /*loop*/
  42.         0xFF, 0xE3 /*jmp ebx*/
  43.     };
  44.  
  45.     int i = 4;
  46.     int j = 0;
  47.  
  48.     fprintf(BinaryGoal, "%с", data[0]);
  49.     for (j = 0; j < StartInHex->length; j++)
  50.         fprintf(BinaryGoal, "%с", StartInHex->digit[j]);
  51.        
  52.     while (j < 4) {
  53.         fprintf(BinaryGoal, "%с", 0);
  54.         j++;
  55.     }
  56.    
  57.     fprintf(BinaryGoal, "%c", data[1]);
  58.     for (j = 0; j < EntryPoint->length; j++)
  59.         fprintf(BinaryGoal, "%c", EntryPoint->digit[j]);
  60.  
  61.     while (j < 4) {
  62.         fprintf(BinaryGoal, "%c", 0);
  63.         j++;
  64.     }
  65.  
  66.     fprintf(BinaryGoal, "%c", data[2]);
  67.     fprintf(BinaryGoal, "%c", data[3]);
  68.     for (j = 0; j < NumberOfData->length; j++)
  69.         fprintf(BinaryGoal, "%c", NumberOfData->digit[j]);
  70.    
  71.     while (j < 2) {
  72.         fprintf(BinaryGoal, "%c", 0);
  73.         j++;
  74.     }
  75.  
  76.     for (; i < 16; i++)
  77.         fprintf(BinaryGoal, "%c", data[i]);
  78.  
  79.     fclose(BinaryGoal);
  80.     printf("[ok]\n");
  81.  
  82.     printf("Set Flags Value...");
  83.     SetFlags(pLastSectHeader);
  84.     printf("[ok]\n");
  85.  
  86.     unsigned long NewEntryPoint = pLastSectHeader->Misc.VirtualSize;
  87.     pHeader->OptionalHeader.BaseOfCode = NewEntryPoint;
  88.  
  89.     pLastSectHeader->Misc.VirtualSize += 26;
  90.     pLastSectHeader->SizeOfRawData += 26;
  91.    
  92.     if (pLastSectHeader->SizeOfRawData % FileAlignment) {
  93.         unsigned long temp = pLastSectHeader->SizeOfRawData / FileAlignment;
  94.         temp = pLastSectHeader->SizeOfRawData - (temp * FileAlignment);
  95.         pLastSectHeader->SizeOfRawData += FileAlignment - temp;
  96.     }
  97.    
  98.     pHeader->OptionalHeader.SizeOfImage = pLastSectHeader->Misc.VirtualSize + pLastSectHeader->VirtualAddress;
  99.     if (pHeader->OptionalHeader.SizeOfImage % SectionAlignment) {
  100.         unsigned long temp = pHeader->OptionalHeader.SizeOfImage / SectionAlignment;
  101.         temp = pHeader->OptionalHeader.SizeOfImage - (temp * SectionAlignment);
  102.         pHeader->OptionalHeader.SizeOfImage += SectionAlignment - temp;
  103.     }
  104.    
  105.     StartPoint = pFirstSectHeader->PointerToRawData;
  106.     FinishPoint = pLastSectHeader->PointerToRawData;
  107.     int counter = StartPoint;
  108.     printf("Encrypt...");
  109.     /*XOR'ed Data*/
  110.     while (counter != FinishPoint) {
  111.         pBase[counter] = pBase[counter] ^ 2;
  112.         counter++;
  113.     }
  114.     printf("[ok]\n");
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement