Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from a promiscuous network device

I want to write a real-time analysis tool for wireless traffic.

Does anyone know how to read from a promiscuous (or sniffing) device in C?

I know that you need to have root access to do it. I was wondering if anyone knows what functions are necessary to do this. Normal sockets don't seem to make sense here.

like image 490
jbleners Avatar asked Sep 22 '08 13:09

jbleners


People also ask

What happens when a device enters promiscuous mode?

Answer: In a network, promiscuous mode allows a network device to intercept and read each network packet that arrives in its entirety. In an Ethernet local area network ( LAN), promiscuous mode is a mode of operation in which every data packet transmitted can be received and read by a network adapter.

What is used to detect if network in promiscuous mode?

Promiscuous mode and packet sniffing Tcpdump is a free network packet analyzer software that enables users to display Transmission Control Protocol/Internet Protocol (TCP/IP) and other packets being transmitted or received over a network.

Is promiscuous mode security risk?

Typically, promiscuous mode is used and implemented by a snoop program that captures all network traffic visible on all configured network adapters on a system. Because of its ability to access all network traffic on a segment, promiscuous mode is also considered unsafe.

What is interface promiscuous mode?

Promiscuous mode is a type of computer networking operational mode in which all network data packets can be accessed and viewed by all network adapters operating in this mode.


1 Answers

I once had to listen on raw ethernet frames and ended up creating a wrapper for this. By calling the function with the device name, ex eth0 I got a socket in return that was in promiscuous mode. What you need to do is to create a raw socket and then put it into promiscuous mode. Here is how I did it.

int raw_init (const char *device)
{
    struct ifreq ifr;
    int raw_socket;

    memset (&ifr, 0, sizeof (struct ifreq));

    /* Open A Raw Socket */
    if ((raw_socket = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 1)
    {
        printf ("ERROR: Could not open socket, Got #?\n");
        exit (1);
    }

    /* Set the device to use */
    strcpy (ifr.ifr_name, device);

    /* Get the current flags that the device might have */
    if (ioctl (raw_socket, SIOCGIFFLAGS, &ifr) == -1)
    {
        perror ("Error: Could not retrive the flags from the device.\n");
        exit (1);
    }

    /* Set the old flags plus the IFF_PROMISC flag */
    ifr.ifr_flags |= IFF_PROMISC;
    if (ioctl (raw_socket, SIOCSIFFLAGS, &ifr) == -1)
    {
        perror ("Error: Could not set flag IFF_PROMISC");
        exit (1);
    }
    printf ("Entering promiscuous mode\n");

    /* Configure the device */

    if (ioctl (raw_socket, SIOCGIFINDEX, &ifr) < 0)
    {
        perror ("Error: Error getting the device index.\n");
        exit (1);
    }

    return raw_socket;
}

Then when you have your socket you can just use select to handle packets as they arrive.

like image 50
user30684 Avatar answered Oct 04 '22 14:10

user30684