通过原始套接字截取本地网卡的所有数据

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <WinSock2.h>
  5. #include <WS2tcpip.h>
  6. #pragma comment(lib, "Ws2_32")
  7. using namespace std;
  8. #define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
  9. //TCP数据段头
  10. #pragma pack(1)
  11. typedef struct _TCP
  12. {
  13.  WORD SrcPort; // 源端口
  14.  WORD DstPort; // 目的端口
  15.  DWORD SeqNum; // 顺序号
  16.  DWORD AckNum; // 确认号
  17.  BYTE DataOff; // TCP头长
  18.  BYTE Flags; // 标志(URG、ACK等)
  19.  WORD Window; // 窗口大小
  20.  WORD Chksum; // 校验和
  21.  WORD UrgPtr; // 紧急指针
  22. } TCP;
  23. #pragma pack
  24. typedef TCP *LPTCP;
  25. typedef TCP UNALIGNED * ULPTCP;
  26. //IP数据段头
  27. #pragma pack(1)
  28. typedef struct _IP{
  29.  union
  30.  {
  31.   BYTE Version; // 版本
  32.   BYTE HdrLen; // IHL
  33.  };
  34.  BYTE ServiceType; // 服务类型
  35.  WORD TotalLen; // 总长
  36.  WORD ID; // 标识
  37.  union
  38.  {
  39.   WORD Flags; // 标志
  40.   WORD FragOff; // 分段偏移
  41.  };
  42.  BYTE TimeToLive; // 生命期
  43.  BYTE Protocol; // 协议
  44.  WORD HdrChksum; // 头校验和
  45.  DWORD SrcAddr; // 源地址
  46.  DWORD DstAddr; // 目的地址
  47.  BYTE Options; // 选项
  48. } IP;
  49. #pragma pack
  50. typedef IP * LPIP;
  51. typedef IP UNALIGNED * ULPIP;
  52. string GetProtocolType(int Protocol)
  53. {
  54.  switch (Protocol)
  55.  {
  56.  case IPPROTO_ICMP : //1 /* control message protocol */
  57.   return "ICMP";
  58.  case IPPROTO_TCP : //6 /* tcp */
  59.   return "TCP";
  60.  case IPPROTO_UDP : //17 /* user datagram protocol */
  61.   return "UDP";
  62.  default:
  63.   return "UNKNOW_TYPE";
  64.  }
  65. }
  66. int _tmain(int argc, _TCHAR* argv[])
  67. {
  68.  WSADATA wsaData;
  69.  unsigned int iValue = 1;
  70.  int iRet = WSAStartup(MAKEWORD(2,2), &wsaData);
  71.  SOCKET sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
  72.  iRet = setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*) &iValue, sizeof(iValue));
  73.  sockaddr_in addr;
  74.  memset((void*) &addr, 0, sizeof(addr));
  75.  addr.sin_family = AF_INET;
  76.  addr.sin_addr.S_un.S_addr = inet_addr("192.168.6.141");
  77.  addr.sin_port = htons(0);
  78.  iRet = bind(sock, (struct sockaddr*) &addr, sizeof(addr));
  79.  BOOL bSniff = TRUE;
  80.  unsigned long ulBytes;
  81.  iRet = WSAIoctl(sock,SIO_RCVALL,&bSniff, sizeof(bSniff), NULL, 0, &ulBytes, NULL, NULL);
  82.  char *buf = new char[65535];
  83.  while (true)
  84.  {
  85.   memset((void*) buf, 0, 65535);
  86.   sockaddr_in sockAddr;
  87.   memset((void*) &sockAddr, 0, sizeof(sockAddr));
  88.   int iLen = sizeof(sockAddr);
  89.   iRet = recvfrom(sock, buf, 65535, 0, (struct sockaddr*) &sockAddr, &iLen);
  90.   // 对数据包进行分析,并输出分析结果
  91.   IP ip = *(IP*)buf;
  92.   TCP tcp = *(TCP*)(buf + ip.HdrLen);
  93.   string strProtocol = GetProtocolType(ip.Protocol);
  94.   cout<<"protocol: "<<strProtocol<<endl;
  95.   cout<<"IP src address: "<<inet_ntoa(*(in_addr*)&ip.SrcAddr)<<endl;
  96.   cout<<"IP tag address: "<<inet_ntoa(*(in_addr*)&ip.DstAddr)<<endl;
  97.   cout<<"TCP src port: "<<tcp.SrcPort<<endl;
  98.   cout<<"TCP tag port: "<<tcp.DstPort<<endl;
  99.   cout<<"Buf Len: "<<ntohs(ip.TotalLen)<<endl;
  100.   cout<<"-------------------------------------------------------------"<<endl;
  101.  }
  102.  delete [] buf;
  103.  closesocket(sock);
  104.  WSACleanup();
  105.  return 0;
  106. }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值