网络安全学习笔记(5)

本文是网络安全学习笔记的一部分,主要介绍如何在Linux环境中使用libpcap库进行网络抓包操作。通过libpcap,可以捕获并分析网络流量,深入理解网络通信细节。
摘要由CSDN通过智能技术生成

前面对网络分析抓包的一些知识进行了学习,现在对这一阶段的最后成果进行展示,下面是Linux中,利用libpcap实现抓包操作,以及分析:

#include "pcap.h"
#include <stdio.h>
#include <netinet/in.h>
#include <stdlib.h>
void ethernet_protocol_packet_callback(u_char *argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content);
void ip_protool_packet_callback(u_char *argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content);
void tcp_protool_packet_callback(u_char *argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content);
void udp_protool_packet_callback(u_char *argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content);
 unsigned int persist_ssl_hashKeyConvert(char *pUserInput, unsigned char *pKeyArray);
//以太网协议头
struct ether_header
{
	u_int8_t ether_dhost[6]; //目的Mac地址
	u_int8_t ether_shost[6]; //源Mac地址
	u_int16_t ether_type;    //协议类型
};
 
//IPv4协议头
struct ip_header
{
#if defined(WORDS_BIENDIAN)
	u_int8_t    ip_version:4,ip_header_length:4;
#else
	u_int8_t    ip_header_length:4,ip_version:4;
#endif
	u_int8_t    ip_tos;
	u_int16_t   ip_length;
	u_int16_t   ip_id;
	u_int16_t   ip_off;
	u_int8_t    ip_ttl;
	u_int8_t    ip_protocol;
	u_int16_t   ip_checksum;
	struct in_addr ip_souce_address;
	struct in_addr ip_destination_address;
};
 
//UDP协议头
struct udphdr
{
	u_int16_t source_port; /*源地址端口*/
	u_int16_t dest_port;    /*目的地址端口*/
	u_int16_t len;     /*UDP长度*/
	u_int16_t check;   /*UDP校验和*/
}; 
 
//TCP协议头
#define __LITTLE_ENDIAN_BITFIELD
struct tcphdr 
{
	u_int16_t   source_port;         /*源地址端口*/
	u_int16_t   dest_port;           /*目的地址端口*/
	u_int32_t   seq;            /*序列号*/
	u_int32_t   ack_seq;        /*确认序列号*/
#if defined(__LITTLE_ENDIAN_BITFIELD)
	u_int16_t res1:4,   /*保留*/
	doff:4,				/*偏移*/
	fin:1,              /*关闭连接标志*/
	syn:1,              /*请求连接标志*/
	rst:1,              /*重置连接标志*/
	psh:1,              /*接收方尽快将数据放到应用层标志*/
	ack:1,              /*确认序号标志*/
	urg:1,              /*紧急指针标志*/
	ece:1,              /*拥塞标志位*/
	cwr:1;              /*拥塞标志位*/
#elif defined(__BIG_ENDIAN_BITFIELD)
	u_int16_t doff:4,   /*偏移*/
	res1:4,             /*保留*/
	cwr:1,              /*拥塞标志位*/
	ece:1,              /*拥塞标志位*/
	urg:1,              /*紧急指针标志*/
	ack:1,              /*确认序号标志*/
	psh:1,              
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值