|
| 1 | +/* Copyright (C) 2024 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 | +use super::{ |
| 19 | + DetectHelperTransformRegister, DetectSignatureAddTransform, InspectionBufferCheckAndExpand, |
| 20 | + InspectionBufferLength, InspectionBufferPtr, InspectionBufferTruncate, SCTransformTableElmt, |
| 21 | +}; |
| 22 | +use crate::detect::SIGMATCH_NOOPT; |
| 23 | + |
| 24 | +use std::os::raw::{c_int, c_void}; |
| 25 | +use std::ptr; |
| 26 | + |
| 27 | +static mut G_TRANSFORM_DOT_PREFIX_ID: c_int = 0; |
| 28 | + |
| 29 | +#[no_mangle] |
| 30 | +unsafe extern "C" fn dot_prefix_setup( |
| 31 | + _de: *mut c_void, s: *mut c_void, _raw: *const std::os::raw::c_char, |
| 32 | +) -> c_int { |
| 33 | + return DetectSignatureAddTransform(s, G_TRANSFORM_DOT_PREFIX_ID, ptr::null_mut()); |
| 34 | +} |
| 35 | + |
| 36 | +fn dot_prefix_transform_do(input: &[u8], output: &mut [u8]) { |
| 37 | + output[0] = b'.'; |
| 38 | + output[1..].copy_from_slice(input); |
| 39 | +} |
| 40 | + |
| 41 | +#[no_mangle] |
| 42 | +unsafe extern "C" fn dot_prefix_transform(buffer: *mut c_void, _ctx: *mut c_void) { |
| 43 | + let input = InspectionBufferPtr(buffer); |
| 44 | + let input_len = InspectionBufferLength(buffer); |
| 45 | + if input.is_null() || input_len == 0 { |
| 46 | + return; |
| 47 | + } |
| 48 | + let input = build_slice!(input, input_len as usize); |
| 49 | + |
| 50 | + let output = InspectionBufferCheckAndExpand(buffer, input_len + 1); |
| 51 | + if output.is_null() { |
| 52 | + // allocation failure |
| 53 | + return; |
| 54 | + } |
| 55 | + let output = std::slice::from_raw_parts_mut(output, (input_len + 1) as usize); |
| 56 | + |
| 57 | + dot_prefix_transform_do(input, output); |
| 58 | + |
| 59 | + InspectionBufferTruncate(buffer, input_len + 1); |
| 60 | +} |
| 61 | + |
| 62 | +#[no_mangle] |
| 63 | +pub unsafe extern "C" fn DetectTransformDotPrefixRegister() { |
| 64 | + let kw = SCTransformTableElmt { |
| 65 | + name: b"dotprefix\0".as_ptr() as *const libc::c_char, |
| 66 | + desc: b"modify buffer to extract the dotprefix\0".as_ptr() as *const libc::c_char, |
| 67 | + url: b"/rules/transforms.html#dotprefix\0".as_ptr() as *const libc::c_char, |
| 68 | + Setup: dot_prefix_setup, |
| 69 | + flags: SIGMATCH_NOOPT, |
| 70 | + Transform: dot_prefix_transform, |
| 71 | + Free: None, |
| 72 | + TransformValidate: None, |
| 73 | + }; |
| 74 | + unsafe { |
| 75 | + G_TRANSFORM_DOT_PREFIX_ID = DetectHelperTransformRegister(&kw); |
| 76 | + if G_TRANSFORM_DOT_PREFIX_ID < 0 { |
| 77 | + SCLogWarning!("Failed registering transform dot_prefix"); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use super::*; |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn test_dot_prefix_transform() { |
| 88 | + let buf = b"example.com"; |
| 89 | + let mut out = vec![0; b"example.com".len() + 1]; |
| 90 | + dot_prefix_transform_do(buf, &mut out); |
| 91 | + assert_eq!(out, b".example.com"); |
| 92 | + let buf = b"hello.example.com"; |
| 93 | + let mut out = vec![0; b"hello.example.com".len() + 1]; |
| 94 | + dot_prefix_transform_do(buf, &mut out); |
| 95 | + assert_eq!(out, b".hello.example.com"); |
| 96 | + } |
| 97 | +} |
0 commit comments