选修了一门信息安全专业的课,做了个实验,是实现网络抓包的功能:

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/ip_icmp.h>
#include<netinet/tcp.h>
#include<netinet/udp.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<sys/types.h>#define BUFFSIZE 1024int main(int argc,char **argv ){if(argc!=2){printf("Usage:Proto_Name\n");return 0;}int rawsock;unsigned char buff[BUFFSIZE];int n;int count = 0;char *ipr_name=argv[1];char *ipr_tcp="TCP";char *ipr_udp="UDP";char *ipr_icmp="ICMP";if(strcmp(ipr_name,ipr_tcp)==0)rawsock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP);else if(strcmp(ipr_name,ipr_udp)==0)rawsock = socket(AF_INET,SOCK_RAW,IPPROTO_UDP);else if(strcmp(ipr_name,ipr_icmp)==0)rawsock = socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);if(rawsock < 0){printf("raw socket error!\n");exit(1);}while(1)
{   n = recvfrom(rawsock,buff,BUFFSIZE,0,NULL,NULL);if(n<0){printf("receive error!\n");exit(1);}count++;struct ip *ip = (struct ip*)buff;printf("%4d  %15s",count,inet_ntoa(ip->ip_src));printf("%15s    %5d %5d\n",inet_ntoa(ip->ip_dst),ip->ip_p,ntohs(ip->ip_len)); int i=0,j=0;for(i=0;i<n;i++){if(i!=0 && i%16==0){printf("   ");for(j=i-16;j<i;j++){if(buff[j]>=32&&buff[j]<=128)printf("%c",buff[j]);else printf(".");}printf("\n");}if(i%16 == 0) printf("%04x ",i);          printf("%02x",buff[i]);if(i==n-1){for(j=0;j<15-i%16;j++) printf("  ");printf(" ");for(j=i-i%16;j<=i;j++){if(buff[j]>=32&&buff[j]<127)printf("%c",buff[j]);else printf(".");}}}printf("\n"); printf("internet protocol\n");printf("version:%u\n",ip->ip_v);printf("Header Length:%u bytes\n",(ip->ip_hl)*4);printf("totle length:%d\n",ntohs(ip->ip_len));printf("Identification:%u\n",ip->ip_id);if((IP_RF&0x8000)!=0)printf("reserved bits:set\n");elseprintf("reserved bits:not set\n");if((IP_DF&0x4000)!=0)printf("dont fragment: not set\n");elseprintf("dont fragment: set\n");if((IP_MF&0x2000)!=0)printf("more fragment: set\n");elseprintf("more fragment: not set\n");printf("Time to live:%u\n",ip->ip_ttl);if(ip->ip_p==6)printf("protocol TCP(6)\n");else if(ip->ip_p==1)printf("protocol ICMP(1)\n");else if(ip->ip_p==17)printf("protocol UDP(17)\n");printf("source ip:%s\n",inet_ntoa(ip->ip_src));printf("destination ip:%s\n",inet_ntoa(ip->ip_dst));//TCPif(ip->ip_p==6){printf("transmission control protocol\n");struct tcphdr *tcp=(struct tcphdr *)(buff+(ip->ip_hl)*4);  printf("source port:%u\n",ntohs(tcp->source));printf("destation port:%u\n",ntohs(tcp->dest));printf("sequence number:%u\n",ntohl(tcp->seq));printf("acknowledgement number:%u\n",ntohl(tcp->ack_seq));printf("head length:%d\n",ntohs((tcp->doff)*4));if(tcp->urg==1)printf("urgent:set\n");elseprintf("urgent:not set\n");if(tcp->ack==1)printf("acknowledgment:set\n");elseprintf("acknowledgment:not set\n");if(tcp->psh==1)printf("push:set\n");elseprintf("push:not set\n");if(tcp->rst==1)printf("reset:set\n");elseprintf("reset:not set\n");if(tcp->syn==1)printf("syn:set\n");elseprintf("syn:not set\n");if(tcp->fin==1)printf("fin:set\n");elseprintf("fin:not set\n");printf("window size:%u\n",ntohs(tcp->window));}//UDPif(ip->ip_p==17){struct udphdr *udp=(struct udphdr*)(buff+(ip->ip_hl)*4);printf("user datagram protocol\n");printf("source port:%u\n",udp->source);printf("destination port:%u\n",udp->dest);printf("length:%u\n",ntohs(udp->len));}//ICMPif(ip->ip_p==1){ struct icmphdr *icmp = (struct icmphdr *)(buff+(ip->ip_hl)*4);printf("Internet Control Message Protocol\n");printf("type:%u",icmp->type);if(icmp->type==0)printf("(Echo Reply)\n");else if(icmp->type==8)printf("(Echo)\n");else if(icmp->type==5)printf("(Redirect)\n");else if(icmp->type==3)printf("(Dest Unreach)\n");else if(icmp->type==4)printf("(Source quench)\n");else if(icmp->type==13)printf("(Time Stamp)\n");else if(icmp->type==14)printf("(Time Stamp Reply)\n");printf("Code:%u\n",icmp->code);if(icmp->type==0||icmp->type==8){printf("idetifier:0x%x\n",ntohs(icmp->un.echo.id));printf("Sequence:%u\n",ntohs(icmp->un.echo.sequence));}if(icmp->type==3||icmp->type==4){printf("Unused:%u\n",ntohs(icmp->un.frag.__unused));printf("Mtu:%u\n",ntohs(icmp->un.frag.mtu));}  if(icmp->type==5)printf("Gateway:%u\n",ntohs(icmp->un.gateway));}     printf("\n\n");
}}

