detect/nfs: move nfs.version to rust

Ticket: 3195
pull/15974/head
Philippe Antoine 2 months ago committed by Victor Julien
parent e976db7261
commit 3e70a79ed2

@ -20,8 +20,9 @@
use suricata_sys::sys::AppProtoEnum::ALPROTO_NFS;
use suricata_sys::sys::{
AppProto, DetectEngineCtx, DetectEngineThreadCtx, Flow, SCDetectHelperBufferProgressRegister,
SCDetectHelperKeywordRegister, SCDetectSignatureSetAppProto, SCSigMatchAppendSMToList,
SCSigTableAppLiteElmt, SigMatchCtx, Signature,
SCDetectHelperKeywordAliasRegister, SCDetectHelperKeywordRegister,
SCDetectSignatureSetAppProto, SCSigMatchAppendSMToList, SCSigTableAppLiteElmt, SigMatchCtx,
Signature,
};
use super::nfs::{NFSTransaction, NFSTransactionTypeData};
@ -29,6 +30,7 @@ use super::types::{NfsProc2, NfsProc3, NfsProc4};
use crate::core::STREAM_TOSERVER;
use crate::detect::uint::{
detect_match_uint, detect_parse_uint_enum, detect_parse_uint_inclusive, DetectUintData,
SCDetectU32Free, SCDetectU32ParseInclusive,
};
use crate::detect::{SIGMATCH_INFO_ENUM_UINT, SIGMATCH_INFO_UINT32};
@ -37,6 +39,8 @@ use std::os::raw::c_void;
static mut G_NFS_PROCEDURE_KW_ID: u16 = 0;
static mut G_NFS_PROCEDURE_BUFFER_ID: c_int = 0;
static mut G_NFS_VERSION_KW_ID: u16 = 0;
static mut G_NFS_VERSION_BUFFER_ID: c_int = 0;
struct DetectNfsProcedureDataVersion {
v2: Option<DetectUintData<u32>>,
@ -159,8 +163,47 @@ unsafe extern "C" fn nfs_procedure_free(_de: *mut DetectEngineCtx, ctx: *mut c_v
std::mem::drop(Box::from_raw(ctx));
}
unsafe extern "C" fn nfs_version_setup(
de: *mut DetectEngineCtx, s: *mut Signature, raw: *const libc::c_char,
) -> c_int {
if SCDetectSignatureSetAppProto(s, ALPROTO_NFS as AppProto) != 0 {
return -1;
}
let ctx = SCDetectU32ParseInclusive(raw) as *mut c_void;
if ctx.is_null() {
return -1;
}
if SCSigMatchAppendSMToList(
de,
s,
G_NFS_VERSION_KW_ID,
ctx as *mut SigMatchCtx,
G_NFS_VERSION_BUFFER_ID,
)
.is_null()
{
nfs_version_free(std::ptr::null_mut(), ctx);
return -1;
}
return 0;
}
unsafe extern "C" fn nfs_version_match(
_de: *mut DetectEngineThreadCtx, _f: *mut Flow, _flags: u8, _state: *mut c_void,
tx: *mut c_void, _sig: *const Signature, ctx: *const SigMatchCtx,
) -> c_int {
let tx = cast_pointer!(tx, NFSTransaction);
let ctx = cast_pointer!(ctx, DetectUintData<u32>);
return detect_match_uint(ctx, tx.nfs_version as u32) as c_int;
}
unsafe extern "C" fn nfs_version_free(_de: *mut DetectEngineCtx, ctx: *mut c_void) {
let ctx = cast_pointer!(ctx, DetectUintData<u32>);
SCDetectU32Free(ctx);
}
#[no_mangle]
pub unsafe extern "C" fn SCDetectNfsProcedureRegister() {
pub unsafe extern "C" fn SCDetectNfsRegister() {
let kw = SCSigTableAppLiteElmt {
name: b"nfs_procedure\0".as_ptr() as *const libc::c_char,
desc: b"match NFS procedure\0".as_ptr() as *const libc::c_char,
@ -177,6 +220,28 @@ pub unsafe extern "C" fn SCDetectNfsProcedureRegister() {
STREAM_TOSERVER,
0,
);
let kw = SCSigTableAppLiteElmt {
name: b"nfs.version\0".as_ptr() as *const libc::c_char,
desc: b"match NFS version\0".as_ptr() as *const libc::c_char,
// TODO write doc /rules/nfs-keywords.html#nfs-version\0
url: std::ptr::null(),
AppLayerTxMatch: Some(nfs_version_match),
Setup: Some(nfs_version_setup),
Free: Some(nfs_version_free),
flags: SIGMATCH_INFO_UINT32,
};
G_NFS_VERSION_KW_ID = SCDetectHelperKeywordRegister(&kw);
G_NFS_VERSION_BUFFER_ID = SCDetectHelperBufferProgressRegister(
b"nfs.version\0".as_ptr() as *const libc::c_char,
ALPROTO_NFS as AppProto,
STREAM_TOSERVER,
0,
);
SCDetectHelperKeywordAliasRegister(
G_NFS_VERSION_KW_ID,
b"nfs_version\0".as_ptr() as *const libc::c_char,
);
}
#[cfg(test)]

@ -2079,11 +2079,6 @@ unsafe extern "C" fn nfs_get_tx_data(
export_state_data_get!(nfs_get_state_data, NFSState);
#[no_mangle]
pub unsafe extern "C" fn SCNfsTxGetVersion(tx: &mut NFSTransaction, version: *mut u32) {
*version = tx.nfs_version as u32;
}
#[no_mangle]
pub unsafe extern "C" fn SCNfsInit(context: &'static mut SuricataFileContext) {
SURICATA_NFS_FILE_CONFIG = Some(context);

@ -249,7 +249,6 @@ noinst_HEADERS = \
detect-metadata.h \
detect-modbus.h \
detect-msg.h \
detect-nfs-version.h \
detect-noalert.h \
detect-nocase.h \
detect-offset.h \
@ -826,7 +825,6 @@ libsuricata_c_a_SOURCES = \
detect-metadata.c \
detect-modbus.c \
detect-msg.c \
detect-nfs-version.c \
detect-noalert.c \
detect-nocase.c \
detect-offset.c \

@ -69,7 +69,6 @@
#include "detect-http-host.h"
#include "detect-mark.h"
#include "detect-nfs-version.h"
#include "detect-engine-event.h"
#include "decode.h"
@ -685,8 +684,6 @@ void SigTableSetup(void)
DetectTlsRegister();
DetectTlsValidityRegister();
DetectTlsVersionRegister();
SCDetectNfsProcedureRegister();
DetectNfsVersionRegister();
DetectUrilenRegister();
DetectBsizeRegister();
DetectDetectionFilterRegister();
@ -771,6 +768,7 @@ void SigTableSetup(void)
SCDetectIkeRegister();
SCDetectDcerpcRegister();
SCDetectKrb5Register();
SCDetectNfsRegister();
for (size_t i = 0; i < preregistered_callbacks_nb; i++) {
PreregisteredCallbacks[i]();

@ -1,179 +0,0 @@
/* Copyright (C) 2017-2020 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* 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
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Victor Julien <victor@inliniac.net>
*/
#include "suricata-common.h"
#include "threads.h"
#include "decode.h"
#include "detect.h"
#include "detect-parse.h"
#include "detect-engine.h"
#include "detect-engine-mpm.h"
#include "detect-content.h"
#include "detect-pcre.h"
#include "detect-nfs-version.h"
#include "detect-engine-uint.h"
#include "app-layer-parser.h"
#include "flow.h"
#include "flow-util.h"
#include "flow-var.h"
#include "util-unittest.h"
#include "util-unittest-helper.h"
#include "util-byte.h"
#include "app-layer-nfs-tcp.h"
#include "rust.h"
static int DetectNfsVersionSetup (DetectEngineCtx *, Signature *s, const char *str);
static void DetectNfsVersionFree(DetectEngineCtx *de_ctx, void *);
static int g_nfs_request_buffer_id = 0;
static int DetectNfsVersionMatch (DetectEngineThreadCtx *, Flow *,
uint8_t, void *, void *, const Signature *,
const SigMatchCtx *);
/**
* \brief Registration function for nfs_procedure keyword.
*/
void DetectNfsVersionRegister (void)
{
sigmatch_table[DETECT_NFS_VERSION].name = "nfs.version";
sigmatch_table[DETECT_NFS_VERSION].alias = "nfs_version";
sigmatch_table[DETECT_NFS_VERSION].desc = "match NFS version";
// TODO write doc sigmatch_table[DETECT_NFS_VERSION].url =
// "/rules/nfs-keywords.html#nfs-version";
sigmatch_table[DETECT_NFS_VERSION].AppLayerTxMatch = DetectNfsVersionMatch;
sigmatch_table[DETECT_NFS_VERSION].Setup = DetectNfsVersionSetup;
sigmatch_table[DETECT_NFS_VERSION].Free = DetectNfsVersionFree;
sigmatch_table[DETECT_NFS_VERSION].flags = SIGMATCH_INFO_UINT32;
// unit tests were the same as DetectNfsProcedureRegisterTests
DetectAppLayerInspectEngineRegister(
"nfs_request", ALPROTO_NFS, SIG_FLAG_TOSERVER, 0, DetectEngineInspectGenericList, NULL);
g_nfs_request_buffer_id = DetectBufferTypeGetByName("nfs_request");
SCLogDebug("g_nfs_request_buffer_id %d", g_nfs_request_buffer_id);
}
/**
* \internal
* \brief Function to match version of a TX
*
* \param t Pointer to thread vars.
* \param det_ctx Pointer to the pattern matcher thread.
* \param f Pointer to the current flow.
* \param flags Flags.
* \param state App layer state.
* \param s Pointer to the Signature.
* \param m Pointer to the sigmatch that we will cast into
* DetectU32Data.
*
* \retval 0 no match.
* \retval 1 match.
*/
static int DetectNfsVersionMatch (DetectEngineThreadCtx *det_ctx,
Flow *f, uint8_t flags, void *state,
void *txv, const Signature *s,
const SigMatchCtx *ctx)
{
SCEnter();
const DetectU32Data *dd = (const DetectU32Data *)ctx;
uint32_t version;
SCNfsTxGetVersion(txv, &version);
SCLogDebug("version %u mode %u lo %u hi %u", version, dd->mode, dd->arg1, dd->arg2);
if (DetectU32Match(version, dd))
SCReturnInt(1);
SCReturnInt(0);
}
/**
* \internal
* \brief Function to parse options passed via tls validity keywords.
*
* \param rawstr Pointer to the user provided options.
*
* \retval dd pointer to DetectU32Data on success.
* \retval NULL on failure.
*/
static DetectU32Data *DetectNfsVersionParse(const char *rawstr)
{
return SCDetectU32ParseInclusive(rawstr);
}
/**
* \brief Function to add the parsed tls validity field into the current signature.
*
* \param de_ctx Pointer to the Detection Engine Context.
* \param s Pointer to the Current Signature.
* \param rawstr Pointer to the user provided flags options.
* \param type Defines if this is notBefore or notAfter.
*
* \retval 0 on Success.
* \retval -1 on Failure.
*/
static int DetectNfsVersionSetup (DetectEngineCtx *de_ctx, Signature *s,
const char *rawstr)
{
SCLogDebug("\'%s\'", rawstr);
if (SCDetectSignatureSetAppProto(s, ALPROTO_NFS) != 0)
return -1;
DetectU32Data *dd = DetectNfsVersionParse(rawstr);
if (dd == NULL) {
SCLogError("Parsing \'%s\' failed", rawstr);
return -1;
}
/* okay so far so good, lets get this into a SigMatch
* and put it in the Signature. */
SCLogDebug("low %u hi %u", dd->arg1, dd->arg2);
if (SCSigMatchAppendSMToList(de_ctx, s, DETECT_NFS_VERSION, (SigMatchCtx *)dd,
g_nfs_request_buffer_id) == NULL) {
goto error;
}
return 0;
error:
DetectNfsVersionFree(de_ctx, dd);
return -1;
}
/**
* \internal
* \brief Function to free memory associated with DetectU32Data.
*
* \param de_ptr Pointer to DetectU32Data.
*/
void DetectNfsVersionFree(DetectEngineCtx *de_ctx, void *ptr)
{
SCDetectU32Free(ptr);
}

@ -1,30 +0,0 @@
/* Copyright (C) 2017 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* 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
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Victor Julien <victor@inliniac.net>
*/
#ifndef SURICATA_DETECT_NFS_VERSION_H
#define SURICATA_DETECT_NFS_VERSION_H
/* prototypes */
void DetectNfsVersionRegister (void);
#endif /* SURICATA_DETECT_NFS_VERSION_H */
Loading…
Cancel
Save