detect/ssh: move ssh.software to rust

Adds helper function SCDetectHelperBufferProgressMpmRegister on the way
pull/14639/head
Philippe Antoine 8 months ago committed by Victor Julien
parent c0d6747e82
commit ea8ac2a02a

@ -15,9 +15,16 @@
* 02110-1301, USA.
*/
use super::ssh::SSHTransaction;
use super::ssh::{SSHConnectionState, SSHTransaction, ALPROTO_SSH};
use crate::core::{STREAM_TOCLIENT, STREAM_TOSERVER};
use crate::detect::{helper_keyword_register_sticky_buffer, SigTableElmtStickyBuffer};
use crate::direction::Direction;
use std::os::raw::{c_int, c_void};
use std::ptr;
use suricata_sys::sys::{
DetectEngineCtx, SCDetectBufferSetActiveList, SCDetectHelperBufferProgressMpmRegister,
SCDetectHelperKeywordAliasRegister, SCDetectSignatureSetAppProto, Signature,
};
#[no_mangle]
pub unsafe extern "C" fn SCSshTxGetProtocol(
@ -50,8 +57,8 @@ pub unsafe extern "C" fn SCSshTxGetProtocol(
#[no_mangle]
pub unsafe extern "C" fn SCSshTxGetSoftware(
tx: *mut std::os::raw::c_void, buffer: *mut *const u8, buffer_len: *mut u32, direction: u8,
) -> u8 {
tx: *const c_void, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32,
) -> bool {
let tx = cast_pointer!(tx, SSHTransaction);
match direction.into() {
Direction::ToServer => {
@ -59,7 +66,7 @@ pub unsafe extern "C" fn SCSshTxGetSoftware(
if !m.is_empty() {
*buffer = m.as_ptr();
*buffer_len = m.len() as u32;
return 1;
return true;
}
}
Direction::ToClient => {
@ -67,14 +74,13 @@ pub unsafe extern "C" fn SCSshTxGetSoftware(
if !m.is_empty() {
*buffer = m.as_ptr();
*buffer_len = m.len() as u32;
return 1;
return true;
}
}
}
*buffer = ptr::null();
*buffer_len = 0;
return 0;
return false;
}
#[no_mangle]
@ -134,3 +140,40 @@ pub unsafe extern "C" fn SCSshTxGetHasshString(
return 0;
}
unsafe extern "C" fn ssh_software_setup(
de: *mut DetectEngineCtx, s: *mut Signature, _raw: *const std::os::raw::c_char,
) -> c_int {
if SCDetectSignatureSetAppProto(s, ALPROTO_SSH) != 0 {
return -1;
}
if SCDetectBufferSetActiveList(de, s, G_SSH_SOFTWARE_BUFFER_ID) < 0 {
return -1;
}
return 0;
}
static mut G_SSH_SOFTWARE_BUFFER_ID: c_int = 0;
#[no_mangle]
pub unsafe extern "C" fn SCDetectSshRegister() {
let kw = SigTableElmtStickyBuffer {
name: String::from("ssh.software"),
desc: String::from("ssh.software sticky buffer"),
url: String::from("/rules/ssh-keywords.html#ssh-software"),
setup: ssh_software_setup,
};
let ssh_software_kw_id = helper_keyword_register_sticky_buffer(&kw);
G_SSH_SOFTWARE_BUFFER_ID = SCDetectHelperBufferProgressMpmRegister(
b"ssh_software\0".as_ptr() as *const libc::c_char,
b"ssh software field\0".as_ptr() as *const libc::c_char,
ALPROTO_SSH,
STREAM_TOSERVER | STREAM_TOCLIENT,
Some(SCSshTxGetSoftware),
SSHConnectionState::SshStateBannerDone as c_int,
);
SCDetectHelperKeywordAliasRegister(
ssh_software_kw_id,
b"ssh_software\0".as_ptr() as *const libc::c_char,
);
}

@ -31,7 +31,7 @@ use suricata_sys::sys::{
SCAppLayerProtoDetectConfProtoDetectionEnabled,
};
static mut ALPROTO_SSH: AppProto = ALPROTO_UNKNOWN;
pub(super) static mut ALPROTO_SSH: AppProto = ALPROTO_UNKNOWN;
static HASSH_ENABLED: AtomicBool = AtomicBool::new(false);
static mut ENCRYPTION_BYPASS_ENABLED: EncryptionHandling =

@ -452,6 +452,13 @@ extern "C" {
alproto: AppProto, direction: u8, GetData: InspectionSingleBufferGetDataPtr,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn SCDetectHelperBufferProgressMpmRegister(
name: *const ::std::os::raw::c_char, desc: *const ::std::os::raw::c_char,
alproto: AppProto, direction: u8, GetData: InspectionSingleBufferGetDataPtr,
progress: ::std::os::raw::c_int,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn SCDetectHelperMultiBufferMpmRegister(
name: *const ::std::os::raw::c_char, desc: *const ::std::os::raw::c_char,

@ -290,7 +290,6 @@ noinst_HEADERS = \
detect-ssh-proto-version.h \
detect-ssh-proto.h \
detect-ssh-software-version.h \
detect-ssh-software.h \
detect-ssl-state.h \
detect-ssl-version.h \
detect-stream_size.h \
@ -890,7 +889,6 @@ libsuricata_c_a_SOURCES = \
detect-ssh-proto-version.c \
detect-ssh-proto.c \
detect-ssh-software-version.c \
detect-ssh-software.c \
detect-ssl-state.c \
detect-ssl-version.c \
detect-stream_size.c \

@ -163,7 +163,7 @@ static int SSHParserTestUtilCheck(const char *protoexp, const char *softexp, voi
}
if (softexp != NULL) {
if (SCSshTxGetSoftware(tx, &software, &s_len, flags) != 1)
if (SCSshTxGetSoftware(tx, flags, &software, &s_len) != 1)
return 1;
if (software == NULL)
return 1;
@ -309,7 +309,7 @@ static int SSHParserTest03(void)
uint32_t dummy_len = 0;
if (SCSshTxGetProtocol(tx, &dummy, &dummy_len, STREAM_TOSERVER) != 0)
goto end;
if (SCSshTxGetSoftware(tx, &dummy, &dummy_len, STREAM_TOSERVER) != 0)
if (SCSshTxGetSoftware(tx, STREAM_TOSERVER, &dummy, &dummy_len) != 0)
goto end;
result = 1;
@ -464,7 +464,7 @@ static int SSHParserTest06(void)
uint32_t dummy_len = 0;
if (SCSshTxGetProtocol(tx, &dummy, &dummy_len, STREAM_TOCLIENT) != 0)
goto end;
if (SCSshTxGetSoftware(tx, &dummy, &dummy_len, STREAM_TOCLIENT) != 0)
if (SCSshTxGetSoftware(tx, STREAM_TOCLIENT, &dummy, &dummy_len) != 0)
goto end;
result = 1;
@ -1558,7 +1558,7 @@ static int SSHParserTest25(void)
FAIL_IF(SCSshTxGetFlags(tx, STREAM_TOSERVER) == SshStateBannerDone);
const uint8_t *dummy = NULL;
uint32_t dummy_len = 0;
FAIL_IF(SCSshTxGetSoftware(tx, &dummy, &dummy_len, STREAM_TOCLIENT) != 0);
FAIL_IF(SCSshTxGetSoftware(tx, STREAM_TOCLIENT, &dummy, &dummy_len) != 0);
FLOW_DESTROY(&f);
AppLayerParserThreadCtxFree(alp_tctx);

@ -63,6 +63,25 @@ int SCDetectHelperBufferMpmRegister(const char *name, const char *desc, AppProto
return DetectBufferTypeGetByName(name);
}
int SCDetectHelperBufferProgressMpmRegister(const char *name, const char *desc, AppProto alproto,
uint8_t direction, InspectionSingleBufferGetDataPtr GetData, int progress)
{
if (direction & STREAM_TOSERVER) {
DetectAppLayerInspectEngineRegisterSingle(name, alproto, SIG_FLAG_TOSERVER, progress,
DetectEngineInspectBufferSingle, GetData);
DetectAppLayerMpmRegisterSingle(
name, SIG_FLAG_TOSERVER, 2, PrefilterSingleMpmRegister, GetData, alproto, progress);
}
if (direction & STREAM_TOCLIENT) {
DetectAppLayerInspectEngineRegisterSingle(name, alproto, SIG_FLAG_TOCLIENT, progress,
DetectEngineInspectBufferSingle, GetData);
DetectAppLayerMpmRegisterSingle(
name, SIG_FLAG_TOCLIENT, 2, PrefilterSingleMpmRegister, GetData, alproto, progress);
}
DetectBufferTypeSetDescriptionByName(name, desc);
return DetectBufferTypeGetByName(name);
}
int SCDetectHelperMultiBufferProgressMpmRegister(const char *name, const char *desc,
AppProto alproto, uint8_t direction, InspectionMultiBufferGetDataPtr GetData, int progress)
{

@ -84,6 +84,8 @@ int SCDetectHelperBufferRegister(const char *name, AppProto alproto, uint8_t dir
int SCDetectHelperBufferMpmRegister(const char *name, const char *desc, AppProto alproto,
uint8_t direction, InspectionSingleBufferGetDataPtr GetData);
int SCDetectHelperBufferProgressMpmRegister(const char *name, const char *desc, AppProto alproto,
uint8_t direction, InspectionSingleBufferGetDataPtr GetData, int progress);
int SCDetectHelperMultiBufferMpmRegister(const char *name, const char *desc, AppProto alproto,
uint8_t direction, InspectionMultiBufferGetDataPtr GetData);
int SCDetectHelperMultiBufferProgressMpmRegister(const char *name, const char *desc,

@ -236,7 +236,6 @@
#include "detect-tls-version.h"
#include "detect-ssh-proto.h"
#include "detect-ssh-proto-version.h"
#include "detect-ssh-software.h"
#include "detect-ssh-software-version.h"
#include "detect-ssh-hassh.h"
#include "detect-ssh-hassh-server.h"
@ -716,7 +715,6 @@ void SigTableSetup(void)
DetectAsn1Register();
DetectSshProtocolRegister();
DetectSshVersionRegister();
DetectSshSoftwareRegister();
DetectSshSoftwareVersionRegister();
DetectSshHasshRegister();
DetectSshHasshServerRegister();
@ -803,6 +801,7 @@ void SigTableSetup(void)
SCDetectSdpRegister();
SCDetectDNSRegister();
SCDetectPgsqlRegister();
SCDetectSshRegister();
for (size_t i = 0; i < preregistered_callbacks_nb; i++) {
PreregisteredCallbacks[i]();

@ -191,7 +191,6 @@ enum DetectKeywordId {
DETECT_NFS_VERSION,
DETECT_SSH_PROTOCOL,
DETECT_SSH_PROTOVERSION,
DETECT_SSH_SOFTWARE,
DETECT_SSH_SOFTWAREVERSION,
DETECT_SSH_HASSH,
DETECT_SSH_HASSH_SERVER,

@ -1,119 +0,0 @@
/* Copyright (C) 2007-2016 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.
*/
/**
* \ingroup sshlayer
*
* @{
*/
/**
* \file
*
* \author Victor Julien <victor@inliniac.net>
*
* Implements support ssh_software sticky buffer
*/
#include "suricata-common.h"
#include "threads.h"
#include "decode.h"
#include "detect.h"
#include "detect-parse.h"
#include "detect-engine.h"
#include "detect-engine-buffer.h"
#include "detect-engine-mpm.h"
#include "detect-engine-state.h"
#include "detect-engine-prefilter.h"
#include "detect-engine-content-inspection.h"
#include "app-layer.h"
#include "app-layer-parser.h"
#include "app-layer-ssh.h"
#include "detect-ssh-software.h"
#include "rust.h"
#define KEYWORD_NAME "ssh.software"
#define KEYWORD_NAME_LEGACY "ssh_software"
#define KEYWORD_DOC "ssh-keywords.html#ssh-software"
#define BUFFER_NAME "ssh_software"
#define BUFFER_DESC "ssh software field"
static int g_buffer_id = 0;
static InspectionBuffer *GetSshData(DetectEngineThreadCtx *det_ctx,
const DetectEngineTransforms *transforms, Flow *_f,
const uint8_t flow_flags, void *txv, const int list_id)
{
SCEnter();
InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
if (buffer->inspect == NULL) {
const uint8_t *software = NULL;
uint32_t b_len = 0;
if (SCSshTxGetSoftware(txv, &software, &b_len, flow_flags) != 1)
return NULL;
if (software == NULL || b_len == 0) {
SCLogDebug("SSH software version not set");
return NULL;
}
InspectionBufferSetupAndApplyTransforms(
det_ctx, list_id, buffer, software, b_len, transforms);
}
return buffer;
}
static int DetectSshSoftwareSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
if (SCDetectBufferSetActiveList(de_ctx, s, g_buffer_id) < 0)
return -1;
if (SCDetectSignatureSetAppProto(s, ALPROTO_SSH) < 0)
return -1;
return 0;
}
void DetectSshSoftwareRegister(void)
{
sigmatch_table[DETECT_SSH_SOFTWARE].name = KEYWORD_NAME;
sigmatch_table[DETECT_SSH_SOFTWARE].alias = KEYWORD_NAME_LEGACY;
sigmatch_table[DETECT_SSH_SOFTWARE].desc = BUFFER_NAME " sticky buffer";
sigmatch_table[DETECT_SSH_SOFTWARE].url = "/rules/" KEYWORD_DOC;
sigmatch_table[DETECT_SSH_SOFTWARE].Setup = DetectSshSoftwareSetup;
sigmatch_table[DETECT_SSH_SOFTWARE].flags |= SIGMATCH_INFO_STICKY_BUFFER | SIGMATCH_NOOPT;
DetectAppLayerMpmRegister(BUFFER_NAME, SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister,
GetSshData, ALPROTO_SSH, SshStateBannerDone),
DetectAppLayerMpmRegister(BUFFER_NAME, SIG_FLAG_TOCLIENT, 2,
PrefilterGenericMpmRegister, GetSshData, ALPROTO_SSH, SshStateBannerDone),
DetectAppLayerInspectEngineRegister(BUFFER_NAME, ALPROTO_SSH, SIG_FLAG_TOSERVER,
SshStateBannerDone, DetectEngineInspectBufferGeneric, GetSshData);
DetectAppLayerInspectEngineRegister(BUFFER_NAME, ALPROTO_SSH, SIG_FLAG_TOCLIENT,
SshStateBannerDone, DetectEngineInspectBufferGeneric, GetSshData);
DetectBufferTypeSetDescriptionByName(BUFFER_NAME, BUFFER_DESC);
g_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
}

@ -1,29 +0,0 @@
/* Copyright (C) 2007-2016 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_SSH_SOFTWARE_H
#define SURICATA_DETECT_SSH_SOFTWARE_H
void DetectSshSoftwareRegister(void);
#endif /* SURICATA_DETECT_SSH_SOFTWARE_H */

@ -92,7 +92,7 @@ static int LuaSshTxGetSoftware(lua_State *L, uint8_t flags)
lua_pushnil(L);
return 1;
}
if (SCSshTxGetSoftware(ltx->tx, &buf, &b_len, flags) != 1) {
if (!SCSshTxGetSoftware(ltx->tx, flags, &buf, &b_len)) {
lua_pushnil(L);
return 1;
}

Loading…
Cancel
Save