<menu id="guoca"></menu>
<nav id="guoca"></nav><xmp id="guoca">
  • <xmp id="guoca">
  • <nav id="guoca"><code id="guoca"></code></nav>
  • <nav id="guoca"><code id="guoca"></code></nav>

    4.1 Snort 數據結構

    許多數據結構是API的核心。下面幾節定義了它們的定義。

    4.1.1 DynamicPluginMeta

    DynamicPluginMeta結構定義了動態模塊的類型(預處理器、規則或檢測引擎)、版本信息和共享庫的路徑。共享庫可以實現所有這三種類型,但通常限于單一功能,如預處理器。在’ sf_dynamic_meta.h ‘中定義為:

    #define MAX_NAME_LEN 1024
    
    #define TYPE_ENGINE 0x01
    #define TYPE_DETECTION 0x02
    #define TYPE_PREPROCESSOR 0x04
    
    typedef struct _DynamicPluginMeta
    {
        int type;
        int major;
        int minor;
        int build;
        char uniqueName[MAX_NAME_LEN];
        char *libraryPath;
    } DynamicPluginMeta;

    4.1.2 DynamicPreprocessorData

    DynamicPreprocessorData結構定義預處理器用于與snort本身交互的接口。這包括注冊預處理器配置解析、重啟、退出和處理函數的函數。它包括記錄消息、錯誤、致命錯誤和調試信息的功能。它還包括設置警報、處理內聯drop、訪問StreamAPI的信息,并提供對規范化http和備用數據緩沖區的訪問。在加載預處理器共享庫時,應該初始化該數據結構。它在’ sf_dynamic_preprocessor.h ‘中定義。檢查頭文件以獲取當前定義。

    4.1.3 DynamicEngineData

    DynamicEngineData 結構定義檢測引擎用于與snort本身交互的接口。這包括記錄消息、錯誤、致命錯誤和調試信息的函數,以及注冊和檢查流位的方法。它還包含一個位置,用于存儲加載的動態規則的規則存根,并提供對規范化http和備用數據緩沖區的訪問。在’ sf_dynamic_engine.h ‘中定義為:

    typedef struct _DynamicEngineData
    {
        int version;
        u_int8_t *altBuffer;
        UriInfo *uriBuffers[MAX_URIINFOS];
        RegisterRule ruleRegister;
        RegisterBit flowbitRegister;
        CheckFlowbit flowbitCheck;
        DetectAsn1 asn1Detect;
        LogMsgFunc logMsg;
        LogMsgFunc errMsg;
        LogMsgFunc fatalMsg;
        char *dataDumpDirectory;
    
        GetPreprocRuleOptFuncs getPreprocOptFuncs;
    
        SetRuleData setRuleData;
        GetRuleData getRuleData;
    
        DebugMsgFunc debugMsg;
    #ifdef HAVE_WCHAR_H
        DebugWideMsgFunc debugWideMsg;
    #endif
    
        char **debugMsgFile;
        int *debugMsgLine;
    
        PCRECompileFunc pcreCompile;
        PCREStudyFunc pcreStudy;
        PCREExecFunc pcreExec;
    
    } DynamicEngineData;

    4.1.4 SFSnortPacket

    SFSnortPacket結構反映了snort包結構,并提供對包含在給定包中的所有數據的訪問。

    它和它合并的數據結構在‘sf_snort_package .h’中定義。可以定義其他數據結構來引用其他協議字段。檢查頭文件以獲取當前定義。

    4.1.5 Dynamic規則

    Dynamic規則應該使用以下任何一種數據結構。以下結構是在‘sf_snort_plugin_api.h’中定義的。

    4.1.5.1 Rule

    Rule結構定義了規則的基本大綱,并包含與文本規則相同的一組信息。其中包括協議、地址和端口信息以及規則信息(分類、生成器和簽名id、修訂、優先級、分類和引用列表)。它還包括一個規則選項列表和一個可選的計算函數。

    #define RULE_MATCH 1
    #define RULE_NOMATCH 0
    
    typedef struct _Rule
    {
        IPInfo ip;
        RuleInformation info;
    
        RuleOption **options; /* NULL terminated array of RuleOption union */
    
        ruleEvalFunc evalFunc;
    
        char initialized;     /* Rule Initialized, used internally */
        u_int32_t numOptions; /* Rule option count, used internally */
        char noAlert;         /* Flag with no alert, used internally */
        void *ruleData;    /* Hash table for dynamic data pointers */
    } Rule;
    
    The rule evaluation function is defined as
    
    typedef int (*ruleEvalFunc)(void *);
    
    where the parameter is a pointer to the SFSnortPacket structure.

    4.1.5.2 RuleInformation

    RuleInformation結構定義了規則的元數據,包括生成器ID、簽名ID、修訂、分類、優先級、消息文本和引用列表。

    typedef struct _RuleInformation
    {
        u_int32_t genID;
        u_int32_t sigID;
        u_int32_t revision;
        char     *classification; /* String format of classification name */
        u_int32_t priority;
        char     *message;
        RuleReference **references; /* NULL terminated array of references */
        RuleMetaData **meta; /* NULL terminated array of references */
    } RuleInformation;

    4.1.5.3 RuleReference

    RuleReference 結構定義單個規則引用,包括系統名稱和重新引用標識符。

    typedef struct _RuleReference
    {
        char *systemName;
        char *refIdentifier;
    } RuleReference;

    4.1.5.4 IPInfo

    IPInfo結構定義了規則的初始匹配標準,包括協議,src地址和端口,目的地址和端口,以及方向。一些標準的字符串和變量是預定義的——any, HOME_NET, HTTP_SERVERS, HTTP_PORTS,等等。

    typedef struct _IPInfo
    {
        u_int8_t protocol;
        char *   src_addr;
        char *   src_port; /* 0 for non TCP/UDP */
        char     direction;     /* non-zero is bi-directional */
        char *   dst_addr;
        char *   dst_port; /* 0 for non TCP/UDP */
    } IPInfo;
    
    #define ANY_NET         "any"
    #define HOME_NET        "$HOME_NET"
    #define EXTERNAL_NET    "$EXTERNAL_NET"
    #define ANY_PORT        "any"
    #define HTTP_SERVERS    "$HTTP_SERVERS"
    #define HTTP_PORTS      "$HTTP_PORTS"
    #define SMTP_SERVERS    "$SMTP_SERVERS"

    4.1.5.5 RuleOption

    RuleOption 結構將單個規則選項定義為選項類型和對特定于該選項的數據的引用。每個選項都有一個flags字段,該字段包含該選項的特定標記以及一個“Not”標記。“Not”標志用于否定該選項的評估結果。

    typedef enum Dynamic選項類型 {
         OPTION_TYPE_PREPROCESSOR,
         OPTION_TYPE_CONTENT,
         OPTION_TYPE_PCRE,
         OPTION_TYPE_FLOWBIT,
         OPTION_TYPE_FLOWFLAGS,
         OPTION_TYPE_ASN1,
         OPTION_TYPE_CURSOR,
         OPTION_TYPE_HDR_CHECK,
         OPTION_TYPE_BYTE_TEST,
         OPTION_TYPE_BYTE_JUMP,
         OPTION_TYPE_BYTE_EXTRACT,
         OPTION_TYPE_SET_CURSOR,
         OPTION_TYPE_LOOP,
         OPTION_TYPE_MAX
    };
    
    typedef struct _RuleOption
    {
        int optionType;
        union
        {   
            void *ptr;
            ContentInfo *content;
            CursorInfo *cursor;
            PCREInfo *pcre;
            FlowBitsInfo *flowBit;
            ByteData *byte;
            ByteExtract *byteExtract;
            FlowFlags *flowFlags;
            Asn1Context *asn1;
            HdrOptCheck *hdrData;
            LoopInfo    *loop;
            PreprocessorOption *preprocOpt;
        } option_u;
    } RuleOption;
    
    #define NOT_FLAG                0x10000000

    一些選項還包含在運行時初始化的信息,如編譯的PCRE信息、Boyer-Moore內容信息、流位的整數ID等。

    下面列出了選項類型和相關結構。

    • 選項類型: content & Structure: ContentInfo

      ContentInfo 結構為內容搜索定義了一個選項。它包括模式、深度和偏移量以及標志(其中一個必須指定要搜索的緩沖區——raw、URI或規范化)。其他標志包括nocase、relative、unicode和用于snorts快速模式計算的此內容的名稱。應該標記最獨特的內容,即將該規則區分為與包的可能匹配的內容,以便進行快速模式評估。在Snort提供的動態檢測引擎中,如果給定規則中沒有ContentInfo結構使用該標志,則將使用內容長度最長的標志。

        typedef struct _ContentInfo
        {
            u_int8_t *pattern;
            u_int32_t depth;
            int32_t   offset;
            u_int32_t flags;        /* must include a CONTENT_BUF_X */
            void     *boyer_ptr;
            u_int8_t *patternByteForm;
            u_int32_t patternByteFormLength;
            u_int32_t incrementLength;
        } ContentInfo;
    
        #define CONTENT_NOCASE          0x01
        #define CONTENT_RELATIVE        0x02
        #define CONTENT_UNICODE2BYTE    0x04
        #define CONTENT_UNICODE4BYTE    0x08
        #define CONTENT_FAST_PATTERN    0x10
        #define CONTENT_END_BUFFER      0x20
    
        #define CONTENT_BUF_NORMALIZED  0x100
        #define CONTENT_BUF_RAW         0x200
        #define CONTENT_BUF_URI         0x400
    • 選項類型: PCRE & Structure: PCREInfo

      PCREInfo 結構為PCRE搜索定義了一個選項。它包括PCRE表達式pcre_flags,如PCRE中定義的caseless。和指定緩沖區的標志。

        /*
        pcre.h provides flags:
    
        PCRE_CASELESS
        PCRE_MULTILINE
        PCRE_DOTALL
        PCRE_EXTENDED
        PCRE_ANCHORED
        PCRE_DOLLAR_ENDONLY
        PCRE_UNGREEDY
        */
    
        typedef struct _PCREInfo
        {
            char     *expr;
            void     *compiled_expr;
            void     *compiled_extra;
            u_int32_t compile_flags;
            u_int32_t flags; /* must include a CONTENT_BUF_X */
        } PCREInfo;
    • 選項類型: Flowbit & Structure: FlowBitsInfo

      FlowBitsInfo 結構定義了一個flowbits選項。它包括flowbit的名稱和操作(set, setx, unset, toggle, isset, isnotset)。

        #define FLOWBIT_SET       0x01
        #define FLOWBIT_UNSET     0x02
        #define FLOWBIT_TOGGLE    0x04
        #define FLOWBIT_ISSET     0x08
        #define FLOWBIT_ISNOTSET  0x10
        #define FLOWBIT_RESET     0x20
        #define FLOWBIT_NOALERT   0x40
        #define FLOWBIT_SETX      0x80
    
        typedef struct _FlowBitsInfo
        {
            char     *flowBitsName;
            uint8_t   operation;
            uint16_t  id;
            uint32_t  flags;
            char     *groupName;
            uint8_t   eval;
            uint16_t *ids;
            uint8_t   num_ids;
    
        } FlowBitsInfo;
    • 選項類型: Flow Flags & Structure: FlowFlags

      FlowFlags結構定義了一個流選項。它包括指定方向(from_server、to_server)、已建立會話等的標志。

        #define FLOW_ESTABLISHED 0x10
        #define FLOW_IGNORE_REASSEMBLED 0x1000
        #define FLOW_ONLY_REASSMBLED    0x2000
        #define FLOW_FR_SERVER   0x40
        #define FLOW_TO_CLIENT   0x40 /* Just for redundancy */
        #define FLOW_TO_SERVER   0x80
        #define FLOW_FR_CLIENT   0x80 /* Just for redundancy */
    
        typedef struct _FlowFlags
        {
            u_int32_t   flags;
        } FlowFlags;
    • 選項類型: ASN.1 & Structure: Asn1Context

      Asn1Context結構定義了ASN1選項的信息。它鏡像ASN1規則選項,還包括一個標志字段。

        #define ASN1_ABS_OFFSET 1
        #define ASN1_REL_OFFSET 2
    
        typedef struct _Asn1Context
        {
            int bs_overflow;
            int double_overflow;
            int print;
            int length;
            unsigned int max_length;
            int offset;
            int offset_type;
            u_int32_t  flags;
        } Asn1Context;
    • 選項類型: Cursor Check & Structure:CursorInfo

      CursorInfo 結構定義游標計算的選項。游標是計算緩沖區中的當前位置,與內容和PCRE搜索以及字節測試和字節跳轉相關。它包括指定緩沖區的偏移量和標志。這可以用來驗證是否有足夠的數據來繼續評估,類似于isdataat規則選項。

        typedef struct _CursorInfo
        {
            int32_t   offset;
            u_int32_t flags;        /* specify one of CONTENT_BUF_X */
        } CursorInfo;
    • 選項類型: Protocol Header & Structure: HdrOptCheck

      HdrOptCheck 結構定義了一個選項,用于檢查協議頭的特定值。它包括頭字段、操作(<、>、=等)、一個值、用于忽略頭字段的掩碼和標志。

        #define IP_HDR_ID           0x0001  /* IP Header ID */
        #define IP_HDR_PROTO        0x0002  /* IP Protocol */
        #define IP_HDR_FRAGBITS     0x0003  /* Frag Flags set in IP Header */
        #define IP_HDR_FRAGOFFSET   0x0004  /* Frag Offset set in IP Header */
        #define IP_HDR_OPTIONS      0x0005  /* IP Options -- is option xx included */
        #define IP_HDR_TTL          0x0006  /* IP Time to live */
        #define IP_HDR_TOS          0x0007  /* IP Type of Service */
        #define IP_HDR_OPTCHECK_MASK 0x000f
    
        #define TCP_HDR_ACK         0x0010  /* TCP Ack Value */
        #define TCP_HDR_SEQ         0x0020  /* TCP Seq Value */
        #define TCP_HDR_FLAGS       0x0030  /* Flags set in TCP Header */
        #define TCP_HDR_OPTIONS     0x0040  /* TCP Options -- is option xx included */
        #define TCP_HDR_WIN         0x0050  /* TCP Window */
        #define TCP_HDR_OPTCHECK_MASK 0x00f0
    
        #define ICMP_HDR_CODE       0x1000  /* ICMP Header Code */
        #define ICMP_HDR_TYPE       0x2000  /* ICMP Header Type */
        #define ICMP_HDR_ID         0x3000  /* ICMP ID for ICMP_ECHO/ICMP_ECHO_REPLY */
        #define ICMP_HDR_SEQ        0x4000  /* ICMP ID for ICMP_ECHO/ICMP_ECHO_REPLY */
        #define ICMP_HDR_OPTCHECK_MASK 0xf000
    
        typedef struct _HdrOptCheck
        {
            u_int16_t hdrField;   /* Field to check */
            u_int32_t op;         /* Type of comparison */
            u_int32_t value;      /* Value to compare value against */
            u_int32_t mask_value; /* bits of value to ignore */
            u_int32_t flags;
        } HdrOptCheck;
    • 選項類型: Byte Test & Structure: ByteData

      ByteData結構定義字節測試和字節跳轉操作的信息。它包括字節數、操作(對于ByteTest,< >,=等)、值、偏移量、乘數和標志。標記必須指定緩沖區。

        #define CHECK_EQ            0
        #define CHECK_NEQ           1
        #define CHECK_LT            2
        #define CHECK_GT            3
        #define CHECK_LTE           4
        #define CHECK_GTE           5
        #define CHECK_AND           6
        #define CHECK_XOR           7
        #define CHECK_ALL           8
        #define CHECK_ATLEASTONE    9
        #define CHECK_NONE          10
    
        typedef struct _ByteData
        {
            u_int32_t bytes;      /* Number of bytes to extract */
            u_int32_t op;         /* Type of byte comparison, for checkValue */
            u_int32_t value;      /* Value to compare value against, for checkValue, or extracted value */
            int32_t   offset;     /* Offset from cursor */
            u_int32_t multiplier; /* Used for byte jump -- 32bits is MORE than enough */
            u_int32_t flags;      /* must include a CONTENT_BUF_X */
        } ByteData;
    • 選項類型: Byte Jump & Structure: ByteData

      請參閱上面的Byte Test

    • 選項類型: Set Cursor & Structure: CursorInfo

      請參閱上面的Cursor Check

    • 選項類型: Loop & Structures: LoopInfo,ByteExtract,DynamicElement

      LoopInfo結構定義了要重復評估的一組選項的信息。loop選項的作用類似于FOR循環,包括開始、結束和增量值以及終止的比較操作。它包括在循環的每次迭代中發生的游標調整,以及在每次迭代中計算對定義規則選項的規則info結構的引用。這些選項之一可能是字節提取。

        typedef struct _LoopInfo
        {
            DynamicElement *start;      /* Starting value of FOR loop (i=start) */
            DynamicElement *end;        /* Ending value of FOR loop (i OP end) */
            DynamicElement *increment;  /* Increment value of FOR loop (i+= increment) */
            u_int32_t op;               /* Type of comparison for loop termination */
            CursorInfo *cursorAdjust;   /* How to move cursor each iteration of loop */
            struct _Rule *subRule;      /* Pointer to SubRule & options to evaluate within
                                         * the loop */
            u_int8_t initialized;       /* Loop initialized properly (safeguard) */
            u_int32_t flags;            /* can be used to negate loop results, specifies                                 * relative. */
        } LoopInfo;
    

    ByteExtract結構定義在為循環中使用的DynamicElement提取字節時要使用的信息。它包括字節數、偏移量、乘數、指定緩沖區的標志和對DynamicElement的引用。

        typedef struct _ByteExtract
        {
            u_int32_t bytes;      /* Number of bytes to extract */
            int32_t   offset;     /* Offset from cursor */
            u_int32_t multiplier; /* Multiply value by this (similar to byte jump) */
            u_int32_t flags;      /* must include a CONTENT_BUF_X */
            char *refId;          /* To match up with a DynamicElement refId */
            void *memoryLocation; /* Location to store the data extracted */
        } ByteExtract;

    DynamicElement*結構用于定義循環求值的值。它包括元素是靜態的(整數)還是動態的(從包中的緩沖區中提取)以及值。對于動態元素,值由循環中的相關ByteExtract選項填充。

        #define DYNAMIC_TYPE_INT_STATIC 1
        #define DYNAMIC_TYPE_INT_REF    2
    
        typedef struct _DynamicElement
        {
            char dynamicType;           /* type of this field - static or reference */
            char *refId;                /* reference ID (NULL if static) */
            union
            {
                void *voidPtr;          /* Holder */
                int32_t staticInt;        /* Value of static */
                int32_t *dynamicInt;  /* Pointer to value of dynamic */
            } data;
        } DynamicElement;

    本文章首發在 網安wangan.com 網站上。

    上一篇 下一篇
    討論數量: 0
    只看當前版本


    暫無話題~
    亚洲 欧美 自拍 唯美 另类