閱讀上一個主題 :: 閱讀下一個主題 |
作者 |
留言 |
---|
思一克 版主 - 天使
註冊時間: 2003-08-25 最後登錄: 2005-01-10 帖子總數: 1165 精華帖子: 1 原創精華: 0 來自: 東城
在線狀態: ...保密...
|
發表於: 2005-01-07 15:01 發表主題: 給出一個 netdump 程序, 抓包用的. 如果改進了,也希望貼出 |
|
| 最基本的,在linux i386上
代碼: |
//netdump.c
#include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/ip.h> #include <string.h> #include <netdb.h> #include <netinet/tcp.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <net/if.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <fcntl.h>
void die(char *why, int n) { perror(why); exit(n); }
int do_promisc(char *nif, int sock ) { struct ifreq ifr; strncpy(ifr.ifr_name, nif,strlen(nif)+1); if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) { die("ioctl", 2); } ifr.ifr_flags |= IFF_PROMISC; if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 ) { die("ioctl", 3); } }
char buf[2*32767];
main() { struct sockaddr_in addr; struct iphdr *ip; struct tcphdr *tcp; int sock, r, len; char *data; char ss[32], dd[32];
if((sock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP)) == -1) die("socket", 1); do_promisc("eth0", sock); for(;;) { len = sizeof(addr); r = recvfrom(sock,(char *)buf,sizeof(buf),0,(struct sockaddr *)&addr,&len); buf[r] = 0; ip = (struct iphdr *)buf; tcp = (struct tcphdr *)(buf + sizeof(struct iphdr));
printf("PktSize: %d IPLEN %d PROT %d %s:%d-->%s:%d %d /n", r, ip->tot_len, ip->protocol, strcpy(ss, inet_ntoa(*(struct in_addr*)&(ip->saddr))), ntohs(tcp->source), strcpy(dd, inet_ntoa(*(struct in_addr*)&(ip->daddr))), ntohs(tcp->dest), tcp->doff ); data = (char*)tcp + 4*tcp->doff; printf("data = %s/n", data); }
}
|
_________________ 提供QMAIL系統技術支持。構建更完美的MAIL系統。Email: [email protected] |
|
返回頁首 |
|
|
win_hate 版主 - 大天使
150)this.width=140" border=0>
註冊時間: 2003-05-14 最後登錄: 2005-01-10 帖子總數: 1813 精華帖子: 1 原創精華: 1 來自: 廣東廣州 BLOG主頁:進入 在線狀態: ...保密...
|
|
返回頁首 |
|
|
思一克 版主 - 天使
註冊時間: 2003-08-25 最後登錄: 2005-01-10 帖子總數: 1165 精華帖子: 1 原創精華: 0 來自: 東城
在線狀態: ...保密...
|
發表於: 2005-01-07 15:01 發表主題: |
|
| 那符號, 是飄揚,還是批評?
_________________ 提供QMAIL系統技術支持。構建更完美的MAIL系統。Email: [email protected] |
|
返回頁首 |
|
|
win_hate 版主 - 大天使
150)this.width=140" border=0>
註冊時間: 2003-05-14 最後登錄: 2005-01-10 帖子總數: 1813 精華帖子: 1 原創精華: 1 來自: 廣東廣州 BLOG主頁:進入 在線狀態: ...保密...
|
|
返回頁首 |
|
|
superdoctor 風雲使者
150)this.width=140" border=0>
註冊時間: 2003-01-16 最後登錄: 2005-01-10 帖子總數: 474 精華帖子: 0 原創精華: 0 來自: beijing city BLOG主頁:進入 在線狀態: ...離線...
|
|
返回頁首 |
|
|
思一克 版主 - 天使
註冊時間: 2003-08-25 最後登錄: 2005-01-10 帖子總數: 1165 精華帖子: 1 原創精華: 0 來自: 東城
在線狀態: ...保密...
|
發表於: 2005-01-07 15:01 發表主題: |
|
| 精華什麼。我就胡亂搞一個,因爲看這幾天總有問的帖子。 沒有放回貼是因爲怕一會就消失了
_________________ 提供QMAIL系統技術支持。構建更完美的MAIL系統。Email: [email protected] |
|
返回頁首 |
|
|
黃山鬆 聖騎士
註冊時間: 2004-09-16 最後登錄: 2005-01-10 帖子總數: 92 精華帖子: 0 原創精華: 0
在線狀態: ...離線...
|
發表於: 2005-01-07 20:01 發表主題: |
|
| 還不錯,呵呵 不過你這個功能稍微太弱了點 我幫你改了改,你原來的只能收到IP層的數據,而且只能有針對性的收,現在可以收鏈路層也就是以太網包 而且是大小各個類型統吃,呵呵
代碼: | #include <stdio.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/ip.h> #include <string.h> #include <netdb.h> #include <netinet/tcp.h> #include <netinet/udp.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <net/if.h> #include <sys/ioctl.h> #include <sys/stat.h> #include <fcntl.h> #include <linux/if_ether.h>
void die(char *why, int n) { perror(why); exit(n); }
int do_promisc(char *nif, int sock ) { struct ifreq ifr; strncpy(ifr.ifr_name, nif,strlen(nif)+1); if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) { die("ioctl", 2); } ifr.ifr_flags |= IFF_PROMISC; if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 ) { die("ioctl", 3); }
}
char buf[2*32767];
main() { struct sockaddr_in addr; struct ethhdr *peth; struct iphdr *pip; struct tcphdr *ptcp; struct udphdr *pudp; /*add more protocol head here....*/ int sock, r, len; char *data; char *ptemp; char ss[32], dd[32]; int i;
if((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) die("socket", 1);
do_promisc("eth0", sock); for(;;) { len = sizeof(addr); r = recvfrom(sock,(char *)buf,sizeof(buf), 0, (struct sockaddr *)&addr,&len); buf[r] = 0; ptemp = buf; /*which can get source mac address and destnation address, and which network packet, here is OSI-2, link layer*/ peth = (struct ethhdr *)ptemp; ptemp += sizeof(struct ethhdr); /*which get IP layer informations, includes which transport protocol, source and destnation IP address...*/ pip = (struct iphdr *)ptemp;
/* * which can get transport layer informations, such as: transport socket port, transport layer includes * TCP, UDP, ICMP, IGMP......, can get which transport protocol from IP header */ ptemp += sizeof(struct iphdr); switch(pip->protocol) { case IPPROTO_TCP: ptcp = (struct tcphdr *)ptemp; printf("TCP pkt:/n"); /* * and your service code.... */ break;
case IPPROTO_UDP: pudp = (struct udphdr *)ptemp; printf("UDP pkt:/n len:%d payload len:%d from %s:%d to %s:%d/n", r, ntohs(pudp->len), strcpy(ss, inet_ntoa(*(struct in_addr*)&(pip->saddr))), ntohs(pudp->source), strcpy(dd, inet_ntoa(*(struct in_addr*)&(pip->daddr))), ntohs(pudp->dest) ); /* * and your service code.... */ break;
case IPPROTO_ICMP: printf("ICMP pkt:/n"); break; case IPPROTO_IGMP: printf("IGMP pkt:/n"); break;
/* . . . . . */ default: printf("Unkown pkt, protocl:%d/n", pip->protocol); break; } } } | [/code]
最後進行編輯的是 黃山鬆 on 2005-01-10 13:01, 總計第 1 次編輯 |
|
返回頁首 |
|
|
黃山鬆 聖騎士
註冊時間: 2004-09-16 最後登錄: 2005-01-10 帖子總數: 92 精華帖子: 0 原創精華: 0
在線狀態: ...離線...
|
發表於: 2005-01-07 20:01 發表主題: |
|
| 順便建議一下,我覺得精華區應該更精華一些! 不能是版主就精華哦
|
|
返回頁首 |
|
|
win_hate 版主 - 大天使
150)this.width=140" border=0>
註冊時間: 2003-05-14 最後登錄: 2005-01-10 帖子總數: 1813 精華帖子: 1 原創精華: 1 來自: 廣東廣州 BLOG主頁:進入 在線狀態: ...保密...
|
發表於: 2005-01-07 22:01 發表主題: |
|
|
黃山鬆 寫到: | 順便建議一下,我覺得精華區應該更精華一些! 不能是版主就精華哦 |
建議是好的,不過我們這裏有
的情況麼?
_________________ 閉關中......偶爾冒泡...各位多擔待。
In computing, turning the obvious into the useful is living definition of the word "frustration".
http://my.chinaunix.net/win_hate/ || http://www.gzisi.com/ |
|
返回頁首 |
|
|
黃山鬆 聖騎士
註冊時間: 2004-09-16 最後登錄: 2005-01-10 帖子總數: 92 精華帖子: 0 原創精華: 0
在線狀態: ...離線...
|
發表於: 2005-01-08 10:01 發表主題: |
|
|
win_hate 寫到: |
的情況麼? |
有則改之,無則加勉:) 沒別的意思,只想c/c++版更好,本貼樓主好象是版主吧?
|
|
返回頁首 |
|
|
assiss 風雲使者
150)this.width=140" border=0>
註冊時間: 2003-11-12 最後登錄: 2005-01-10 帖子總數: 504 精華帖子: 0 原創精華: 0
BLOG主頁:進入 在線狀態: ...離線...
|
發表於: 2005-01-08 10:01 發表主題: |
|
|
黃山鬆 寫到: | 有則改之,無則加勉:) 沒別的意思,只想c/c++版更好,本貼樓主好象是版主吧? |
也難怪黃山鬆老兄會產生誤解。 CU的頭銜實在讓人頭大。 發的帖子多了,就是版主?還是某一個版的版主?反正我是沒明白。
|
|
返回頁首 |
|
|
win_hate 版主 - 大天使
150)this.width=140" border=0>
註冊時間: 2003-05-14 最後登錄: 2005-01-10 帖子總數: 1813 精華帖子: 1 原創精華: 1 來自: 廣東廣州 BLOG主頁:進入 在線狀態: ...保密...
|
發表於: 2005-01-08 10:01 發表主題: |
|
| 在我眼裏,思一克是一位樂於助人的技術高手,我都沒注意到他是斑竹。何況還不是c 版的。
您要是覺得這個精華給得不對,可以說出理由,我跟別的斑竹商量一下。
我給精華的理由如下:
問這種問題的人常有,直接能用的答覆就沒有。一般的回答就是“看xxx的代碼”,或者“用 libpcap"(我的答覆)。而思一克的回覆直接能用。
思一克給出的是一個最小例子,演示瞭如何使用 ioctl 把 網絡設備設置爲混雜模式。規模小,單純,就容易被理解,並可在其上作擴展,比如您後來的例子。
_________________ 閉關中......偶爾冒泡...各位多擔待。
In computing, turning the obvious into the useful is living definition of the word "frustration".
http://my.chinaunix.net/win_hate/ || http://www.gzisi.com/ |
|
返回頁首 |
|
|
honkily 俠客
150)this.width=140" border=0>
註冊時間: 2004-09-12 最後登錄: 2005-01-08 帖子總數: 19 精華帖子: 0 原創精華: 0 來自: 上海
在線狀態: ...離線...
|
發表於: 2005-01-08 17:01 發表主題: |
|
| 確實好用!收藏~
|
|
返回頁首 |
|
|
arcsiny 聖騎士
150)this.width=140" border=0>
註冊時間: 2003-04-17 最後登錄: 2005-01-10 帖子總數: 92 精華帖子: 1 原創精華: 0
BLOG主頁:進入 在線狀態: ...離線...
|
發表於: 2005-01-10 09:01 發表主題: |
|
| 呵呵 客觀的講 樓主給的例子確實很簡單,如果給精華的話,我想 黃山鬆 的更好一些。我想黃山鬆也是好意,包括我在內。
_________________ 曾因酒醉鞭名馬 生怕情多累美人 |
|
返回頁首 |
|
|
思一克 版主 - 天使
註冊時間: 2003-08-25 最後登錄: 2005-01-10 帖子總數: 1165 精華帖子: 1 原創精華: 0 來自: 東城
在線狀態: ...保密...
|
發表於: 2005-01-10 09:01 發表主題: |
|
| 謝謝你的意見。
我原來在WIN上做過那東西,有個非常全的程序,顯示幾乎所有協議,並按DOS DEBUG的D命令格式顯示的。
我是故意裁剪下來貼出來的,目的就是教學用,爲了是問題本質更清楚。
arcsiny 寫到: | 呵呵 客觀的講 樓主給的例子確實很簡單,如果給精華的話,我想 黃山鬆 的更好一些。我想黃山鬆也是好意,包括我在內。 |
_________________ 提供QMAIL系統技術支持。構建更完美的MAIL系統。Email: [email protected] |
|
返回頁首 |
|
|