关于原始套接字的问题,高手们多指教

pitchstar 2001-12-14 02:54:16
听说过 syn flood 攻击,书上说原始套接字可以发任意 ip 包,甚至错误的包。
我想用他发一个 tcp syn 请求包,应该怎么写?我的代码不干活

WSADATA wsd;
SOCKET s=INVALID_SOCKET;
struct sockaddr_in remote;
IpHeader ih;
BOOL bOpt;
int totallen;
char * ipsrc="172.16.0.111";
char * ipdes="172.16.0.188";
char * sendbuff;
//定义了一个 tcp 头,里边的数据是截包截下来的,省地自己去定义每个部分。
char tcphead[28] = {(char)0x11,(char)0x8d,(char)0x00,(char)0x50,(char)0x0d,(char)0xb8,(char)0x85,
(char)0x8f,(char)0x00,(char)0x00,(char)0x00,(char)0x00,(char)0x70,(char)0x02,
(char)0x40,(char)0x00,(char)0x00,(char)0x00,(char)0x00,(char)0x00,(char)0x02,
(char)0x04,(char)0x05,(char)0xb4,(char)0x01,(char)0x01,(char)0x04,(char)0x02};


sendbuff=(char *)malloc(1024);
ZeroMemory(sendbuff,1024);
WSAStartup(MAKEWORD(2,2),&wsd);
//下面用了 IPPROTO_UDP,不知道这样是否可行?
s=WSASocket(AF_INET,SOCK_RAW,IPPROTO_UDP,NULL,0,0);

::setsockopt (s,IPPROTO_IP,IP_HDRINCL,(char *)&bOpt,sizeof(bOpt));
totallen=sizeof(ih)+sizeof(tcphead);
ih.ip_verlen = (4<<4)|sizeof(ih);
ih.tos =0;
ih.total_len = totallen;
ih.ident = 0;
ih.ip_offset = 0;
ih.ttl = 128;
ih.proto = 6; //tcp, 0x11 for udp
ih.checksum = 0;
ih.sourceIP = inet_addr(ipsrc);
ih.desIP = inet_addr(ipsrc);

//计算校验值
*(unsigned short *)(tcphead+16)=checksum(sendbuff+sizeof(ih),sizeof(tcphead));
ih.checksum = checksum(sendbuff,sizeof(ih)+sizeof(tcphead));

//把 ip 头复制到发送缓冲区里
memcpy(sendbuff,&ih,sizeof(ih));
//把tcp 头复制到缓冲区里
memcpy(sendbuff+sizeof(ih),tcphead,sizeof(tcphead));

remote.sin_family = AF_INET;
remote.sin_port = htons(80);
remote.sin_addr.s_addr = inet_addr(ipdes);

int j=sendto(s,sendbuff,totallen,0,(SOCKADDR *)&remote,sizeof(remote));
int t=::WSAGetLastError ();
最后 t 的值为:WSAEADDRNOTAVAIL,好象是地址无效。。。
...全文
117 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pitchstar 2001-12-14
  • 打赏
  • 举报
回复
typedef struct _iphdr{
unsigned char ip_verlen;
unsigned char tos;
unsigned short total_len;
unsigned short ident;
unsigned short ip_offset;
unsigned char ttl;
unsigned char proto;
unsigned short checksum;
unsigned int sourceIP;
unsigned int desIP;
} IpHeader;

以上是 IP 头的数据结构定义。
原则上,原始套接字不管你的包是否合理都可以发出去,我这里 sendto 的返回值是 -1
pitchstar 2001-12-14
  • 打赏
  • 举报
回复
先谢谢楼上各位,等问题解决了一起给分
pitchstar 2001-12-14
  • 打赏
  • 举报
回复
superdreamer
换过了,还是一样的错误

哪位大侠有成功了的代码给参考一下
superdreamer 2001-12-14
  • 打赏
  • 举报
回复
你将 bOpt=TRUE试试
supperapplication 2001-12-14
  • 打赏
  • 举报
回复
我也想知道
pitchstar 2001-12-14
  • 打赏
  • 举报
回复
查过,字节对齐没问题,
我出次用 raw ,很多细节不了解,winsock 编程基础讲的也比较含糊。。
florist2000 2001-12-14
  • 打赏
  • 举报
回复
如果你是按照协议做的,是不会出现问题的
如果你确定按照协议做了,但是出现问题
那么,建议你检查一下你的程序的字节对齐方式
比如你定义的struct是不事真的和你定义的大小一样

pitchstar 2001-12-14
  • 打赏
  • 举报
回复
大家帮忙

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