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.pswp.cn/news/393767.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393767.shtml
英文地址,請注明出處:http://en.pswp.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…