没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
最近由于项目需要,研究了一下ICMP和ROW SOCKET协议,顺便在这里记下,一是方便给自已
留个笔记,也方便和朋友们一起学习和进步。
昨天在修改公司一个wince的程序时,偶然发现其中一个ping主机的模块代码有内存泄露问题,但我也有几年没有去看过ICMP的东东,因此,把以前项目用过的ICMP和row socket协议代码拿出来再翻翻,把BUG给修正了,顺道也把row socet实现IP欺骗的代码也贴出来。(PING的代码就不贴了,网上很多例子,简单的就几个ICMP的api就可实现,也有用socket来实现的,代码量多一点而已。)
我对协议的理解是在每层加上一个包头而已,IP层位于网络层,socket提供的函数只能修改网络层的东东,因此,平常我们用的socket函数一般只能使用在网络层以后面的像HTTP层上,Http这些应该属于应用层,只是在网络层上再加一个Http包头而已。
下面的代码是在三年前一个用于做自动网络拓朴发现中用到的IP欺骗的一个模块,说起Auto拓朴发现,就是通过一个网管程序,采用SNMP+ICMP+socket+TD等各设备厂家提供的自有协议的前提下,能够自动把企业内的网络图画出来,刚好那时候公司安排我做的这个模块,因此也研究过阿尔卡特\北电\华为\思科等的路由器和交换机,对他们的公有、私有mib库都有一些了解,二层和三层的拓朴图还是画得比较完善的,只是在交换机到普通的PC服务器之间的线画得不全,后来才通过采用IP欺骗才得到进一点提高。
简单的讲一下IP欺骗,目的是向A设备发送ICMP包,在包里面把本应该是填自已的IP地址改为B设备的IP地址,让A设备误以为是B设备向他发的包,因此,A就会把回应的包发到B设备去。而这样,原A和B设备之间也许没有通信,通过IP欺骗主动使他们交流一下,这样就方便我在A设备的mib库中找到关于更多B的信息。
说不多说了,下面是代码,是用C++写的,但因为主项目是用python写的,因为下面的代码中,会有一些关于生成python可以调用的dll的东东,如果你编译时报错可以直接去掉即可。因为你不需要生成python能调的dll,呵呵。
//#define WIN32_LEAN_AND_MEAN
#include "include\Python.h"
#include <winsock2.h>
//#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>
//
// Set up some default values
//
#define DEFAULT_COUNT 4
#define DEFAULT_MESSAGE "This is a test"
#define ICMP_FLOOD 0
#define IP_HDRINCL 2
#define IP_TTL 255
typedef struct ip_hdr
{
//BYTE h_len:4; // Length of the header in dwords
//BYTE version:4; // Version of IP
unsigned char ip_verlen; // IP version & length
unsigned char ip_tos; // IP type of service
unsigned short ip_totallength; // Total length
unsigned short ip_id; // Unique identifier
unsigned short ip_offset; // Fragment offset field
unsigned char ip_ttl; // Time to live
unsigned char ip_protocol; // Protocol(TCP, UDP, etc.)
unsigned short ip_checksum; // IP checksum
unsigned int ip_srcaddr; // Source address
unsigned int ip_destaddr; // Destination address
} ;
typedef struct icmp_hdr {
BYTE type; // ICMP packet type
BYTE code; // Type sub code
USHORT checksum;
USHORT id;
USHORT seq;
//ULONG timestamp; // not part of ICMP, but we need it
};
struct pkt
{
typedef struct ip_hdr
{
//BYTE h_len:4; // Length of the header in dwords
//BYTE version:4; // Version of IP
unsigned char ip_verlen; // IP version & length
unsigned char ip_tos; // IP type of service
unsigned short ip_totallength; // Total length
unsigned short ip_id; // Unique identifier
unsigned short ip_offset; // Fragment offset field
unsigned char ip_ttl; // Time to live
unsigned char ip_protocol; // Protocol(TCP, UDP, etc.)
unsigned short ip_checksum; // IP checksum
unsigned int ip_srcaddr; // Source address
unsigned int ip_destaddr; // Destination address
} ;
typedef struct icmp_hdr {
BYTE type; // ICMP packet type
BYTE code; // Type sub code
USHORT checksum;
USHORT id;
USHORT seq;
//ULONG timestamp; // not part of ICMP, but we need it
};
struct pkt
{
剩余7页未读,继续阅读
xiangding
- 粉丝: 81
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页