<?xml version="1.0"  encoding="ISO-8859-15" ?>
<rss version="2.0">
  <channel>
    <title></title>
    <link>http://www.thehackademy.net</link>
    <description></description>
    <language>fr-fr</language>
    <pubDate>02:02 09:08:10 CEST</pubDate>
    <lastBuildDate>02:02 09:08:10 CEST</lastBuildDate>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>phpSlash 0.8</generator>
    <managingEditor>crashfr@thehackademy.net</managingEditor>
    <webMaster>crashfr@thehackademy.net</webMaster>

    <item>
      <title>Coding sniffer rules</title>
      <link>http://www.thehackademy.net/index.php?story_id=137</link>
      <description>HZVSNIFF


We are going to talk
about “how to make your own sniffer ?”. At first, i have to tell
you that this paper is more a development guide which should help you
to make your own program, and to understand sniffing concepts in a
raw level mode. 



So this paper will only
be usefull to people who already something about C programming, as
well as in networking. 





What is a sniffer ?


It is a tool which can be
used to read all network packets going through your network card. The
aim is originally to detect some problems which may occurs on a lan /
wan. But another way to use such a program is to be able to read all
packets' content after having hijacking them on your computer... For
what ? In order to intercept some logins/passwords ...  We don't
explain in this article how to hijack a connection, but only how to
read and decode all packets going through your network cards. 



Moreover, if you are on a
broadcast network (meaning that each packet are sent to every
computers on the Lan), it won't be necessary to set up an hijacking
attack : all packets from the network are received on your network
card. But how make them “accepted” by your card, and how to read
them ?




Promiscious mode:


Let's talk about the
default communication process between two computers : the first one
send through its network card  one packet, which contains MAC address
of the target. A system just takes care about packets its MAC address
as the destination, and drops all others. So on a broadcasted
network, all computers receive all sent packets, and drop all those
for which they are not the destination. Promiscious allow to escape
that process in order to catch every packets, whatever MAC
destination is. 



You really want to use a
library to develop the sniffer to don't deal directly with the
network raw level. The libpcap is designed to used in this way, to
develop your soft, and is moreover present onto Unix and Windows.
Maybe you already know tcpdump or ethereal, and guess what : both of
them have been developed onto it. 



First, you have to
install this lib :


$&amp;gt; tar xvzf
libpcap-0.6.2.tar.gz
.......
$&amp;gt; cd libpcap-0.6.2
$&amp;gt; ./configure
$&amp;gt; make
$&amp;gt; make install


You won't forget to
include the good header in your sources : 

#include 


Have a look on the
development manpages


$&amp;gt; man libpcap






Lets now have a look on
APIs' syntax :


pcap_open_live


It is tha main function,
used to set up one sniffing session by using that lib. It returns a
file descriptor you can use to read intercepted packets, to create
and apply some filters ... Of course, you have to call this function
!!




pcap_t
*pcap_open_live(char *device, int snaplen, int promisc, int to_ms,
char *ebuf)


Char *device : Network
interface you used to intercept data. You have to give its character
string representation (eth0, eth1, wlan0 ...)


int snaplen : Max size of
information which will be read in one packet. In general, you can use
1500 bytes on an ethernet Lan. 



int promisc: do you want
to enable the promiscious mode (1), or not (0) ... You probably want
to use it. 



Int to_ms: timeout


char *ebuf : In case of
errors, they are returned onto that address. On success case, NULL is
returned.




You should know that the
file descriptor is given as an argument of the pcap_t structure,
returned by the main API pcap_open_live. 





pcap_next :


This function is used to
return address of each intercepted packet written in a queue. Each
returned pointer points to an usigned character string, which are one
captured packets in the queue. 





u_char *pcap_next(pcap_t
*p, struct pcap_pkthdr *h)


pcap_t *p : File
descriptor returned by pcap_open_live.


pcap_pkhdr *p :  pointer
on to pcap_pkhdr structure, into which headers of received packets
are formated to be read. You find it in the pcap.h file.




pcap_lookupnet :


This function returns the
network's address and subnet.


int pcap_lookupnet(char
*device,bpf_u_int32 *netp,bpf_u_int32 *maskp, char *errbuf);


Char *device : Network
interface used to capture traffic.


bpf_u_int32 *netp :
Address where is saved the network address


bpf_u_int32 *maskp :
Address where is saved the sbnet address


char *errbuf: Address
where are returned errors.




pcap_stats :


Get some statistics on
received packets. These informations are saved, and formated to be
read in a pcap_stat structure.




int pcap_stats(pcap_t *p,
struct pcap_stat *ps)


pcap_t *p : file
desciptor return by pcap_open_live 

struct pcap_stat *ps :
Target where are wrote statistics about traffic 





struct pcap_stat {
u_int ps_recv;   //
Number of received packet
u_int ps_drop; // Number
of dropped packet
u_int ps_ifdrop; // Not
yet supported
};




pcap_lookupdev :


Return default network
interface.




u_char
pcap_lookupdev(char *errbuf)


Char *errbuf :  Address
where are return errors, NULL otherwise.




