📄 sp_byte_check.c
字号:
/* $Id$ *//* ** Copyright (C) 2002-2006 Sourcefire, Inc. ** Author: Martin Roesch ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License Version 2 as ** published by the Free Software Foundation. You may not use, modify or ** distribute this program under any other version of the GNU General ** Public License. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* sp_byte_check * * Purpose: * Test a byte field against a specific value (with operator). Capable * of testing binary values or converting represenative byte strings * to their binary equivalent and testing them. * * * Arguments: * Required: * <bytes_to_convert>: number of bytes to pick up from the packet * <operator>: operation to perform to test the value (<,>,=,!) * <value>: value to test the converted value against * <offset>: number of bytes into the payload to start processing * Optional: * ["relative"]: offset relative to last pattern match * ["big"]: process data as big endian (default) * ["little"]: process data as little endian * ["string"]: converted bytes represented as a string needing conversion * ["hex"]: converted string data is represented in hexidecimal * ["dec"]: converted string data is represented in decimal * ["oct"]: converted string data is represented in octal * * sample rules: * alert udp $EXTERNAL_NET any -> $HOME_NET any \ * (msg:"AMD procedure 7 plog overflow "; \ * content: "|00 04 93 F3|"; \ * content: "|00 00 00 07|"; distance: 4; within: 4; \ * byte_test: 4,>, 1000, 20, relative;) * * alert tcp $EXTERNAL_NET any -> $HOME_NET any \ * (msg:"AMD procedure 7 plog overflow "; \ * content: "|00 04 93 F3|"; \ * content: "|00 00 00 07|"; distance: 4; within: 4; \ * byte_test: 4, >,1000, 20, relative;) * * alert udp any any -> any 1234 \ * (byte_test: 4, =, 1234, 0, string, dec; \ * msg: "got 1234!";) * * alert udp any any -> any 1235 \ * (byte_test: 3, =, 123, 0, string, dec; \ * msg: "got 123!";) * * alert udp any any -> any 1236 \ * (byte_test: 2, =, 12, 0, string, dec; \ * msg: "got 12!";) * * alert udp any any -> any 1237 \ * (byte_test: 10, =, 1234567890, 0, string, dec; \ * msg: "got 1234567890!";) * * alert udp any any -> any 1238 \ * (byte_test: 8, =, 0xdeadbeef, 0, string, hex; \ * msg: "got DEADBEEF!";) * * Effect: * * Reads in the indicated bytes, converts them to an numeric * representation and then performs the indicated operation/test on * the data using the value field. Returns 1 if the operation is true, * 0 if it is not. * * Comments: * * Any comments? * */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <sys/types.h>#include <stdlib.h>#include <ctype.h>#ifdef HAVE_STRINGS_H#include <strings.h>#endif#include <errno.h>#include "bounds.h"#include "byte_extract.h"#include "rules.h"#include "decode.h"#include "plugbase.h"#include "parser.h"#include "debug.h"#include "util.h"#include "plugin_enum.h"#include "mstring.h"#define BT_LESS_THAN 1#define BT_EQUALS 2#define BT_GREATER_THAN 3#define BT_AND 4#define BT_OR 5#define BIG 0#define LITTLE 1#define PARSELEN 10#define TEXTLEN (PARSELEN + 2)extern const u_int8_t *doe_ptr;typedef struct _ByteTestData{ u_int32_t bytes_to_compare; /* number of bytes to compare */ u_int32_t cmp_value; u_int32_t operator; int32_t offset; u_int8_t not_flag; u_int8_t relative_flag; u_int8_t data_string_convert_flag; u_int8_t endianess; u_int32_t base;} ByteTestData;extern u_int8_t DecodeBuffer[DECODE_BLEN];void ByteTestInit(char *, OptTreeNode *, int);void ByteTestParse(char *, ByteTestData *, OptTreeNode *);int ByteTest(Packet *, struct _OptTreeNode *, OptFpList *);/**************************************************************************** * * Function: SetupByteTest() * * Purpose: Load 'er up * * Arguments: None. * * Returns: void function * ****************************************************************************/void SetupByteTest(void){ /* map the keyword to an initialization/processing function */ RegisterPlugin("byte_test", ByteTestInit, OPT_TYPE_DETECTION); DEBUG_WRAP(DebugMessage(DEBUG_PLUGIN,"Plugin: ByteTest Setup\n"););}/**************************************************************************** * * Function: ByteTestInit(char *, OptTreeNode *) * * Purpose: Generic rule configuration function. Handles parsing the rule * information and attaching the associated detection function to * the OTN. * * Arguments: data => rule arguments/data * otn => pointer to the current rule option list node * protocol => protocol the rule is on (we don't care in this case) * * Returns: void function * ****************************************************************************/void ByteTestInit(char *data, OptTreeNode *otn, int protocol){ ByteTestData *idx; OptFpList *fpl; /* allocate the data structure and attach it to the rule's data struct list */ idx = (ByteTestData *) calloc(sizeof(ByteTestData), sizeof(char)); if(idx == NULL) { FatalError("%s(%d): Unable to allocate byte_test data node\n", file_name, file_line); } /* this is where the keyword arguments are processed and placed into the rule option's data structure */ ByteTestParse(data, idx, otn); fpl = AddOptFuncToList(ByteTest, otn); /* attach it to the context node so that we can call each instance * individually */ fpl->context = (void *) idx; if (idx->relative_flag == 1) fpl->isRelative = 1;}/**************************************************************************** * * Function: ByteTestParse(char *, ByteTestData *, OptTreeNode *) * * Purpose: This is the function that is used to process the option keyword's * arguments and attach them to the rule's data structures. * * Arguments: data => argument data * idx => pointer to the processed argument storage * otn => pointer to the current rule's OTN * * Returns: void function * ****************************************************************************/void ByteTestParse(char *data, ByteTestData *idx, OptTreeNode *otn){ char **toks; char *endp; int num_toks; char *cptr; int i =0; toks = mSplit(data, ",", 12, &num_toks, 0); if(num_toks < 4) FatalError("ERROR %s (%d): Bad arguments to byte_test: %s\n", file_name, file_line, data); /* set how many bytes to process from the packet */ idx->bytes_to_compare = strtol(toks[0], &endp, 10); if(toks[0] == endp) { FatalError("%s(%d): Unable to parse as byte value %s\n", file_name, file_line, toks[0]); } if(idx->bytes_to_compare > PARSELEN || idx->bytes_to_compare == 0) { FatalError("%s(%d): byte_test can't process more than " "10 bytes!\n", file_name, file_line); } cptr = toks[1]; while(isspace((int)*cptr)) {cptr++;} if(*cptr == '!') { DEBUG_WRAP(DebugMessage(DEBUG_PATTERN_MATCH, "enabling not flag\n");); idx->not_flag = 1; cptr++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -