Simple sniffer program in Linux

Source: Internet
Author: User

/***************************************
* Author: Li Yannan
* Time: 2006-02 2
* Function: detects the local network.
****************************************/

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <errno. h>
# Include <unistd. h>
# Include <sys/socket. h>
# Include <sys/types. h>
# Include <Linux/in. h>
# Include <Linux/if_ether.h>
# Include <net/If. h>
# Include <sys/IOCTL. h>
# Define interface "eth0"
Int set_promisc (char * interface, int sock );
Void write_file (File * FP, unsigned char * Buf, int Len );
Int main ()
{
Int sock, size, I, J;
File * FP;
Unsigned char buffer [2048];
Unsigned char * data;
Fp = fopen ("sniffer.txt", "W ");
If (! FP)
{
Printf ("can not open the file/N ");
Return;
}
If (sock = socket (pf_packet, sock_raw, htons (eth_p_ip) <0)
{
Printf ("socket init error/N ");
Return-1;
}

Set_promisc (interface, sock );
While (1)
{
Printf ("-------------------/N ");
Size = recvfrom (sock, buffer, 2048,0, null, null );
Printf ("% d bytes read/N", size );
If (size <14)
{
Printf ("Recv error/N ");
Close (sock );
Return-1;
}
Write_file (FP, buffer, size );
Printf ("the DST Mac is % 02x: % 02x: % 02x: % 02x: % 02x: % 02x/N", buffer [0], buffer [1], buffer [2],
Buffer [3], buffer [4], buffer [5]);
Printf ("the source MAC is % 02x: % 02x: % 02x: % 02x: % 02x: % 02x/N", buffer [6], buffer [7], buffer [8],
Buffer [9], buffer [10], buffer [11]);
Printf ("packet type is % 02x % 02x/N", buffer [12], buffer [13]);
Data = buffer + 14;
Size = size-14;
For (I = 0; I <size; I + = 16)
{
For (j = I; j <I + 16; j ++)
{
If (j <size)
Printf ("% 02x", data [J]);
Else
Break;
}
Printf ("/N ");
}
}
Fclose (FP );
Return 0;
}
// Set the NIC to the hybrid mode so that it can receive packets not sent to the Local Machine
Int set_promisc (char * interface, int sock)
{
Struct ifreq IFR;
Strncpy (IFR. ifr_name, interface, strnlen (Interface) + 1 );
If (IOCTL (sock, siocgifflags, & IFR) =-1 ))
{
Printf ("couldn't retrive flags for the interface/N ");
Exit (0 );
}
Printf ("the interface is: % s/n", interface );
Printf ("retrieved flags from the interface successly/N ");
IFR. ifr_flags | = iff_promisc;
If (IOCTL (sock, siocsifflags, & IFR) =-1)
{
Printf ("cocould not set the promisc flag/N ");
Exit (0 );
}
Printf ("setting interface: % s: To promisc/N", interface );
Return (0 );
}
// Write an object
Void write_file (File * FP, unsigned char * Buf, int Len)
{
Int I;
Int base;
/*
Fp = fopen ("sniffer.txt", "");
If (! FP)
{
Printf ("can not open the file/N ");
Return;
}
*/
Fprintf (FP, "% s: % d/N", "receive packet:", Len );
If (BUF [12] = 0x88 & Buf [13] = 0x63)
{
Fprintf (FP, "% s/n" "The pppoe discovery packet found ");
}
For (base = 0; base <Len; base + = 16)
{
For (I = base; I <base + 16; I ++)
{
If (I <Len)
{
Fprintf (FP, "% 02x", (unsigned) BUF [I]);
}
Else
{
Fprintf (FP ,"");
}
}
Fprintf (FP ,"");
For (I = base; I <base + 16; I ++)
{
If (I <Len)
{
If (isprint (BUF [I])
{
Fprintf (FP, "% C", Buf [I]);
}
Else
{
Fprintf (FP ,".");
}
}
Else {
Break;
}
}
Fprintf (FP, "/N ");
}
Fprintf (FP, "/N ");
// Fclose (FP );
}

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.