1. 程式人生 > >[ios]安全攻防之程式碼混淆的一個小工具

[ios]安全攻防之程式碼混淆的一個小工具

看了“念茜”的這篇文章:

http://blog.csdn.net/yiyaaixuexi/article/details/29201699

覺得非常好,不過裡面提到一個func.list的檔案。

規則:

建立函式名列表func.list,寫入待混淆的函式名,如:
-(void)sample;
-(void)seg1:(NSString *)string seg2:(NSUInteger)num;

就這樣寫:
sample
seg1
seg2

所以我用c語言寫了以下的程式碼,專門用來生成func.list檔案。

程式碼的功能是遍歷你的工程,把裡面的.h和.m檔案找出來。並且把裡面的類名,方法名,引數名等按上面的規則列出來。

注:因為一般.m和.h是配對的,比如test.m就有test.h,不過有時也只有config.h,而沒有對應的.m檔案。所以裡面有一個小邏輯是如果有m檔案,就不處理h檔案,怕重複類名方法名等。

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
 

//編譯方法,
//gcc file.c -o file
//執行方法
//./file
//然後就會輸出你工程裡的類名方法名,就可以把這裡組裝成一個需要混淆的配置檔案了。
//缺限
//1.輸出名字沒有做重複處理。
//2.判斷方法時只是處理了兩種情況,如果空格多於2個沒有處理。


#if 0//放開除錯
#define Printf_info(x,y) printf(x,y);
#else
#define Printf_info(x,y)
#endif
 

int Mydir(const char *filepath);
int checkFile(const char *filepath);

int findNum = 0;

int main(int argc, char *argv[])
{
	//此程式碼的功能是遍歷ios的專案的所有。h和。m的檔案,把裡面的類名,方法名,引數名等都提取出來,
	//方便用CSDN博主“念茜”的方法來混淆自己的程式碼用。
        Mydir("/myProject");//填寫自己的工程的根目錄路徑。

        return 0;
}
void Print_fileName(const char * filename)
{
	int a = strlen(filename);
	for(int i=0;i<a - 2; i++){
		printf("%c",filename[i]);
	}
	printf("\n");
}
int Mydir(const char *filepath)
{
    char *fullpath,*filetype,*tmp;
    struct stat statbuf;
    DIR *dr;
    struct dirent *drt;
    
    if((dr=opendir(filepath))==NULL)
        return 2;
    
    while((drt=readdir(dr))!=NULL)
    {
        
        if(strcmp(drt->d_name,".")==0||strcmp(drt->d_name,"..")==0||strcmp(drt->d_name,".DS_Store")==0)
            continue;
        
        fullpath=strdup(filepath);
        fullpath=strcat(fullpath,"/");
        fullpath=strcat(fullpath,drt->d_name);
        
        Printf_info("%s\n",fullpath);
        
        if(stat(fullpath,&statbuf)<0)
            continue;
        
        if (S_ISREG(statbuf.st_mode))
        {
            filetype="reguler";
            int a = strlen(drt->d_name);
            char *pp = drt->d_name;
            //printf("----  %c %c" , pp[a - 2], pp[a - 1]);
            
            if(pp[a - 2] == '.' && pp[a - 1] == 'm')
            {
                Print_fileName(drt->d_name);
                checkFile(fullpath);
            }
#if 0
            else if(pp[a - 2] == '.' && pp[a - 1] == 'h')
            {
                char *mPath = strdup(fullpath);
                //printf("\nmpath: %s\n",mPath);
                char *ppp = mPath;//drt->d_name;
                int a = strlen(ppp);
                ppp[a - 1] = 'm';//檢查m檔案是否存在。
                //printf("\nmpath: %s\n",mPath);
                if((access(mPath,F_OK))==0){
                    continue;
                }
                Print_fileName(drt->d_name);
                checkFile(fullpath);
            }
#endif
        }
        else if(S_ISDIR(statbuf.st_mode))
        {
            filetype="directory";
            //fullpath=strcat(fullpath,"/");
            //printf("%s,%s\n",fullpath,filetype);
            tmp=strdup(fullpath);
            Mydir(tmp);
        }
        else
        {
            filetype="invalid";
            printf("%s,%s\n",fullpath,filetype);
        }
        //printf("%s,%s\n",fullpath,filetype);
        bzero(fullpath,strlen(fullpath));
    }
    return 0;
}
int print_Method(char *text)
{
    char *p = text;
    char c;
    int start = 0;
    while((c = *p++) !='\n'){//Method
        if(c == ':' || c == '{' || c == ';'){
            start = 0;
            break;
        }
        if(start == 1){
            printf("%c", c);
        }
        if(c == ')')
            start = 1;
    }
    printf("\n");
#if 0
    start = 0;
    while((c = *p++) !='\n'){//arge
        if(c == ':'){
            start = 0;
            printf("\n");
        }
        if(start == 2 && c != '{'){
            printf("%c", c);
        }
        if(c == ' ' && start == 1)
            start = 2;
        if(c == ')')
            start = 1;
    }
    //printf("\n");
#endif
    return 0;
}
int findMethod(char *text)
{
    char *p = text;
    char c;
    while((c = *p++) !='\n'){
        if(c == '-' && ((*p == ' ' && *(p + 1) == '(') || *p == '(' )){
            if( text[0] == '-' )
            {
                //printf("%d %s\n", findNum++, text);
                print_Method(text);
            }
            //else
            //	printf("%d %s\n", findNum++, text);
        }
        if(c == '+' && ((*p == ' ' && *(p + 1) == '(') || *p == '(' )){
            if( text[0] == '+' )
            {
                //printf("%d %s\n", findNum++, text);
                print_Method(text);
            }
            //else
            //	printf("%d %s\n", findNum++, text);
        }
    }
    return 0;
}
int checkFile(const char *filepath)
{
    //printf("====%s\n", filepath);
    
    FILE *fp1;//定義檔案流指標,用於開啟讀取的檔案
    char text[40961];//定義一個字串陣列,用於儲存讀取的字元
    fp1 = fopen(filepath,"r");//只讀方式開啟檔案a.txt
    while(fgets(text,40960,fp1)!=NULL)//逐行讀取fp1所指向檔案中的內容到text中
    {
        //puts(text);//輸出到螢幕
        findMethod(text);
    }
    fclose(fp1);//關閉檔案a.txt,有開啟就要有關閉
    
    return 0;
}


程式碼是c語言實現的