Snort预处理插件HelloWorld程序开发

文章结构

  • 1,预处理插件开发注意
  • 2,开发步骤
  • 3,DPX介绍
  • 4,附录:代码
  • 5,参考文献

预处理器开发注意

Preprocessors perform some function once for each packet. This is different from detection plugins, which are accessed depending on the standard rules. When adding a plugin to the system, be sure to add the “Setup” function to the InitPreprocessors() function call in plugbase.c!

注意:本文的Snort版本是2.9.13。

预处理器对每一个数据包只执行一次,它不同于检测插件(检测插件的获取依赖于基本规则 rules).当我们给Snort添加预处理插件的时候,请务必将"Setup"函数添加到plugbase.c源文件中的**InitPreprocessors()**函数中去。

注意:上面是Snort源文件中spp_template.c (预处理器模板)中的注释,这个注释的版本没有及时更新,原文中的InitPreprocessors( ) 函数已经被替换成了 RegistPreprocessors( )

开发步骤

编写过程简述:

  1. 根据spp_template.c里面的内容修改spp_template.cspp_template.h
    (1) 将这两个文件名称修改为spp_hello.cspp_hello.h
  2. 修改spp_hello.c函数
    (1) SetupHello
    (2) HelloFunction
  3. 修改spp_hello.h函数
    (1) 声明SetupHello
  4. 修改plugbase.c函数
    (1) #include preprocessors/spp_hello.h
    (2) RegistProcesser函数中调用SetupHello
  5. 修改preprocids.h
    (1) #define PP_MAX 38
    (2) #define PP_HELLO 37
    对新增的解码类型自己定义的标志
  6. 修改Makefile.am,并在根目录运行automake命令
  7. make, make install 直接覆盖已安装的snort
  8. 修改/etc/snort/snort.conf
  9. 测试:snort -dev -c /etc/snort/snort.conf

过程中出现的问题:
1- aumake版本的确定
a) 查看源代码目录下的文件Makefile
Makefile.in generated by automake 1.16.1 from Makefile.am
b) 版本为automake1.16.1

2- 预处理插件被加载而且初始化完成,但是预处理插件主函数不工作
a) 原因是新版本的Snort新增加了session_api->enable_preproc_all_ports
加入这一行代码后,预处理器运行正常

3-安装完之后需要配置Snort
每个预处理器都在snort.conf中进行单独配置,如果不配置就不能使用

preprocessor Hello

4-关于Snort的安装
请参考Snort官网给出的学习文档(很好用哦)或者网上的一些参考资料
( p.s.这里就是懒一下,或许我以后会补上来)

5-关于spp_template文件的一些说明

详情请参考spp_template.c中的注释。

由于这里只是实现了比较简单地在控制台打印语句的HelloWolrd程序,所以像参数鸡西函数ParseTemplateArgs等等就没有使用。
改名:

原名 新名 修改
spp_tempalte.h spp_hello.h 添加SetupHello函数的声明
spp_tempalte.c spp_hello.c
TemplateInit HelloInit 添加初始化操作,通过调用AddFuncToPreprocList 将PreprocHello、PreprocCleanExitHello、PreprocRestartHello注册给系统
SetupTemplate SetupHello 调用RegisterPreprocessor 将此插件的初始化函数HelloInit注册给系统
ParseTemplateArgs xxxx 添加对参数的处理操作
PreprocFunciton HelloFunction 加入预处理逻辑代码
PreproCleanExitFunction xxxx 添加插件退出时的清理操作
PreproRestartFunction xxxx 添加插件重启时的操作。(不常用,空操作)

运行结果:
Snort预处理插件HelloWorld程序开发_第1张图片

DPX

You can think of this as a dynamic preprocessor “starter kit”, the goal of which is to make it as simple as possible to prototype a dynamic preprocessor.This one is called DPX, for “Dynamic Preprocessor Example”. You can build DPX,which is very trivial, and then change it to do what you need.
(from readme file)

DPX(Dynamic Preprocessor Example,动态预处理器范例) 是动态预处理插件的简单构建器,它的目的是尽可能简单地编写一个预处理器。你可以很轻松地build DPX,把它改写成你需要的预处理器。

Snort官网上提供源代码下载。需要的请自行去下载。
下载地址:https://www.snort.org/documents/38

p.s 这个预处理器构建器,我还没有仔细研究。等有需要的时候再研究 (to do…)

附录

代码

spp_hello.c

