作者:小四 < mailto: scz@isbase.com > 首頁:http://www.isbase.com 日期:2000-07-24 20:10可能最好的協助是原始碼例子,準備用syn-flood為例說明libnet的使用。也是被人 抓去寫DoS工具,就以這個題目灌水幾篇吧。使用libnet的好處很多,考慮相容性, libnet for NT/2K已經有下載。 首先我們介紹libnet_name_resolve()函數,顧名思義,該函數實現了網域名稱解析功能。 其函數原型如下: u_long libnet_name_resolve ( u_char * hostname, u_short use_name ); hostname為標準的asciiz串,以NULL結尾。可以是點分十進位IPv4地址,比如 "192.168.8.30"。可以是FQDN,比如"bbs.isbase.com"。甚至可以是一般的主機名稱, 只要你確信該主機名稱可以經過網域名稱系統(包括/etc/hosts檔案)得到正向解析。在頭文 件/usr/include/libnet/libnet-macros.h中有兩個宏對應了use_name的取值: #define LIBNET_DONT_RESOLVE 0 /* 對應點分十進位IPv4地址 */ #define LIBNET_RESOLVE 1 /* 對應主機名稱 */ 根據這個取值確定hostname使用的是點分十進位IPv4地址(0)還是主機名稱(1)。無論本 機使用何種位元組序,該函數最後得到的u_long是big-endian序(也是網路位元組序)4字 節值,比如192.168.8.30對應0xc0 a8 08 1e。現代Unix作業系統的網域名稱解析函數支 持把"192.168.8.30"作為形參輸入,所以一般都指定該函數的第二個參數為 LIBNET_RESOLVE。 -------------------------------------------------------------------------- /* 編譯命令相對古怪點,確認你的系統上有libnet-config指令碼 */ /* gcc -O3 -o rt resolveTest.c `libnet-config --defines --cflags` `libnet-config --libs` */ #include #include #include /* 使用libnet必須包含這個標頭檔 */ #define SUCCESS 0 #define FAILURE -1 int main ( int argc, char * argv[] ) { u_long ipUl = 0x01020304; /* 測試用 */ u_char * ipUc; int i; u_short method; if ( argc != 3 ) { fprintf( stderr, "%s \n", argv[0] ); exit( FAILURE ); } method = ( u_short )strtoul( argv[2], NULL, 10 ); if ( ( method != 0 ) && ( method != 1 ) ) { /* 第一個參數為LIBNET_ERR_FATAL,會導致exit */ libnet_error( LIBNET_ERR_FATAL, "Bad method: %s\n", argv[2] ); } /* 這裡返回-1表示出錯,事實上我並不確定,man手冊裡沒有提到 */ if ( ( ipUl = libnet_name_resolve( argv[1], method ) ) == -1 ) { /* 僅僅顯示警告資訊,並不終止進程 */ libnet_error( LIBNET_ERR_WARNING, "Bad resolveObject: %s\n", argv[1] ); } ipUc = ( u_char * )&ipUl; i = 0; while ( i < 4 ) { fprintf( stderr, "%02x ", ipUc[i] ); i++; } fprintf( stderr, "\n" ); return( SUCCESS ); } /* end of main */ -------------------------------------------------------------------------- 注意編譯使用libnet庫的原始碼的時候要輔助以libnet-config指令碼: [scz@ /home/scz/src]> gcc -O3 -o rt resolveTest.c `libnet-config --defines --cflags` `libnet-config --libs` [scz@ /home/scz/src]> ./rt scz 0 Warning: Bad resolveObject: scz <-- 雖然出現警告資訊,但並未終止進程 ff ff ff ff <-- 正向解析失敗,因為這裡指定了0,scz不是點分10進位IPv4地址 [scz@ /home/scz/src]> ./rt scz 1 c0 a8 08 5a <-- 正向解析成功 [scz@ /home/scz/src]> ./rt 192.168.8.30 0 c0 a8 08 1e [scz@ /home/scz/src]> ./rt 192.168.8.30 1 c0 a8 08 1e <-- 無論指定0還是1,對於點分地址都能解析成功 [scz@ /home/scz/src]> ./rt 256.0.0.0 0 Warning: Bad resolveObject: 256.0.0.0 ff ff ff ff <-- 解析失敗,因為這個點分地址非法 [scz@ /home/scz/src]> ./rt 256.0.0.0 1 Warning: Bad resolveObject: 256.0.0.0 ff ff ff ff [scz@ /home/scz/src]> ./rt www.isbase.com 0 Warning: Bad resolveObject: www.isbase.com ff ff ff ff [scz@ /home/scz/src]> ./rt www.isbase.com 1 ca 63 0b a1 <-- 可以解析FQDN [scz@ /home/scz/src]> ./rt www.isbase.com 2 Fatal: Bad method: 2 <-- libnet_error()第一個參數為LIBNET_ERR_FATAL,會導致exit [scz@ /home/scz/src]> 反引號``表示執行其中的命令,並作為這裡的輸入,所以你可以單獨看看究竟輸入了 什麼: [scz@ /home/scz/src]> libnet-config --defines --cflags -D_BSD_SOURCE -D__BSD_SOURCE -D__FAVOR_BSD -DHAVE_NET_ETHERNET_H -DLIBNET_LIL_ENDIAN [scz@ /home/scz/src]> libnet-config --libs -lnet [scz@ /home/scz/src]> file /usr/bin/libnet-config /usr/bin/libnet-config: Bourne shell script text [scz@ /home/scz/src]> 以前有ansi88問過編譯時間出現LIBNET_BIG_ENDIAN相關錯誤,很可能是直接指定了 -lnet,卻沒有指定前面那一堆宏(-D用於命令列傳遞宏定義),沒有仔細man的結果。 libnet-config是個指令碼,如果你感興趣,可以自己查看。 [scz@ /home/scz/src]> ls -l /usr/lib/libnet.a -rw-r--r-- 43142 /usr/lib/libnet.a [scz@ /home/scz/src]> libnet.a是一個靜態庫,這樣編譯得到的可執行程式將來執行的時候不依賴於系統中 是否存在該庫。 |