EXE文件脱壳解压#层序遍历搜索#源码

来源:互联网 发布:社交网络sns 编辑:程序博客网 时间:2024/04/30 12:07

// 20160218_ReadTree.cpp : Defines the entry point for the console application.
//
//采用 <链表> 方式实现对各层目录进行遍历

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>  //system()
#include<io.h>
#include<conio.h>   //getch()
#include <windows.h>

#include <direct.h>

#define Path_Length 30

//    system("dir /b /s /a-d D:\\*.* >d:\\allfiles.txt");  //深度遍历D:下的文件目录  ;命令行方式。

/*
1.查找是否有exe文件,若有,全部解压;  涉及到next的遍历;
2.查找是否有文件夹,若有,进入文件夹,重复 1 操作;  涉及到next 的遍历;
*/

void scanFolder(char * path);  //函数1 ,扫描文件夹
int decompress1(char * path , char * exeName);  //函数2 ,解压EXE文件
int FindTree(char * path , char * addpath); //函数3 扫描文件夹
int Findexe(char * path);  //查找当前目录下面的EXE文件(名)
//void decompress(char * addpath);


int main(){   //cheak_exe(path)
        char treename[20]="";    //剥离setup.exe文件的文件名,作为下一次scan的中间路径。

        char firstExeName[30]="";
        printf("输入待解压exe文件名:");
        scanf("%s",firstExeName);


        if( strstr(firstExeName,".exe")==NULL ){
            printf("输入的EXE文件名有误!\n");
            return 0;
        }

        int k=0;
        while( firstExeName[k]!='.' ){
            treename[k]=firstExeName[k];   //treename就是新添加的中间路径名
            k++;        
        }
        //解压 第一个exe文件,传入的treename是不带后缀名的文件名称
        decompress1(".\\", treename);
        //扫描文件夹
        FindTree(".\\" , treename);  //(初始目录,新增目录)

        char firstPath[20]="";
        strcat(firstPath , ".\\");
        strcat(firstPath , treename);

        Findexe(firstPath);   //查找当前目录下面的所有EXE文件

        char path[Path_Length]="";

        strcat(path,"\\ling");  //当前目录下;
        long Handle;
        struct _finddata_t FileInfo;
        if((Handle=_findfirst(path,&FileInfo))==-1L){   //path表示 待查找 路径文件名 ;FileInfo表 当前路径 的句柄。
            printf("没有找到匹配的项目\n");

            //接下来:查找文件夹目录;
        }
        else
        {
            printf("文件夹:%s\n",FileInfo.name);   //打印查找到的文件名;此处应该改成 EXE文件的 解压操作;
            while( _findnext(Handle,&FileInfo)==0){
                printf("文件是:%s\n",FileInfo.name); //同上;
                /////////////////////////////////////////////////////////////////////////////////////////////////////
                int j=0;
                while( FileInfo.name[j]!='.' ){
                    treename[j]=FileInfo.name[j];   //treename就是新添加的中间路径名
                    j++;
                
                }
                printf("treename  : %s\n",treename);
            }

            //此处应该添加:查找文件夹目录的操作;
            
            _findclose(Handle);
        }
    getch();
    return 0;
}



//扫描路径下面的所有文件夹名称
int FindTree(char * path , char * addpath)  
{
//  获取目录下的所有文件名
    char szFileName[50]="";

    strcat(szFileName, path);
    strcat(szFileName, "\\");
    strcat(szFileName, addpath);
    strcat(szFileName, "\\");
    strcat(szFileName, "\\*.*");
    //char szFileName[30];
//    strcpy(szFileName, path);
    //printf("szFileName : %s \n",szFileName);

    WIN32_FIND_DATA findData;
    HANDLE hFindFile;
    
    hFindFile=::FindFirstFile(szFileName,&findData);
    if(hFindFile!=INVALID_HANDLE_VALUE)  //如果句柄不是无效的,那么..
    {
        do
        {
            if(findData.cFileName[0]=='.')
              continue;
            if(findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY){
              printf("%s\n",findData.cFileName);          //获取文件目录名+path =new_path  , 然后扫描此文件目录下的exe
            }
        }while(::FindNextFile(hFindFile,&findData));
    }
    //getchar();
    printf("find tree success\n");
    return 0;
}


//查找当前目录下面的EXE文件(名)
int Findexe(char * path)
{

    char newpath[50]="";
    strcat(newpath , path);
    strcat(newpath , "\\*.exe");
    struct _finddata_t fa;
    long fHandle;
    
    if( (fHandle=_findfirst( newpath, &fa ))==-1L )//这里可以改成需要的目录
    {
        printf( "%s当前目录下没有exe文件\n",newpath);
        return 0;
    }
    else
    do
    {
        printf( "%s\n", fa.name );  // fa.name即为目录下面的EXE文件,循环输出;需要函数调用;
    }while( _findnext(fHandle,&fa)==0 );
    _findclose( fHandle );


    return 0;
}


//******  解压单个exe文件  ***  
//传入文件路径和文件名称,即进行文件的解压;
//     path是 相对路径,
int decompress1(char * path , char * exeName){
    //   .\\path\\exeName.exe
    char command[200]="";

    //command第一部分参数
    strcat(command,".\\UniExtract.exe  ");  //每次深入一层目录就在前面多加一个。解压所用到的UniExtract.exe都是一个:工程文件下面的。

    //command第二部分参数
    strcat(command,path);  
    strcat(command,"\\");
    strcat(command,exeName);  //此处的exeName是不带后缀名的名字。
    strcat(command,".exe  ");

    //command第三部分参数
    strcat(command,exeName);

    /*
    //获取当前绝对路径:
    char buf[40]="";  
    _getcwd(buf, 40);
    int m=0;
    while(buf[m]){
        printf("buf: %c\n\n",buf[m]);
        m++;
    }
    strcat(buf , "\\");
    strcat(buf , exeName);
    if(  fopen(buf,"r")==NULL ){
        printf("this file is not exist\n");//文件不存在
    }else
    {
        printf("this file exist\n");//文件存在
    }

    */
    system(command);
    if( !access(exeName, 0) ){
        return 1;   //新的路径存在;
    
    }else{
        return 0;
    }
}





