国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

delete yourself(C#)

2024-07-21 02:18:17
字體:
供稿:網(wǎng)友
  • 本文來源于網(wǎng)頁設(shè)計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。
  • 一種巧妙的刪除程序自己的方法
    關(guān)鍵字 一種巧妙的刪除程序自己的方法


    大家都知道,一般的程序運行的時候,可執(zhí)行文件本身是被操作系統(tǒng)保護的,不能用改寫的方式訪問,更別提在本身還在運行的時侯刪除自己了。在lu0的主頁上看到一種undocument的方法,通過改變系統(tǒng)底層的文件訪問模式實現(xiàn)刪除自己,那是實在功夫。我看 了很是佩服。但是有沒有一種用在msdn上就能查到的函數(shù)實現(xiàn)呢?有!jeffrey richter給我們做了一個范例:
     
    deleteme.cpp
     
    module name: deleteme.cpp
    written by: jeffrey richter
    description: allows an executable file to delete itself
    **************************************************/
     
    #include
    #include
    #include
     
    /////////////////////////////////////////////////
     
    int winapi winmain(hinstance h, hinstance b, lpstr psz, int n) {
     
    // is this the original exe or the clone exe?
    // if the command-line 1 argument, this is the original exe
    // if the command-line >1 argument, this is the clone exe
     
    if (__argc == 1) {
     
    // original exe: spawn clone exe to delete this exe
    // copy this executable image into the user's temp directory
     
    tchar szpathorig[_max_path], szpathclone[_max_path];
    getmodulefilename(null, szpathorig, _max_path);
    gettemppath(_max_path, szpathclone);
    gettempfilename(szpathclone, __text("del"), 0, szpathclone);
    copyfile(szpathorig, szpathclone, false);
     
    //***注意了***:
    // open the clone exe using file_flag_delete_on_close
    handle hfile = createfile(szpathclone, 0, file_share_read, null, open_existi
    ng, file_flag_delete_on_close, null);
     
    // spawn the clone exe passing it our exe's process handle
    // and the full path name to the original exe file.
    tchar szcmdline[512];
    handle hprocessorig = openprocess(synchronize, true, getcurrentprocessid());

    wsprintf(szcmdline, __text("%s %d /"%s/""), szpathclone, hprocessorig, szpat
    horig);
    startupinfo si;
    zeromemory(&si, sizeof(si));
    si.cb = sizeof(si);
    process_information pi;
    createprocess(null, szcmdline, null, null, true, 0, null, null, &si, &pi);
    closehandle(hprocessorig);
    closehandle(hfile);
     
    // this original process can now terminate.
    } else {
     
    // clone exe: when original exe terminates, delete it
    handle hprocessorig = (handle) _ttoi(__targv[1]);
    waitforsingleobject(hprocessorig, infinite);
    closehandle(hprocessorig);
    deletefile(__targv[2]);
    // insert code here to remove the subdirectory too (if desired).
     
    // the system will delete the clone exe automatically
    // because it was opened with file_flag_delete_on_close
    }
    return(0);
    }
     
    看懂了嗎?
     
    這一段程序思路很簡單:不是不能在運行時直接刪除本身嗎?好,那么程序先復(fù)制(clone)一個自己,用復(fù)制品起動另一個進程,然后自己結(jié)束運行,則原來的exe文件不被系統(tǒng)保護.這時由新進程作為殺手刪除原來的exe文件,并且繼續(xù)完成程序其他的功能。

     
    新進程在運行結(jié)束后,復(fù)制品被自動刪除。這又是值得介紹的一個把戲了,注意:
    // open the clone exe using file_flag_delete_on_close
    handle hfile = createfile(szpathclone, 0, file_share_read, null,open_existin
    g, file_flag_delete_on_close, null);
    這里面的file_flag_delete_on_close標(biāo)志,這個標(biāo)志是告訴操作系統(tǒng),當(dāng)和這個文件相關(guān)的所有句柄都被關(guān)閉之后(包括上面這個createfile創(chuàng)建的句炳),就把這個文件刪除。幾乎所有的臨時文件在創(chuàng)建時,都指明了這個標(biāo)志。另外要注意的是:在復(fù)制品進程對原始程序操刀之前,應(yīng)該等待原進程退出.在這里用的是進程同步技術(shù).用handle hprocessorig = openprocess(synchronize, true,getcurrentprocessid());得到原進程句柄.synchronice標(biāo)志在nt下有效,作用是使openprocess得到的句柄可以做為同步對象.復(fù)制品進程用waitforsingleobject函數(shù)進行同步,然后一個deletefile,以及進行其它銷毀證據(jù)(jeffrey說:比如刪目錄)的工作,打完收工!
     
    程序是基于console的,通過傳入的參數(shù)確定是原始的進程還是復(fù)制品新進程,并且得到需要操作的目標(biāo)文件的信息(主要是路徑),復(fù)制品放在系統(tǒng)的temp目錄(gettemppath得到),你也可以隨便找個你認(rèn)為安全的地方(比如:windows/system32等等)。這里面沒有甚么深的技術(shù).再看其他的一些實現(xiàn)刪除自己的例子,比如說在進程退出前,用fwrite等方法輸出一個.bat文件,在里面寫幾句del,然后winexec一下這個bat文件即可.玩兒過dos的蟲蟲大多都會。今天又學(xué)一招,爽。

    發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發(fā)表
    主站蜘蛛池模板: 遵义市| 饶河县| 多伦县| 军事| 碌曲县| 普兰县| 新余市| 乐业县| 武强县| 辰溪县| 山阳县| 搜索| 霍山县| 沂水县| 监利县| 信宜市| 綦江县| 河西区| 华宁县| 怀远县| 新疆| 道孚县| 茶陵县| 怀仁县| 石门县| 临汾市| 湖南省| 克拉玛依市| 巴彦淖尔市| 扎鲁特旗| 韶关市| 台北市| 宜黄县| 宁阳县| 东宁县| 拜城县| 兴化市| 全州县| 关岭| 永清县| 铜山县|