转载于:https://www.cnblogs.com/justcxtoworld/archive/2013/04/02/2996548.html

写程序实现wireshark的抓包功能相关推荐

  1. Linux 内核抓包功能实现基础(三) 抓包服务器的实现

    上回博客我们讲到了内核抓包内核端的实现,通过上篇博客的例子我们就能够开始抓包了,整个抓包的拓扑图如下: 当开始抓包后,抓包机器将抓到的报文送到服务器上,假设服务器地址是192.168.199.123: ...

  2. wireshark Fiddler抓包分析与解密https Fiddler修改https请求和响应

    Https理论 在说HTTPS之前先说说什么是HTTP,HTTP就是我们平时浏览网页时候使用的一种协议.HTTP协议传输的数据都是未加密的,也就是明文的,因此使用HTTP协议传输隐私信息非常不安全.为 ...

  3. 【Wireshark系列十】wireshark怎么抓包、wireshark抓包详细图文教程

    wireshark怎么抓包.wireshark抓包详细图文教程 wireshark是非常流行的网络封包分析软件,功能十分强大.可以截取各种网络封包,显示网络封包的详细信息.使用wireshark的人必 ...

  4. 【计算机网络】 0、各网络命令 + tcpdump + Wireshark、抓包实战、TCP 握手挥手、防火墙、保活、MTU

    文章目录 一.各层网络工具 应用层 找到服务器的 IP 查接口.对象的耗时 删除指定网站的Cookie 表示层.会话层 tcpdump.wireshard 传输层 telnet: 路径可达性测试 nc ...

  5. 实验十四:Wireshark数据抓包分析之ARP协议

    实验十四:Wireshark数据抓包分析之ARP协议 目录 一.实验目的及要求 二.实验原理 1.什么是ARP 2.ARP工作流程 3.ARP缓存表 三.实验环境 四.实验步骤及内容 实验步骤一 1. ...

  6. Wireshark数据抓包教程之Wireshark的基础知识

    Wireshark数据抓包教程之Wireshark的基础知识 Wireshark的基础知识 在这个网络信息时代里,计算机安全始终是一个让人揪心的问题,网络安全则有过之而无不及.Wireshark作为国 ...

  7. Wireshark数据抓包教程之Wireshark捕获数据

    Wireshark数据抓包教程之Wireshark捕获数据 Wireshark抓包方法 在使用Wireshark捕获以太网数据,可以捕获分析到自己的数据包,也可以去捕获同一局域网内,在知道对方IP地址 ...

  8. Wireshark数据抓包分析(网络协议篇)第1章网络协议抓包概述

    Wireshark数据抓包分析(网络协议篇)第1章网络协议抓包概述 网络协议是用于不同计算机之间进行网络通信的.网络协议是网络上所有设备(如网络服务器.计算机.交换机.路由器等)之间通信规则的集合,它 ...

  9. Wireshark数据抓包分析——网络协议篇

    Wireshark数据抓包分析--网络协议篇 Wireshark是目前最受欢迎的抓包工具.它可以运行在Windows.Linux及MAC OS X操作系统中,并提供了友好的图形界面.同时,Wiresh ...

最新文章

  1. html省市联动插件,jquery实现的交互体验更友好省市区三级联动插件
  2. 干货 | 一文教你如何快速高效阅读Paper(硕士生版)
  3. android 点击图片事件,android图文混排点击事件
  4. Android安全教程(3)---Fiddler简易使用教程之抓取https包
  5. python 异常处理 变量_Python基础入门:从变量到异常处理
  6. ADO.NET Entity Framework学习笔记(4)ObjectQuery对象
  7. iOS开发遇到的坑之五--解决工程已存在plist表,数据却不能存入的问题
  8. 谷歌大脑AI飞速解锁雅达利,训练不用两小时:预测能力“前所未有”
  9. 51单片机电子制作------篮球比赛计分器
  10. 第二阶段团队站立会议08
  11. java 快速排序流程图_java简单快速排序实例解析
  12. 洛谷 [P1890] gcd区间
  13. mysql 外键 150_mysql之创建外键报150错误的处理方法
  14. 【Visual C++】游戏开发笔记三十五 站在巨人的肩膀上:游戏引擎导论
  15. 利用ffmpeg将H264解码为RGB
  16. Win7 不支持此接口问题
  17. Ollydbg之字符串、WindowsAPI搜索
  18. 利用mininet模拟SDN架构并进行DDoS攻击与防御模拟(Ryu+mininet+sflow+postman)
  19. ZJOI2019Round#1
  20. 《信息物理融合系统(CPS)设计、建模与仿真——基于 Ptolemy II 平台》——2.6 类和继承...

热门文章

  1. 64位x86的函数调用栈布局
  2. 数字证书转换cer---pem
  3. IntelliJ IDEA 安装本地插件
  4. 安卓第三天笔记--通知-进度条-时期
  5. ice(Internet Communications Engine) window 安装与配置
  6. C BNF grammar
  7. Gartner 2018 年WAF魔力象限报告:云WAF持续增长,Bot管理与API安全拥有未来
  8. python安装pymssql等包时出现microsoft visual c++ 14.0 is required问题无需下载visualcppbuildtools的解决办法...
  9. shell打印正三角形
  10. Python 将json字符串 进行列表化可循环