Since the different types of plugins are very similar in structure, I will explain the process of developing one by example. For this example we will follow the development of an output plugin. Specifically, we will incorporate the FastAlert commandline output format into a plugin. The format for this plugin (see the section about the rule file for more information on syntax) will be:
output fastalert: /path/alertfileThere are six simple steps to writing a plugin. These are:
#include "snort.h" #ifndef __SPO_FASTALERT_H__ #define __SPO_FASTALERT_H__ /* list of function prototypes for this plugin */ #endif /* __SPO_FAST_ALERT_H__ */This includes the snort header file, so we have access to other definitions, and it adds a set of defines to protect against recursive includes.
/* plugin header file */ #include "spo_alert_fast.h" /* external globals from rules.c */ extern char *file_name; extern int file_line;This includes the plugin header file and defines some global variables that are useful when reporting a syntax error from the parsing function. Additionally, these two files will often contain comments describing what the plugin does and copyright and licensing information.
void SetupFastAlert()
{
/* link the preprocessor keyword to the init function in
the preproc list */
RegisterOutputPlugin("fastalert", NT_OUTPUT_ALERT, FastAlertInit);
#ifdef DEBUG
printf("Output plugin: FastAlert is setup...\n");
#endif
}
This function calls the function RegisterOutputPlugin with arguments for the
keyword, the output type, and the name of the initialization function. There
are currently two supported output types: alert and log. The definition for
the alert output type is NT_OUTPUT_ALERT. For the log output type it
is NT_OUTPUT_LOG. The functions for registering preprocessors and
detection plugins are slightly different. They do not have the argument for
the output type. Here are sample calls for registering a preprocessor
and detection plugin, respectively.
RegisterPreprocessor("keyword", PreprocessorInit);
RegisterPlugin("keyword", DetectionPluginInit);
In addition to the code in spo_alert_fast.c, we must add a function prototype
to the file spo_alert_fast.h. This code would need to be inserted just after
the "/* list of function prototypes ..." comment and it would look like this:
void SetupFastAlert();It is important to remember that the keywords used for plugins must be unique. Once we have one plugin with the keyword fastalert we cannot have another one.
void SetupPlugin()
{
/* map the keyword to an initialization/processing function */
RegisterPlugin("keyword", PluginInit);
#ifdef DEBUG
printf("Plugin: PluginName Registered\n");
#endif
}
In order for this plugin to be available in snort, the setup function must be
called.