Skip to content

Commit 3327cf4

Browse files
catenacybervictorjulien
authored andcommitted
detect/krb5: move krb5_err_code to rust
Ticket: 8648 Make it a generic integer on the way
1 parent e560db9 commit 3327cf4

7 files changed

Lines changed: 79 additions & 303 deletions

File tree

doc/userguide/rules/kerberos-keywords.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ Kerberos error code (integer). This field is matched in Kerberos error messages
8888

8989
For a list of error codes, refer to RFC4120 section 7.5.9.
9090

91+
krb5_err_code uses :ref:`unsigned 32-bit integer <rules-integer-keywords>`.
92+
9193
Syntax::
9294

9395
krb5_err_code:<number>

rust/src/krb/detect.rs

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use suricata_sys::sys::{
2727

2828
use crate::core::{STREAM_TOCLIENT, STREAM_TOSERVER};
2929
use crate::detect::uint::{
30-
detect_parse_uint_enum, DetectUintData, SCDetectU32Free, SCDetectU32Match,
30+
detect_parse_uint, detect_parse_uint_enum, DetectUintData, SCDetectU32Free, SCDetectU32Match,
3131
};
3232
use crate::detect::{SIGMATCH_INFO_ENUM_UINT, SIGMATCH_INFO_MULTI_UINT, SIGMATCH_INFO_UINT32};
3333
use kerberos_parser::krb5::EncryptionType;
@@ -43,19 +43,6 @@ use nom8::Parser;
4343
use std::ffi::{c_int, CStr};
4444
use std::os::raw::c_void;
4545

46-
/// Get error code, if present in transaction
47-
/// Return 0 if error code was filled, else 1
48-
#[no_mangle]
49-
pub unsafe extern "C" fn SCKrb5TxGetErrorCode(tx: &KRB5Transaction, ptr: *mut i32) -> u32 {
50-
match tx.error_code {
51-
Some(ref e) => {
52-
*ptr = e.0;
53-
0
54-
}
55-
None => 1,
56-
}
57-
}
58-
5946
#[no_mangle]
6047
pub unsafe extern "C" fn SCKrb5TxGetCname(
6148
_de: *mut DetectEngineThreadCtx, tx: *const c_void, _flags: u8, i: u32, buffer: *mut *const u8,
@@ -354,7 +341,8 @@ mod tests {
354341
}
355342

356343
static mut G_KRB5_MSG_TYPE_KW_ID: u16 = 0;
357-
static mut G_KRB5_MSG_TYPE_BUFFER_ID: c_int = 0;
344+
static mut G_KRB5_GENERIC_BUFFER_ID: c_int = 0;
345+
static mut G_KRB5_ERR_CODE_KW_ID: u16 = 0;
358346

359347
// We should apply the derive on the kerberos_parser MessageType
360348
#[repr(u32)]
@@ -403,7 +391,7 @@ unsafe extern "C" fn krb5_msg_type_setup(
403391
s,
404392
G_KRB5_MSG_TYPE_KW_ID,
405393
ctx as *mut SigMatchCtx,
406-
G_KRB5_MSG_TYPE_BUFFER_ID,
394+
G_KRB5_GENERIC_BUFFER_ID,
407395
)
408396
.is_null()
409397
{
@@ -428,8 +416,66 @@ unsafe extern "C" fn krb5_msg_type_free(_de: *mut DetectEngineCtx, ctx: *mut c_v
428416
SCDetectU32Free(ctx);
429417
}
430418

419+
unsafe extern "C" fn krb5_parse_err_code(
420+
ustr: *const std::os::raw::c_char,
421+
) -> *mut DetectUintData<u32> {
422+
let ft_name: &CStr = CStr::from_ptr(ustr); //unsafe
423+
if let Ok(s) = ft_name.to_str() {
424+
// maybe we should own enumeration
425+
if let Ok((_, ctx)) = detect_parse_uint::<u32>(s) {
426+
let boxed = Box::new(ctx);
427+
return Box::into_raw(boxed) as *mut _;
428+
}
429+
}
430+
return std::ptr::null_mut();
431+
}
432+
433+
unsafe extern "C" fn krb5_err_code_setup(
434+
de: *mut DetectEngineCtx, s: *mut Signature, raw: *const libc::c_char,
435+
) -> c_int {
436+
if SCDetectSignatureSetAppProto(s, ALPROTO_KRB5 as AppProto) != 0 {
437+
return -1;
438+
}
439+
let ctx = krb5_parse_err_code(raw) as *mut c_void;
440+
if ctx.is_null() {
441+
return -1;
442+
}
443+
if SCSigMatchAppendSMToList(
444+
de,
445+
s,
446+
G_KRB5_ERR_CODE_KW_ID,
447+
ctx as *mut SigMatchCtx,
448+
G_KRB5_GENERIC_BUFFER_ID,
449+
)
450+
.is_null()
451+
{
452+
krb5_err_code_free(std::ptr::null_mut(), ctx);
453+
return -1;
454+
}
455+
return 0;
456+
}
457+
458+
unsafe extern "C" fn krb5_err_code_match(
459+
_de: *mut DetectEngineThreadCtx, _f: *mut Flow, _flags: u8, _state: *mut c_void,
460+
tx: *mut c_void, _sig: *const Signature, ctx: *const SigMatchCtx,
461+
) -> c_int {
462+
let tx = cast_pointer!(tx, KRB5Transaction);
463+
let ctx = cast_pointer!(ctx, DetectUintData<u32>);
464+
match tx.error_code {
465+
Some(ref e) => {
466+
SCDetectU32Match(e.0 as u32, ctx)
467+
}
468+
None => 0,
469+
}
470+
}
471+
472+
unsafe extern "C" fn krb5_err_code_free(_de: *mut DetectEngineCtx, ctx: *mut c_void) {
473+
let ctx = cast_pointer!(ctx, DetectUintData<u32>);
474+
SCDetectU32Free(ctx);
475+
}
476+
431477
#[no_mangle]
432-
pub unsafe extern "C" fn SCDetectKrb5MsgTypeRegister() {
478+
pub unsafe extern "C" fn SCDetectKrb5Register() {
433479
let kw = SCSigTableAppLiteElmt {
434480
name: b"krb5_msg_type\0".as_ptr() as *const libc::c_char,
435481
desc: b"match Kerberos 5 message type\0".as_ptr() as *const libc::c_char,
@@ -440,10 +486,21 @@ pub unsafe extern "C" fn SCDetectKrb5MsgTypeRegister() {
440486
flags: SIGMATCH_INFO_MULTI_UINT | SIGMATCH_INFO_ENUM_UINT | SIGMATCH_INFO_UINT32,
441487
};
442488
G_KRB5_MSG_TYPE_KW_ID = SCDetectHelperKeywordRegister(&kw);
443-
G_KRB5_MSG_TYPE_BUFFER_ID = SCDetectHelperBufferProgressRegister(
444-
b"krb5_msg_type\0".as_ptr() as *const libc::c_char,
489+
G_KRB5_GENERIC_BUFFER_ID = SCDetectHelperBufferProgressRegister(
490+
b"krb5_generic\0".as_ptr() as *const libc::c_char,
445491
ALPROTO_KRB5 as AppProto,
446492
STREAM_TOCLIENT | STREAM_TOSERVER,
447493
1,
448494
);
495+
496+
let kw = SCSigTableAppLiteElmt {
497+
name: b"krb5_err_code\0".as_ptr() as *const libc::c_char,
498+
desc: b"match Kerberos 5 error code\0".as_ptr() as *const libc::c_char,
499+
url: b"/rules/kerberos-keywords.html#krb5-err-code\0".as_ptr() as *const libc::c_char,
500+
AppLayerTxMatch: Some(krb5_err_code_match),
501+
Setup: Some(krb5_err_code_setup),
502+
Free: Some(krb5_err_code_free),
503+
flags: SIGMATCH_INFO_UINT32,
504+
};
505+
G_KRB5_ERR_CODE_KW_ID = SCDetectHelperKeywordRegister(&kw);
449506
}

src/Makefile.am

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ noinst_HEADERS = \
243243
detect-itype.h \
244244
detect-ja4-hash.h \
245245
detect-krb5-cname.h \
246-
detect-krb5-errcode.h \
247246
detect-krb5-sname.h \
248247
detect-krb5-ticket-encryption.h \
249248
detect-l3proto.h \
@@ -824,7 +823,6 @@ libsuricata_c_a_SOURCES = \
824823
detect-itype.c \
825824
detect-ja4-hash.c \
826825
detect-krb5-cname.c \
827-
detect-krb5-errcode.c \
828826
detect-krb5-sname.c \
829827
detect-krb5-ticket-encryption.c \
830828
detect-l3proto.c \

src/detect-engine-register.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@
197197
#include "detect-ipv4hdr.h"
198198
#include "detect-ipv6hdr.h"
199199
#include "detect-krb5-cname.h"
200-
#include "detect-krb5-errcode.h"
201200
#include "detect-krb5-sname.h"
202201
#include "detect-krb5-ticket-encryption.h"
203202
#include "detect-sip-method.h"
@@ -717,8 +716,6 @@ void SigTableSetup(void)
717716
DetectIpv4hdrRegister();
718717
DetectIpv6hdrRegister();
719718
DetectKrb5CNameRegister();
720-
DetectKrb5ErrCodeRegister();
721-
SCDetectKrb5MsgTypeRegister();
722719
DetectKrb5SNameRegister();
723720
DetectKrb5TicketEncryptionRegister();
724721
DetectSipMethodRegister();
@@ -779,6 +776,7 @@ void SigTableSetup(void)
779776
SCDetectSmbRegister();
780777
SCDetectIkeRegister();
781778
SCDetectDcerpcRegister();
779+
SCDetectKrb5Register();
782780

783781
for (size_t i = 0; i < preregistered_callbacks_nb; i++) {
784782
PreregisteredCallbacks[i]();

src/detect-engine-register.h

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

261-
DETECT_KRB5_ERRCODE,
262261
DETECT_KRB5_CNAME,
263262
DETECT_KRB5_SNAME,
264263
DETECT_KRB5_TICKET_ENCRYPTION,

0 commit comments

Comments
 (0)