custom ip header

原创 2012年03月25日 18:16:56
#include #include #include #define SOURCE_PORT 7234 #define MAX_RECEIVEBYTE 255 typedef struct ip_hdr //定义IP首部 { unsigned char h_verlen; //4位首部长度,4位IP版本号 unsigned char tos; //8位服务类型TOS unsigned short total_len; //16位总长度(字节) unsigned short ident; //16位标识 unsigned short frag_and_flags; //3位标志位 unsigned char ttl; //8位生存时间 TTL unsigned char proto; //8位协议 (TCP, UDP 或其他) unsigned short checksum; //16位IP首部校验和 unsigned int sourceIP; //32位源IP地址 unsigned int destIP; //32位目的IP地址 }IPHEADER; typedef struct tsd_hdr //定义TCP伪首部 { unsigned long saddr; //源地址 unsigned long daddr; //目的地址 char mbz; char ptcl; //协议类型 unsigned short tcpl; //TCP长度 }PSDHEADER; typedef struct tcp_hdr //定义TCP首部 { USHORT th_sport; //16位源端口 USHORT th_dport; //16位目的端口 unsigned int th_seq; //32位序列号 unsigned int th_ack; //32位确认号 unsigned char th_lenres; //4位首部长度/6位保留字 unsigned char th_flag; //6位标志位 USHORT th_win; //16位窗口大小 USHORT th_sum; //16位校验和 USHORT th_urp; //16位紧急数据偏移量 }TCPHEADER; //CheckSum:计算校验和的子函数 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); } void useage() { printf("******************************************\n"); printf("TCPPing\n"); printf("\t Written by Refdom\n"); printf("\t Email: refdom@263.net\n"); printf("Useage: TCPPing.exe Target_ip Target_port \n"); printf("*******************************************\n"); } int main(int argc, char* argv[]) { WSADATA WSAData; SOCKET sock; SOCKADDR_IN addr_in; IPHEADER ipHeader; TCPHEADER tcpHeader; PSDHEADER psdHeader; char szSendBuf[60]={0}; BOOL flag; int rect,nTimeOver; useage(); if (argc!= 3) { return false; } if (WSAStartup(MAKEWORD(2,2), &WSAData)!=0) { printf("WSAStartup Error!\n"); return false; } if ((sock=WSASocket(AF_INET,SOCK_RAW,IPPROTO_RAW,NULL,0,WSA_FLAG_OVERLAPPED))==INVALID_SOCKET) { printf("Socket Setup Error!\n"); return false; } flag=true; if (setsockopt(sock,IPPROTO_IP, IP_HDRINCL,(char *)&flag,sizeof(flag))==SOCKET_ERROR) { printf("setsockopt IP_HDRINCL error!\n"); return false; } nTimeOver=1000; if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&nTimeOver, sizeof(nTimeOver))==SOCKET_ERROR) { printf("setsockopt SO_SNDTIMEO error!\n"); return false; } addr_in.sin_family=AF_INET; addr_in.sin_port=htons(atoi(argv[2])); addr_in.sin_addr.S_un.S_addr=inet_addr(argv[1]); // // //填充IP首部 ipHeader.h_verlen=(4<<4 | sizeof(ipHeader)/sizeof(unsigned long)); // ipHeader.tos=0; ipHeader.total_len=htons(sizeof(ipHeader)+sizeof(tcpHeader)); ipHeader.ident=1; ipHeader.frag_and_flags=0; ipHeader.ttl=128; ipHeader.proto=IPPROTO_TCP; ipHeader.checksum=0; ipHeader.sourceIP=inet_addr("本地地址"); ipHeader.destIP=inet_addr(argv[1]); //填充TCP首部 tcpHeader.th_dport=htons(atoi(argv[2])); tcpHeader.th_sport=htons(SOURCE_PORT); //源端口号 tcpHeader.th_seq=htonl(0x12345678); tcpHeader.th_ack=0; tcpHeader.th_lenres=(sizeof(tcpHeader)/4<<4|0); tcpHeader.th_flag=2; //修改这里来实现不同的标志位探测,2是SYN,1是FIN,16是ACK探测 等等 tcpHeader.th_win=htons(512); tcpHeader.th_urp=0; tcpHeader.th_sum=0; psdHeader.saddr=ipHeader.sourceIP; psdHeader.daddr=ipHeader.destIP; psdHeader.mbz=0; psdHeader.ptcl=IPPROTO_TCP; psdHeader.tcpl=htons(sizeof(tcpHeader)); //计算校验和 memcpy(szSendBuf, &psdHeader, sizeof(psdHeader)); memcpy(szSendBuf+sizeof(psdHeader), &tcpHeader, sizeof(tcpHeader)); tcpHeader.th_sum=checksum((USHORT *)szSendBuf,sizeof(psdHeader)+sizeof(tcpHeader)); memcpy(szSendBuf, &ipHeader, sizeof(ipHeader)); memcpy(szSendBuf+sizeof(ipHeader), &tcpHeader, sizeof(tcpHeader)); memset(szSendBuf+sizeof(ipHeader)+sizeof(tcpHeader), 0, 4); ipHeader.checksum=checksum((USHORT *)szSendBuf, sizeof(ipHeader)+sizeof(tcpHeader)); memcpy(szSendBuf, &ipHeader, sizeof(ipHeader)); rect=sendto(sock, szSendBuf, sizeof(ipHeader)+sizeof(tcpHeader), 0, (struct sockaddr*)&addr_in, sizeof(addr_in)); if (rect==SOCKET_ERROR) { printf("send error!:%d\n",WSAGetLastError()); return false; } else printf("send ok!\n"); closesocket(sock); WSACleanup(); return

