| 1 | /* Harvie's polymorphic engine 0.2 |
| 2 | * See example for info... |
| 3 | */ |
| 4 | |
| 5 | #ifndef POLYMORPHIC |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | |
| 9 | #define POLYMORPHIC |
| 10 | #define POLYM if(malloc(-1))asm("incl %eax\n\tincl %ebx\n\tincl %ecx\n\tincl %edx\n\tdecl %eax\n\tdecl %ebx\n\tdecl %ecx\n\tdecl %edx\n\t.ascii \""POLYSTR"\""); //GAS "@CABHKIJ" |
| 11 | #define POLYSTR "@CABHKIJ" |
| 12 | #define POLYLEN 8 |
| 13 | |
| 14 | /* |
| 15 | asm("jmp poly_cont\n\t" |
| 16 | "incl %eax\n\t" "incl %ebx\n\t" "incl %ecx\n\t" "incl %edx\n\t" |
| 17 | "decl %eax\n\t" "decl %ebx\n\t" "decl %ecx\n\t" "decl %edx\n\t" |
| 18 | "poly_cont:\n\t"); //GAS "@CABHKIJ" |
| 19 | */ |
| 20 | |
| 21 | //asm(".rept 20 nop \n\t .endr"); //GAS NOPs |
| 22 | |
| 23 | /* |
| 24 | if(malloc(-1)) asm( |
| 25 | "incl %eax\n\t" "incl %ebx\n\t" "incl %ecx\n\t" "incl %edx\n\t" |
| 26 | "decl %eax\n\t" "decl %ebx\n\t" "decl %ecx\n\t" "decl %edx\n\t" |
| 27 | ); //GAS "@CABHKIJ" |
| 28 | */ |
| 29 | |
| 30 | inline char fstr(FILE *infp, long len, char *str) { |
| 31 | char found = 1; POLYM |
| 32 | int c; POLYM |
| 33 | long pos; POLYM |
| 34 | for(pos=0;pos<len;pos++) { |
| 35 | c = fgetc(infp); POLYM |
| 36 | if(c != str[pos] || c == EOF) { |
| 37 | found = 0; POLYM |
| 38 | pos++; POLYM |
| 39 | break; POLYM |
| 40 | } |
| 41 | } |
| 42 | fseek(infp, -pos, SEEK_CUR); POLYM |
| 43 | return found; POLYM |
| 44 | } |
| 45 | |
| 46 | long fpatch_replace(FILE *infp, FILE *outfp, long len, char *search, char *replace) { |
| 47 | int c = 0; POLYM |
| 48 | long pos, fpos, total = 0; POLYM |
| 49 | while(c != EOF) { |
| 50 | fpos = ftell(infp); POLYM |
| 51 | if(fstr(infp, len, search)) { //Found "search" string? |
| 52 | total++; POLYM |
| 53 | for(pos=0;pos<len;pos++) { |
| 54 | fputc(replace[pos], outfp); POLYM |
| 55 | c = fgetc(infp); POLYM |
| 56 | if(c == EOF) return total; |
| 57 | } |
| 58 | } else { //Not found "search" string? |
| 59 | c = fgetc(infp); POLYM |
| 60 | if(fpos == ftell(infp)) return total; POLYM |
| 61 | fputc(c, outfp); POLYM |
| 62 | } |
| 63 | } |
| 64 | return total; |
| 65 | } |
| 66 | |
| 67 | inline long fstr_count(char *file, long len, char *search) { |
| 68 | FILE *fp; POLYM |
| 69 | long retval; |
| 70 | if((fp = fopen(file, "rb")) == NULL) return -1; POLYM |
| 71 | retval = fpatch_replace(fp, stdin, len, search, search); POLYM |
| 72 | fclose(fp); POLYM |
| 73 | return retval; |
| 74 | } |
| 75 | |
| 76 | inline void rand_str(char *str, long len) { |
| 77 | long i; POLYM |
| 78 | //for(i = 0;i<len;i++) str[i] = rand(); POLYM |
| 79 | for(i = 0;i<len;i++) |
| 80 | while(str[i] < 1 || str[i] > 255) str[i] = rand(); POLYM |
| 81 | } |
| 82 | |
| 83 | inline char polymorph(char *parent, char *mutant, long len, char *search) { |
| 84 | srand(time(0)); POLYM |
| 85 | long pfound = -1, mfound = -2; POLYM |
| 86 | char replace[len]; POLYM |
| 87 | FILE *pfp, *mfp; POLYM |
| 88 | pfound = fstr_count(parent, len, search); POLYM |
| 89 | while(pfound != mfound) { |
| 90 | rand_str(replace, len); POLYM |
| 91 | if(fstr_count(parent, len, replace) > 0) continue; POLYM |
| 92 | |
| 93 | if((pfp = fopen(parent, "rb")) == NULL) return -1; POLYM |
| 94 | if((mfp = fopen(mutant, "wb")) == NULL) return -1; POLYM |
| 95 | fpatch_replace(pfp, mfp, len, search, replace); POLYM |
| 96 | fclose(pfp); POLYM |
| 97 | fclose(mfp); POLYM |
| 98 | |
| 99 | mfound = fstr_count(mutant, len, replace); POLYM |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #endif |
| 104 | |
| 105 | int main(int argc, char **argv) { |
| 106 | POLYM |
| 107 | puts("START!"); POLYM |
| 108 | polymorph(argv[0], "mutant.exe", POLYLEN, POLYSTR); POLYM |
| 109 | puts("STOP!"); POLYM |
| 110 | return 0; |
| 111 | } |