/*
//  ***  解压单个exe文件  ***  
void decompress(char * addpath){
    system("copy setup.exe .\\ling\\teng\\");  //测试用的;复制当前目录下面的setup.exe文件到指定目录下。

    char command[200]="";  //
    char filename[100]="" ;   //初始化,命令的第二个参数,待解压的文件;EXE文件路径 可能会延伸
    char filenameNew[100]="";  //用于保存当前的EXE文件目录。

    strcat(filenameNew, ".\\");
    strcat(filenameNew, addpath);    //下一级目录名; 也可能为空;
    strcat(filenameNew, "\\");     //filenameNew保存当前的目录,以便于下一步目录的延伸。
    strcpy(filename, filenameNew);
    strcat(filename, "\\setup.exe ");   //command命令的第二部分:filename
    
    char file[20]="setup ";    //命令的第三个参数,解压后的文件名 ; 默认解压到setup.exe文件所在的目录。(所以不需要单独设置“解压到”目录)

    

    strcat(command,".\\UniExtract.exe  ");  //每次深入一层目录就在前面多加一个。解压所用到的UniExtract.exe都是一个:工程文件下面的。
    strcat(command,filename);  //需要改变此处的路径;
    strcat(command,file);

    system(command);

    system("del .\\ling\\teng\\setup.exe ");   //将已经解压的exe文件删除(后面scan目录)
    //注意,解压的时候,只能一次解压一个EXE文件;
}
*/


//////////////////////////////////   以下仅供参考   ////////////////////////////////////////


/*
void help(void)
{
    printf("delfile dirfilename\n\n");
    exit(1);
}

void error_quit(const char *msg)
{
    printf("Error:%s:%s\n",msg,strerror(errno));
}

void path_change(char *path)
{
    printf("Leave %s Successed . . .\n",getcwd(NULL,0));

    if(chdir(path)==-1)
        error_quit("Chdir");

    printf("Entry %s Successed . . .\n",getcwd(NULL,0));
}

void delfile(char *dirname)
{
    DIR *dirp;
    struct dirent *dir;
    struct stat buf;
    char *p=getcwd(NULL,0);

    if((dirp=opendir(dirname))==NULL)
        error_quit("Opendir");
    path_change(dirname);

    while(dir=readdir(dirp))
    {
        if((strcmp(dir->d_name,".")==0) || (strcmp(dir->d_name,".."))==0)
            continue;
        if(stat(dir->d_name,&buf)==-1)
            error_quit("Get stat");
        if(S_ISDIR(buf.st_mode))
        {
            delfile(dir->d_name);
            continue;
        }
        if(remove(dir->d_name)==-1)
            error_quit(dir->d_name);
        printf("Del %s Successed . . .\n",dir->d_name);

    }

    closedir(dirp);

    path_change(p);
}

int main(int argc,char **argv)
{
    if(argc!=2)
        help();

    delfile(argv[1]);

    printf("Del All File In %s OK . . .\n\n",argv[1]);

    return 0;
}
//struct _finddata_t  {
//             unsigned attrib;                       /* 表示文件的属性 */
 //            time_t time_create;                 /* 表示文件创建的时间 */  
 //            time_t time_access;                 /* 表示文件最后访问的时间 */
 //            time_t time_write;                   /* 表示文件最后写入的时间 */
 //            _fsize_t size;                            /* 表示文件的大小 */
 //            char name[FILENAME_MAX];       /* 表示文件的名称 */
 //       };
    /*
int Findexe()
{

    struct _finddata_t fa;
    long fHandle;
    
    if( (fHandle=_findfirst( "*.exe", &fa ))==-1L )//这里可以改成需要的目录
    {
        printf( "当前目录下没有exe文件\n");
        return 0;
    }
    else
    do
    {
        printf( "找到文件:%s\n", fa.name );
    }while( _findnext(fHandle,&fa)==0 );
    _findclose( fHandle );


    return 0;
}
    */
/*
int main(int argc, char *argv[])
{
  long file;
    struct _finddata_t find;
    
     _chdir("d:\\");
     if((file=_findfirst("*.*", &find))==-1L)
  {
      printf("空白!\n");
     exit(0);
     }
     printf("%s\n", find.name);
     while(_findnext(file, &find)==0)
     {
          printf("%s\n", find.name);
     }
     _findclose(file);
 
     system("PAUSE");
     return 0;
}

*/

/*
int main(int argc,char *argv[])
{
    char szFileName[]="d:\\*.*";//查找的根目录
    WIN32_FIND_DATA findData;
    HANDLE hFindFile;
    
    hFindFile=::FindFirstFile(szFileName,&findData);
    if(hFindFile!=INVALID_HANDLE_VALUE)
    {
        do
        {
            if(findData.cFileName[0]=='.')
              continue;
            if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
              printf("%s\n",findData.cFileName);
            //此处findData.cFileName返回的值就是文件名(文件夹名)
            //system("cd findData.cFileName");   //找不到指定路径
            }
        }while(::FindNextFile(hFindFile,&findData));
    }
    getchar();
    return 0;
}
*/
0 0
原创粉丝点击