相关文章推荐

sourcegrid 应用实例(全部来自官网下载的例子)——Real Grid basic (custom header, alternate backcolor)

 源码:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using Sys...

Vi and Vim Autocommand: 3 Steps to Add Custom Header To Your File Automatically

In this article, using 3 simple steps, let us review how to use this powerful autocmd feature of Vim...

Develop Web Service With Axis2 #7 - Add Custom Soap Header in Response Soap Message

This article will talk about how to add soap headers in response soap message, not work around solu...
  • wxhawk
  • wxhawk
  • 2011年01月04日 18:07
  • 1409

tcp/ip-HTTP-Header-消息报头

  • 2012年02月20日 15:43
  • 9KB
  • 下载

Vivado开发工具熟悉之Create custom IP

今天突然想继续写点东西,想起来年前最后一篇blog后还差一块vivado create custom IP基本上就完成了所有关于vivado开发工具的熟悉,有了这些之后基本上可以完整的通过vivado...

Wireshark捕获的outgoing TCP包的IP header checksum error问题

转自:http://www.cnblogs.com/laoyur/archive/2011/03/22/1991688.html Wireshark捕获的outgoing TCP包的IP heade...

TCP-IP详解:AH(Authentication Header)

IPSec核心协议是AH和ESP,本文主要介绍下AH协议的数据封装与传输

构造HTTP请求Header实现“伪造来源IP”

构造 HTTP请求 Header 实现“伪造来源 IP ”   在阅读本文前,大家要有一个概念,在实现正常的TCP/IP 双方通信情况下,是无法伪造来源 IP 的,也就是说,在 TCP/I...

Python 黑帽子 snffer ip header decoder

解决方案来自:stackoverflow 出现两种error 错误主要与平台有关我使用的是kali 2.0 64 Buffer size too small (20 instead of at...
  • Z_LiT0
  • Z_LiT0
  • 2016年06月30日 13:24
  • 444

TCP/IP HTTP-Header-消息报头的获取

String useragent = request.getHeader("User-Agent"); 通过这里的消息报头,可以获取用户是iPhone或者安卓或者其他的浏览器的请求; ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:custom ip header
举报原因:
原因补充:

(最多只允许输入30个字)