上一篇:电脑高手常用的五个按钮 >>
Ping的原代码, 网络安全者必看
| #pragma pack(4) #define win32_lean_and_mean #include <winsock2.h> #include <stdio.h> #include <stdlib.h> #define icmp_echo 8 #define icmp_echoreply 0 #define icmp_min 8 // minimum 8 byte icmp packet (just header) /* the ip header */ typedef struct iphdr { unsigned int h_len:4; // length of the header unsigned int version:4; // version of ip unsigned char tos; // type of service unsigned short total_len; // total length of the packet unsigned short ident; // unique identifier unsigned short frag_and_flags; // flags unsigned char ttl; unsigned char proto; // protocol (tcp, udp etc) unsigned short checksum; // ip checksum unsigned int sourceip; unsigned int destip; }ipheader; // // icmp header // typedef struct _ihdr { byte i_type; byte i_code; /* type sub code */ ushort i_cksum; ushort i_id; ushort i_seq; /* this is not the std header, but we reserve space for time */ ulong timestamp; }icmpheader; #define status_failed 0xffff #define def_packet_size 32 #define max_packet 1024 #define xmalloc(s) heapalloc(getprocessheap(),heap_zero_memory,(s)) #define xfree(p) heapfree (getprocessheap(),0,(p)) void fill_icmp_data(char *, int); ushort checksum(ushort *, int); void decode_resp(char *,int ,struct sockaddr_in *); void usage(char *progname){ fprintf(stderr,"usage:\n"; fprintf(stderr,"%s <host> [data_size]\n",progname); fprintf(stderr,"datasize can be up to 1kb\n"; exitprocess(status_failed); } int main(int argc, char **argv){ wsadata wsadata; socket sockraw; struct sockaddr_in dest,from; struct hostent * hp; int bread,datasize; int fromlen = sizeof(from); int timeout = 1000; char *dest_ip; char *icmp_data; char *recvbuf; unsigned int addr=0; ushort seq_no = 0; if (wsastartup(makeword(2,1),&wsadata) != 0){ fprintf(stderr,"wsastartup failed: %d\n",getlasterror()); exitprocess(status_failed); } if (argc <2 ) { usage(argv[0]); } sockraw = wsasocket (af_inet, sock_raw, ipproto_icmp, null, 0,0); if (sockraw == invalid_socket) { fprintf(stderr,"wsasocket() failed: %d\n",wsagetlasterror()); exitprocess(status_failed); } bread = setsockopt(sockraw,sol_socket,so_rcvtimeo,(char*)&timeout, sizeof(timeout)); if(bread == socket_error) { fprintf(stderr,"failed to set recv timeout: %d\n",wsagetlasterror()); exitprocess(status_failed); } timeout = 1000; bread = setsockopt(sockraw,sol_socket,so_sndtimeo,(char*)&timeout, sizeof(timeout)); if(bread == socket_error) { fprintf(stderr,"failed to set send timeout: %d\n",wsagetlasterror()); exitprocess(status_failed); } memset(&dest,0,sizeof(dest)); hp = gethostbyname(argv[1]); if (!hp){ addr = inet_addr(argv[1]); } if ((!hp) && (addr == inaddr_none) ) { fprintf(stderr,"unable to resolve %s\n",argv[1]); exitprocess(status_failed); } if (hp != null) memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length); else dest.sin_addr.s_addr = addr; if (hp) dest.sin_family = hp->h_addrtype; else dest.sin_family = af_inet; dest_ip = inet_ntoa(dest.sin_addr); if (argc >2) { datasize = atoi(argv[2]); if (datasize == 0) datasize = def_packet_size; } else datasize = def_packet_size; datasize += sizeof(icmpheader); icmp_data = xmalloc(max_packet); recvbuf = xmalloc(max_packet); if (!icmp_data) { fprintf(stderr,"heapalloc failed %d\n",getlasterror()); exitprocess(status_failed); } memset(icmp_data,0,max_packet); fill_icmp_data(icmp_data,datasize); while(1) { int bwrote; ((icmpheader*)icmp_data)->i_cksum = 0; ((icmpheader*)icmp_data)->timestamp = gettickcount(); ((icmpheader*)icmp_data)->i_seq = seq_no++; ((icmpheader*)icmp_data)->i_cksum = checksum((ushort*)icmp_data, datasize); bwrote = sendto(sockraw,icmp_data,datasize,0,(struct sockaddr*)&dest, sizeof(dest)); if (bwrote == socket_error){ if (wsagetlasterror() == wsaetimedout) { printf("timed out\n"; continue; } fprintf(stderr,"sendto failed: %d\n",wsagetlasterror()); exitprocess(status_failed); } if (bwrote < datasize ) { fprintf(stdout,"wrote %d bytes\n",bwrote); } bread = recvfrom(sockraw,recvbuf,max_packet,0,(struct sockaddr*)&from, &fromlen); if (bread == socket_error){ if (wsagetlasterror() == wsaetimedout) { printf("timed out\n"; continue; } fprintf(stderr,"recvfrom failed: %d\n",wsagetlasterror()); exitprocess(status_failed); } decode_resp(recvbuf,bread,&from); sleep(1000); } return 0; } /* the response is an ip packet. we must decode the ip header to locate the icmp data */ void decode_resp(char *buf, int bytes,struct sockaddr_in *from) { ipheader *iphdr; icmpheader *icmphdr; unsigned short iphdrlen; iphdr = (ipheader *)buf; iphdrlen = iphdr->h_len * 4 ; // number of 32-bit words *4 = bytes if (bytes < iphdrlen + icmp_min) { printf("too few bytes from %s\n",inet_ntoa(from->sin_addr)); } icmphdr = (icmpheader*)(buf + iphdrlen); if (icmphdr->i_type != icmp_echoreply) { fprintf(stderr,"non-echo type %d recvd\n",icmphdr->i_type); return; } if (icmphdr->i_id != (ushort)getcurrentprocessid()) { fprintf(stderr,"someone else's packet!\n"; return ; } printf("%d bytes from %s:",bytes, inet_ntoa(from->sin_addr)); printf(" icmp_seq = %d. ",icmphdr->i_seq); printf(" time: %d ms ",gettickcount()-icmphdr->timestamp); printf("\n"; } ushort checksum(ushort *buffer, int size) { unsigned long cksum=0; while(size >1) { cksum+=*buffer++; size -=sizeof(ushort); } if(size ) { cksum += *(uchar*)buffer; } cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >>16); return (ushort)(~cksum); } /* helper function to fill in various stuff in our icmp request. */ void fill_icmp_data(char * icmp_data, int datasize){ icmpheader *icmp_hdr; char *datapart; icmp_hdr = (icmpheader*)icmp_data; icmp_hdr->i_type = icmp_echo; icmp_hdr->i_code = 0; icmp_hdr->i_id = (ushort)getcurrentprocessid(); icmp_hdr->i_cksum = 0; icmp_hdr->i_seq = 0; datapart = icmp_data + sizeof(icmpheader); // // place some junk in the buffer. // memset(datapart,'e', datasize - sizeof(icmpheader)); } 下一篇:冲浪DDoS(拒绝服务)攻击的趋势与防御 >> 相关文章:
© 2006 www.java-asp.net
|
