阅读上一个主题 :: 阅读下一个主题 |
作者 | 留言 |
---|
思一克 版主 - 天使
注册时间: 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: cindy_f_ye@yahoo.com.cn |
|
返回页首 |
|
|
win_hate 版主 - 大天使
注册时间: 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: cindy_f_ye@yahoo.com.cn |
|
返回页首 |
|
|
win_hate 版主 - 大天使
注册时间: 2003-05-14 最后登录: 2005-01-10 帖子总数: 1813 精华帖子: 1 原创精华: 1 来自: 广东广州 BLOG主页:进入 在线状态: ...保密...
|
发表于: 2005-01-07 15:01 发表主题: | |
| 是这个:
我只会用 libpcap 抓,
_________________ 闭关中......偶尔冒泡...各位多担待。
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/
最后进行编辑的是 win_hate on 2005-01-07 15:01, 总计第 1 次编辑 |
|
返回页首 |
|
|
superdoctor 风云使者
注册时间: 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: cindy_f_ye@yahoo.com.cn |
|
返回页首 |
|
|
黄山松 圣骑士
注册时间: 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 版主 - 大天使
注册时间: 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 风云使者
注册时间: 2003-11-12 最后登录: 2005-01-10 帖子总数: 504 精华帖子: 0 原创精华: 0
BLOG主页:进入 在线状态: ...离线...
|
发表于: 2005-01-08 10:01 发表主题: | |
|
黄山松 写到: | 有则改之,无则加勉:) 没别的意思,只想c/c++版更好,本贴楼主好象是版主吧? |
也难怪黄山松老兄会产生误解。 CU的头衔实在让人头大。 发的帖子多了,就是版主?还是某一个版的版主?反正我是没明白。
|
|
返回页首 |
|
|
win_hate 版主 - 大天使
注册时间: 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 侠客
注册时间: 2004-09-12 最后登录: 2005-01-08 帖子总数: 19 精华帖子: 0 原创精华: 0 来自: 上海
在线状态: ...离线...
|
发表于: 2005-01-08 17:01 发表主题: | |
| 确实好用!收藏~
|
|
返回页首 |
|
|
arcsiny 圣骑士
注册时间: 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: cindy_f_ye@yahoo.com.cn |
|
返回页首 |
|
|