Programmation-analyse avancée du réseau Linux des paquets de données à chaque couche

Nous avons volé une photo en ligne

Nous utilisons des prises brutes pour vérifier l'authenticité de cette image [je sais que c'est vrai]

#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/ether.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <stdio.h>


static int32_t Debug = 1;

using namespace std;

int32_t main(int32_t argc, const char *argv[])
{
    unsigned char buf[1519]; // 1518 + 1
	int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
	string line(50, '-');
	while(1) {// 解析协议  这些协议的字段我在网络篇都介绍了
        cout << line << endl;
		unsigned char src_mac[18] = "";
		unsigned char dst_mac[18] = "";
		recvfrom(sock_raw_fd, buf, sizeof(buf), 0, NULL, NULL);
		//"%x:%x:%x:%x:%x:%x"
		sprintf((char*)dst_mac, "%02x:%02x:%02x:%02x:%02x:%02x",  // MAC
			buf[0],buf[1],buf[2],buf[3],buf[4],buf[5]);
	
		sprintf((char*)src_mac, "%02x:%02x:%02x:%02x:%02x:%02x",
			buf[6],buf[7],buf[8],buf[9],buf[10],buf[11]);
        
        printf(" MAC : src_mac: %s => dst_mac: %s\n", src_mac, dst_mac);
        if(buf[12] == 0x08 && buf[13] == 0x00){ // IP
            printf("IP packet \n");
            unsigned char src_ip[17] = "";
		    unsigned char dst_ip[17] = "";
            sprintf((char*)src_ip, "%d.%d.%d.%d", buf[26],buf[27],buf[28],buf[29]);
            sprintf((char*)dst_ip, "%d.%d.%d.%d", buf[30],buf[31],buf[32],buf[33]);

             printf(" IP : src_ip: %s => dst_ip: %s\n", src_ip, dst_ip);
            if(buf[23] == 6){
                printf("TCP packet\n");
            }else if(buf[23] == 17){
                printf("UDP packet\n");
            }
            
        }else if(buf[12] == 0x08 && buf[13] == 0x06){
            printf("ARP packet\n");
        }else if(buf[12] == 0x80 && buf[13] == 0x35){
            printf("RARP packet\n");
        }
	    cout << line << endl;
        memset(buf, 0, sizeof(buf));
	}


	return 0;
}

Effet: [Le programme d'exécution doit ajouter sudo (exécuter avec l'autorité racine) ]

Voici les informations d'interaction entre mon shell de terminal distant et la machine virtuelle

Informations Ubuntu dans la machine virtuelle

Informations sur l'hôte local

Vérifié avec succès 

 

Je suppose que tu aimes

Origine blog.csdn.net/qq_44065088/article/details/109272062
conseillé
Classement