2020use crate :: krb:: krb5:: { test_weak_encryption, KRB5Transaction } ;
2121use suricata_sys:: sys:: AppProtoEnum :: ALPROTO_KRB5 ;
2222use 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
2830use crate :: core:: { STREAM_TOCLIENT , STREAM_TOSERVER } ;
2931use 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+ } ;
3338use kerberos_parser:: krb5:: EncryptionType ;
3439
3540use nom8:: branch:: alt;
@@ -43,8 +48,7 @@ use nom8::Parser;
4348use std:: ffi:: { c_int, CStr } ;
4449use 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 {
343346static mut G_KRB5_MSG_TYPE_KW_ID : u16 = 0 ;
344347static mut G_KRB5_GENERIC_BUFFER_ID : c_int = 0 ;
345348static 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]
478505pub 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}
0 commit comments