1 /* Copyright (C) 2020 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Jeff Lucovsky <jeff@lucovsky.org>
22  *
23  * Implements the pcrexform transform keyword with option support
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "detect.h"
29 #include "detect-engine.h"
30 #include "detect-parse.h"
31 #include "detect-transform-pcrexform.h"
32 
33 typedef DetectParseRegex DetectTransformPcrexformData;
34 
35 static int DetectTransformPcrexformSetup (DetectEngineCtx *, Signature *, const char *);
36 static void DetectTransformPcrexformFree(DetectEngineCtx *, void *);
37 static void DetectTransformPcrexform(InspectionBuffer *buffer, void *options);
38 #ifdef UNITTESTS
39 void DetectTransformPcrexformRegisterTests (void);
40 #endif
41 
DetectTransformPcrexformRegister(void)42 void DetectTransformPcrexformRegister(void)
43 {
44     sigmatch_table[DETECT_TRANSFORM_PCREXFORM].name = "pcrexform";
45     sigmatch_table[DETECT_TRANSFORM_PCREXFORM].desc =
46         "modify buffer via PCRE before inspection";
47     sigmatch_table[DETECT_TRANSFORM_PCREXFORM].url = "/rules/transforms.html#pcre-xform";
48     sigmatch_table[DETECT_TRANSFORM_PCREXFORM].Transform =
49         DetectTransformPcrexform;
50     sigmatch_table[DETECT_TRANSFORM_PCREXFORM].Free =
51         DetectTransformPcrexformFree;
52     sigmatch_table[DETECT_TRANSFORM_PCREXFORM].Setup =
53         DetectTransformPcrexformSetup;
54 #ifdef UNITTESTS
55     sigmatch_table[DETECT_TRANSFORM_PCREXFORM].RegisterTests = DetectTransformPcrexformRegisterTests;
56 #endif
57     sigmatch_table[DETECT_TRANSFORM_PCREXFORM].flags |= SIGMATCH_QUOTES_MANDATORY;
58 }
59 
DetectTransformPcrexformFree(DetectEngineCtx * de_ctx,void * ptr)60 static void DetectTransformPcrexformFree(DetectEngineCtx *de_ctx, void *ptr)
61 {
62     if (ptr != NULL) {
63         DetectTransformPcrexformData *pxd = (DetectTransformPcrexformData *) ptr;
64         SCFree(pxd);
65     }
66 }
67 /**
68  *  \internal
69  *  \brief Apply the pcrexform keyword to the last pattern match
70  *  \param det_ctx detection engine ctx
71  *  \param s signature
72  *  \param regexstr options string
73  *  \retval 0 ok
74  *  \retval -1 failure
75  */
DetectTransformPcrexformSetup(DetectEngineCtx * de_ctx,Signature * s,const char * regexstr)76 static int DetectTransformPcrexformSetup (DetectEngineCtx *de_ctx, Signature *s, const char *regexstr)
77 {
78     SCEnter();
79 
80     // Create pxd from regexstr
81     DetectTransformPcrexformData *pxd = SCCalloc(sizeof(*pxd), 1);
82     if (pxd == NULL) {
83         SCLogDebug("pxd allocation failed");
84         SCReturnInt(-1);
85     }
86 
87     if (!DetectSetupParseRegexesOpts(regexstr, pxd, 0)) {
88         SCFree(pxd);
89         SCReturnInt(-1);
90     }
91 
92     int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_PCREXFORM, pxd);
93     if (r != 0) {
94         SCFree(pxd);
95     }
96 
97     SCReturnInt(r);
98 }
99 
DetectTransformPcrexform(InspectionBuffer * buffer,void * options)100 static void DetectTransformPcrexform(InspectionBuffer *buffer, void *options)
101 {
102     const char *input = (const char *)buffer->inspect;
103     const uint32_t input_len = buffer->inspect_len;
104     DetectTransformPcrexformData *pxd = options;
105 
106     int ov[MAX_SUBSTRINGS];
107     int ret = DetectParsePcreExecLen(pxd, input, input_len, 0, 0, ov, MAX_SUBSTRINGS);
108 
109     if (ret > 0) {
110         const char *str;
111         ret = pcre_get_substring((char *) buffer->inspect, ov,
112                                   MAX_SUBSTRINGS, ret - 1, &str);
113 
114         if (ret >= 0) {
115             InspectionBufferCopy(buffer, (uint8_t *)str, (uint32_t) ret);
116             pcre_free_substring(str);
117         }
118     }
119 }
120 
121 #ifdef UNITTESTS
122 #include "tests/detect-transform-pcrexform.c"
123 #endif
124