raw socket 发送阻断包
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
来源:http://bugkill.01safe.com/thread-207-1-1.html *int sndpkt_tcp_rst(u32 seq,u32 src_ip, u32 dst_ip, u16 src_prt, u16 dst_prt) { static int i=0; int one=1; /* R.Stevens says we need this variable for the setsockopt call */ int tcphdrlen = sizeof(struct tcphdr); int iphdrlen = sizeof(struct iphdr); /* Raw socket file descriptor */ // int rawsocket=0; /* Buffer for the TCP/IP SYN Packets */ char packet[tcphdrlen+iphdrlen +1 ]; /* It will point to start of the packet buffer */ struct iphdr *ipheader = (struct iphdr *)packet; /* It will point to the end of the IP header in packet buffer */ struct tcphdr *tcpheader = (struct tcphdr *) (packet +iphdrlen); /* TPC Pseudoheader (used in checksum) */ tcp_phdr_t pseudohdr; /* TCP Pseudoheader + TCP actual header used for computing the checksum */ char tcpcsumblock[ sizeof(tcp_phdr_t) + TCPSYN_LEN ]; /* Although we are creating our own IP packet with the destination address */ /* on it, the sendto() system call requires the sockaddr_in structure */ struct sockaddr_in dstaddr; // memset(&pseudohdr,0,sizeof(tcp_phdr_t)); // memset(&packet, 0, sizeof(packet)); // memset(&dstaddr, 0, sizeof(dstaddr)); dstaddr.sin_family = AF_INET; /* Address family: Internet protocols */ dstaddr.sin_port = dst_prt; /* Leave it empty */ dstaddr.sin_addr.s_addr = dst_ip; /* Destination IP */ #if 0 /* Get a raw socket to send TCP packets */ if ((rawsocket = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0) { perror("TCP_RST_send():socket()"); exit(1); } /* We need to tell the kernel that we'll be adding our own IP header */ /* Otherwise the kernel will create its own. The ugly "one" variable */ /* is a bit obscure but R.Stevens says we have to do it this way ;-) */ if(setsockopt(rawsocket, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0) { perror("TCP_RST_send():setsockopt()"); exit(1); } #endif /* IP Header */ ipheader->ihl = 5; /* Header lenght in octects */ ipheader->version = 4; /* Ip protocol version (IPv4) */ ipheader->tos = 0; /* Type of Service (Usually zero) */ ipheader->tot_len = __hns(iphdrlen +tcphdrlen); ipheader->frag_off = 0; /* Fragment offset. We'll not use this */ ipheader->ttl = 64; /* Time to live: 64 in Linux, 128 in Windows... */ ipheader->protocol = 6; /* Transport layer prot. TCP=6, UDP=17, ICMP=1... */ ipheader->check= 0; /* Checksum. It has to be zero for the moment */ ipheader->id = __hns(1337); ipheader->saddr = src_ip; /* Source IP address */ ipheader->daddr = dst_ip; /* Destination IP address */ /* TCP Header */ tcpheader->seq = seq; /* Sequence Number */ tcpheader->ack_seq = __hnl(1); /* Acknowledgement Number */ tcpheader->res1 = 0; /* Variable in 4 byte blocks. (Deprecated) */ tcpheader->doff = 5; /* Segment offset (Lenght of the header) */ tcpheader->fin = 0; tcpheader->syn = 0; tcpheader->rst = 1; tcpheader->psh = 0; tcpheader->ack = 0; tcpheader->urg = 0; tcpheader->ece = 0; tcpheader->cwr = 0; /* TCP Flags. We set the Reset Flag */ tcpheader->window = __hns(5000);/* Window size */ tcpheader->urg_ptr = 0; /* Urgent pointer. */ tcpheader->source = src_prt; /* Source Port */ tcpheader->dest = dst_prt; /* Destination Port */ tcpheader->check=0; /* Checksum. (Zero until computed) */ /* Fill the pseudoheader so we can compute the TCP checksum*/ pseudohdr.src = ipheader->saddr; pseudohdr.dst = ipheader->daddr; pseudohdr.zero = 0; pseudohdr.protocol = ipheader->protocol; pseudohdr.tcplen = __hns(tcphdrlen); /* Copy header and pseudoheader to a buffer to compute the checksum */ // memcpy(tcpcsumblock, &pseudohdr, sizeof(tcp_phdr_t)); // memcpy(tcpcsumblock+sizeof(tcp_phdr_t),tcpheader, sizeof(struct tcphdr)); tcpheader->check = CalcTCPSum((unsigned short *)&pseudohdr,(unsigned short *)tcpheader,tcphdrlen); /* Compute the TCP checksum as the standard says (RFC 793) */ // tcpheader->check = in_cksum((unsigned short *)(tcpcsumblock), sizeof(tcpcsumblock)); /* Compute the IP checksum as the standard says (RFC 791) */ ipheader->check = in_cksum((unsigned short *)ipheader,iphdrlen); /* Send it through the raw socket */ if(sendto(g_rawsocket , packet, __nhs(ipheader->tot_len), 0,(struct sockaddr *) &dstaddr, sizeof(dstaddr)) < 0) { printf("Reset Faild\n"); fprintf(stderr, "Recv Error %s\n", strerror(errno)); return -1; } // WADEBUG(D_WARNING)("Reset OK\n"); /* printf("Sent RST Packet:\n"); printf(" SRC: %s:%d\n", inet_ntoa(ipheader->ip_src), ntohs(tcpheader->th_sport)); printf(" DST: %s:%d\n", inet_ntoa(ipheader->ip_dst), ntohs(tcpheader->th_dport)); printf(" Seq=%u\n", ntohl(tcpheader->th_seq)); printf(" Ack=%d\n", ntohl(tcpheader->th_ack)); printf(" TCPsum: %02x\n", tcpheader->th_sum); printf(" IPsum: %02x\n", ipheader->ip_sum); */ #if 0 close(rawsocket); #endif return 1; } /* End of IP_Id_send() */ |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=333