NIDS-端口扫描检测初步(抓包部分)

这篇博客介绍了如何将原来的NIDS(网络入侵检测系统)端口扫描检测代码进行重构,以实现更高的模块化。作者提到原有的代码耦合性较高,并详细列出了重构后的头文件和源文件清单,但指出扫描部分的功能仍有待进一步扩充。
摘要由CSDN通过智能技术生成

原来的代码耦合性太高了,我重构了一下,模块化了。

扫描部分还待扩充


头文件:

config.h

#ifndef lib_pcap_pcap_h
#include "pcap.h"
#endif

#ifndef HAVE_PARAM_H
#include "param.h"
#endif

#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H

/*
	IPSIZE			IP地址的长度
	MACSIZE			MAC地址的长度
	PORTNUM			PORT的个数
*/
#define IPSIZE 16					
#define MACSIZE 14
#define PORTNUM 65536

const int judge_ip_times = 4;		// 设置判为可疑的次数
const int judge_port_times = 100;
const char port_scan_log[] = "port_scan_log.txt";

#endif		// CONFIG_H


dnmc_packet_data.h

#ifndef HAVE_CONFIG_H
#include "config.h"
#endif

#ifndef HAVE_DNMC_PACKET_DATA_H
#define HAVE_DNMC_PACKET_DATA_H

typedef struct protocol_type {
	int dport_hash[PORTNUM];
	int dport_last_time[PORTNUM];		// ??预想画出特征曲线
}protocol_type;

typedef struct smac_info {
	char *dmac;
	struct protocol_type *pprot_type;

	struct smac_info* next;
}smac_info;

typedef struct list_head {
	char *smac;
	struct smac_info *psmac_info;

	struct list_head *next;
}list_head;

typedef struct rule_list_node {
	char *name;
	int mode;
	int rval;							// 0表示不检测,1表示检测
	struct rule_list_node* next;
	struct list_head* rule_list;
}rule_list_node;

#endif		// DNMC_PACKET_DATA_H

main.h

#ifndef lib_pcap_pcap_h
#include "pcap.h"
#endif

#ifdef WINDOWS
#define unsigned (unsigned int)
#endif

#ifdef LINUX
#define unsigned (unsigned long)
#endif

char *packet_filter;				// 需要从文件中读取过滤数据参数

struct pcap_pkthdr **pool_header;	// 数据包池
u_char **pool_pkt_data;
u_int pool_in_index;
u_int pool_out_index;

packet.h

#ifndef HAVE_CONFIG_H
#include "config.h"
#define HAVE_CONFIG_H
#endif


#ifndef HAVE_PACKET_H
#define HAVE_PACKET_H

// 以太网协议格式的定义
typedef struct ether_header {
	u_char dhost[6];		// 目标地址
	u_char shost[6];		// 源地址
	u_short type;			// 以太网类型
}ether_header;

// 用户保存4字节的IP地址
typedef struct ip_address {
	u_char byte1;
	u_char byte2;
	u_char byte3;
	u_char byte4;
}ip_address;

// 用于保存IPV4的首部
typedef struct ip_header {
#ifdef WORDS_BIGENDIAN
	u_char ip_version : 4, header_length : 4;
#else
	u_char header_length : 4, ip_version : 4;
#endif

	u_char ver_ihl;		// 版本以及首部长度,各4位
	u_char tos;			// 服务质量
	u_short tlen;		// 总长度
	u_short identification;		// 身份识别
	u_short offset;			// 分组偏移
	u_char ttl;			// 生命周期
	u_char protocol;		// 协议类型
	u_short checksum;		// 包头测验码
	ip_address saddr;	// 源IP地址
	ip_address daddr;	// 目的IP地址
	u_int op_pad;		//可选 填充字段
}ip_header;

// 保存TCP首部
typedef struct tcp_header {
	u_short sport;
	u_short dport;
	u_int sequence;		// 序列码
	u_int ack;					// 回
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值