We have shown all
functions you have to use in your sniffer, and to make it work well.
But, you will find lots of more functions in development manpages.






-----------------------------------------------start---------------------------------------------------
#include  /* Some
basic libraries */
#include 
int main(void)
{
 int
i,a=0,nbrpaquets; 


/* variable (counter)
i as a counter on
received packets
a as the number of
received packets
nbrpaquets number of
packets already captured
*/


char interface[10]; 

/* Network interface used
to sniff */
bpf_u_int32 netp,maskp; 

/* Network and subnet
addresses  */


int affichage=0; 

/* Define the desired
formatting to display captured packets */


char
erreur[PCAP_ERRBUF_SIZE]; 

/* Buffer use to write
errors, max sie defined in pcap.h as the macro  PCAP_ERRBUF_SIZE  */


pcap_t *descriptPaquet =
NULL; 

/* File descriptor
returned by pcap_open_live */
struct pcap_stat
*statistiques; 

/* Statistics ' structure
*/


struct pcap_pkthdr
paquethdr;
 /* Header structure used
by pcap_next. */


u_char *paquet; 

/* content of captured
packets */
statistiques = (struct
pcap_stat*)malloc(sizeof(struct pcap_stat)); 

/* Memory allocation to
write statistic structure */


printf(&amp;quot;\n\n\n-+-+-+-+-+-+-+-+HZVSniff+-+-+-+-+-+-+-+-\n&amp;quot;);



printf(&amp;quot; CoDeD By
ReDiLs For HZVManual\n\n&amp;quot;);
printf(&amp;quot;Interface to
sniff (default :0) &amp;quot;);
scanf(&amp;quot;%10s&amp;quot;,interface);
/* we get a character
string representation of the used to sniff interface*/
printf(&amp;quot;\nHow many
packets to intercept: &amp;quot;);
scanf(&amp;quot;%d&amp;quot;,&amp;amp;nbrpaquets);

/* We get number of
packets to sniff.*/


 printf(&amp;quot;\nChoisissez
le type d'affichage des données :\n&amp;quot;);
 printf(&amp;quot; 1 -&amp;gt;
Display : Characters  Mode 

 printf(&amp;quot; 2-&amp;gt;
Display hexa mode\n&amp;quot;);
 scanf(&amp;quot;%d&amp;quot;,&amp;amp;affichage);
 while((affichage!=1)
&amp;amp;&amp;amp; (affichage!=2)) 
 {
 printf(&amp;quot;Choose
1 oo 2 \n&amp;quot;);
 scanf(&amp;quot;%d&amp;quot;,&amp;amp;affichage);
 }
&amp;nbsp;
 if(strcmp(interface,&amp;quot;0&amp;quot;)==0)strcpy(interface,pcap_lookupdev(erreur)); 
/* We check if user want
to call pcap_lookupdev to find default network interface*/


 if ((descriptPaquet
= pcap_open_live(interface, 1500, 0, 1000, erreur))==NULL) 
/* File descriptor
allocation*/
 {
 printf(&amp;quot;Erreur
: %s\n&amp;quot;,erreur); 
/* If errors, we display
them*/
 exit(1);
 }


 pcap_lookupnet(interface,&amp;amp;netp,&amp;amp;maskp,erreur); 
/* We get network's
address and subnet */


 printf(&amp;quot;\nNetwork
: %x\n&amp;quot;,netp); 
/* We display network in
hexa mode.*/




 printf(&amp;quot;\nMask
: %x\n&amp;quot;,maskp); 
/* Display Mask in hexa
mode*/
 pcap_stats(descriptPaquet,
statistiques); 
/* We call the
statistics' function*/
 printf(&amp;quot;Displaying
network traffic : %s\n&amp;quot;,interface);
 while
(a!=nbrpaquets) 
/* Until desired number
of captured packet is not done, we get them */
 {
  paquet =
(u_char *) pcap_next(descriptPaquet, &amp;amp;paquethdr); 
/* We get
packets'content*/
 if (paquet !=
NULL) 
/* If content is not emty
(some times, it may be), we don't display it.*/
 {
  for (i=0;
i&amp;lt;500; i++) 
/* We display the first
500 bytes of packets*/
  {
  if(affichage==1)printf(&amp;quot;%c&amp;quot;,*paquet); 
/* Which format to
display output ?*/
  else
  paquet++;
  }
  printf(&amp;quot;\n*********************************************************************\n&amp;quot;);
 }
 ++;
 }
 pcap_stats(descriptPaquet,
statistiques); 


 printf(&amp;quot;Statistis
:\n&amp;quot;);
 printf(&amp;quot;Received
packets : %d\n&amp;quot;,statistiques-&amp;gt;ps_recv); 
 printf(&amp;quot;Dropped
packets : %d\n&amp;quot;,statistiques-&amp;gt;ps_drop); 
}
--------------------------------------end---------------------------------------------------------




To compile your code : 



gcc -lpcap -o hzvsniff
hzvsniff.c</description>
      <pubDate>Friday September  9, 2005 07:42PM</pubDate>
      <guid>http://www.thehackademy.net/index.php?story_id=137</guid>
    </item>

  </channel>
</rss>

