linux 监听数据包,linux下网络监听与发送数据包的方法(即libpcap、libnet两种类库的使用方法)...

linux下可以用libpcap函数库实现监听数据包,使用libnet 函数库发送数据包

安装:

在命令行下apt-get install 就可以了

libpcap的使用:

/*author hjj

date 2011-1-21

function:capture packet with the ruler and output the packet information

modify 2011-1-23

function:get dns packet*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define ETHER_ADDR_LEN 6

/*以太网头*/

struct sniff_ethernet

{

u_char ether_dhost[ETHER_ADDR_LEN];

u_char ether_shost[ETHER_ADDR_LEN];

u_short ether_type;

};

/*IP头*/

struct sniff_ip

{

u_char ip_vhl;

u_char ip_tos;

u_short ip_len;

u_short ip_id;

u_short ip_off;

#define IP_RF 0x8000

#define IP_DF 0x4000

#define IP_MF 0x2000

#define IP_OFFMASK 0x1fff

u_char ip_ttl;

u_char ip_p;

u_short ip_sum;

struct in_addr ip_src,ip_dst;

};

/*TCP头*/

typedef u_int tcp_seq;

struct sniff_tcp

{

u_short th_sport;

u_short th_dport;

tcp_seq th_seq;

tcp_seq th_ack;

u_char th_offx2;

u_char th_flags;

u_short th_win;

u_short th_sum;

u_short th_urp;

};

/*UDP报头*/

struct sniff_udp

{

u_short udp_sport;

u_short udp_dport;

u_short udp_len;

u_short udp_sum;

};

/*DNS报头*/

struct sniff_dns

{

u_short dns_id;

u_short dns_flag;

u_short dns_ques;

u_short dns_ans;

u_short dns_auth;

u_short dns_add;

u_int8_t *dsn_data;

};

//数据包到达回调函数void packetcall(u_char *user,const struct pcap_pkthdr *pcap_head,const u_char *packet);

char *ipstr(struct in_addr s_addr);

char* getpackettype(u_short packet_type);

char* toString(u_long s);

//由u_char[6]获取网卡地址字符串char *getMac(u_char *host);

int main(int argc,char **argv)

{

char *dev,errbuf[PCAP_ERRBUF_SIZE];

pcap_t *handler;

struct bpf_program fp;

char filter_exp[50]="ip and dst 172.20.92.118";

if(argc==3)

{

sprintf(filter_exp,"dst %s and dst port %s",argv[1],argv[2]);

}

if(argc==5)

{

sprintf(filter_exp,"dst %s and dst port %s or src %s and src port %s",argv[1],argv[2],argv[3],argv[4]);

}

bpf_u_int32 mask;

bpf_u_int32 net;

struct pcap_pkthdr header;

const u_char *packet;

dev=pcap_lookupdev(errbuf);

if(dev==NULL)

{

fprintf(stderr,"could not find default device:%s\n",errbuf);

return 2;

}

printf("device:%s\n",dev);

if(pcap_lookupnet(dev,&net,&mask,errbuf)==-1)

{

fprintf(stderr,"counld not get netmask for device %s;%s\n",dev,errbuf);

net=0;

mask=0;

}

handler=pcap_open_live(dev,BUFSIZ,1,10000,errbuf);

if(handler==NULL)

{

fprintf(stderr,"could not open device %s;%s",dev,errbuf);

return 2;

}

if(pcap_compile(handler,&fp,filter_exp,0,net)==-1)

{

fprintf(stderr,"counld not parse filter %s;%s\n",filter_exp,pcap_geterr(handler));

return 2;

}

if(pcap_setfilter(handler,&fp)==-1)

{

fprintf(stderr,"counld not install filter %s;%s\n",filter_exp,pcap_geterr(handler));

return 2;

}

//捕获数据包 int packetnums=20;

packet=pcap_loop(handler,packetnums,packetcall,NULL);

pcap_close(handler);

return 0;

}

//数据包到达回调函数void packetcall(u_char *user,const struct pcap_pkthdr *pcap_head,const u_char *packet)

