summaryrefslogtreecommitdiff
path: root/include/MESA/rulescan.h
diff options
context:
space:
mode:
author[email protected] <[email protected]>2021-11-02 12:34:05 +0800
committer[email protected] <[email protected]>2021-11-02 12:34:05 +0800
commit31f55f0b88d4af34a8a36497f5e49c69b88b2fbf (patch)
tree63515b3ceb361369cdc88ae6db1a808fc80e5b42 /include/MESA/rulescan.h
Diffstat (limited to 'include/MESA/rulescan.h')
-rw-r--r--include/MESA/rulescan.h324
1 files changed, 324 insertions, 0 deletions
diff --git a/include/MESA/rulescan.h b/include/MESA/rulescan.h
new file mode 100644
index 0000000..c5d2801
--- /dev/null
+++ b/include/MESA/rulescan.h
@@ -0,0 +1,324 @@
+/*
+ *
+ * Copyright (c) 2014
+ * String Algorithms Research Group
+ * Institute of Information Engineering, Chinese Academy of Sciences (IIE-CAS)
+ * National Engineering Laboratory for Information Security Technologies (NELIST)
+ * All rights reserved
+ *
+ * Written by: LIU YANBING ([email protected])
+ * Last modification: 2016-06-05
+ *
+ * This code is the exclusive and proprietary property of IIE-CAS and NELIST.
+ * Usage for direct or indirect commercial advantage is not allowed without
+ * written permission from the authors.
+ *
+ */
+
+#ifndef H_RULE_SCAN_H
+#define H_RULE_SCAN_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+ /* rulescan_set_param���������õIJ������� */
+ enum RULESCAN_PARA_NAME
+ {
+ RULESCAN_DETAIL_RESULT=1, /* ����־λ��ʾ��������ϸ����λ�õ���Ϣ, optval��ΪNULL��optlen��Ϊ0��Ĭ�ϲ�������ϸ��Ϣ*/
+ RULESCAN_REGEX_GROUP =2, /* ����־λ��ʾ�������������ʽƥ��ķ�����Ϣ���������ֶΣ���Ҫ������RULESCAN_DETAIL_RESULT��־λ,optval��ΪNULL��optlen��Ϊ0��Ĭ�ϲ����ط�����Ϣ */
+ RULESCAN_QUICK_SCAN, /* ������Ҫ����ɨ��ģʽ��sub_type, ���û��Լ����壬optval��ֵΪ0-4096��optlen��Ϊ4��Ĭ����������ͨɨ��ģʽ��һ������ʽ�б��뱣֤�ñ���ʽ�������������Ҫô
+ ����quickģʽ����Ҫô���Ƿ�quickģʽ */
+ RULEACAN_ERRLOG_CLOSE, /* ����־λ��ʾ���ر�Rulescan������־�����optval��ΪNULL��optlen��Ϊ0�������õĻ�Ĭ�ϴ�Rulescan������־��� */
+ RULESCAN_ERRLOG_FILE_PATH, /* ����Rulescan������־��·�����������ļ����������û����룬optval��ֵΪ�����ļ�������־·����optlenΪ·�����ȡ����û���趨��
+ ����־Ĭ�ϴ洢�ڿ�ִ�г���ǰĿ¼�µ�rulescan_tmp�� */
+ };
+
+ #define MAX_REGEX_GROUP_NUM 5 /* �����������ʽ����֧�ֵ�������ĸ��� */
+
+ #define MAX_EXPR_ITEM_NUM (1U<<3) /* ÿ�������ʽ�����MAX_EXPR_ITEM_NUM��������� */
+ #define MAX_MATCH_POS_NUM 1024 /* ÿ����������������ص�����λ�õĸ��� */
+ #define MATCH_POS_NUM_INC 64 /* ÿ�������������ص�����λ�õĸ�����ʼֵ������ֵ */
+
+ /* ���岻ͬ�Ĺ������� */
+ const unsigned int RULETYPE_STR = 0; /* �ַ���������ƹ��� */
+ const unsigned int RULETYPE_REG = 1; /* �������ʽ���� */
+ const unsigned int RULETYPE_INT = 2; /* ��ֵ������� */
+ const unsigned int RULETYPE_IPv4 = 3; /* IPv4���� */
+ const unsigned int RULETYPE_IPv6 = 4; /* IPv6���� */
+
+ const unsigned int MAX_RULETYPE = 5; /* ������������ */
+ const unsigned int MAX_SUB_RULETYPE = 4096; /* �������������� */
+
+ /* �ַ������͹��򣨿ɱ�ʾ�ı��ַ������������ַ������������ʽ�� */
+ typedef struct _string_rule_t
+ {
+ char * str; /* �ַ������ݣ�������������ʽ������'\0'�������ɲ�ָ������ */
+ unsigned int len; /* �ַ������� */
+ unsigned char case_sensitive; /* �Ƿ��Сд����ƥ�䣨1�����У�0�������У� */
+ unsigned char match_mode; /* ƥ��ģʽ���Ӵ�ƥ�䣨0��������ƥ�䣨1�������Ծ�ȷ��ƥ����Ч */
+ int l_offset; /* ��ʾģʽ��ֻ�����ı���Χ[l_offset, r_offset]�г��֣�-1��ʾ������,-2��ʾ��ƥ�䣻���Ծ�ȷ��ƥ����Ч */
+ int r_offset; /* ��ʾģʽ��ֻ�����ı���Χ[l_offset, r_offset]�г��֣�-1��ʾ������,-2��ʾ��ƥ�䣻���Ծ�ȷ��ƥ����Ч */
+ }string_rule_t;
+
+ /* ������ֵ������򣬱�ʾ��������[lb, ub] */
+ typedef struct _interval_rule_t
+ {
+ unsigned int lb; /* ����������½磨����lb����������Ĭ��Ϊ0 */
+ unsigned int ub; /* ����������½磨����ub����������Ĭ��Ϊ0 */
+ }interval_rule_t;
+
+ /* �������IPv4���� */
+ typedef struct _ipv4_rule_t
+ {
+ unsigned int saddr; /* ԴIP��ַ��0��ʾ���Ա��ֶ� */
+ unsigned int smask; /* ԴIP��ַ���룻0��ʾ�̶�IP=saddr */
+ unsigned int daddr; /* Ŀ��IP��ַ��0��ʾ���Ա��ֶ� */
+ unsigned int dmask; /* Ŀ��IP��ַ���룻0��ʾ�̶�IP=daddr */
+ unsigned short int min_sport; /* Դ�˿ڷ�Χ�½磻0��ʾ���Ա��ֶ� */
+ unsigned short int max_sport; /* Դ�˿ڷ�Χ�Ͻ磻0��ʾ�̶��˿�=min_sport */
+ unsigned short int min_dport; /* Ŀ�Ķ˿ڷ�Χ�½磻0��ʾ���Ա��ֶ� */
+ unsigned short int max_dport; /* Ŀ�Ķ˿ڷ�Χ�Ͻ磻0��ʾ�̶��˿�=min_dport */
+ unsigned short int proto; /* �����Э�飬6��ʾTCP��17��ʾUDP��0��ʾ���Ա��ֶ� */
+ unsigned short int direction; /* ����0��ʾ˫��1��ʾ���� */
+ }ipv4_rule_t;
+
+ /* �������IPv6���� */
+ typedef struct _ipv6_rule_t
+ {
+ unsigned int saddr[4]; /* ԴIP��ַ��0��ʾ���Ա��ֶ� */
+ unsigned int smask[4]; /* ԴIP��ַ���룻0��ʾ�̶�IP=saddr */
+ unsigned int daddr[4]; /* Ŀ��IP��ַ��0��ʾ���Ա��ֶ� */
+ unsigned int dmask[4]; /* Ŀ��IP��ַ���룻0��ʾ�̶�IP=daddr */
+ unsigned short int min_sport; /* Դ�˿ڷ�Χ�½磻0��ʾ���Ա��ֶ� */
+ unsigned short int max_sport; /* Դ�˿ڷ�Χ�Ͻ磻0��ʾ�̶��˿�=min_sport */
+ unsigned short int min_dport; /* Ŀ�Ķ˿ڷ�Χ�½磻0��ʾ���Ա��ֶ� */
+ unsigned short int max_dport; /* Ŀ�Ķ˿ڷ�Χ�Ͻ磻0��ʾ�̶��˿�=min_dport */
+ unsigned short int proto; /* �����Э�飬6��ʾTCP��17��ʾUDP��������Ĭ��Ϊ0 */
+ unsigned short int direction; /* ����0��ʾ˫��1��ʾ���� */
+ }ipv6_rule_t;
+
+ /* ͨ�õĹ������� */
+ typedef struct _scan_rule_t
+ {
+ unsigned int rule_type; /* �������ͣ�����Ϊ����ö�ٹ�������֮һ */
+ unsigned int sub_type; /* �������ͣ��û��Զ��壬�������͸�������������MAX_SUB_RULETYPE����ǰ�Ķ��壩 */
+ union /* ����rule_type�����������ַ�������ֵ���䡢����IP���� */
+ {
+ string_rule_t string_rule; /* �ַ��������ַ����������ơ��������ʽ�� */
+ interval_rule_t interval_rule; /* ������ֵ������� */
+ ipv4_rule_t ipv4_rule; /* �������IPv4���� */
+ ipv6_rule_t ipv6_rule; /* �������IPv6���� */
+ };
+ }scan_rule_t;
+
+ /* һ�������ʽ���� */
+ typedef struct _boolean_expr_t
+ {
+ unsigned int expr_id; /* �����ʽ��ID */
+ unsigned int operation; /* �������ʽִ�еIJ�����0��ʾ���ӣ�1��ʾɾ�� */
+ unsigned int rnum; /* �������ʽ�������ٸ�����operation=1����rnum=0���� */
+ scan_rule_t * rules; /* ��������ʽ������operation=1����rules=NULL���� */
+ void * tag; /* �û��Զ������ݣ�����ʱ��ƥ�������� */
+ }boolean_expr_t;
+
+
+ /* ��ɨ����ı��������� */
+ typedef struct _text_data_t
+ {
+ const char * text; /* �ı��������� */
+ unsigned int tlen; /* �ı����ݳ��� */
+ int toffset;/* �����ı������������������е�ƫ��������ʽɨ���������Ч�����û����룬���������Ϊ0(���������Ϊ0) */
+ }text_data_t;
+
+ /* ��ɨ���IPv4Ԫ�� */
+ typedef struct _ipv4_data_t
+ {
+ unsigned int saddr; /* ԴIP��ַ */
+ unsigned int daddr; /* Ŀ��IP��ַ */
+ unsigned short int sport; /* Դ�˿� */
+ unsigned short int dport; /* Ŀ�Ķ˿� */
+ unsigned short int proto; /* �����Э�飬6��ʾTCP��17��ʾUDP */
+ }ipv4_data_t;
+
+ /* ��ɨ���IPv6Ԫ�� */
+ typedef struct _ipv6_data_t
+ {
+ unsigned int saddr[4]; /* ԴIP��ַ */
+ unsigned int daddr[4]; /* Ŀ��IP��ַ */
+ unsigned short int sport; /* Դ�˿� */
+ unsigned short int dport; /* Ŀ�Ķ˿� */
+ unsigned short int proto; /* �����Э�飬6��ʾTCP��17��ʾUDP */
+ }ipv6_data_t;
+
+ /* ͨ�õĴ�ɨ���������� */
+ typedef struct _scan_data_t
+ {
+ unsigned int rule_type; /* �������ͣ�����Ϊ����ö�ٹ�������֮һ */
+ unsigned int sub_type; /* �������ͣ��û��Զ��壬�������͸�������������MAX_SUB_RULETYPE����ǰ�Ķ��壩 */
+ union /* ����rule_type�������ݸ������ַ�������ֵ������IPԪ�� */
+ {
+ text_data_t text_data; /* ��ɨ���ı����ݣ���ƥ���ַ����������ơ��������ʽ�� */
+ unsigned int int_data; /* ������ֵ����ƥ����ֵ���䣩 */
+ ipv4_data_t ipv4_data; /* ��ɨ���IPv4Ԫ�� */
+ ipv6_data_t ipv6_data; /* ��ɨ���IPv6Ԫ�� */
+ };
+ }scan_data_t;
+
+ /*
+ ɨ��������scan_result_t��rule_result_t˵����
+ 1���������е�ÿ����������ʽ��һ������ʽ��Ӧһ��scan_result_t������ò�������ʽ����rnum������ÿ���������Ӧ��һ�����scan_result_t::result[k]��0<=k<rnum����
+
+ 2�����ھ�ȷ�ַ������������position��length�������£�
+ ���еľ�ȷ�ַ�������result_num��ƥ��������i��0<=i<result_num����ƥ��������ʼλ�úͳ��ȷֱ��ǣ�
+ (position[i], length[i])
+
+ 3�������������ʽ���������position��length�������£�
+ ���е��������ʽ����result_num��ƥ������ÿ��ƥ��������group_num+1������Ԫ�ء����ڵ�i��0<=i<result_num����ƥ������
+ ��a�������������ʽƥ�����ʼλ�úͳ��ȷֱ��ǣ�
+ (position[(group_num+1)*i], length[(group_num+1)*i])
+ ��b����j��0<=j<group_num�����������ʼλ�úͳ��ȷֱ��ǣ�
+ (position[(group_num+1)*i+j+1], length[(group_num+1)*i+j+1])
+ */
+
+ /* �����ɨ�������� */
+ typedef struct _rule_result_t
+ {
+ unsigned int rule_type; /* ��������ͣ�����Ϊ����ö�ٹ�������֮һ */
+ unsigned int group_num; /* �����������ʽ���͹��򣬱��ֶα�ʾ������飨capturing group���ĸ�������������Ϊ0 */
+ unsigned int result_num; /* �ù����������н���ĸ��� */
+ unsigned int position[MAX_MATCH_POS_NUM]; /* �ù����������н������ʼλ�� */
+ unsigned int length[MAX_MATCH_POS_NUM]; /* �ù����������н���ij��ȣ���������н��������ǰ�����ݰ������еģ������ö�Ӧ��length=0, ��������������IP�࣬������Ϊ0*/
+ }rule_result_t;
+
+ /* ��������ʽ��ɨ�������ͣ�quickģʽ��ֻ��expr_id�Լ�tag������Ч */
+ typedef struct _scan_result_t
+ {
+ unsigned int expr_id; /* �����ʽ��ID */
+ unsigned int rnum; /* �������ʽ�������ٸ����� */
+ rule_result_t result[MAX_EXPR_ITEM_NUM]; /* ��������ʽ��ÿ�������Ӧ��ɨ���� */
+ void * tag; /* �û��Զ������ݣ�����ʱ��ƥ�������� */
+ }scan_result_t;
+
+ /*
+ ���ܣ�ɨ������ʼ��������ɨ��������
+ ������
+ max_thread_num [in]: ɨ�����ɲ���ִ�е��߳�����
+ ����ֵ��
+ ɨ�������������ֵΪNULLʱ����ʾ��ʼ��ʧ�ܡ�
+ */
+ void * rulescan_initialize(unsigned int max_thread_num);
+
+ /*
+ ���ܣ�
+ ����ɨ���������������rulescan_update֮ǰ�ɶ�ε��ã�ÿ������һ��ɨ�����͡�Rulescan��Ĭ�ϲ���������λ�õ���ϸ��Ϣ
+ ������
+ instance[in]: ɨ��������ָ�룻
+ optname [in]: �������ͣ�
+ optval [in]: optval��optlen��ʾ�����ľ������ݣ�
+ optlen [in]: optval��optlen��ʾ�����ľ������ݡ�
+ ����ֵ��
+ 1����ȷ���ã�-1������ʧ�ܡ�
+ */
+ int rulescan_set_param(void * instance, enum RULESCAN_PARA_NAME optname, const void * optval, unsigned int optlen);
+
+ /*
+ ���ܣ���̬ע��һ�������ʽ������ɨ�������󡣶���ͬһ��instance�����������ͬʱ�ж�������̡߳�
+ ������
+ instance[in]: ɨ��������ָ�룻
+ expr_array[in]: һ�������ʽ��
+ epxr_num[in]: �����ʽ�ĸ�����
+ failed_ids[out]: �Ƿ������id��failed_ids[0]��ʾ�Ƿ�id�ĸ�����failed_ids[1...failed_ids[0]]��¼�Ƿ�id��Ŀǰֻ�Ժ�һ�������������಼������ʽ��Ч��
+ failed_size[in]: failed_ids����Ĵ�С��
+ ����ֵ��
+ ����ֵΪ1ʱ����ʾע��ɹ�������ֵΪ-1ʱ����ʾ������
+ */
+ int rulescan_update(void * instance, boolean_expr_t * expr_array, unsigned int expr_num, unsigned int * failed_ids, unsigned int failed_size);
+
+ /*
+ ���ܣ��ͷ�ɨ�������󣻱�����������һ�Ρ�
+ ������
+ instance [in]: ɨ����ָ�롣
+ ����ֵ��
+ �ޡ�
+ */
+ void rulescan_destroy(void * instance);
+
+ /*
+ ���ܣ�����һ����ʽɨ�裬���뱣����״̬�IJ�����
+ ������
+ instance [in]: ɨ����ָ�룻
+ thread_id [in]: ��ǰִ��ɨ����߳�id�������ڷ�Χ[0, max_thread_num-1]֮�ڡ�
+ ����ֵ��
+ ���ر�����״̬�IJ���������ֵΪNULLʱ����ʾʧ�ܡ�
+ */
+ void * rulescan_startstream(void * instance, unsigned int thread_id);
+
+ /*
+ ���ܣ���ʽɨ��ӿڣ�ɨ��һ������scan_data�������м�ɨ������������״̬stream_param�У�
+ ������֧������ɨ��ģʽ��
+ ��1��presults��ΪNULL������������ɨ�����������ͨ��*presults����һ��˴ε����н����
+ ��2��presultsΪNULL ����ȫɨ��ģʽ����ʾ�ȴ�������ɨ��������ټ��㲢������������ȫ�����н����
+ ���rulescan_computeresult������
+ ������
+ stream_param [in]: ������״̬�IJ�����
+ scan_data [in]: ��ɨ�����ݣ�
+ presults [out]: ���presults��ΪNULL�������DZ������е�һ��ɨ������
+ size [in]: �������presults�Ĵ�С��
+ ����ֵ��
+ �������ʽ����ȫɨ�裨presultsΪNULL�����򷵻�ֵΪ����ɨ�����еĹؼ��ʣ���ֵ���䡢IP����ȣ��ĸ�����
+ ���presult��ΪNULL���򷵻�ֵΪ����ɨ�����е������ʽ�ĸ�����-1��ʾ������
+ */
+ int rulescan_searchstream(void * stream_param, scan_data_t * scan_data, scan_result_t * presults, unsigned int size);
+
+ /*
+ ���ܣ�������ɨ������󣬼���ɨ������
+ ������
+ stream_param [in]: ������״̬��ָ�룻
+ presults [out]: ������飬�����������е������ʽ����id��
+ size [in]: �������presults�Ĵ�С��
+ ����ֵ��
+ ���������ʽ�����������<=size��������ֵΪ-1��ʾ������
+ */
+ int rulescan_computeresult(void * stream_param, scan_result_t * presults, unsigned int size);
+
+ /*
+ ���ܣ�������ʽɨ�裬�ͷ���������������������µ��øýӿ��ͷ�����������������ָ�ڽ�����ɨ��֮ǰ��û�е���
+ rulescan_destroy����rulescan�����
+ ������
+ stream_param [in]: ������ָ�롣
+ ����ֵ��
+ �ޡ�
+ */
+ void rulescan_endstream(void * stream_param);
+
+ /*
+ ���ܣ�������ʽɨ�裬�ͷ��������������ȵ���rulescan_destroy������rulescan���֮���ٽ�����ɨ��������
+ ��Ҫ����rulescan_endstream_simple���ͷ�����������������������²ŵ��øýӿڡ�
+ ������
+ stream_param [in]: ������ָ�롣
+ ����ֵ��
+ �ޡ�
+ */
+ void rulescan_endstream_simple(void * stream_param);
+
+ /*
+ ���ܣ�����ʽɨ��ӿڣ�ɨ������󷵻��������н����
+ ������
+ instance [in]: ɨ����ָ�룻
+ thread_id [in]: ��ǰִ��ɨ����߳�id�������ڷ�Χ[0, max_thread_num-1]֮�ڣ�
+ scan_data [in]: ��ɨ�����ݣ�
+ presults [out]: ������飬�����������е������ʽ����id��
+ size [in]: �������presults�Ĵ�С��
+ ����ֵ��
+ ���������ʽ�����������<=size��������ֵΪ-1��ʾ������
+ */
+ int rulescan_search(void * instance, unsigned int thread_id, scan_data_t * scan_data, scan_result_t * presults, unsigned int size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* !defined(H_RULE_SCAN_H) */
+