Skip to content

Commit 8f0dbc0

Browse files
catenacybervictorjulien
authored andcommitted
detect/krb5: move krb5.cname and krb5.sname to rust
Ticket: 8648
1 parent 3327cf4 commit 8f0dbc0

8 files changed

Lines changed: 78 additions & 223 deletions

rust/src/krb/detect.rs

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,21 @@
2020
use crate::krb::krb5::{test_weak_encryption, KRB5Transaction};
2121
use suricata_sys::sys::AppProtoEnum::ALPROTO_KRB5;
2222
use suricata_sys::sys::{
23-
AppProto, DetectEngineCtx, DetectEngineThreadCtx, Flow, SCDetectHelperBufferProgressRegister,
24-
SCDetectHelperKeywordRegister, SCDetectSignatureSetAppProto, SCSigMatchAppendSMToList,
25-
SCSigTableAppLiteElmt, SigMatchCtx, Signature,
23+
AppProto, DetectEngineCtx, DetectEngineThreadCtx, Flow, SCDetectBufferSetActiveList,
24+
SCDetectHelperBufferProgressRegister, SCDetectHelperKeywordAliasRegister,
25+
SCDetectHelperKeywordRegister, SCDetectHelperMultiBufferProgressMpmRegister,
26+
SCDetectSignatureSetAppProto, SCSigMatchAppendSMToList, SCSigTableAppLiteElmt, SigMatchCtx,
27+
Signature,
2628
};
2729

2830
use crate::core::{STREAM_TOCLIENT, STREAM_TOSERVER};
2931
use crate::detect::uint::{
3032
detect_parse_uint, detect_parse_uint_enum, DetectUintData, SCDetectU32Free, SCDetectU32Match,
3133
};
32-
use crate::detect::{SIGMATCH_INFO_ENUM_UINT, SIGMATCH_INFO_MULTI_UINT, SIGMATCH_INFO_UINT32};
34+
use crate::detect::{
35+
helper_keyword_register_multi_buffer, SigTableElmtStickyBuffer, SIGMATCH_INFO_ENUM_UINT,
36+
SIGMATCH_INFO_MULTI_UINT, SIGMATCH_INFO_UINT32,
37+
};
3338
use kerberos_parser::krb5::EncryptionType;
3439

3540
use nom8::branch::alt;
@@ -43,8 +48,7 @@ use nom8::Parser;
4348
use std::ffi::{c_int, CStr};
4449
use std::os::raw::c_void;
4550