{

static int count=1;//数据包计数 struct sniff_ethernet *ethernet;//以太网包头

struct sniff_ip *ip;//ip包头

struct sniff_udp *udp;//udp包头

struct sniff_dns *dns;//dns报头

const u_char *payload;//数据包负载的数据

int pay_size;//数据包负载的数据大小

ethernet=(struct sniff_ethernet*)(packet);

ip=(struct sniff_ip*)(packet + sizeof(struct sniff_ethernet));

udp=(struct sniff_udp*)(packet + sizeof(struct sniff_ethernet)+sizeof(struct sniff_ip));

dns=(struct sniff_dns*)(packet + sizeof(struct sniff_ethernet) + sizeof(struct sniff_ip) + sizeof(struct sniff_udp));

payload=(u_char *)(packet+sizeof(struct sniff_ethernet)+sizeof(struct sniff_ip)+sizeof(struct sniff_udp)+sizeof(struct sniff_dns));

pay_size=ntohs(udp->udp_len)-sizeof(struct sniff_udp)-sizeof(struct sniff_dns);

printf("-------------数据包:%d\n",count);

printf("数据包类型:%s\n",getpackettype(ethernet->ether_type));

printf("源地址:%X:%X:%X:%X:%X:%X\n",

(ethernet->ether_shost)[0],

(ethernet->ether_shost)[1],

(ethernet->ether_shost)[2],

(ethernet->ether_shost)[3],

(ethernet->ether_shost)[4],

(ethernet->ether_shost)[5]);

printf("目的地址:%X:%X:%X:%X:%X:%X\n",

(ethernet->ether_dhost)[0],

(ethernet->ether_dhost)[1],

(ethernet->ether_dhost)[2],

(ethernet->ether_dhost)[3],

(ethernet->ether_dhost)[4],

(ethernet->ether_dhost)[5]);

printf("From:%s\n",inet_ntoa(ip->ip_src));

printf("To:%s\n",inet_ntoa(ip->ip_dst));

printf("源端口:%d\n",ntohs(udp->udp_sport));

printf("目的端口:%d\n",ntohs(udp->udp_dport));

printf("DNS查询问题数%d\n",ntohs(dns->dns_ques));

if(pay_size>0)

{

printf("Payload data size %d\n",pay_size);

const u_char *ch=payload;

int i,j;

for(i=0;idns_ques);i++)

{

//获取各查询名 printf("第%d个查询名\n",i);

int k=1;//标志符号; while(1)

{

if(*ch==0)

break;

u_int8_t identify_size=*ch;

printf("\t第%d个标志符号\n",k);

ch++;

for(j=0;j

{

if(isprint(*ch))

{

printf("%c",*ch);

}else

{

printf(".");

}

}

k++;

}

}

}

count++;

}

libnet的使用

/*author hjj

date 2011-1-20

function: send an arp packet to all machine on local net*/

#include

#include

#define MAC_ADDR_LEN 6

#define IP_ADDR_LEN 4

#define LIBNET_DNS_H 0xc

int main(int argc,char **argv)

