我们去网上偷了一张图
我们用 原始套接字 来 验证一下 这个 图的 真伪【我知道 它是真的】
#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;
}
效果:【执行程序一定要加 sudo (root权限执行)】
这里应该是 我 远程终端 shell 与 虚拟机之间的 交互信息
虚拟机中的ubuntu 信息
本地主机的信息
验证成功