first commit

master
Pippo 2017-10-18 20:04:21 +02:00
commit 5772fd26b8
3 changed files with 439 additions and 0 deletions

4
.gitignore vendored 100644
View File

@ -0,0 +1,4 @@
compila
ifdmp
ifdmp.o

416
ifdmp.c 100644
View File

@ -0,0 +1,416 @@
/*
------------------------------------------------------------------------------
File : ifdmp.c
Progetto :
Inizio : xx.11.2004
Fine :
Autore/i : Giorgio D.Plescan
Commento :
------------------------------------------------------------------------------
*/
/*
------------------------------------------------------------------------------
Include
------------------------------------------------------------------------------
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <time.h>
# include <unistd.h>
# include <errno.h>
# include <sys/ioctl.h>
# include <sys/types.h>
# include <sys/time.h>
# include <sys/socket.h>
# include <sys/select.h>
# include <sys/utsname.h>
# include <asm/types.h>
# include <linux/if_packet.h>
# include <linux/if_ether.h>
# include <linux/if_arp.h>
# include <linux/filter.h>
# include <arpa/inet.h>
/*
------------------------------------------------------------------------------
Define
------------------------------------------------------------------------------
*/
# ifndef TRUE
# define TRUE 1
# endif
# ifndef FALSE
# define FALSE 0
# endif
# ifndef MAX
# define MAX(x,y) (((x) > (y))? (x): (y))
# endif
# ifndef MIN
# define MIN(x,y) (((x) < (y))? (x): (y))
# endif
# ifndef ABS
# define ABS(x) (((x) < 0)? -(x): (x))
# endif
/*
---------------------------------------------------------------------------
Funzioni
---------------------------------------------------------------------------
*/
void print_buffer (char * buf, short buflen);
int main (int ac, char ** av);
/*
---------------------------------------------------------------------------
Funzione : print_buffer
Input : char *
Output : short
Commento :
---------------------------------------------------------------------------
*/
void print_buffer (char * buf, short buflen)
{
char
linea [200],
num [5];
short
byte = 0,
i = 0,
j = 0,
k = 0;
if (buf == NULL)
return;
if (buflen == 0)
return;
memset (linea, 0x20, sizeof (linea));
linea [74] = 0;
k = 0;
i = 0;
for (j = 0; j < buflen; j++)
{
if ((j > 0) && ((j % 16) == 0))
{
printf ("%04x %s\n", i, linea);
memset (linea, 0x20, sizeof (linea));
linea [74] = 0;
k = 0;
i += 16;
}
byte = (short)(buf [j] & 0x00ff);
sprintf (num, "%02x ", byte);
linea [k * 3] = num[0];
linea [(k * 3) + 1] = num[1];
linea [(k * 3) + 2] = num[2];
if ((byte > 32) && (byte < 128))
linea [50 + k] = buf [j];
else
linea [50 + k] = '.';
k++;
}
if (k > 0)
printf ("%04x %s\n", i, linea);
else
printf ("\n");
}
/*
---------------------------------------------------------------------------
Funzione : main
Input : int
: char **
Output : int
Commento :
---------------------------------------------------------------------------
*/
int main (int ac, char ** av)
{
char
* AppName = "ifdmp",
* Credits = "LEP FreeSoft by GioDim",
* Version = "ver 1.1",
if_device [20] = "eth1",
if_address [40],
buffer [2000];
int
i = 0,
if_index = -1,
sa_family = -1,
err = 0,
len = 0,
nfds = 0,
ret = -1,
fdsock = -1;
long
packet_num = 0L;
time_t
adesso;
fd_set
in_set;
socklen_t
errlen = sizeof (err);
struct tm
* oggi = NULL;
struct sockaddr_ll
sll;
struct ifreq
ifr;
struct packet_mreq
mr;
//struct sock_filter
// total_insn = BPF_STMT(BPF_RET | BPF_K, 0),
// gio_insn[] = {BPF_STMT(BPF_RET+BPF_K, BPF_LEN)};
//struct sock_fprog
// total_fcode = { 1, &total_insn },
// gio_fcode = { 1, gio_insn };
if (ac > 1)
strcpy (if_device, av[1]);
/* open socket */
if ((fdsock = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) == -1)
{
fprintf (stderr, "%s - socket() : Errno=%1d\n", AppName, errno);
exit (-1);
}
/* get if address */
memset (&ifr, 0x00, sizeof (ifr));
strncpy (ifr.ifr_name, if_device, sizeof (ifr.ifr_name));
if (ioctl (fdsock, SIOCGIFADDR, &ifr) == -1)
{
fprintf (stderr, "%s - ioctl(SIOCGIFADDR) : Errno=%1d\n", AppName, errno);
exit (-1);
}
strncpy (if_address, inet_ntoa (((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr), 15);
/* get if index */
memset (&ifr, 0x00, sizeof (ifr));
strncpy (ifr.ifr_name, if_device, sizeof (ifr.ifr_name));
if (ioctl (fdsock, SIOCGIFINDEX, &ifr) == -1)
{
fprintf (stderr, "%s - ioctl(SIOCGIFINDEX) : Errno=%1d\n", AppName, errno);
exit (-1);
}
if_index = ifr.ifr_ifindex;
/* get if hardware type */
memset (&ifr, 0x00, sizeof (ifr));
strncpy (ifr.ifr_name, if_device, sizeof (ifr.ifr_name));
if (ioctl (fdsock, SIOCGIFHWADDR, &ifr) == -1)
{
fprintf (stderr, "%s - ioctl(SIOCGIFHWADDR) : Errno=%1d\n", AppName, errno);
exit (-1);
}
sa_family = ifr.ifr_hwaddr.sa_family;
/* get if index */
memset (&ifr, 0x00, sizeof (ifr));
strncpy (ifr.ifr_name, if_device, sizeof (ifr.ifr_name));
if (ioctl (fdsock, SIOCGIFINDEX, &ifr) == -1)
{
fprintf (stderr, "%s - ioctl(SIOCGIFINDEX) : Errno=%1d\n", AppName, errno);
exit (-1);
}
if_index = ifr.ifr_ifindex;
/* bind if */
memset (&sll, 0x00, sizeof (sll));
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons (ETH_P_ALL);
sll.sll_ifindex = if_index;
sll.sll_pkttype = PACKET_HOST;
if (bind (fdsock, (struct sockaddr *)&sll, sizeof (sll)) == -1)
{
fprintf (stderr, "%s - bind() : Errno=%1d\n", AppName, errno);
exit (-1);
}
/* get error */
if (getsockopt (fdsock, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1)
{
fprintf (stderr, "%s - getsockopt() : Errno=%1d\n", AppName, errno);
exit (-1);
}
if (err > 0)
{
fprintf (stderr, "%s - bind() : Errno=%1d\n", AppName, errno);
exit (-1);
}
/* set promisc mode */
memset (&mr, 0, sizeof (mr));
mr.mr_ifindex = if_index;
mr.mr_type = PACKET_MR_PROMISC;
if (setsockopt (fdsock, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mr, sizeof (mr)) == -1)
{
fprintf (stderr, "%s - setsockopt() : Errno=%1d\n", AppName, errno);
exit (-1);
}
# ifdef USE_FILTER
/* set filter */
if (setsockopt (fdsock, SOL_SOCKET, SO_ATTACH_FILTER, &total_fcode, sizeof (total_fcode)) == -1)
{
fprintf (stderr, "%s - setsockopt() : Errno=%1d\n", AppName, errno);
exit (-1);
}
if (setsockopt (fdsock, SOL_SOCKET, SO_ATTACH_FILTER, &gio_fcode, sizeof (gio_fcode)) == -1)
{
fprintf (stderr, "%s - setsockopt() : Errno=%1d\n", errno, AppName);
exit (-1);
}
# endif
/* capture loop */
printf ("%s - %s - %s : Listening on %s (%s)\n", AppName, Version, Credits, if_device, if_address);
memset (&in_set, 0x00, sizeof (in_set));
nfds = MAX(nfds,fdsock);
for (;;)
{
FD_ZERO (&in_set);
FD_SET (fdsock, &in_set);
if ((ret = select (nfds + 1, &in_set, NULL, NULL, NULL)) == -1)
{
fprintf (stderr, "%s - select() : Errno=%1d\n", AppName, errno);
exit (-1);
}
adesso = time (NULL);
oggi = localtime (&adesso);
for (i = 0; i < ret; i++)
{
if (FD_ISSET (fdsock, &in_set) == TRUE)
{
FD_CLR (fdsock, &in_set);
memset (buffer, 0x00, sizeof (buffer));
if ((len = recvfrom (fdsock, buffer, sizeof (buffer), MSG_TRUNC, NULL, NULL)) == -1)
{
fprintf (stderr, "%s - recvfrom() : Errno=%1d\n", AppName, errno);
exit (-1);
}
packet_num++;
printf ("\n%4d.%02d.%02d %02d:%02d:%02d - Packet# %4ld - %3d byte(s) on %s (%s)\n",
oggi->tm_year + 1900,
oggi->tm_mon + 1,
oggi->tm_mday,
oggi->tm_hour,
oggi->tm_min,
oggi->tm_sec,
packet_num,
len,
if_device,
if_address);
print_buffer (buffer, len);
}
}
}
return 0;
}

19
makefile 100644
View File

@ -0,0 +1,19 @@
#
#
#
GCC=gcc
RM=rm -rf
CFLAGS=-Wall -c
LDFLAGS= -o
all:ifdmp
ifdmp:ifdmp.o
$(GCC) ifdmp.o $(LDFLAGS) ifdmp
ifdmp.o:ifdmp.c
$(GCC) $(CFLAGS) ifdmp.c
clean:
$(RM) *.o ifdmp