{

libnet_t *net_t=NULL;

char *dev="eth0";

char err_buf[LIBNET_ERRBUF_SIZE];

libnet_ptag_t p_tag;

unsigned char src_mac[MAC_ADDR_LEN]={0x00,0x00,0xf1,0xe8,0x0e,0xc8};//发送者网卡地址

unsigned char dst_mac[MAC_ADDR_LEN]={0xff,0xff,0xff,0xff,0xff,0xff};//接收者网卡地址 char *src_ip_str="172.20.92.117";

if(argc==2)

{

if(strcmp(argv[1],"-h")==0||strcmp(argv[1],"--help")==0)

{

printf("%s","help message");

}else

{

src_ip_str=argv[1];

}

}

unsigned long src_ip,dst_ip=0;

src_ip=libnet_name2addr4(net_t,src_ip_str,LIBNET_RESOLVE);//将字符串类型的ip转换为顺序网络字节流 net_t=libnet_init(LIBNET_LINK_ADV,dev,err_buf);//初始化发送包结构 if(net_t==NULL)

{

printf("libnet_init error\n");

exit(0)

}

p_tag=libnet_build_arp(

ARPHRD_ETHER,//hardware type ethernet ETHERTYPE_IP,//protocol type MAC_ADDR_LEN,//mac length IP_ADDR_LEN,//protocol length ARPOP_REPLY,//op type (u_int8_t*)src_mac,//source mac addr这里的作用是更新目的地的arp表 (u_int8_t*)&src_ip,//source ip addr (u_int8_t*)dst_mac,//source mac addr (u_int8_t*)&dst_ip,//dest ip addr NULL,//payload 0,//payload length net_t,//libnet context 0//0 stands to build a new one );

if(-1 == p_tag)

{

printf("libnet_build_arp error");

exit(0);

}

//以太网头部 p_tag=libnet_build_ethernet(//create ethernet header (u_int8_t*)dst_mac,//dest mac addr (u_int8_t*)src_mac,//source mac addr ETHERTYPE_ARP,//protocol type NULL,//payload 0,//payload length net_t,//libnet context 0//0 to build a new one );

if(-1 == p_tag)

{

printf("libnet_build_ethernet error!\n");

exit(1);

}

int res;

if(-1==(res=libnet_write(net_t)))

{

printf("libnet_write error!\n");

exit(1);

}

libnet_destroy(net_t);

return 0;

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393767.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

命令模式(Command Pattern)

1命令模式是一个高内聚的模式。定义如下:将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。 2.角色说明: ● Receive接收者角色 该角色就…

BZOJ 3270: 博物馆

传送门 显然可以状态转移: 设 $f[k][x][y]$ 表示第 $k$ 时刻,第一个人在 $x$ ,第二个人在 $y$ 时的概率 那么转移显然: $f[k][x][y]\sum_{u}\sum_{v}f[k-1][u][v]*(1-P_u)(1-P_v)/du[u]/du[v]$ 其中 $u$ 和 $x$ 有边相连&#xff…

graphpad7.04多组比较p值_同是折线图为何你却这么优秀,这才是多组数据作图应该有的样子...

相信大家对Excel做折线图应该不陌生,在展示数据的时候,图表是一种最好的展示方法。但是经常会碰到一种尴尬的事情就是,当数据维多比较多的时候,做出的图表就会显得非常难看。今天我们就来学习一下,多组数据怎么做折线图…

Logic-算法-八个箱子找一个最轻的

ylbtech-Arithmetic:Logic-算法-八个箱子找一个最轻的-- -- ylb:算法-- Type:算法[logic]-- munu:八个箱子-找一个最轻的-- thankyou:gaoZhimin -- 7:11 2012/3/17-- 有八个正方形的箱子,外观大小都一样,其中七个是50斤的,一个是…

由衷的信来激励有抱负的开发人员

by Logan Wright洛根赖特(Logan Wright) 由衷的信来激励有抱负的开发人员 (A heartfelt letter to inspire the aspiring developer) I’m writing a letter to my friend. You should read it. He studies Computer Science, and he hates it. I build React Apps and I love…

linux 运行 chom,Hadoop安装-单节点/伪分布(2.7.3)

1,下载Hadoop目前在Ubuntu的软件库里面 没有发现Hadoop的压缩包,没猜错Hadoop不是可执行文件 只是一个压缩包吧!所以我们只能自己到官网下载(http://hadoop.apache.org/releases.html);在Apache社区中,下载软件的时候…

leetcode944. 删列造序

给定由 N 个小写字母字符串组成的数组 A,其中每个字符串长度相等。 你需要选出一组要删掉的列 D,对 A 执行删除操作,使 A 中剩余的每一列都是 非降序 排列的,然后请你返回 D.length 的最小可能值。 删除 操作的定义是&#xff1…

python学习:re模块

常用正则表达式符号 123456789101112131415161718192021. 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行^ 匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","\nabc\neee&qu…

app之---豆果美食

1.抓包 2.代码 抓取: #!/usr/bin/env python # -*- coding: utf-8 -*- #author tom import requests from multiprocessing import Queue from handle_pymongo import mongo from concurrent.futures import ThreadPoolExecutorclass Douguo():def __init__(self):s…

语言坐标度分秒的换算_测量位置度说明

测量位置度说明位置度是限制被测要素的实际位置对理想位置变动量的指标。它的定位尺寸为理论正确尺寸。位置度公差在评定实际要素位置的正确性, 是依据图样上给定的理想位置。位置度包括点的位置度、线的位置度和面的位置度。[1] 点的位置度:如公差带前加S¢&#xf…

OpenStack创建win7实例遇到的问题(尚未解决,求帮助)

原地址在这里:(作者也是我,害羞)http://www.aboutyun.com/forum.php?modviewthread&tid22898 小白经过两天尝试,用fuel部署好了OpenStack的云平台,接下来想在Compute节点上创建一个win7 实例&#xff…

VMware使两台windows虚拟机能够互相ping通

如果以下内容测试无效,可参考另一篇:VMware虚拟机配置内网电脑能访问 1.关闭防火墙 cmd命令行里输入:netsh firewall set opmode disable 2.测试如果还不能ping通,就把网络类型选nat类型 3.测试:vmware网关默认是.2 转…

linux账号前有个base,安装 aconda 后Linux的终端界面前部出现(base)字样

aconda 是做什么用的这里就不说了,一般玩Python的都知道这东西,最早接触这东西是因为它把NVIDIA中cuda计算和Python互连的一个库拿下了,是买下来了还是专业,还是唯一合作的也就记不清了,那就是 numba , 那些年头Python…

回复邮件时如何不要邮件头_如何为阅读,点击和回复率达到100%的CEO设计一封冷邮件...

回复邮件时如何不要邮件头by Theo Strauss由西奥斯特劳斯(Theo Strauss) 如何为阅读,点击和回复率达到100%的CEO设计一封冷邮件 (How to design a cold email for a CEO with a 100% read, click, and response rate) 银河电子邮件指南:第二…

leetcode1007. 行相等的最少多米诺旋转(贪心)

在一排多米诺骨牌中,A[i] 和 B[i] 分别代表第 i 个多米诺骨牌的上半部分和下半部分。(一个多米诺是两个从 1 到 6 的数字同列平铺形成的 —— 该平铺的每一半上都有一个数字。) 我们可以旋转第 i 张多米诺,使得 A[i] 和 B[i] 的值…

Spring 学习教程(一): 认识 Spring 框架

Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of Control,控制反转) 和 AOP(Aspect Oriented Programming,面向切面编程)。 Spring 的框架结构 Data Access/Int…

小米网关控制空调伴侣_小米有品上架移动空调,支持语音控制

近日小米有品商城上架了一款互联网可移动空调,机身仅有小米空气净化器一般大小,底部安装了万向轮,支持多方位自由移动,拥有三大功能,兼顾去暑除湿能力,产品售价1599元,有需求的用户可以在小米有…

错误: 找不到符号

Error:(31, 29) 错误: 找不到符号 符号: 类 OnLaunchPluginCallback 位置: 类 IreaderPlugApi 明明我都可以ctrl 单击点过去,但是就是运行的时候报错。说错误: 找不到符号。 我试了两遍,把工程clearn, 删除build下面的文件夹,弄了两遍&am…

leetcode910. 最小差值 II(贪心)

给定一个整数数组 A,对于每个整数 A[i],我们可以选择 x -K 或是 x K,并将 x 加到 A[i] 中。 在此过程之后,我们得到一些数组 B。 返回 B 的最大值和 B 的最小值之间可能存在的最小差值。 示例 1: 输入&#xff1…

laravel 检测sql_在Laravel PHP应用程序中轻松进行面部检测

laravel 检测sqlby Darren Chowles达伦乔尔斯(Darren Chowles) 在Laravel PHP应用程序中轻松进行面部检测 (Easy facial detection in your Laravel PHP application) 使用Google Cloud Vision API检测图像中的人脸 (Detect faces in images using the Google Cloud Vision AP…