完成第一阶段,读取一个pacp包并解析。
This commit is contained in:
@@ -0,0 +1,41 @@
|
|||||||
|
#ifndef NETSENTRY_CAPTURE_PCAP_READER_H
|
||||||
|
#define NETSENTRY_CAPTURE_PCAP_READER_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <functional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
// libpcap 的 RAII 封装。
|
||||||
|
// 设计要点:
|
||||||
|
// 1. "打开"是重操作且可能失败 -> 构造保持轻量,open() 返回 bool,失败看 lastError()
|
||||||
|
// 2. 资源类标配:禁拷贝(两个对象管同一个 pcap_t* 必然双重释放)
|
||||||
|
// 3. 成员用 void* 而不是 pcap_t*:pcap.h 只允许出现在 .cpp 里,
|
||||||
|
// 这个头文件对外不泄漏 libpcap 的存在(依赖隔离,将来换抓包后端不动公共头)
|
||||||
|
class PcapReader {
|
||||||
|
private:
|
||||||
|
void* handle_ = nullptr;
|
||||||
|
std::string last_error_;
|
||||||
|
public:
|
||||||
|
PcapReader() {}
|
||||||
|
~PcapReader();
|
||||||
|
|
||||||
|
PcapReader(const PcapReader&) = delete;
|
||||||
|
PcapReader& operator=(const PcapReader&) = delete;
|
||||||
|
|
||||||
|
// 每包回调;返回 false 可提前终止遍历(配合规则引擎用)
|
||||||
|
typedef std::function<bool(const uint8_t* data, uint32_t len)> PacketCallback;
|
||||||
|
|
||||||
|
bool open(const std::string& path);
|
||||||
|
void close();
|
||||||
|
|
||||||
|
// 逐包回调。返回 false 表示中途出错,错误说明看 lastError()
|
||||||
|
bool forEachPacket(const PacketCallback& cb);
|
||||||
|
|
||||||
|
const std::string& lastError() const { return last_error_; }
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
|
|
||||||
|
#endif // NETSENTRY_CAPTURE_PCAP_READER_H
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef NETSENTRY_DECODE_BYTEORDER_H
|
||||||
|
#define NETSENTRY_DECODE_BYTEORDER_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
// 网络字节序 = 大端(低地址放高位字节),x86 的内存是小端。
|
||||||
|
// 所以包头字段必须按字节手工组装。禁止把包头指针直接 cast 成 struct 来读,
|
||||||
|
// 原因有两个,缺一个都不行:
|
||||||
|
// 1) 字节序不对:cast 后按主机序(小端)解释,读出来的值是反的
|
||||||
|
// 2) 对齐不安全:包头在缓冲区里是紧贴着的,uint32_t* 要求 4 字节对齐,
|
||||||
|
// 直接 cast 在 ARM 等严格对齐平台上直接崩,x86 上是未定义行为
|
||||||
|
// (顺带一提:readU16BE 与 ntohs、readU32BE 与 ntohl 效果等价,
|
||||||
|
// 我们手写是为了不依赖系统头、并让你看清它到底做了什么)
|
||||||
|
inline uint16_t readU16BE(const uint8_t* p) {
|
||||||
|
return static_cast<uint16_t>(static_cast<uint16_t>(p[0]) << 8 | p[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline uint32_t readU32BE(const uint8_t* p) {
|
||||||
|
return static_cast<uint32_t>(static_cast<uint32_t>(p[0]) << 24 |
|
||||||
|
static_cast<uint32_t>(p[1]) << 16 |
|
||||||
|
static_cast<uint32_t>(p[2]) << 8 |
|
||||||
|
static_cast<uint32_t>(p[3]));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
|
|
||||||
|
#endif // NETSENTRY_DECODE_BYTEORDER_H
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef NETSENTRY_DECODE_ETHERNET_H
|
||||||
|
#define NETSENTRY_DECODE_ETHERNET_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "netsentry/string_slice.h"
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
// 链路层只支持 Ethernet II(14 字节帧头,ethertype >= 0x0600)。
|
||||||
|
// 802.3 LLC/SNAP 在真实流量里极少见,遇到按"不认识的上层协议"处理
|
||||||
|
struct MacAddress {
|
||||||
|
uint8_t octets[6];
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string toString(const MacAddress& mac);
|
||||||
|
|
||||||
|
struct EtherHeader {
|
||||||
|
MacAddress dst;
|
||||||
|
MacAddress src;
|
||||||
|
uint16_t ethertype = 0; // 主机序
|
||||||
|
};
|
||||||
|
|
||||||
|
// 帧进 -> 头出 + 剩余载荷切片出(可能为空)。
|
||||||
|
// 失败返回 false,且不改动 *out 和 *payload(失败路径不产生半成品,测试好写)
|
||||||
|
bool parseEthernet(const StringSlice& frame, EtherHeader* out, StringSlice* payload);
|
||||||
|
|
||||||
|
// 常用 ethertype(主机序),IANA/IEEE 分配
|
||||||
|
const uint16_t kEtherTypeIpv4 = 0x0800;
|
||||||
|
const uint16_t kEtherTypeArp = 0x0806;
|
||||||
|
const uint16_t kEtherTypeIpv6 = 0x86DD;
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
|
|
||||||
|
#endif // NETSENTRY_DECODE_ETHERNET_H
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef NETSENTRY_DECODE_IPV4_H
|
||||||
|
#define NETSENTRY_DECODE_IPV4_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "netsentry/string_slice.h"
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
struct Ipv4Address {
|
||||||
|
uint8_t octets[4];
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string toString(const Ipv4Address& ip);
|
||||||
|
|
||||||
|
struct Ipv4Header {
|
||||||
|
Ipv4Address src; //源IP
|
||||||
|
Ipv4Address dst; //目的IP
|
||||||
|
uint8_t protocol = 0; // 主机序:6=TCP, 17=UDP
|
||||||
|
uint16_t total_length = 0; // 主机序:IP 头 + 载荷总长(声明值)
|
||||||
|
uint8_t ihl_bytes = 0; // 头长(含 options),范围 20~60
|
||||||
|
bool fragmented = false; // MF 置位 或 分片偏移非 0
|
||||||
|
};
|
||||||
|
|
||||||
|
// 协议号(IANA 分配),主机序比较
|
||||||
|
const uint8_t kIpProtoTcp = 6;
|
||||||
|
const uint8_t kIpProtoUdp = 17;
|
||||||
|
|
||||||
|
bool parseIpv4(const StringSlice& data, Ipv4Header* out, StringSlice* payload);
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
|
|
||||||
|
#endif // NETSENTRY_DECODE_IPV4_H
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef NETSENTRY_DECODE_TCP_H
|
||||||
|
#define NETSENTRY_DECODE_TCP_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "netsentry/string_slice.h"
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
struct TcpHeader {
|
||||||
|
uint16_t src_port = 0; // 主机序
|
||||||
|
uint16_t dst_port = 0;
|
||||||
|
uint32_t seq = 0;
|
||||||
|
uint32_t ack = 0;
|
||||||
|
uint8_t data_offset_bytes = 0; // 头长(含 options),范围 20~60
|
||||||
|
uint8_t flags = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 标志位(byte 13 的低 6 位)
|
||||||
|
const uint8_t kTcpFin = 0x01;
|
||||||
|
const uint8_t kTcpSyn = 0x02;
|
||||||
|
const uint8_t kTcpRst = 0x04;
|
||||||
|
const uint8_t kTcpPsh = 0x08;
|
||||||
|
const uint8_t kTcpAck = 0x10;
|
||||||
|
const uint8_t kTcpUrg = 0x20;
|
||||||
|
|
||||||
|
bool parseTcp(const StringSlice& data, TcpHeader* out, StringSlice* payload);
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
|
|
||||||
|
#endif // NETSENTRY_DECODE_TCP_H
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef NETSENTRY_DECODE_UDP_H
|
||||||
|
#define NETSENTRY_DECODE_UDP_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "netsentry/string_slice.h"
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
struct UdpHeader {
|
||||||
|
uint16_t src_port = 0; // 主机序
|
||||||
|
uint16_t dst_port = 0;
|
||||||
|
uint16_t length = 0; // 声明的总长(含 8 字节头),主机序
|
||||||
|
};
|
||||||
|
|
||||||
|
bool parseUdp(const StringSlice& data, UdpHeader* out, StringSlice* payload);
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
|
|
||||||
|
#endif // NETSENTRY_DECODE_UDP_H
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#ifndef NETSENTRY_STRING_SLICE_H
|
||||||
|
#define NETSENTRY_STRING_SLICE_H
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
// C++11 版 string_view:一段"只读、不拥有"的内存视图(指针 + 长度)。
|
||||||
|
// 两条铁律:
|
||||||
|
// 1. slice 自己不分配内存,它指向别人的缓冲区 —— slice 的寿命不能超过被指向的缓冲区
|
||||||
|
// 2. slice() 做了完整边界检查,越界一律给空/截断,绝不会读到缓冲区之外一个字节
|
||||||
|
// 整个解析栈全靠它做"零拷贝":包头解析只在原缓冲上切视图,不 memcpy
|
||||||
|
class StringSlice {
|
||||||
|
private:
|
||||||
|
const char* data_;
|
||||||
|
std::size_t size_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
StringSlice() : data_(nullptr), size_(0) {}
|
||||||
|
StringSlice(const char* data, std::size_t size) : data_(data), size_(size) {}
|
||||||
|
// 便利构造:从 std::string 借视图(s 必须活得比 slice 久,测试里常用)
|
||||||
|
StringSlice(const std::string& s) : data_(s.data()), size_(s.size()) {}
|
||||||
|
|
||||||
|
const char* data() const { return data_; }
|
||||||
|
std::size_t size() const { return size_; }
|
||||||
|
bool empty() const { return size_ == 0; }
|
||||||
|
|
||||||
|
// 取子切片:offset 从本 slice 起点算。
|
||||||
|
// offset 越界 -> 空 slice;len 超长 -> 截断到末尾。不抛异常
|
||||||
|
StringSlice slice(std::size_t offset, std::size_t len) const {
|
||||||
|
if (data_ == nullptr || offset >= size_) {
|
||||||
|
return StringSlice();
|
||||||
|
}
|
||||||
|
const std::size_t avail = size_ - offset;
|
||||||
|
return StringSlice(data_ + offset, len < avail ? len : avail);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
|
|
||||||
|
#endif // NETSENTRY_STRING_SLICE_H
|
||||||
@@ -1,13 +1,37 @@
|
|||||||
|
# ---- 找 libpcap ----
|
||||||
|
find_path(PCAP_INCLUDE_DIR pcap.h)
|
||||||
|
find_library(PCAP_LIBRARY NAMES pcap)
|
||||||
|
if(NOT PCAP_INCLUDE_DIR OR NOT PCAP_LIBRARY)
|
||||||
|
message(FATAL_ERROR "libpcap not found. Try: sudo apt install libpcap-dev")
|
||||||
|
endif()
|
||||||
|
message(STATUS "libpcap: ${PCAP_LIBRARY}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ---- 核心静态库:以后所有模块代码都长在这里 ----
|
# ---- 核心静态库:以后所有模块代码都长在这里 ----
|
||||||
|
|
||||||
add_library(netsentry_core STATIC
|
add_library(netsentry_core STATIC
|
||||||
version.cpp
|
version.cpp
|
||||||
options.cpp
|
options.cpp
|
||||||
|
decode/ethernet.cpp
|
||||||
|
decode/ipv4.cpp
|
||||||
|
decode/udp.cpp
|
||||||
|
decode/tcp.cpp
|
||||||
|
capture/pcap_reader.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
# 对外暴露头文件目录。PUBLIC 的意思是:链接我这个库的目标,自动继承这个 include 路径
|
# 对外暴露头文件目录。PUBLIC 的意思是:链接我这个库的目标,自动继承这个 include 路径
|
||||||
target_include_directories(netsentry_core PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
target_include_directories(netsentry_core PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
||||||
target_compile_options(netsentry_core PRIVATE -Wall -Wextra)
|
target_compile_options(netsentry_core PRIVATE -Wall -Wextra)
|
||||||
|
|
||||||
|
|
||||||
|
# pcap.h 只是实现细节:include 路径给 PRIVATE,别让上层也看见 libpcap 的头;
|
||||||
|
# 但链接必须 PUBLIC —— 静态库自己不真正链接,它的依赖要传给最终的可执行和测试,
|
||||||
|
# 否则符号 pcap_open_offline 找不到,报 undefined reference
|
||||||
|
target_include_directories(netsentry_core PRIVATE ${PCAP_INCLUDE_DIR})
|
||||||
|
target_link_libraries(netsentry_core PUBLIC ${PCAP_LIBRARY})
|
||||||
|
|
||||||
|
|
||||||
# ---- 可执行文件:只负责组装入口,业务逻辑都放 core 里 ----
|
# ---- 可执行文件:只负责组装入口,业务逻辑都放 core 里 ----
|
||||||
add_executable(netsentry main.cpp)
|
add_executable(netsentry main.cpp)
|
||||||
target_link_libraries(netsentry PRIVATE netsentry_core)
|
target_link_libraries(netsentry PRIVATE netsentry_core)
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
#include "netsentry/capture/pcap_reader.h"
|
||||||
|
|
||||||
|
#include <pcap.h>
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
PcapReader::~PcapReader() { close(); }
|
||||||
|
|
||||||
|
bool PcapReader::open(const std::string& path) {
|
||||||
|
close(); //防重复打开,先关旧的
|
||||||
|
last_error_.clear();
|
||||||
|
|
||||||
|
char errbuf[PCAP_ERRBUF_SIZE] = "";
|
||||||
|
pcap_t* h = pcap_open_offline(path.c_str(), errbuf);
|
||||||
|
if (h == nullptr) {
|
||||||
|
last_error_ = errbuf;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 只支持 Ethernet 链路层。DLT 不同(Linux SLL、BSD loopback...)帧头布局不同,
|
||||||
|
// 解析层会全军覆没,必须在门口拦住,而不是让协议解析器产出垃圾
|
||||||
|
if (pcap_datalink(h) != DLT_EN10MB) {
|
||||||
|
last_error_ = "unsupported link type (only Ethernet II / DLT_EN10MB)";
|
||||||
|
pcap_close(h);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_ = h;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PcapReader::close() {
|
||||||
|
if (handle_ != nullptr) {
|
||||||
|
pcap_close(static_cast<pcap_t*>(handle_));
|
||||||
|
handle_ = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PcapReader::forEachPacket(const PacketCallback& cb) {
|
||||||
|
if (handle_ == nullptr) {
|
||||||
|
last_error_ = "not open";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pcap_t* h = static_cast<pcap_t*>(handle_);
|
||||||
|
pcap_pkthdr* hdr = nullptr;
|
||||||
|
const u_char* data = nullptr;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
// pcap_next_ex 返回:1=读到一包,0=超时(离线读不会出现),-1=出错,-2=文件尾
|
||||||
|
const int ret = pcap_next_ex(h, &hdr, &data);
|
||||||
|
if (ret == 1) {
|
||||||
|
if (hdr->caplen > 0 && !cb(data, static_cast<uint32_t>(hdr->caplen))) {
|
||||||
|
return true; // 回调主动叫停,不算错误
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (ret == -2) {
|
||||||
|
return true; // 正常读完
|
||||||
|
}
|
||||||
|
last_error_ = pcap_geterr(h);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#include "netsentry/decode/ethernet.h"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
#include "netsentry/decode/byteorder.h"
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
namespace { //文本私有,其他的CPP看不到,C++替代static修饰文件级变量的写法
|
||||||
|
const std::size_t kEtherHeaderLen = 14;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string toString(const MacAddress& mac) {
|
||||||
|
char buf[18];
|
||||||
|
std::snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||||
|
mac.octets[0], mac.octets[1], mac.octets[2],
|
||||||
|
mac.octets[3], mac.octets[4], mac.octets[5]);
|
||||||
|
return std::string(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool parseEthernet(const StringSlice& frame, EtherHeader* out, StringSlice* payload) {
|
||||||
|
if (frame.size() < kEtherHeaderLen) {
|
||||||
|
return false; // 比帧头还短,连"听写"都不够 失败不产生半成品
|
||||||
|
}
|
||||||
|
const uint8_t* p = reinterpret_cast<const uint8_t*>(frame.data());
|
||||||
|
|
||||||
|
for (int i = 0; i < 6; ++i) {
|
||||||
|
out->dst.octets[i] = p[i];
|
||||||
|
out->src.octets[i] = p[6 + i];
|
||||||
|
}
|
||||||
|
out->ethertype = readU16BE(p + 12);
|
||||||
|
|
||||||
|
*payload = frame.slice(kEtherHeaderLen, frame.size() - kEtherHeaderLen);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#include "netsentry/decode/ipv4.h"
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
#include "netsentry/decode/byteorder.h"
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
const std::size_t kMinHeaderLen = 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string toString(const Ipv4Address& ip) {
|
||||||
|
char buf[16];
|
||||||
|
std::snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
|
||||||
|
ip.octets[0], ip.octets[1], ip.octets[2], ip.octets[3]);
|
||||||
|
return std::string(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool parseIpv4(const StringSlice& data, Ipv4Header* out, StringSlice* payload) {
|
||||||
|
if (data.size() < kMinHeaderLen) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const uint8_t* p = reinterpret_cast<const uint8_t*>(data.data());
|
||||||
|
|
||||||
|
// 第 0 字节:高 4 位版本号,低 4 位 IHL(头长,单位 4 字节)
|
||||||
|
const uint8_t version = p[0] >> 4;
|
||||||
|
const uint8_t ihl = static_cast<uint8_t>(p[0] & 0x0F);
|
||||||
|
if (version != 4 || ihl < 5) {
|
||||||
|
return false; // 不是 IPv4,或头长小于最小值 20 字节
|
||||||
|
}
|
||||||
|
const std::size_t header_len = static_cast<std::size_t>(ihl) * 4;
|
||||||
|
if (data.size() < header_len) {
|
||||||
|
return false; // 声称的头长比实际字节还长 —— 被截断的坏包
|
||||||
|
}
|
||||||
|
|
||||||
|
out->total_length = readU16BE(p + 2);
|
||||||
|
|
||||||
|
// 第 6-7 字节:flags(3 bit) + fragment offset(13 bit)。
|
||||||
|
// MF = 0x2000;offset 掩码 = 0x1FFF。任一非 0 即视为"分片的其中一块",
|
||||||
|
// 它的 L4 头不在本包里,上层不能直接解析(重组是后面的课)
|
||||||
|
const uint16_t flags_frag = readU16BE(p + 6);
|
||||||
|
out->fragmented = (flags_frag & 0x3FFF) != 0;
|
||||||
|
|
||||||
|
out->protocol = p[9];
|
||||||
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
out->src.octets[i] = p[12 + i];
|
||||||
|
out->dst.octets[i] = p[16 + i];
|
||||||
|
}
|
||||||
|
out->ihl_bytes = static_cast<uint8_t>(header_len);
|
||||||
|
|
||||||
|
// 注:真实抓包常有 snaplen 截断,实际字节比 total_length 声称的少。
|
||||||
|
// 我们以实际拿到的字节为准切 payload,不在这里校验 total_length 一致性
|
||||||
|
*payload = data.slice(header_len, data.size() - header_len);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#include "netsentry/decode/tcp.h"
|
||||||
|
|
||||||
|
#include "netsentry/decode/byteorder.h"
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
const std::size_t kMinHeaderLen = 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool parseTcp(const StringSlice& data, TcpHeader* out, StringSlice* payload) {
|
||||||
|
if (data.size() < kMinHeaderLen) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const uint8_t* p = reinterpret_cast<const uint8_t*>(data.data());
|
||||||
|
|
||||||
|
out->src_port = readU16BE(p);
|
||||||
|
out->dst_port = readU16BE(p + 2);
|
||||||
|
out->seq = readU32BE(p + 4);
|
||||||
|
out->ack = readU32BE(p + 8);
|
||||||
|
|
||||||
|
// 第 12 字节高 4 位是 Data Offset(头长,单位 4 字节),最小 5(=20 字节)
|
||||||
|
const uint8_t offset = static_cast<uint8_t>(p[12] >> 4);
|
||||||
|
if (offset < 5) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const std::size_t header_len = static_cast<std::size_t>(offset) * 4;
|
||||||
|
if (data.size() < header_len) {
|
||||||
|
return false; // 头被截断
|
||||||
|
}
|
||||||
|
out->data_offset_bytes = static_cast<uint8_t>(header_len);
|
||||||
|
out->flags = p[13];
|
||||||
|
|
||||||
|
*payload = data.slice(header_len, data.size() - header_len);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#include "netsentry/decode/udp.h"
|
||||||
|
|
||||||
|
#include "netsentry/decode/byteorder.h"
|
||||||
|
|
||||||
|
namespace netsentry {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
const std::size_t kHeaderLen = 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool parseUdp(const StringSlice& data, UdpHeader* out, StringSlice* payload) {
|
||||||
|
if (data.size() < kHeaderLen) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const uint8_t* p = reinterpret_cast<const uint8_t*>(data.data());
|
||||||
|
|
||||||
|
out->src_port = readU16BE(p);
|
||||||
|
out->dst_port = readU16BE(p + 2);
|
||||||
|
out->length = readU16BE(p + 4);
|
||||||
|
if (out->length < kHeaderLen) {
|
||||||
|
return false; // 协议规定 length 至少覆盖 8 字节头,小于这个就是坏包
|
||||||
|
}
|
||||||
|
|
||||||
|
// 同样以实际字节为准切载荷(snaplen 截断时 length 可能大于实际)
|
||||||
|
*payload = data.slice(kHeaderLen, data.size() - kHeaderLen);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace netsentry
|
||||||
+135
-14
@@ -1,29 +1,150 @@
|
|||||||
|
#include <cstdio>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "netsentry/version.h"
|
#include "netsentry/capture/pcap_reader.h"
|
||||||
|
#include "netsentry/decode/ethernet.h"
|
||||||
|
#include "netsentry/decode/ipv4.h"
|
||||||
|
#include "netsentry/decode/tcp.h"
|
||||||
|
#include "netsentry/decode/udp.h"
|
||||||
#include "netsentry/options.h"
|
#include "netsentry/options.h"
|
||||||
|
#include "netsentry/string_slice.h"
|
||||||
|
#include "netsentry/version.h"
|
||||||
|
|
||||||
int main(int argc,char* argv[])
|
namespace {
|
||||||
{
|
|
||||||
//main只做三件事:解析参数->分发->收尾。逻辑全在core中,main永远保持简洁
|
|
||||||
netsentry::Options opt = netsentry::parseArgs(argc,argv);
|
|
||||||
|
|
||||||
if (!opt.ok)
|
struct Stats {
|
||||||
{
|
std::size_t total = 0;
|
||||||
std::cerr << "usage: netsentry -r <pcap file>\n"; //标准错误输出流
|
std::size_t eth_ok = 0;
|
||||||
|
std::size_t eth_bad = 0;
|
||||||
|
std::size_t l3_other = 0; // ARP / IPv6 / 其它 ethertype
|
||||||
|
std::size_t ipv4 = 0;
|
||||||
|
std::size_t ipv4_frag = 0;
|
||||||
|
std::size_t tcp = 0;
|
||||||
|
std::size_t udp = 0;
|
||||||
|
std::size_t l4_bad = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
netsentry::Options opt = netsentry::parseArgs(argc, argv);
|
||||||
|
|
||||||
|
if (!opt.ok) {
|
||||||
|
std::cerr << "usage: netsentry -r <pcap file>\n";
|
||||||
std::cerr << " netsentry -h\n";
|
std::cerr << " netsentry -h\n";
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (opt.show_help)
|
|
||||||
{
|
if (opt.show_help) {
|
||||||
std::cout << "NetSentry v" << netsentry::kVersion << "\n";
|
std::cout << "NetSentry v" << netsentry::kVersion << "\n";
|
||||||
std::cout << "usage:netsentry -r <pcpa file>\n";
|
std::cout << "usage: netsentry -r <pcap file>\n";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "NetSentry v" << netsentry::kVersion << "\n";
|
netsentry::PcapReader reader;
|
||||||
std::cout << "TODO: parse" << opt.pcap_file << "\n"; //下一节内容
|
if (!reader.open(opt.pcap_file)) {
|
||||||
|
std::cerr << "error: " << reader.lastError() << "\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Stats stats;
|
||||||
|
std::vector<std::string> samples; // 前 5 条五元组,肉眼对照用
|
||||||
|
|
||||||
|
// [&] 按引用捕获:lambda 在 forEachPacket 内同步执行,栈上引用安全。
|
||||||
|
// 如果哪天回调被存起来异步执行,捕获方式就要重新想 —— 到并发课再细说
|
||||||
|
const bool ok = reader.forEachPacket(
|
||||||
|
[&](const uint8_t* data, uint32_t len) -> bool {
|
||||||
|
++stats.total;
|
||||||
|
|
||||||
|
netsentry::EtherHeader eth;
|
||||||
|
netsentry::StringSlice l3;
|
||||||
|
const netsentry::StringSlice frame(
|
||||||
|
reinterpret_cast<const char*>(data), len);
|
||||||
|
if (!netsentry::parseEthernet(frame, ð, &l3)) {
|
||||||
|
++stats.eth_bad;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
++stats.eth_ok;
|
||||||
|
|
||||||
|
if (eth.ethertype != netsentry::kEtherTypeIpv4) {
|
||||||
|
++stats.l3_other;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
netsentry::Ipv4Header ip;
|
||||||
|
netsentry::StringSlice l4;
|
||||||
|
if (!netsentry::parseIpv4(l3, &ip, &l4)) {
|
||||||
|
++stats.eth_bad; // 声称是 IP 但解析失败,按坏包计
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
++stats.ipv4;
|
||||||
|
|
||||||
|
if (ip.fragmented) {
|
||||||
|
++stats.ipv4_frag; // 分片重组是后面的课,先统计
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ip.protocol == netsentry::kIpProtoTcp) {
|
||||||
|
netsentry::TcpHeader tcp;
|
||||||
|
netsentry::StringSlice payload;
|
||||||
|
if (netsentry::parseTcp(l4, &tcp, &payload)) {
|
||||||
|
++stats.tcp;
|
||||||
|
if (samples.size() < 5) {
|
||||||
|
char line[128];
|
||||||
|
std::snprintf(line, sizeof(line),
|
||||||
|
"%s:%u -> %s:%u tcp len=%u",
|
||||||
|
netsentry::toString(ip.src).c_str(),
|
||||||
|
static_cast<unsigned>(tcp.src_port),
|
||||||
|
netsentry::toString(ip.dst).c_str(),
|
||||||
|
static_cast<unsigned>(tcp.dst_port),
|
||||||
|
static_cast<unsigned>(payload.size()));
|
||||||
|
samples.push_back(line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
++stats.l4_bad;
|
||||||
|
}
|
||||||
|
} else if (ip.protocol == netsentry::kIpProtoUdp) {
|
||||||
|
netsentry::UdpHeader udp;
|
||||||
|
netsentry::StringSlice payload;
|
||||||
|
if (netsentry::parseUdp(l4, &udp, &payload)) {
|
||||||
|
++stats.udp;
|
||||||
|
if (samples.size() < 5) {
|
||||||
|
char line[128];
|
||||||
|
std::snprintf(line, sizeof(line),
|
||||||
|
"%s:%u -> %s:%u udp len=%u",
|
||||||
|
netsentry::toString(ip.src).c_str(),
|
||||||
|
static_cast<unsigned>(udp.src_port),
|
||||||
|
netsentry::toString(ip.dst).c_str(),
|
||||||
|
static_cast<unsigned>(udp.dst_port),
|
||||||
|
static_cast<unsigned>(payload.size()));
|
||||||
|
samples.push_back(line);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
++stats.l4_bad;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!ok) {
|
||||||
|
std::cerr << "error: " << reader.lastError() << "\n";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::printf("=== NetSentry decode summary ===\n");
|
||||||
|
std::printf("packets : %zu\n", stats.total);
|
||||||
|
std::printf("ethernet ok : %zu\n", stats.eth_ok);
|
||||||
|
std::printf("ethernet bad : %zu\n", stats.eth_bad);
|
||||||
|
std::printf("non-ipv4 frames : %zu\n", stats.l3_other);
|
||||||
|
std::printf("ipv4 : %zu\n", stats.ipv4);
|
||||||
|
std::printf("ipv4 fragmented : %zu\n", stats.ipv4_frag);
|
||||||
|
std::printf("tcp : %zu\n", stats.tcp);
|
||||||
|
std::printf("udp : %zu\n", stats.udp);
|
||||||
|
std::printf("l4 decode fail : %zu\n", stats.l4_bad);
|
||||||
|
for (std::size_t i = 0; i < samples.size(); ++i) {
|
||||||
|
std::printf("sample: %s\n", samples[i].c_str());
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ namespace netsentry {
|
|||||||
opt.ok = true;
|
opt.ok = true;
|
||||||
return opt;
|
return opt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (arg1 == "-r" && argc >= 3) {
|
||||||
|
opt.pcap_file = argv[2];
|
||||||
|
opt.ok = true;
|
||||||
|
return opt;
|
||||||
|
}
|
||||||
|
|
||||||
return opt; //ok = false;不认识的参数,或-r后面缺文件名
|
return opt; //ok = false;不认识的参数,或-r后面缺文件名
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user