原始套接字IP报文嗅探

一个简单的Sniffer程序,可以用来捕获和打印接收到的IP数据包。

实现多IP报文、ARP、TCP和UDP的简单打印,

IP报文0800

ARP报文0806

TCP:6

UDP:17

ICMP:1

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/if_ether.h>
#include <linux/in.h>
//#include <arpa/inet.h>
#define BUFFER_MAX 2048

struct my_ethhdr {
    unsigned char h_dest[ETH_ALEN];
    unsigned char h_source[ETH_ALEN];
    unsigned short h_proto;
};

struct my_arphdr {
    unsigned short ar_hrd;
    unsigned short ar_pro;
    unsigned char ar_hln;
    unsigned char ar_pln;
    unsigned short ar_op;
    unsigned char ar_sha[ETH_ALEN];
    unsigned char ar_sip[4];
    unsigned char ar_tha[ETH_ALEN];
    unsigned char ar_tip[4];
};

struct my_iphdr {
    unsigned char ihl:4, version:4;
    unsigned char tos;
    unsigned short tot_len;
    unsigned short id;
    unsigned short frag_off;
    unsigned char ttl;
    unsigned char protocol;
    unsigned short check;
    unsigned int saddr;
    unsigned int daddr;
};

struct my_tcphdr {
    unsigned short source;
    unsigned short dest;
    unsigned int seq;
    unsigned int ack_seq;
    unsigned short res1:4, doff:4, fin:1, syn:1, rst:1, psh:1, ack:1, urg:1, res2:2;
    unsigned short window;
    unsigned short check;
    unsigned short urg_ptr;
};

struct my_udphdr {
    unsigned short source;
    unsigned short dest;
    unsigned short len;
    unsigned short check;
};

struct my_icmphdr {
    uint8_t type;
    uint8_t code;
    uint16_t checksum;
    uint32_t data;
};

void uint32_ip_2_str_ip(uint32_t ip,char *str_ip) {
    unsigned char bytes[4];
    bytes[0] = (ip >> 24) & 0xFF;
    bytes[1] = (ip >> 16) & 0xFF;
    bytes[2] = (ip >> 8) & 0xFF;
    bytes[3] = ip & 0xFF;
    sprintf(str_ip,"%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
}

int main(int argc, char *argv[])
{

	int sock, n_read, proto;        
	char buffer[BUFFER_MAX];
	struct my_ethhdr *ethhead;
	struct my_iphdr *iphead;
	struct my_tcphdr *tcphead;
	struct my_udphdr *udphead;
	struct my_icmphdr *icmphead;
	char *p;

	if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0)
	{
		fprintf(stdout, "create socket error/n");
		exit(0);
	}

	while(1) 
	{
		n_read = recvfrom(sock, buffer, 2048, 0, NULL, NULL);

		if(n_read < 46) 
		{
			fprintf(stdout, "以太网帧数据长度最小为 46 字节\n");
			continue;
		}

		ethhead = (struct my_ethhdr *)buffer;
		int n = 0XFF;
		printf("MAC: %.2X:%02X:%02X:%02X:%02X:%02X==>""[%x]""==>"
				"%.2X:%.2X:%.2X:%.2X:%.2X:%.2X\n",
				ethhead->h_dest[0], ethhead->h_dest[1], ethhead->h_dest[2], ethhead->h_dest[3], ethhead->h_dest[4], ethhead->h_dest[5],ntohs(ethhead->h_proto),
				ethhead->h_source[0], ethhead->h_source[1], ethhead->h_source[2], ethhead->h_source[3], ethhead->h_source[4], ethhead->h_source[5]);

		iphead = (struct my_iphdr *)(buffer + 14);  
		char sip[32] = {};
		char dip[32] = {};
 		uint32_ip_2_str_ip(ntohl(iphead->saddr),sip); 
 		uint32_ip_2_str_ip(ntohl(iphead->daddr),dip); 
		printf("IP: %s ==[%d]=> %s\n",sip,iphead->protocol,dip);

	}
} 

使用root权限运行
~/下载/$ sudo ./a.out 
MAC: FF:FF:FF:FF:FF:FF==>[800]==>88:34:c1:b4:a1:23
IP: 192.168.10.155 ==[17]=> 192.168.10.255
MAC: 78:ab:be:32:67:1a==>[800]==>c1:34:b4:23:a1:a6
IP: 56.107.67.93 ==[6]=> 192.168.10.14
MAC: 78:ab:be:32:67:1a==>[800]==>c1:34:b4:23:a1:a6
IP: 56.107.67.93 ==[6]=> 192.168.10.14
MAC: 00:00:00:00:00:00==>[800]==>00:00:00:00:00:00
IP: 127.0.0.1 ==[1]=> 127.0.0.1
MAC: 00:00:00:00:00:00==>[800]==>00:00:00:00:00:00
IP: 127.0.0.1 ==[1]=> 127.0.0.1
^C
~/下载/$ 
更多原始套接字参看下面
https://blog.csdn.net/weixin_43288201/article/details/106266418
相关推荐
dengjiayue22 分钟前
tcp 的重传,流量控制,拥塞控制
网络协议·tcp/ip
咕德猫宁丶1 小时前
探秘Xss:原理、类型与防范全解析
java·网络·xss
黑子哥呢?2 小时前
Linux---防火墙端口设置(firewalld)
linux·服务器·网络
hellojackjiang20112 小时前
开源轻量级IM框架MobileIMSDK的鸿蒙NEXT客户端库已发布
网络·即时通讯·im开发·mobileimsdk-鸿蒙端
WebDeveloper20013 小时前
如何使用美国域名中心US Domain Center和WordPress创建商业网站
运维·服务器·css·网络·html
车载诊断技术4 小时前
电子电气架构 --- 什么是EPS?
网络·人工智能·安全·架构·汽车·需求分析
KevinRay_4 小时前
Python超能力:高级技巧让你的代码飞起来
网络·人工智能·python·lambda表达式·列表推导式·python高级技巧
2301_819287125 小时前
ce第六次作业
linux·运维·服务器·网络
CIb0la5 小时前
GitLab 停止为中国区用户提供 GitLab.com 账号服务
运维·网络·程序人生
Black_mario6 小时前
链原生 Web3 AI 网络 Chainbase 推出 AVS 主网, 拓展 EigenLayer AVS 应用场景
网络·人工智能·web3