免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4367 | 回复: 7
打印 上一主题 下一主题

linux下用c写一个sniffer程序,这是源代码,编译通过运行有问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-05-21 14:48 |只看该作者 |倒序浏览
/************************sniffer.c****************************/
#include <stdio.h>;
#include <stdlib.h>;
#include <unistd.h>;
#include <string.h>;
#include <signal.h>;
#include <netinet/in.h>;
#include <netinet/ip.h>;
#include <netinet/tcp.h>;
#include <net/if.h>;
#include <netdb.h>;
#include <sys/ioctl.h>;
#include <sys/stat.h>;
#include <fcntl.h>;
#include <ctype.h>;
#include <sys/file.h>;
#include <sys/time.h>;
#include <sys/socket.h>;
#include <arpa/inet.h>;
#include <netinet/if_ether.h>;
#define INTERFACE "eth0"                      /* 网卡 */


int set_promisc(char *interface,int sock)              /* 杂乱模式 */
{
struct ifreq ifr;

strncpy(ifr.ifr_name, interface,strlen(interface)+1);
if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) {
printf("Could not receive flags for the interface\n";
exit(0);
}
ifr.ifr_flags |= IFF_PROMISC;
if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 ) {
printf("Could not set the PROMISC flag.\n";
exit(0);
}
printf("Setting interface ::: %s ::: to promisc\n", interface);

}

main()
{
int sock,bytes_received,len;
char *data;
char buffer[65535];
struct sockaddr_in addr;
struct iphdr *ip;
struct tcphdr *tcp;

if((sock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP)) == -1)
{    /* 使用SOCK_RAW */
printf("sniffer failt\n";
exit(0);
}
set_promisc(INTERFACE,sock);
while(1)
{
len = sizeof(addr);
bytes_received = recvfrom(sock,(char *)buffer,sizeof(buffer),0,(struct sockaddr *)&addr,&len);
printf("\nBytes received %5d\n",bytes_received);
printf("Source address %s \n",inet_ntoa(addr.sin_addr));
ip = (struct iphdr *)buffer;                 /* 格式化buffer的内容 */
printf("IP header length %d\n",ip->;tot_len);
printf("rotocol %d\n",ip->;protocol);
tcp = (struct tcphdr *)(buffer+sizeof(struct iphdr));    /* 格式化ip数据后面的buffer内容 */
printf("Source port %d\n",ntohs(tcp->;source));
printf("Dest port %d \n",ntohs(tcp->;dest));
data = &buffer[sizeof(struct iphdr) + sizeof(struct tcphdr)];  /* data 等于去掉iphdr和tcphdr后的buffer内容 */
printf("data: %s",data);
}
}
/*************************************************************************/
编译运行了以后似乎不可以抓取所有通过本机网卡的数据包,只显示目的地址是本机的数据包。数据data显示不正常,看不到应该有的数据,ip头的长度有1万多字节,正常吗?本来一个很简单的程序有这么多问题能帮我看看吗?
希望能得到你们的帮助,谢谢。

论坛徽章:
0
2 [报告]
发表于 2004-05-21 14:58 |只看该作者

linux下用c写一个sniffer程序,这是源代码,编译通过运行有问题

我用ifconfig看了,我的程序的确是把网卡设到了promisc上,可是数据包还是截不到别人的,数据也显示不出来。

论坛徽章:
0
3 [报告]
发表于 2004-05-21 15:44 |只看该作者

linux下用c写一个sniffer程序,这是源代码,编译通过运行有问题

不熟就先在windows下面调试过了在上linux!希望是个好的建议。呵呵

论坛徽章:
0
4 [报告]
发表于 2004-05-21 17:00 |只看该作者

linux下用c写一个sniffer程序,这是源代码,编译通过运行有问题

就2个困难
显示数据和
混杂模式不能截取其他机器数据包

怎么解决,没人做过吗?

论坛徽章:
0
5 [报告]
发表于 2004-05-22 10:51 |只看该作者

linux下用c写一个sniffer程序,这是源代码,编译通过运行有问题

请帮忙,谢谢

论坛徽章:
0
6 [报告]
发表于 2004-05-25 21:03 |只看该作者

linux下用c写一个sniffer程序,这是源代码,编译通过运行有问题

就2个困难
显示数据和
混杂模式不能截取其他机器数据包
新新手 该用户已被删除
7 [报告]
发表于 2004-05-26 10:29 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
8 [报告]
发表于 2004-06-03 20:19 |只看该作者

linux下用c写一个sniffer程序,这是源代码,编译通过运行有问题

就是hub的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP