Thread: C program to SNIFF wifi packets without using lpcap

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    2

    Post C program to SNIFF wifi packets without using lpcap

    Hi All,

    I have a requirement to sniff the wifi packets. There are examples available which does the sniffing using pcap functionality. I need a C program which will sniff the wifi packets without using pcap functionality.. Kindly help....

    Code:
    #include<pcap.h>
    #include<stdio.h>
    #include<stdlib.h> // for exit()
    #include<string.h> //for memset
    
    #include<sys/socket.h>
    #include<arpa/inet.h> // for inet_ntoa()
    #include<net/ethernet.h>
    #include<netinet/ip_icmp.h>   //Provides declarations for icmp header
    #include<netinet/udp.h>   //Provides declarations for udp header
    #include<netinet/tcp.h>   //Provides declarations for tcp header
    #include<netinet/ip.h>    //Provides declarations for ip header
    
    
    int main()
    {
        pcap_if_t *alldevsp , *device;
        pcap_t *handle; //Handle of the device that shall be sniffed
        struct bpf_program fp;              /* The compiled filter */
        char filter_exp[] = "((type mgt subtype auth) or (type mgt subtype assoc-req) or (type mgt subtype deauth) )";      /* The filter expression */
        //char filter_exp[] = "tcp";        /* The filter expression */
                    bpf_u_int32 mask;               /* Our netmask */
                    bpf_u_int32 net;                /* Our IP */
    
        char errbuf[100] , *devname , devs[100][100];
        int count = 1 , n;
    
        //First get the list of available devices
        printf("Finding available devices ... ");
        if( pcap_findalldevs( &alldevsp , errbuf) )
        {
            printf("Error finding devices : %s" , errbuf);
            exit(1);
        }
        printf("Done");
    
        //Print the available devices
        printf("\nAvailable Devices are :\n");
        for(device = alldevsp ; device != NULL ; device = device->next)
        {
            printf("%d. %s - %s\n" , count , device->name , device->description);
            if(device->name != NULL)
            {
                strcpy(devs[count] , device->name);
            }
            count++;
        }
    
    
    
        //Ask user which device to sniff
        printf("Enter the number of the device you want to sniff : ");
        scanf("%d" , &n);
        devname = devs[n];
    
        //Open the device for sniffing
        printf("Opening device %s for sniffing ... " , devname);
        handle = pcap_open_live(devname , 65536 , 1 , 0 , errbuf);
    
        if (handle == NULL)
        {
            fprintf(stderr, "Couldn't open device %s : %s\n" , devname , errbuf);
            exit(1);
        }
        printf("Done\n");
    
        logfile=fopen("log.txt","w");
        if(logfile==NULL)
        {
                printf("Unable to create file.");
        }
    #if 1
        /* Compile and apply the filter */
        if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
                fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
                return(2);
        }
        if (pcap_setfilter(handle, &fp) == -1) {
                fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
                return(2);
        }
    #endif
    
        //Put the device in sniff loop
        pcap_loop(handle , -1 , process_packet , NULL);
    
        return 0;
    }

  2. #2
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    That's easy, go read the source for pcap then, it will show you how to do it.
    "A Professor of Computer Science gave a paper on how he uses Linux to teach his undergraduates about operating systems. Someone in the audience asked why use Linux rather than Plan 9?' and the professor answered:Plan 9 looks like it was written by experts; Linux looks like something my students could aspire to write'."

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    It's system dependent and it doesn't really have anything to do with C. On Linux, I have personally taken the following approach.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <linux/if_ether.h>
    
    int main(void)
    {
    	unsigned char buf[1500];
    	int fd, n;
    
    	if ((fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) {
    		fprintf(stderr, "failed to initialise socket\n");
    		return EXIT_FAILURE;
    	}
    	while ((n = read(fd, buf, sizeof buf)) > 0)
    		fwrite(buf, 1, n, stdout);
    	close(fd);
    	return EXIT_FAILURE;
    }
    I have no idea about other operating systems, but usually this sort of thing isn't very well documented ...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps because our little hacker wannabe wants to run this on a machine where he doesn't have admin privileges.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by Salem View Post
    Perhaps because our little hacker wannabe wants to run this on a machine where he doesn't have admin privileges.
    Yeah, that is probably true. I could see scenarios where you might need to do this, but I guess that is not the case here.
    "A Professor of Computer Science gave a paper on how he uses Linux to teach his undergraduates about operating systems. Someone in the audience asked why use Linux rather than Plan 9?' and the professor answered:Plan 9 looks like it was written by experts; Linux looks like something my students could aspire to write'."

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    Please experiment with libpcap and socket, and find out for yourselves where the described security feature is implemented. It is more helpful than making hasty judgements about mahe's character.

  7. #7
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by zyxwvuts View Post
    Please experiment with libpcap and socket, and find out for yourselves where the described security feature is implemented. It is more helpful than making hasty judgements about mahe's character.
    Do you mean to imply that Salem doesn't know what he's talking about? He's been wrong about once or twice since I joined in 2004, and on each of those occasions pigs flew.

    I have written similar code with Berkley Packet Filter on at least one occasion to debug a bunch of networked nodes, that is why I said that I could see scenarios where it might be a valid question. But I think Salem is right though, this is probably a cracker wannabe.
    "A Professor of Computer Science gave a paper on how he uses Linux to teach his undergraduates about operating systems. Someone in the audience asked why use Linux rather than Plan 9?' and the professor answered:Plan 9 looks like it was written by experts; Linux looks like something my students could aspire to write'."

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Jimmy View Post
    on each of those occasions pigs flew.
    Finally visible proof of God, did you get any pictures

  9. #9
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by awsdert View Post
    Finally visible proof of God, did you get any pictures
    With that reasoning, wouldn't that mean that Salem is God?

    That would indeed be even more reason to listen to what Salem says I guess.
    "A Professor of Computer Science gave a paper on how he uses Linux to teach his undergraduates about operating systems. Someone in the audience asked why use Linux rather than Plan 9?' and the professor answered:Plan 9 looks like it was written by experts; Linux looks like something my students could aspire to write'."

  10. #10
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    no the pigs flying would not be salems doing but god's doing to make a point.
    But while we're on the topic of God I'll make a quick point that caused me to find my faith (hopefully others too):

    Existence is infinite in all directions, just as eternal time (not the relative time we perceive) is infinite in the only 2 directions it can go, does it not stand to reason then that the God described by the Holy Bible cannot help but exist and that in turn we should give more serious thought to the words of his son Jesus Christ who now resides on heavens throne where he rightfully belongs:

    None may enter heaven except through him, that whosoever believeth on him shall have everlasting life.

    There is plenty more of what he said that should be paid attention to but that is the most important, I'll look up the exact sentence later for quoting.

  11. #11
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    I don't care for mahe's intentions, or Salem's assumptions about them, as I post to talk about interesting things like programming. :-p

    What I have found is that the call to socket fails whenever I run the program as an unprivileged user. Did you have similar results?

  12. #12
    Registered User
    Join Date
    Feb 2015
    Posts
    2
    Quote Originally Posted by zyxwvuts View Post
    It's system dependent and it doesn't really have anything to do with C. On Linux, I have personally taken the following approach.
    Code:
    #include  #include  #include  #include  #include  #include  #include   int main(void) { 	unsigned char buf[1500]; 	int fd, n;  	if ((fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) { 		fprintf(stderr, "failed to initialise socket\n"); 		return EXIT_FAILURE; 	} 	while ((n = read(fd, buf, sizeof buf)) > 0) 		fwrite(buf, 1, n, stdout); 	close(fd); 	return EXIT_FAILURE; }
    I have no idea about other operating systems, but usually this sort of thing isn't very well documented ...
    Thanks for your reply... Are you using raw socket to get the packets from wireless interface?? My requriement is to capture the packets from wlan0 interface. How to do it via raw socket?? Can you help me??

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by awsdert View Post
    Existence is infinite in all directions, just as eternal time (not the relative time we perceive) is infinite in the only 2 directions it can go, does it not stand to reason...
    I'd be careful of drawing conclusions from such an obviously flawed premise.

    Quote Originally Posted by awsdert View Post
    There is plenty more of what he said that should be paid attention to but that is the most important, I'll look up the exact sentence later for quoting.
    Let's just keep it to programming, shall we?

  14. #14
    Registered User
    Join Date
    Mar 2015
    Posts
    1
    Quote Originally Posted by mahe2789 View Post
    Thanks for your reply... Are you using raw socket to get the packets from wireless interface?? My requriement is to capture the packets from wlan0 interface. How to do it via raw socket?? Can you help me??
    mahe , you want to capture packets in the network you are connected? i mean with wlan0 interface you can only capture packets and filter received to you!! ..else use you want to be in monitor mode !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wifi boost....!!!!
    By bablu1988 in forum Tech Board
    Replies: 2
    Last Post: 08-14-2009, 09:37 AM
  2. cannot find -lpcap collect2: ld returned 1 exit
    By nasim751 in forum C Programming
    Replies: 0
    Last Post: 02-12-2008, 12:37 AM
  3. I love WiFi =)
    By Hunter2 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-14-2006, 03:27 PM
  4. Cell Phones Sniff out Rotten Scents
    By abyssphobia in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-21-2004, 02:39 PM
  5. ah WiFi
    By axon in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-13-2003, 03:55 PM