鱼C论坛

 找回密码
 立即注册
查看: 1155|回复: 0

原始套接字抓包源码求 大神修改 VS2010

[复制链接]
发表于 2015-3-17 08:46:29 | 显示全部楼层 |阅读模式
50鱼币
<p><div class="blockcode"><blockquote>#include<iostream>
#include<stdio.h>
#include<winsock2.h>
#include<ws2tcpip.h>
#pragma comment(lib,"ws2_32")

#define IO_RCVALL _WSAIOW(IOC_VENDOR,1)

typedef struct tcp_hdr
{
    unsigned short    th_sport;
    unsigned short    th_dport;
    unsigned int    th_seq;
    unsigned int    th_ack;
    unsigned char    th_lenres;
    unsigned char    th_flag;
    unsigned short    th_win;
    unsigned short    th_sum;
    unsigned short    th_urp;
}TCP_HEADER;

typedef struct IP_HEAD
{
 union
 {
  unsigned char Version;
  unsigned char HeadLen;
 };
 unsigned char ServiceType;
 unsigned short TotalLen;
 unsigned short Identifier;
 union
 {
  unsigned short Flags;
  unsigned short FragOffset;
 };
 unsigned char TimeToLive;
 unsigned char Protocol;
 unsigned short HeadChecksum;
 unsigned int SourceAddr;
 unsigned int DestinAddr;
 unsigned char Options;
}ip_head;

void main(int argc,char *argv[])
{
 using namespace std;
 argc=2;
 if(argc!=2)
 {
  cout<<endl<<"请以下格式输入命令行:PackParse packet_sum"<<endl;
  return;
 }
 WSADATA WSAData;
 if(WSAStartup(MAKEWORD(2,2), &WSAData)!=0)
 {
  cout<<endl<<"WSASTartup初始化失敗"<<endl;
  return;
 }
 SOCKET sock=socket(AF_INET,SOCK_RAW,IPPROTO_IP);//創建原始套接字
 if(sock==INVALID_SOCKET)
 {
  cout<<endl<<"創建Socket失敗!"<<endl;
  goto exit_clean;
 }
 BOOL flag=TRUE;
 if(setsockopt(sock,IPPROTO_IP,IP_HDRINCL,(char*) &flag,sizeof(flag))==SOCKET_ERROR)//設置端口
 {
  cout<<endl<<"setsockopt操作失敗:"<<WSAGetLastError()<<endl;
  goto exit_clean;
 }
 char hostName[128];
 if(gethostname(hostName,100)==SOCKET_ERROR)//獲取主機名稱
 {
  cout<<endl<<"gethostname操作失敗:"<<WSAGetLastError()<<endl;
  goto exit_clean;
 }
 hostent *pHostIP;
 if((pHostIP=gethostbyname(hostName))==NULL)//獲取主機對應IP地址
 {
  cout<<endl<<"gethostbyname操作失敗:"<<WSAGetLastError()<<endl;
  goto exit_clean;
 }
 sockaddr_in host_addr;
 host_addr.sin_family=AF_INET;
 host_addr.sin_port=htons(8000);
 host_addr.sin_addr=*(in_addr *)pHostIP->h_addr_list[0];
 if(bind(sock,(SOCKADDR*)&host_addr,sizeof(host_addr))==SOCKET_ERROR)
 {
  cout<<endl<<"bind操作失敗:"<<WSAGetLastError()<<endl;
  goto exit_clean;
 }
// char buffer1[65535];
// recv(sock,buffer1,65535,0);
 DWORD dwBufferLen[10];
 DWORD dwBufferInLen=1;
 DWORD dwBytesReturned=0;
 if(WSAIoctl(sock,IO_RCVALL ,&dwBufferInLen ,sizeof(dwBufferInLen) ,&dwBufferLen,sizeof(dwBufferLen),&dwBytesReturned,NULL,NULL)==SOCKET_ERROR)
 {
  cout<<endl<<"WSAIoctl操作失敗:"<<WSAGetLastError()<<endl;
  goto exit_clean;
 }
 cout<<endl<<"開始解析IP包:"<<endl;
 char buffer[65535];
 int packsum=atoi("1231");//

 for(int i=0;i<packsum;i++)
 {
         p:
  if(recv(sock,buffer,65535,0)>0)
  {
   ip_head ip=*(ip_head *)buffer;
   if((int)ip.Protocol!=IPPROTO_TCP)
   {
           printf("no Smtp\n");
           goto p;
   }
   printf("is Smet");
   cout<<"-----------------------"<<endl;
   cout<<"版本:"<<(ip.Version>>4)<<endl;
   cout<<"頭部長度:"<<((ip.HeadLen &0x0f)*4)<<endl;
   cout<<"服務類型:Priority"<<(ip.ServiceType>>5)<<", Service"<<((ip.ServiceType>>1)&0x0f)<<endl;
   cout<<"總長度:"<<ip.TotalLen<<endl;
   cout<<"標示符:"<<ip.Identifier<<endl;
   cout<<"標志位:"<<((ip.Flags>>15)&0x01)<<",DF= "<<((ip.Flags>>14)&0x01)<<",Mf="<<((ip.Flags>>13)&0x01)<<endl;
   cout<<"偏移位:"<<(ip.FragOffset&0x1fff)<<endl;
   cout<<"生存周期:"<<(int)ip.TimeToLive<<endl;
   cout<<"協議:Protocol: "<<(int)ip.Protocol<<endl;
   switch((int)ip.Protocol)
   {
   case IPPROTO_TCP:
           {
                   printf("----------------TCP----------------- \n");
                                /*获取 TCP 相关数据 ??????*/
                   goto to;  
           }
        case IPPROTO_ICMP:
           {
                   printf("---------------------ICMP--------------- \n");
                   goto to;  
           }
                   case IPPROTO_CBT:
           {
                   printf("------------------CBT---------------- \n");
                   goto to;  
           }
                   case IPPORT_FTP:
           {
                   printf("-----------------FTP------------- \n");
                   goto to;  
           }
                   case IPPORT_SMTP:
           {
                   printf("------------------SMTP------------------ \n");
                   goto to;  
           }   
   }
  to:
   cout<<"頭部校驗和:"<<ip.HeadChecksum<<endl;
   cout<<"原地址:"<<inet_ntoa(*(in_addr *)&ip.SourceAddr)<<endl;
   cout<<"目的IP地址:"<<inet_ntoa(*(in_addr *)&ip.DestinAddr)<<endl;
   printf("buffer=%d\n",ip.Protocol);
  }
 }
 Sleep(10000);
 exit_clean:
 closesocket(sock);
 WSACleanup();
}



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-25 19:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表