Unklare Ausgabe
-
Hallo !
Ich habe folgendes Programm geschrieben, das Pakete snifft:#include <sys/types.h> //bind #include <sys/socket.h> //bind #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/ip.h> #include <netinet/tcp.h> #include <netinet/ether.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main( int argc , char** argv ) { int packetsize = sizeof( struct ether_addr ) + sizeof( struct iphdr ) + sizeof( struct tcphdr); char* packet = malloc(packetsize); struct ether_addr* eth = (struct ether_addr*) packet; struct iphdr* ip = (struct iphdr*) (packet + sizeof( struct ether_addr ) ); struct tcphdr* tcp = (struct tcphdr*) (packet + sizeof( struct ether_addr) + sizeof( struct iphdr ) ); int uid = getuid(); if( uid != 0 ) { printf("Deine UID ist %d. Bitte als root anmelden.\n",uid); exit(1); } int socket_fd = socket( AF_INET , SOCK_PACKET , htons(0x3) ); if( socket_fd < 0 ) { printf("Erstellen des Sockets fehlgeschlagen.\n"); exit(1); } while(1) { recv( socket_fd , packet , packetsize , 0 ); printf("Ethernetaddresse: %s\n" , ether_ntoa( eth ) ); printf("%s --> %s \n", inet_ntoa( * (struct in_addr*) &ip->saddr ) , inet_ntoa( * (struct in_addr*) &ip->daddr ) ); } }
Das Programm funktioniert. Allerdings verstehe ich die Ausgabe nicht:
Ausgabe(Auszug)linux:/home/toom/Eigene_Dateien/Programmieren/netzwerk_programmierung # ./server Ethernetaddresse: 0:c:f1:35:c2:53 36.32.64.0 --> 36.32.64.0 Ethernetaddresse: 0:4:e:b6:b6:c5 135.13.64.0 --> 135.13.64.0 Ethernetaddresse: 0:c:f1:35:c2:53 36.33.64.0 --> 36.33.64.0 Ethernetaddresse: 0:4:e:b6:b6:c5 135.15.64.0 --> 135.15.64.0 Ethernetaddresse: 0:c:f1:35:c2:53 36.34.64.0 --> 36.34.64.0 Ethernetaddresse: 0:4:e:b6:b6:c5 135.17.64.0 --> 135.17.64.0
Die Probleme sind folgende:
- Warum bekomme ich unterschiedliche MAC Adressen angezeigt ? Die meiner WLAN Karte lautet 0:c:f1:35:c2:53 und bekam die IP 192.168.178.2 vom Router zugewiesen.
- Wieso sind Quell~ und Zieladresse der IP Paket identisch ? Müßte da nicht immer meine 192.168,178,2 Adresse auftauchen ?
-
ich glaub einmal ist es deine mac und einmal die von dem die packete kommen?
wieso die jetzt identisch weiss ich auch netmfg blan
-
ändy schrieb:
- Wieso sind Quell~ und Zieladresse der IP Paket identisch ?
Das wüsste ich auch gerne. Hab mal sowas in der Art gemacht, und die uell war AUCH identisch mit der Zieladresse.
-
Es wird sogar noch komischer. Jetzt habe ich mal allen Netzverkehr abgeschaltet und in der Konsole ein "ping localhost" aufgerufen. Dann erhalte ich folgende Ausgabe:
linux:/home/toom/Eigene_Dateien/Programmieren/netzwerk_programmierung # ./server Ethernetaddresse: 0:0:0:0:0:0 0.3.64.0 --> 0.3.64.0 Ethernetaddresse: 0:0:0:0:0:0 0.3.64.0 --> 0.3.64.0 Ethernetaddresse: 0:0:0:0:0:0 40.194.0.0 --> 40.194.0.0 Ethernetaddresse: 0:0:0:0:0:0 40.194.0.0 --> 40.194.0.0
Ich habe folgende Strukturen verwenden. Ausschnitt aus /usr/include/netinet/ip.h
struct iphdr { #if __BYTE_ORDER == __LITTLE_ENDIAN unsigned int ihl:4; unsigned int version:4; #elif __BYTE_ORDER == __BIG_ENDIAN unsigned int version:4; unsigned int ihl:4; #else # error "Please fix <bits/endian.h>" #endif u_int8_t tos; u_int16_t tot_len; u_int16_t id; u_int16_t frag_off; u_int8_t ttl; u_int8_t protocol; u_int16_t check; u_int32_t saddr; u_int32_t daddr; /*The options start here. */ };
und ein anderes Mal:
struct ip { #if __BYTE_ORDER == __LITTLE_ENDIAN unsigned int ip_hl:4; /* header length */ unsigned int ip_v:4; /* version */ #endif #if __BYTE_ORDER == __BIG_ENDIAN unsigned int ip_v:4; /* version */ unsigned int ip_hl:4; /* header length */ #endif u_int8_t ip_tos; /* type of service */ u_short ip_len; /* total length */ u_short ip_id; /* identification */ u_short ip_off; /* fragment offset field */ #define IP_RF 0x8000 /* reserved fragment flag */ #define IP_DF 0x4000 /* dont fragment flag */ #define IP_MF 0x2000 /* more fragments flag */ #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ u_int8_t ip_ttl; /* time to live */ u_int8_t ip_p; /* protocol */ u_short ip_sum; /* checksum */ struct in_addr ip_src, ip_dst; /* source and dest address */ };
Bei beiden Strukturen erhalte ich blödsinnige Ausgaben. Was mache ich denn falsch ?
-
hab das programm bei mir mal ausprobiert - da kommt echt absoluter müll raus, bisauf die mac-adresse die könnte stimmen. ich setzt mich später nochmal dran...
mfg blan
-
Okay ich hab den Code mal ein bißchen geändert. Jetzt erhalte ich wenigesten unterschiedliche Werte als Quell und Zieladresse. Zwar weiß ich noch nicht wie ich sie als IP-Adressenpunktnotation darstellen kann, aber es scheint schonmal zu funktionieren.
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/ip.h> #include <netinet/ether.h> #include <net/ethernet.h> #include <unistd.h> #include <stdlib.h> #include <netdb.h> #include <netinet/tcp.h> #include <stdio.h> int main( int argc , char** argv ) { int packetsize = sizeof( struct ether_header ) + sizeof( struct iphdr ) + sizeof( struct tcphdr ); char* packet = malloc( packetsize ); struct ether_header* eth = (struct ether_header*) packet; struct iphdr* ip = (struct iphdr*) (packet + sizeof( struct ether_header ) ); struct tcphdr* tcp = (struct tcphdr*) (packet + sizeof( struct ether_header) + sizeof( struct iphdr ) ); int uid = getuid(); if( uid != 0 ) { printf("Deine UID ist %d. Bitte als root anmelden.\n",uid); exit(1); } int socket_fd = socket( AF_INET , SOCK_PACKET , htons(0x3) ); if( socket_fd < 0 ) { printf("Erstellen des Sockets fehlgeschlagen.\n"); exit(1); } while(1) { recv( socket_fd , packet , packetsize, 0 ); printf("%s\n" , ether_ntoa( (struct ether_addr*) eth ) ); printf("%x --> %x\n" , ip->saddr , ip->daddr ); } }
Zu bemerken ist auch, daß ich ether_addr durch ether_header ersetzt habe. Leider habe ich nun noch ein weiteres Problem. Wie kann ich das ganze richtig formatieren. Wenn ich die Ausgabe
printf("%x --> %x\n" , ip->saddr , ip->daddr );
durch folgendes ersetze:
printf("%s --> %s \n", inet_ntoa( * (struct in_addr*) &ip->saddr ) , inet_ntoa( * (struct in_addr*) &ip->daddr ) );
erhalte ich immernoch Unsinniges.
-
Ich erhalte folgende Ausgabe, wenn ich meinen Router anpinge, was eigentlich nicht sein kann.
Ausgabe (Auschnitt):
Ethernet destination MAC address: 0:3:c9:84:af:cd Source: inet_ntoa( 184723648 ) -> 192.168.2.11 Dest: inet_ntoa( 16951488 ) -> 192.168.2.11 Ethernet destination MAC address: 0:c:f1:35:c2:53 Source: inet_ntoa( 16951488 ) -> 192.168.2.1 Dest: inet_ntoa( 184723648 ) -> 192.168.2.1
Irgendwie finde ich das widersprüchlich...
Würde mich freuen, wenn jemand das Programm mal kompilieren könnte und mir mitteilt was er als Ausgabe erhält ich rauf mir hier gleich die Haare aus...Hier nochmal der Quellcode für Copy&Paste
#include <sys/types.h> //bind #include <sys/socket.h> //bind #include <netinet/in.h> #include <arpa/inet.h> #include <netinet/ip.h> #include <netinet/ether.h> #include <net/ethernet.h> #include <unistd.h> #include <stdlib.h> #include <netdb.h> #include <netinet/tcp.h> #include <stdio.h> int main( int argc , char** argv ) { int packetsize = sizeof( struct ether_header ) + sizeof( struct iphdr ) + sizeof( struct tcphdr ); char* packet = malloc( packetsize ); struct ether_header* eth = (struct ether_header*) packet; struct iphdr* ip = (struct iphdr*) (packet + sizeof( struct ether_header ) ); struct tcphdr* tcp = (struct tcphdr*) (packet + sizeof( struct ether_header) + sizeof( struct iphdr ) ); int uid = getuid(); if( uid != 0 ) { printf("Deine UID ist %d. Bitte als root anmelden.\n",uid); exit(1); } int socket_fd = socket( AF_INET , SOCK_PACKET , htons(0x3) ); if( socket_fd < 0 ) { printf("Erstellen des Sockets fehlgeschlagen.\n"); exit(1); } while(1) { recv( socket_fd , packet , packetsize, 0 ); printf("Ethernet destination MAC address: %s\n" , ether_ntoa( (struct ether_addr*) eth ) ); struct in_addr* daddr = malloc( sizeof( struct in_addr ) ); daddr->s_addr =ip->daddr; printf("Source: inet_ntoa( %d ) -> %s\nDest: inet_ntoa( %d ) -> %s\n" , ip->saddr , inet_ntoa( *(struct in_addr*) &ip->saddr ) , ip->daddr , inet_ntoa( *daddr ) ); } }