n-byte網絡守望者是一款單機版網絡安全工具,簡言之,就是一個用.net開發的個人版防火墻。在n-byte網絡守望者1.0版的開發中,使用了ndis hook driver技術來實現網絡封包過濾功能,這使n-byte網絡守望者能夠在網絡層過濾網絡封包,從而實現強大的功能。
由于軟件的主程序是用c#寫的,c#中沒有提供具有類似deviceiocontrol函數功能的驅動設備控制函數,而ndis hook driver技術下的驅動程序是用ddk下的c語言寫的,為了能夠實現主程序對驅動程序的控制和相互通信,采用了以下設計方案:
在以上方案中,需要一個負責主程序與ndis hook driver驅動程序通信與控制的模塊driverdll.dll,并用c#編寫的一個封裝驅動程序中封包信息的模塊,可以發送這個驅動程序信息到主程序,主程序可識別并操作模塊中的數據類型。
在.net應用程序使用驅動程序的問題上,面臨著兩個問題:
1.怎樣實現.net應用程序控制驅動程序的功能?
2.怎樣從驅動程序向.net應用程序傳遞非托管的數據類型?
以下是我們就這些問題的詳細解決方法:
怎樣實現.net應用程序控制驅動程序的功能?
使用托管c++編寫的driverdll.dll來實現對驅動程序的直接控制,而主程序通過調用其中的方法來實現對驅動程序的間接控制。比如在nbyte.h文件中定義了start_ip_hook常數用來作為傳給驅動程序用來開啟驅動程序封包過濾功能的參數,下面在托管c++模塊中定義了ioctrl托管類并定義了下面的向緩沖區寫入參數的方法:
//向緩沖區寫入數據。
dword writeio(dword code,pvoid buffer,dword count)
{
if(hdriverhandle == null)
return error_driver_handle;
dword bytesreturned;
bool returncode = deviceiocontrol(hdriverhandle,
code,
buffer,
count,
null,
0,
&bytesreturned,
null);
if(!returncode)
return error_io_ctrl;
return success;
}
當然直接使用這個方法不太方便,所以定義一個公有函數,用來提供給主程序調用:
//開始進行封包過濾
bool startiphook()
{
return (writeio(start_ip_hook, null, 0)==success);
}
這樣,只要在主程序中聲明ioctrl的對象ic,就可以通過ic.startiphook()就可以實現對驅動程序過濾功能的開啟,用同樣的方法也可以實現對驅動程序進行其它操作,比如添加、修改封包過濾規則等。
怎樣從驅動程序向.net應用程序傳遞非托管的數據類型?
為了能夠輸出安全日志,必須讓主程序獲得驅動程序中的封包信息。使用信號量機制可以很方便的實現驅動程序和非托管代碼間的信息傳遞,那么對托管代碼呢?這需要向.net應用程序傳遞非托管的數據類型access_info。在nbyte.h中,是這樣定義這個access_info結構的:
typedef struct _access_info
{
ushort protocol;
ulong sourceip;
ulong destinationip;
ushort sourceport;
ushort destinationport;
}access_info;
顯然,直接傳遞非托管數據類型是不可以的,需要轉換一下。首先,在ioctrl類中定義了幾個要傳遞的封包信息參數:
public __gc class ioctrl
{
public:
ushort protocol; //網際協議類型
ulong sourceip; //源ip地址
ulong destinationip; //目的ip地址
ushort sourceport; //源端口
ushort destinationport; //目的端口
………………
}
然后,在getaccessinfo()函數中來給這些參數賦值:
void getaccessinfo()
{
access_info ai;
bool result=(readio(get_info,&ai,sizeof(ai))==success);
this->protocol=ai.protocol;
this->sourceip=ai.sourceip;
this->destinationip=ai.destinationip;
this->sourceport=ai.sourceport;
this->destinationport=ai.destinationport;
}
既然在ioctrl類中獲得了這些信息,但是需要把它們封裝成主程序容易處理的數據類型,這樣,用c#實現了infoevent類用來封裝這些信息:
//本類封裝了數據包的詳細信息,可以通過事件實現對它的模塊間傳遞。
public class infoevent:eventargs
{
string sinfo; //用來存放輸出信息的私有成員
public int plength; //commonfunction.sport數組的長度
public ushort protocol; //網絡通信協議類型
public uint sourceip; //數據包的源ip
public uint destinationip; //數據包的目的ip
public ushort sourceport; //數據包的源端口
public ushort destinationport; //數據包的目的端口
………………………………
}
下面在用托管c++實現的infoprovider驅動程序信息提供者類中把個infoevent類的對象傳遞給主程序,需要使用一個委托生成一個事件:
//聲明委托事件,用來向主程序傳遞數據。
__delegate void driverinfo(object* sender, infoevent* e);
//聲明響應事件函數。
__event driverinfo* ondriverinfo;
然后在infoprovider驅動程序信息提供者類中定義一個方法,在主程序中以線程的方式運行這個方法,在這個方法中使用了事件函數ondriverinfo:
//用來獲得驅動程序信息的進程,在主程序中將開啟該進程。
void getinfothreadproc()
{
this->hevent=openevent(synchronize,false,"nbevent");
if(!ic->getdriverhandle())
{
return;
}
while(true)
{
f(!hevent)
exitthread(0);
waitforsingleobject(this->hevent,infinite);
npackets++;
ic->getaccessinfo();
ic->resetevent();
//定義一個主程序可以識別的對象,通過ondriverinfo傳給主程序。
infoevent*ie=new infoevent(ic->protocol,ic->sourceip,ic->destinationip,ic->sourceport,ic->destinationport);
ondriverinfo(this,ie);
}
ic->closedriverhandle();
return;
}
在主程序中,會開啟這個進程并定義了ondriverinfo的處理函數dealwithinfo:
pinfo=new infoprovider();
//開啟與驅動交換信息的進程
filterthread=new thread(new threadstart(pinfo.getinfothreadproc));
filterthread.isbackground=true;
filterthread.start();
pinfo.ondriverinfo+=new infoprovider.driverinfo(dealwithinfo);
這樣主程序就可以在dealwithinfo函數中加入對infoevent對象的處理了。可見,通過中間模塊ioctrl的轉換,便實現了.net主程序對驅動程序中非托管數據類型的獲取和處理。
|
新聞熱點
疑難解答