1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/ethernet.h>
#include <errno.h>
#include <stdio.h>
#include "hexadump.h"
#define SOCKET_ERROR -1
struct packet_t {
struct ethhdr eth; /* entete d une tram ethernet voir linux/if_ether.h */
//struct ether_header eth;
struct iphdr ip; /* entete d un datagramme ip voir linux/ip.h */
struct tcphdr tcp; /* entete d un paquet tcp voir linux/tcp.h */
char * data; /* données */
};
int main() {
int sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
//int sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL));
if ( sock == SOCKET_ERROR ) {
perror("Socket error");
return -1;
}
struct packet_t buffer;
struct ethhdr *eth_ptr;
//struct ether_header *eth_ptr;
struct iphdr *ip_ptr;
struct tcphdr *tcp_ptr;
unsigned char *src, *dst;
int octets_recus;
eth_ptr = &buffer.eth;
ip_ptr = (struct iphdr *)(((unsigned long)&buffer.ip)-2);
tcp_ptr = (struct tcphdr *)(((unsigned long)&buffer.tcp)-2);
src = (unsigned char *)&(ip_ptr->saddr);
dst = (unsigned char *)&(ip_ptr->daddr);
//while (1) {
printf("Wait...\n");
octets_recus = read(sock, (struct packet_t*)&buffer, sizeof(struct packet_t));
hexdump(&buffer, octets_recus);
printf("===> Packet intercepted, %d bytes sniffed <===\n", (octets_recus));
printf("Ethernet - proto ::: ");
if (eth_ptr->h_proto == ETHERTYPE_IP) {
printf("IP\n");
} else if (eth_ptr->h_proto == ETHERTYPE_ARP) { //ether_type avec ether_header
printf("ARP\n");
}
printf("other (%d)\n", eth_ptr->h_proto);
printf("IP version ::: %d\n", ip_ptr->version);
printf("IP proto ::: %d\n", ip_ptr->protocol); //protocole utilisé ? (TCP, UDP...)
printf("Adress ::: %u.%u.%u.%u -------> %u.%u.%u.%u\n", src[0], src[1], src[2], src[3], dst[0], dst[1], dst[2], dst[3]);
printf("Port ::: %d -------> %d\n",ntohs(tcp_ptr->source), ntohs(tcp_ptr->dest));
printf("Flags ::: SYN=%d | ACK=%d | RST=%d | FIN=%d\n", tcp_ptr->syn, tcp_ptr->ack, tcp_ptr->rst, tcp_ptr->fin);
printf("\n\n");
//}
return 0;
} |
Partager