https://mp.csdn.net/postedit/88376541
前面分析过content的规则选项主要流程包括注册、解析
接下来我一个实例进行分析如何增加一个自定义的规则选项
首先进入RegisterRuleOptions函数中,编写自己的注册接口函数SetupTest, 规则选项关键字test, ,初始换函数TestInit, TestRuleParse解析该字段的函数,在什么时候生效OPT_TYPE_DETECTION, 更新规则选项中的数组ds_list,增加元素链表枚举类型RULE_OPTION_TYPE_TEST,设置处理匹配函数TestDetect,设计这个关键字的数据结构TestRuleOption, 在detection-plugins目录下增加sp_test.h|c两个源文件,并修改当前目录下的Makefile.am, 增加
libspd_a_SOURCES=...
sp_test.c sp_test.h
重新编译安装,这个增加test字段会被编译进程序中
void RegisterRuleOptions(void)
{
LogMessage("Initializing Plug-ins!\n");
SetupPatternMatch();
SetupTCPFlagCheck();
...
/* 自定义一个规则选项 */
SetupTest();
}
void SetupTest(void)
{
RegisterRuleOption("test", TestInit, NULL, OPT_TYPE_DETECTION, NULL);
#ifdef PERF_PROFILING
RegisterPreprocessorProfile("test", &testPerfStats, 3, &ruleOTNEvalPerfStats);
#endif
DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN, "Plugin: TEST Setup\n"););
}
/* encapsulate the rule option */
typedef struct _TestRuleOption
{
...
} TestRuleOption;
static void TestRuleParse(char *rule_args, TestRuleOption *test_rule_option)
{
char **toks;
int num_toks = 0;
toks = mSplit(rule_args, " \t\n", 2, &num_toks, 0);
...
mSplitFree(&toks, num_toks);
}
static void TestInit(struct _SnortConfig *sc, char *data, OptTreeNode *otn, int protocol)
{
TestRuleOption *test_rule_option;
void *ds_ptr_dup;
OptFpList *ofl;
test_rule_option= (TestRuleOption *)SnortAlloc(sizeof(TestRuleOption ));
TestRuleParse(data, cvs_rule_option);
if (add_detection_option(sc, RULE_OPTION_TYPE_TEST, (void *)test_rule_option, &ds_ptr_dup) == DETECTION_OPTION_EQUAL)
{
free(test_rule_option);
test_rule_option = ds_ptr_dup;
}
/* Attach detection function to rule's detect function ptr */
ofl = AddOptFuncToList(TestDetect, otn);
ofl->type = RULE_OPTION_TYPE_TEST;
ofl->context = (void *)test_rule_option;
}
这样就基本把怎么增加自定义规则选项讲完了