資源簡介
在一個局域網環境中,用C++ 語言實現下面的基本功能:
1) 確定截包的方法:包括RAW 模式SOCKET、PACKET32以及直接作為驅動程序掛在NDIS上;
2) 要求截獲以下包的類型并分析:以太網幀格式、IP包、ICMP包、TCP報文段、UDP報文等相關字段進行描述。

代碼片段和文件信息
#define?RCVALL_ON?1
#define?MAX_ADDR_LEN?16?//點分十進制地址的最大長度
#define?MAX_PROTO_TEXT_LEN?16?//子協議名稱(如“TCP“)最大長度
#define?WINSOCK_VERSION?MAKEWORD(2?2)
#pragma?comment(lib?“Ws2_32.lib“)
#include?
#include?
#include?“mstcpip.h“
#include??
typedef?struct?iphdr??????????//定義IP首部
{
????unsigned?char?h_lenver;?//4位首部長度+4位IP版本號
????unsigned?char?tos;?//8位服務類型TOS
????unsigned?short?total_len;?//16位總長度(字節)
????unsigned?short?ident;?//16位標識
????unsigned?short?frag_and_flags;?//3位標志位
????unsigned?char?ttl;?//8位生存時間?TTL
????unsigned?char?proto;?//8位協議?(TCP?UDP?或其他)
????unsigned?short?checksum;?//16位IP首部校驗和
????unsigned?int?sourceIP;?//32位源IP地址
????unsigned?int?destIP;?//32位目的IP地址
}IPHeader;
typedef?struct?_tcphdr????//定義TCP首部
{
?USHORT?th_sport;?//16位源端口
?USHORT?th_dport;?//16位目的端口
?unsigned?int??th_seq;?//32位序列號
?unsigned?int??th_ack;?//32位確認號
?unsigned?char?th_lenres;?//一個字節表示8位4位數據偏移/6位保留字/6位特殊字
?unsigned?char?th_flag;?//6位標志位
?USHORT?th_win;?//16位窗口大小
?USHORT?th_sum;?//16位校驗和
?USHORT?th_urp;?//16位緊急數據偏移量
}TCP_HEADER;
typedef?struct?_udphdr?//定義UDP首部
{
????unsigned?short?uh_sport;?//16位源端口
????unsigned?short?uh_dport;?//16位目的端口
????unsigned?short?uh_len;?//16位長度
????unsigned?short?uh_sum;?//16位校驗和
}UDP_HEADER;
typedef?struct?_icmphdr???//定義ICMP首部
{
?BYTE???i_type;????//8位類型
?BYTE???i_code;????//8位代碼
?USHORT?i_cksum;???//16位校驗和?
?USHORT?i_id;????//識別號(一般用進程號作為識別號)
?USHORT?i_seq;????//報文序列號?
?ULONG??timestamp;???//時間戳
}ICMP_HEADER;
int??iTTLiLENiBYTES;
char?szSourceIP[MAX_ADDR_LEN]?szDestIP[MAX_ADDR_LEN];//裝源地址目的地址,長度為16位
int??iSourcePortiDestPort;
int??fflag=0;//file?flag
#define?PACKAGE_SIZE?sizeof(IPHeader)+1000//數據內容的大小
void?HandleError(char?*func);
//functions
int?DecodeTcpPack(char?*?intFILE?*);?//TCP解包函數
int?DecodeUdpPack(char?*?intFILE?*);?//UDP解包函數
int?DecodeIcmpPack(char?*?intFILE?*);?//ICMP解包函數
//MAIN
int?main(int?argc?char?*argv[])
{
?sockaddr_in?saSourcesaDest;
?WSAData?wsaData;
?char?buf[PACKAGE_SIZE];
????WSAStartup(WINSOCK_VERSION?&wsaData);//對WinSock?DLL進行初始化
????SOCKET?sock?=?socket(AF_INET?SOCK_RAW?IPPROTO_IP);
????if(sock?==?SOCKET_ERROR)
????{
????????HandleError(“socket“);
????????WSACleanup();//終止DLL使用
????????return?-1;
????}
????//獲取本機IP地址
????struct?sockaddr_in?addr;
????memset(&addr?0?sizeof(addr));
????//addr.sin_addr.S_un.S_addr?=?inet_addr(“192.168.1.101“);
char?name[256];
?PHOSTENT?hostinfo;
?if(?gethostname?(?name?sizeof(name))?==?0)
?{
??if((hostinfo?=?gethostbyname(name))?!=?NULL)
??{
???memcpy(&(addr.sin_addr.S_un.S_addr)??(struct?in_addr?*)*hostinfo->h_addr_list??sizeof((struct?in_addr?*)*hostinfo->h_addr_list?));
??}
?}
????addr.sin_family?=?AF_INET;
????if(bind(sock?(struct?sockaddr*)&addr?sizeof(addr))?==?SOCKET_ERROR)//bind
????{
????????HandleError(“bind“);
????}
?//設置SOCK_RAW為SIO_RCVALL,以便接收所有的IP包
????int?on?=?RCVALL_ON;//1
????DWORD?num;
//控制一個套接口的模式。
////將進行的操作的控制代碼
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????8406??2009-12-25?15:12??jiebao2\Cpp1.cpp
?????文件???????3443??2009-12-21?14:43??jiebao2\Cpp1.dsp
?????文件????????514??2009-12-15?13:53??jiebao2\Cpp1.dsw
?????文件????9522176??2009-12-25?15:14??jiebao2\Cpp1.ncb
?????文件??????53760??2009-12-21?14:44??jiebao2\Cpp1.opt
?????文件????????868??2009-12-21?14:43??jiebao2\Cpp1.plg
?????文件????????872??2009-12-25?14:39??jiebao2\Cpp1.sln
????..A..H.?????16896??2009-12-25?15:14??jiebao2\Cpp1.suo
?????文件???????4863??2009-12-25?14:39??jiebao2\Cpp1.vcproj
?????文件???????1409??2009-12-21?13:52??jiebao2\Cpp1.vcproj.lys-PC.lys.user
?????文件??????11572??2009-12-25?15:12??jiebao2\Debug\BuildLog.htm
?????文件????3099648??2009-12-25?15:12??jiebao2\Debug\Cpp1.bsc
?????文件???????1427??2009-12-25?15:14??jiebao2\Cpp1.vcproj.WWW-272012DC625.Administrator.user
?????文件?????483328??2009-12-25?15:12??jiebao2\Debug\Cpp1.exe
?????文件????????146??2009-12-22?14:18??jiebao2\Debug\Cpp1.exe.em
?????文件????????212??2009-12-22?14:18??jiebao2\Debug\Cpp1.exe.em
?????文件????????145??2009-12-25?15:12??jiebao2\Debug\Cpp1.exe.intermediate.manifest
?????文件????1079408??2009-12-25?15:12??jiebao2\Debug\Cpp1.ilk
?????文件??????33578??2009-12-25?15:12??jiebao2\Debug\Cpp1.obj
?????文件??????????0??2009-12-25?15:12??jiebao2\Debug\Cpp1.sbr
?????文件?????????67??2009-12-25?15:12??jiebao2\Debug\mt.dep
?????文件?????388096??2009-12-25?15:12??jiebao2\Debug\vc80.idb
?????文件?????102400??2009-12-25?15:12??jiebao2\Debug\vc80.pdb
?????文件??????58059??2009-12-25?14:46??jiebao2\log.txt
?????目錄??????????0??2009-12-25?15:12??jiebao2\Debug
?????目錄??????????0??2009-12-25?15:12??jiebao2
?????文件????2001920??2009-12-25?15:12??jiebao2\Debug\Cpp1.pdb
-----------?---------??----------?-----??----
?????????????16873213????????????????????27
............此處省略0個文件信息
- 上一篇:MFC編寫DDA畫直線和圓
- 下一篇:醫院管理系統數據庫
評論
共有 條評論