Sniffer

De SoHWiki.

Un sniffer, ou analyseur réseau, est un logiciel permettant de récupérer le traffic réseau reçu par une interface. L'intérêt d'une telle manipulation est multiple :

  • Surveillance du traffic pour détecter des problèmes
  • Reverse-engineering d'applications
  • Récupération d'informations


En hacking, c'est souvent cette dernière qui est la plus intéressante. En effet, lorsque vous envoyez des informations sur un réseau, selon le protocole utilisé, il est possible que celles-ci soient transmises en clair. Cela vaut pour vos mots de passe. C'est le cas avec HTTP, FTP, SMTP, POP, etc. :

root@berga-laptop:/home/berga# dsniff 
dsniff: listening on eth0
-----------------
05/29/09 19:16:31 tcp berga-laptop.local.56442 -> ftpperso.free.fr.21 (ftp)
USER spiritofhack
PASS *******

L'outil utilisé ici est dsniff, un sniffer se chargeant de faire remonter uniquement les informations utiles comme les mots de passe. En effet, le traffic qui transite par une interface est important (protocoles de gestion du réseau, acquittements, broadcast, applications multiples, etc). Le filtrage peut donc être une fonctionnalité intéressante, car la quantité d'information remontées par un sniffer "brut" peut vite noyer les quelques paquets intéressants dans la masse. Par exemple, en deux minutes, mon sniffer fait remonter presque 150 paquets en 30 secondes, avec seulement un client IRC, un client msn et un navigateur lancés. Alors imaginez sur un gros réseau :)

root@berga-laptop:/home/berga# tcpdump -i eth0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
19:25:36.233746 IP berga-laptop.local.35312 > irc.minet.net.ircd: P 1579045087:1579045107(20) ack 1965860779 win 1002 <nop,nop,timestamp 3092617 3823715867>
19:25:36.234764 IP berga-laptop.local.53614 > dns1.proxad.net.domain: 14948+ PTR? 78.194.117.194.in-addr.arpa. (45)
[...]
19:26:15.328276 IP by1msg2245413.phx.gbl.msnp > berga-laptop.local.57104: P 332:340(8) ack 6 win 64204 <nop,nop,timestamp 34170255 3102343>
19:26:15.328329 IP berga-laptop.local.57104 > by1msg2245413.phx.gbl.msnp: . ack 340 win 1002 <nop,nop,timestamp 3102390 34170255>
19:26:20.325685 arp who-has berga-laptop.local tell 192.168.0.254
19:26:20.325711 arp reply berga-laptop.local is-at 00:18:f3:f3:5c:43 (oui Unknown)
19:26:21.327819 IP by1msg2245413.phx.gbl.msnp > berga-laptop.local.57104: P 340:651(311) ack 6 win 64204 <nop,nop,timestamp 34170315 3102390>
19:26:21.327886 IP berga-laptop.local.57104 > by1msg2245413.phx.gbl.msnp: . ack 651 win 1002 <nop,nop,timestamp 3103890 34170315>
^C
147 packets captured
147 packets received by filter
0 packets dropped by kernel
root@berga-laptop:/home/berga# 

Le sniffer utilisé ici est tcpdump, un analyseur réseau distribué avec la plupart des distributions UNIX. Très puissant et complet, il est un outil à maitriser absolument.


Un autre analyseur très complet est Wireshark, disponible sous Unix et Windows, qui présente le gros avantage de disposer d'une interface graphique et de nombreuses options de filtrage et d'analyse.


Pour finir, et pour les intéressés, voici un petit sniffer simple utilisant la libpcap :

#include <pcap.h>
#include <stdlib.h>

// Affiche un message d'erreur
void pcap_fatal(const char *failed_in, const char *errbuf) {
	printf("Fatal Error in %s: %s\n", failed_in, errbuf); 
	exit(1);
}

// affiche les informations du paquet en hexadécimal et en texte si possible
void dump(const unsigned char *data_buffer, const unsigned int length) {
	unsigned char byte;
	unsigned int i, j; 
	for(i=0; i < length; i++) {	
		byte = data_buffer[i];	// On récupère chaque octet un par un
		printf("%02x ", data_buffer[i]);  // on affiche l'octet en hexa
		if(((i%16)==15) || (i==length-1)) {
			for(j=0; j < 15-(i%16); j++)	// un espace entre chaque octet
				printf("   ");
			printf("| ");			// Un caractère pour délimiter une ligne tout les 16 octets
			for(j=(i-(i%16)); j <= i; j++) {  // On affiche les caracètres affichales
				byte = data_buffer[j];
				if((byte > 31) && (byte < 127)) // Si le caractètre est pas affichable
					printf("%c", byte);	// On l'affiche
				else				// SInon
					printf(".");		// On affiche un .
			}
			printf("\n");
		}
	}
}

int main() {
	struct pcap_pkthdr header;
	const u_char *packet;
	char errbuf[PCAP_ERRBUF_SIZE];
	char *device;
	pcap_t *pcap_handle;
	int i;
 	device = pcap_lookupdev(errbuf);	// On essaie de récupèrer l'interface
	if(device == NULL)
		pcap_fatal("pcap_lookupdev", errbuf);
	printf("Sniffing on device %s\n", device);
	pcap_handle = pcap_open_live(device, 4096, 1, 0, errbuf);	// On essaie de se mettre en écoute
	if(pcap_handle == NULL)
		pcap_fatal("pcap_open_live", errbuf);
	while(1) {	// On boucle
		packet = pcap_next(pcap_handle, &header);	// On recupere un paquet
		printf("Got a %d byte packet\n", header.len);
		dump(packet, header.len);	// On affiche son contenu
	}
	pcap_close(pcap_handle);
}


berga@berga-laptop:~/$ gcc -o pcap_sniffer pcap_sniffer.c -lpcap
berga@berga-laptop:~/$ su
Mot de passe : 
root@berga-laptop:/home/berga/# pcap_sniffer 
Sniffing on device eth0
Got a 62 byte packet
00 18 f3 f3 5c 43 00 07 cb a4 d2 d8 08 00 45 00 | ....\C........E.
00 28 00 00 40 00 2f 06 37 ce cf b6 83 99 c0 a8 | .(..@./.7.......
00 0a 01 bb dc 62 e3 9a ed f6 00 00 00 00 50 04 | .....b........P.
00 00 ec 2e 00 00 00 00 00 00 00 00 00 00       | ..............
Got a 158 byte packet
00 18 f3 f3 5c 43 00 07 cb a4 d2 d8 08 00 45 00 | ....\C........E.
00 90 f4 ae 40 00 34 06 0c 43 c2 75 c2 4e c0 a8 | ....@.4..C.u.N..
00 0a 1a 0b 99 d4 8b 52 cf 77 5a b1 81 fd 80 18 | .......R.wZ.....
10 00 b4 08 00 00 01 01 08 0a c8 f9 30 1b 00 34 | ............0..4
91 60 3a 47 75 74 65 6b 21 7e 47 75 74 65 6b 40 | .`:Gutek!~Gutek@
73 74 61 66 66 2e 66 7a 20 50 52 49 56 4d 53 47 | staff.fz PRIVMSG
20 23 66 75 74 75 72 65 7a 6f 6e 65 20 3a 6c 65 |  #futurezone :le
73 20 74 79 70 65 73 20 64 65 20 6c 61 20 64 69 | s types de la di
73 74 72 69 62 20 74 72 6f 70 20 61 20 6a 6f 75 | strib trop a jou
72 2c 20 74 6f 75 74 20 63 61 20 3f 0d 0a       | r, tout ca ?..
Outils personnels