用HOOK來修改API函式的功能 之禁止刪除檔案

2022-08-31 07:51:08 字數 3114 閱讀 5488

在"未文件化函式中"有個函式叫做zwsetinformationfile。這個函式對應的win32的函式有"setfileattributes、setendoffile、setfilepointer、setfiletime、deletefile"。也就是說,以上的函式均是和這個zwsetinformationfile函式有關。如果我們截獲到這個函式那麼我們就可以做到禁止刪除檔案。

下面是我的**。我首先對使用者操作的檔名和我們要保護的檔名進行比較,如果檔名相同,則繼續判斷路徑是否相同。如果相同,那麼這個檔案就是我們保護的檔案。否則繼續使用者的原有操作。

#include "ntddk.h"

#include "bugcodes.h"

#include "ntstatus.h"

#include

#include

#include "stdarg.h"

#include "ntiologc.h"

#define maxpathlen 1024

#define maxbuf 1024

#define min(x,y) ((x) < (y) ? (x) : (y))

.....

typedef struct _file_names_information file_names_information, *pfile_names_information;

...//反刪除檔案需要的函式

//(1).宣告原有函式

typedef ntstatus (*zwsetinformationfile)(

in handle  filehandle,

out pio_status_block  iostatusblock,

in pvoid  fileinformation,

in ulong  length,

in file_information_class  fileinformationclass

);zwsetinformationfile realzwsetinformationfile;

//(2).定義hook反刪除檔案的函式

ntstatus hookzwsetinformationfile(

in handle  filehandle,

out pio_status_block  iostatusblock,

in pvoid  fileinformation,

in ulong  length,

in file_information_class  fileinformationclass

);...

ntstatus driverentry(in pdriver_object driverobject, in punicode_string registrypath)

ntstatus hookzwsetinformationfile(

in handle  filehandle,

out pio_status_block  iostatusblock,

in pvoid  fileinformation,

in ulong  length,

in file_information_class  fileinformationclass);

char tempdel[2]=;

char tempinstalldir[256]=;//定義臨時使用的安裝路徑陣列

unicode_string phidefiledir; //定義反刪除讀取檔案的檔名

unicode_string *pfullpath=null;

int judge=0;

int comparejudge=0;

int i;

pfileinfo = (pfile_name_information)exallocatepool( nonpagedpool, sizeof(file_name_information)*255);

//呼叫zwqueryinformationfile函式將操作檔案的資訊放如pfileinfo。

rc = zwqueryinformationfile( filehandle,&myiostatusblock,pfileinfo,sizeof(file_name_information)*255,filenameinformation );

if(nt_success(rc))

obdereferenceobject(pfileobject);

//將檔案的全路徑轉換成大寫

rtlunicodestringtoansistring(&ansigetfilename_del,pfullpath,true);

rtlunicodestringtoansistring(&m_ansigetfilename_del,pfullpath,true);

rtlupperstring(&ansigetfilename_del,&m_ansigetfilename_del);

rtlfreeansistring(&m_ansigetfilename_del);

memset(deletefiledir,0,maxbuf);

memcpy(deletefiledir,ansigetfilename_del.buffer,ansigetfilename_del.length);

rtlfreeansistring(&ansigetfilename_del);

exfreepool(pfullpath);

//判斷檔案的全路徑是否和我們需要保護的檔案全路徑匹配

if(rtlcomparememory(deletefiledir,"") ) == strlen("")) } 

else}}

//如果檔名稱不匹配,則呼叫原來的操作

rc=realzwsetinformationfile(filehandle,iostatusblock,fileinformation,length,fileinformationclass);

rtlfreeansistring(&ansifilename_del);

exfreepool(punifilename_del);

exfreepool(ptemp);

}exfreepool(pfileinfo);

return(rc);

}

用HOOK來修改API函式的功能(4) 環境搭建

原始出處 作者資訊和本宣告。否則將追究法律責任。這就需要搭建環境,而這個環境你必須使用ddk。我在這裡假設你已經安裝了ddk,並且會使用ddk來編譯乙個 我的sources檔案是這樣寫的 targetname testdriver targettype driver targetpath obj b...

用HOOK來修改API函式的功能(4) 環境搭建

這就需要搭建環境,而這個環境你必須使用ddk。我在這裡假設你已經安裝了ddk,並且會使用ddk來編譯乙個 我的sources檔案是這樣寫的 targetname testdriver targettype driver targetpath obj browser info 1 c defines ...

用Debug函式實現API函式的跟蹤(3)

用debug函式實現api函式的跟蹤 3 彭春華 閱讀人次 條 很明顯,當被除錯程序在函式入口產生中斷除錯資訊時,除錯程式只能得到函式的輸入引數,而不能得到我們希望的輸出引數及返回值!為了實現我們的目標,我們必須在函式呼叫結束時,再次產生中斷,取得函式的輸出引數和返回值。在處理函式入口中斷時,就必須...