2012-11-06 1 views
2

bon après-midi.MakeCodeWritable

J'ai obtenu le code ci-dessous sur un livre. J'essaie de l'exécuter, mais je ne sais pas quels sont les "premiers" et "derniers" paramètres de la fonction MakeCodeWritable, ou où je peux les trouver. Quelqu'un peut-il aider? Ce code concerne la méthode C obfuscation. J'utilise le programme Xcode et le compilateur LLVM GCC 4.2.

#include <stdio.h> 
#include <sys/mman.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/types.h> 

typedef unsigned int uint32; 
typedef char* caddr_t; 
typedef uint32* waddr_t; 


#define Tam_celula 64 

#define ALIGN __attribute__((aligned(Tam_celula))) 

void makeCodeWritable(char* first, char* last) { 

char* firstpage = first - ((int)first % getpagesize()); 

    char* lastpage = last - ((int)last % getpagesize()); 

    int pages = (lastpage-firstpage)/getpagesize()+1; 

    if (mprotect(firstpage,pages*getpagesize(), PROT_READ|PROT_EXEC|PROT_WRITE)==-1)   perror("mprotect"); 

} 


void xor(caddr_t from, caddr_t to, int len){ 
    int i; 
    for(i=0;i<len;i++){ 
     *to ^= *from; from++; to++; 
    } } 
void swap(caddr_t from, caddr_t to, int len){ 
    int i; 
    for(i=0;i<len;i++){ 
     char t = *from; *from = *to; *to = t; from++; to++; 
    } } 
#define CELLSIZE 64 
#define ALIGN asm volatile (".align 64\n"); 


void P() { 
    static int firsttime=1; if (firsttime) { 
     xor(&&cell5,&&cell2,CELLSIZE); 
     xor(&&cell0,&&cell3,CELLSIZE); 
     swap(&&cell1,&&cell4,CELLSIZE); 
     firsttime = 0; } 

    char* a[] = {&&align0,&&align1,&&align2,&&align3,&&align4,&&align5}; 
    char*next[] ={&&cell0,&&cell1,&&cell2,&&cell3, &&cell4,&&cell5}; 
    goto *next[0]; 

align0: ALIGN 
cell0: printf("SPGM0\n"); 
    xor(&&cell0,&&cell3,3*CELLSIZE); 
    goto *next[3]; 

align1: ALIGN 
cell1: printf("SPGM2\n"); xor(&&cell0,&&cell3,3*CELLSIZE); 
    goto *next[4]; 

align2: ALIGN 
cell2: printf("SPGM4\n"); xor(&&cell0,&&cell3,3*CELLSIZE); 
    goto *next[5]; 

align3: ALIGN 
cell3: printf("SPGM1\n"); xor(&&cell3,&&cell0,3*CELLSIZE); 
    goto *next[1]; 

align4: ALIGN 
cell4: printf("SPGM3\n"); xor(&&cell3,&&cell0,3*CELLSIZE); 
    goto *next[2]; 

align5: ALIGN 
cell5: printf("SPGM5\n"); 
    xor(&&cell3,&&cell0,3*CELLSIZE); 

} 

int main (int argc, char *argv[]) { 


    makeCodeWritable(...); 
    P(); P(); 


} 

Répondre

0

L'argument first devrait être (char *)P, car il semble que vous voulez modifier le code dans la fonction P. Le deuxième argument est l'adresse de fin de la fonction P. Vous pouvez d'abord compiler le code, et en utilisant objdump -d pour voir l'adresse de début et de fin de P, puis calculer la taille de la fonction, TAILLE, puis spécifier manuellement dans le makeCodeWritable((char *)P, ((char *)P) + SIZE.

La deuxième méthode consiste à utiliser la as pour obtenir la taille de la fonction P, mais cela dépend de la langue de l'assembleur sur votre plate-forme. C'est le code snipe que j'ai modifié à partir de votre code, il devrait être capable de compiler et de fonctionner en x86, x86_64 en GCC 4.x sur la plate-forme Linux.

align5: ALIGN 
cell5: printf("SPGM5\n"); 
    xor(&&cell3,&&cell0,3*CELLSIZE); 

    // adding an label to the end of function P to assembly code 
    asm ("END_P: \n"); 
    ; 
} 

extern char __sizeof__myfunc[]; 

int main (int argc, char *argv[]) { 
    // calculate the code size, ending - starting address of P 
    asm (" __sizeof__myfunc = END_P-P \n"); 
    // you can see the code size of P 
    printf("code size is %d\n", (unsigned)__sizeof__myfunc); 
    makeCodeWritable((char*)P, ((char *)P) + (unsigned)__sizeof__myfunc); 
    P(); P(); 
} 

Avec quelques modifications pour soutenir LLVM GCC et as sous Mac OS X

int main (int argc, char *argv[]) { 
    size_t sizeof__myfunc = 0; 
    asm volatile ("movq $(_END_P - _P),%0;" 
      : "=r" (sizeof__myfunc) 
      :); 
    printf("%d\n", sizeof__myfunc); 
+0

Merci beaucoup. Je vais l'essayer. – Rosembergue