46-
#[no_mangle]
47-
pub unsafe extern "C" fn SCKrb5TxGetCname(
51+
unsafe extern "C" fn krb5_cname_get_data(
4852
_de: *mut DetectEngineThreadCtx, tx: *const c_void, _flags: u8, i: u32, buffer: *mut *const u8,
4953
buffer_len: *mut u32,
5054
) -> bool {
@@ -60,8 +64,7 @@ pub unsafe extern "C" fn SCKrb5TxGetCname(
6064
false
6165
}
6266

63-
#[no_mangle]
64-
pub unsafe extern "C" fn SCKrb5TxGetSname(
67+
unsafe extern "C" fn krb5_sname_get_data(
6568
_de: *mut DetectEngineThreadCtx, tx: *const c_void, _flags: u8, i: u32, buffer: *mut *const u8,
6669
buffer_len: *mut u32,
6770
) -> bool {
@@ -343,6 +346,8 @@ mod tests {
343346
static mut G_KRB5_MSG_TYPE_KW_ID: u16 = 0;
344347
static mut G_KRB5_GENERIC_BUFFER_ID: c_int = 0;
345348
static mut G_KRB5_ERR_CODE_KW_ID: u16 = 0;
349+
static mut G_KRB5_CNAME_BUFFER_ID: c_int = 0;
350+
static mut G_KRB5_SNAME_BUFFER_ID: c_int = 0;
346351

347352
// We should apply the derive on the kerberos_parser MessageType
348353
#[repr(u32)]
@@ -462,9 +467,7 @@ unsafe extern "C" fn krb5_err_code_match(
462467
let tx = cast_pointer!(tx, KRB5Transaction);
463468
let ctx = cast_pointer!(ctx, DetectUintData<u32>);
464469
match tx.error_code {
465-
Some(ref e) => {
466-
SCDetectU32Match(e.0 as u32, ctx)
467-
}
470+
Some(ref e) => SCDetectU32Match(e.0 as u32, ctx),
468471
None => 0,
469472
}
470473
}
@@ -474,6 +477,30 @@ unsafe extern "C" fn krb5_err_code_free(_de: *mut DetectEngineCtx, ctx: *mut c_v
474477
SCDetectU32Free(ctx);
475478
}
476479

480+
unsafe extern "C" fn krb5_sname_setup(
481+
de: *mut DetectEngineCtx, s: *mut Signature, _raw: *const std::os::raw::c_char,
482+
) -> c_int {
483+
if SCDetectSignatureSetAppProto(s, ALPROTO_KRB5 as AppProto) != 0 {
484+
return -1;
485+
}
486+
if SCDetectBufferSetActiveList(de, s, G_KRB5_SNAME_BUFFER_ID) < 0 {
487+
return -1;
488+
}
489+
return 0;
490+
}
491+
492+
unsafe extern "C" fn krb5_cname_setup(
493+
de: *mut DetectEngineCtx, s: *mut Signature, _raw: *const std::os::raw::c_char,
494+
) -> c_int {
495+
if SCDetectSignatureSetAppProto(s, ALPROTO_KRB5 as AppProto) != 0 {
496+
return -1;
497+
}
498+
if SCDetectBufferSetActiveList(de, s, G_KRB5_CNAME_BUFFER_ID) < 0 {
499+
return -1;
500+
}
501+
return 0;
502+
}
503+
477504
#[no_mangle]
478505
pub unsafe extern "C" fn SCDetectKrb5Register() {
479506
let kw = SCSigTableAppLiteElmt {
@@ -503,4 +530,44 @@ pub unsafe extern "C" fn SCDetectKrb5Register() {
503530
flags: SIGMATCH_INFO_UINT32,
504531
};
505532
G_KRB5_ERR_CODE_KW_ID = SCDetectHelperKeywordRegister(&kw);
533+
534+
let kw = SigTableElmtStickyBuffer {
535+
name: String::from("krb5.sname"),
536+
desc: String::from("sticky buffer to match on Kerberos 5 server name"),
537+
url: String::from("/rules/kerberos-keywords.html#krb5-sname"),
538+
setup: krb5_sname_setup,
539+
};
540+
let krb5_sname_hdr_kw_id = helper_keyword_register_multi_buffer(&kw);
541+
G_KRB5_SNAME_BUFFER_ID = SCDetectHelperMultiBufferProgressMpmRegister(
542+
b"krb5_sname\0".as_ptr() as *const libc::c_char,
543+
b"krb5_sname\0".as_ptr() as *const libc::c_char,
544+
ALPROTO_KRB5 as AppProto,
545+
STREAM_TOCLIENT,
546+
Some(krb5_sname_get_data),
547+
1,
548+
);
549+
SCDetectHelperKeywordAliasRegister(
550+
krb5_sname_hdr_kw_id,
551+
b"krb5_sname\0".as_ptr() as *const libc::c_char,
552+
);
553+
554+
let kw = SigTableElmtStickyBuffer {
555+
name: String::from("krb5.cname"),
556+
desc: String::from("sticky buffer to match on Kerberos 5 client name"),
557+
url: String::from("/rules/kerberos-keywords.html#krb5-cname"),
558+
setup: krb5_cname_setup,
559+
};
560+
let krb5_cname_hdr_kw_id = helper_keyword_register_multi_buffer(&kw);
561+
G_KRB5_CNAME_BUFFER_ID = SCDetectHelperMultiBufferProgressMpmRegister(
562+
b"krb5_cname\0".as_ptr() as *const libc::c_char,
563+
b"krb5_cname\0".as_ptr() as *const libc::c_char,
564+
ALPROTO_KRB5 as AppProto,
565+
STREAM_TOCLIENT,
566+
Some(krb5_cname_get_data),
567+
1,
568+
);
569+
SCDetectHelperKeywordAliasRegister(
570+
krb5_cname_hdr_kw_id,
571+
b"krb5_cname\0".as_ptr() as *const libc::c_char,
572+
);
506573
}

src/Makefile.am

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,6 @@ noinst_HEADERS = \
242242
detect-isdataat.h \
243243
detect-itype.h \
244244
detect-ja4-hash.h \
245-
detect-krb5-cname.h \
246-
detect-krb5-sname.h \
247245
detect-krb5-ticket-encryption.h \
248246
detect-l3proto.h \
249247
detect-lua-extensions.h \
@@ -822,8 +820,6 @@ libsuricata_c_a_SOURCES = \
822820
detect-isdataat.c \
823821
detect-itype.c \
824822
detect-ja4-hash.c \
825-
detect-krb5-cname.c \
826-
detect-krb5-sname.c \
827823
detect-krb5-ticket-encryption.c \
828824
detect-l3proto.c \
829825
detect-lua-extensions.c \

src/detect-engine-register.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,6 @@
196196
#include "detect-icmpv6-mtu.h"
197197
#include "detect-ipv4hdr.h"
198198
#include "detect-ipv6hdr.h"
199-
#include "detect-krb5-cname.h"
200-
#include "detect-krb5-sname.h"
201199
#include "detect-krb5-ticket-encryption.h"
202200
#include "detect-sip-method.h"
203201
#include "detect-sip-uri.h"
@@ -715,8 +713,6 @@ void SigTableSetup(void)
715713
DetectEtherhdrRegister();
716714
DetectIpv4hdrRegister();
717715
DetectIpv6hdrRegister();
718-
DetectKrb5CNameRegister();
719-
DetectKrb5SNameRegister();
720716
DetectKrb5TicketEncryptionRegister();
721717
DetectSipMethodRegister();
722718
DetectSipUriRegister();

src/detect-engine-register.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,6 @@ enum DetectKeywordId {
258258
DETECT_DNP3IND,
259259
DETECT_DNP3OBJ,
260260

261-
DETECT_KRB5_CNAME,
262-
DETECT_KRB5_SNAME,
263261
DETECT_KRB5_TICKET_ENCRYPTION,
264262

265263
DETECT_SIP_METHOD,

src/detect-krb5-cname.c

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/detect-krb5-cname.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/detect-krb5-sname.c

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/detect-krb5-sname.h

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)