/* $Id$ */
/* Snort Preprocessor Plugin Source File Template */

/*
* Purpose:
*
* Preprocessors perform some function *once* for *each* packet.  This is
* different from detection plugins, which are accessed depending on the
* standard rules.  When adding a plugin to the system, be sure to
* add the "Setup" function to the InitPreprocessors() function call in
* plugbase.c!
*
* Arguments:
*
* This is the list of arguements that the plugin can take at the
* "preprocessor" line in the rules file
*
* Effect:
*
* What the preprocessor does.  Check out some of the default ones
* (e.g. spp_frag2) for a good example of this description.
*
* Comments:
*
* Any comments?
*
*/

#include 
#include 
#include 
#include 
#include "snort_debug.h"
#include "session_api.h"
/*
* If you're going to issue any alerts from this preproc you
* should include generators.h and event_wrapper.h
*/
#include "generators.h"
#include "event_wrapper.h"
#include "decode.h"
#include "util.h"
#include "plugbase.h"
#include "parser.h"

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

#include "snort.h"
/*
* put in other inculdes as necessary
*/

/*
* your preprocessor header file goes here if necessary, don't forget
* to include the header file in plugbase.h too!
*/
#include "spp_hello.h"

#define  PROTO_MASK 0x0001

/*
* define any needed data structs for things like configuration
*/
typedef struct _TemplateData
{
   /* Your struct members here */
} TemplateData;

/*
* If you need to instantiate the preprocessor's
* data structure, do it here
*/
TemplateData SomeData;

/*
* function prototypes go here
*/

static void HelloSnortInit(struct _SnortConfig *sc,char *);
static void ParseTemplateArgs(char *);
static void HelloSnortFunct(Packet *);
static void PreprocCleanExitFunction(int, void *);
static void PreprocRestartFunction(int, void *);
static void helloSnortreloadFuction(char *args);
/*
* Function: SetupTemplate()
*
* Purpose: Registers the preprocessor keyword and initialization
*          function into the preprocessor list.  This is the function that
*          gets called from InitPreprocessors() in plugbase.c.
*
* Arguments: None.
*
* Returns: void function
*
*/
void SetupHello()
{
   /*
    * link the preprocessor keyword to the init function in
    * the preproc list
    */

#ifndef SNORT_RELOAD
   RegisterPreprocessor("Hello", HelloSnortInit);
#else
   RegisterPreprocessor("Hello", HelloSnortInit,
                        helloSnortreloadFuction, NULL,NULL, NULL);
#endif

   printf("now call the setupHelloSnort <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>");
   DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,"Preprocessor: HelloSnort is setup...\n"););
}


/*
* Function: TemplateInit(u_char *)
*
* Purpose: Calls the argument parsing function, performs final setup on data
*          structs, links the preproc function into the function list.
*
* Arguments: args => ptr to argument string
*
* Returns: void function
*
*/
static void HelloSnortInit(struct _SnortConfig *sc,char *args)
{
   DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,"Preprocessor: HelloSnortInit Initialized\n"););
    printf("HelloSnortInit ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^is setup");
   /*
    * parse the argument list from the rules file
    */
   //ParseTemplateArgs(args);

   /*
    * perform any other initialization functions that are required here
    */

   /*
    * Set the preprocessor function into the function list
    */
//example: AddFuncToPreprocList(PreprocEvalFunc pp_eval_func, uint16_t priority,uint32_t preproc_id, uint32_t proto_mask)
   AddFuncToPreprocList(sc,HelloSnortFunct, PRIORITY_NETWORK, PP_HELLO, PROTO_MASK);
   session_api->enable_preproc_all_ports( sc, PP_HELLO, PROTO_BIT__IP );

  // AddFuncToCleanExitList(PreprocCleanExitFunction, NULL);
  // AddFuncToRestartList(PreprocRestartFunction, NULL);
}

参考文献

Snort预处理器详细介绍

snort2.9源码预处理流转到detect模块分析(着重http模块)

snort2.9.3预处理插件步骤整理

Snort 2.9.8.2预处理器开发文档

developing-snort-dynamic-preprocessor_32874.pdf (强烈推荐,虽然我没看多少)

Snort官方文档:
《snort_manual_2.9.13.pdf》
《spp_template.c》
《README.PLUGINS》

上述文档均可在我的个人GitHub仓库下载
地址:https://github.com/hustuhao/Snort-Learning

你可能感兴趣的:(Snort)