zubator Гость
|
Добавлено: Пт Ноя 26 2004 14:10 Заголовок сообщения: сниффер под линукс |
|
|
тут решил посмотреть как вообще работает сниффер и по примеру стаьи с opennet.ru написал свой, только малость измененный, только вот в чем проблема, когда я перевожу сетевую карту в неразборчивый режим, он у меня всеравно ловит только мои пакеты, почему? вот мой код
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <asm/types.h>
#include <string.h>
#define ETH_FRAME_LEN 1514
#define ETH_HLEN 14
static volatile int done;
void handler(int signum)
{
done = 1;
}
int main(int argc, char **argv)
{
__u8 buff[ETH_FRAME_LEN];
struct ifreq ifr;
int s, n;
struct iphdr ip;
struct in_addr src;
struct tcphdr tcp;
char dest[INET_ADDRSTRLEN];
if (argc < 2)
{
fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
return 1;
}
s = socket(PF_INET, SOCK_PACKET, htons(0x0003));
if (s == -1)
{
perror("socket");
return 1;
}
strcpy(ifr.ifr_name, argv[1]);
if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
{
perror("ioctl(SIOCGIFFLAGS)");
return 1;
}
ifr.ifr_flags |= IFF_PROMISC;
if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
{
perror("ioctl(SIOCGIFFLAGS)");
return 1;
}
signal(SIGINT, handler);
fflush(stdout);
for (done = 0; !done; )
{
n = read(s, buff, sizeof(buff));
if ( n!=-1 )
{
memcpy((void *)&ip, buff + ETH_HLEN, sizeof(struct iphdr));
memcpy((void *)&tcp, buff + ETH_HLEN + ip.ihl * 4, sizeof(struct cphdr));
if(ip.protocol == IPPROTO_TCP)
{
if (strcmp(inet_ntoa(ip.saddr),"192.168.104.26") != 0)
{
printf("%s (%d)\t->\t",inet_ntoa(ip.saddr), ntohs(tcp.source));
printf("%s (%d)\n",inet_ntoa(ip.daddr), ntohs(tcp.dest));
}
}
}
}
ifr.ifr_flags &= ~IFF_PROMISC;
if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
{
perror("ioctl(SIOCGIFFLAGS)");
return 1;
}
close(s);
return 0;
} |
|