Guest User

richParser.c

a guest
Jan 21st, 2023
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.87 KB | Software | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4.  
  5. void printHex(char *output)
  6. {
  7.         unsigned char *ptr;
  8.         unsigned char ldword[4], udword[4];
  9.         char uformatted[16], lformatted[16];
  10.  
  11.         for (int i = 0; i < 4; i++) {
  12.                 ptr = output + i;
  13.                 ldword[4-i-1] = *ptr;
  14.                 udword[4-i-1] = *(ptr + 4);
  15.         }
  16.  
  17.         for (int i = 0; i < 4; i++) {
  18.                 sprintf(lformatted + i * 2, "%02x", ldword[i]);
  19.                 sprintf(uformatted + i * 2, "%02x", udword[i]);
  20.         }
  21.  
  22.         printf("%s  %s - ", lformatted, uformatted);
  23.  
  24.         if (strcmp(lformatted, "536e6144") == 0) {
  25.                 printf("%-8s", "DanS");
  26.         } else {
  27.                 printf("%d.%d.%d",
  28.                 ldword[2] * 256 + ldword[3],
  29.                 ldword[0] * 256 + ldword[1],
  30.                 udword[2] * 256 + udword[3]);
  31.         }
  32.  
  33.         printf("\n");
  34.  
  35.         return;
  36. }
  37.  
  38. void richParser(char *fbuf)
  39. {
  40.         IMAGE_DOS_HEADER *dosHeader = (IMAGE_DOS_HEADER *)fbuf;
  41.         IMAGE_NT_HEADERS *ntHeaders = (IMAGE_NT_HEADERS *)((size_t)dosHeader + dosHeader->e_lfanew);
  42.         if (dosHeader->e_lfanew == 0x80) {
  43.                 printf("Rich Header does not exist\n");
  44.                 return;
  45.         }
  46.  
  47.         // find "Rich"
  48.         size_t richLen = dosHeader->e_lfanew - 0x80;
  49.         size_t richOffset;
  50.         char richbuf[richLen + 1];
  51.         char *output = calloc(16, 0);
  52.         char key[4];
  53.         char *tok;
  54.         char tmp;
  55.  
  56.         memcpy(richbuf, fbuf + 0x80, richLen);
  57.         tok = strstr(richbuf, "Rich");
  58.         richOffset = tok - richbuf;
  59.         if (tok == NULL) {
  60.                 printf("Broken binary\n");
  61.                 return;
  62.         }
  63.         memcpy(key, tok + 4, 4);
  64.  
  65.         for (int i = 0; i < richOffset; i++) {
  66.                 output[i % 8] = richbuf[i] ^ key[i % 4];
  67.                 // if (i % 4 == 3) printf("%s ", output);
  68.                 if (i % 8 == 7) printHex(output);
  69.         }
  70.  
  71.         free(output);
  72.         free(fbuf);
  73.  
  74.         return;
  75. }
  76.  
  77. BOOL readFile(char *fname, char **fbuf, size_t *len)
  78. {
  79.         FILE *fd = fopen(fname, "rb");
  80.         if (fd) {
  81.                 fseek(fd, 0, SEEK_END);
  82.                 *len = ftell(fd);
  83.                 fseek(fd, 0, SEEK_SET);
  84.                 *fbuf = malloc(*len + 1);
  85.                 fread(*fbuf, *len, 1, fd);
  86.                 return 1;
  87.         }
  88.         printf("Failed to read file: %s\n", fname);
  89.         return 0;
  90. }
  91.  
  92. int main(int argc, char **argv)
  93. {
  94.         if (argc != 2) {
  95.                 printf("Usage: %s <PE Binary>\n", argv[0]);
  96.                 return 0;
  97.         }
  98.  
  99.         char *fbuf; size_t len;
  100.         if (readFile(argv[1], &fbuf, &len)) {
  101.                 richParser(fbuf);
  102.         } else {
  103.                 printf("Unable to parse exe\n");
  104.         }
  105.  
  106.         return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment