Skip to content

Commit a810315

Browse files
catenacybervictorjulien
authored andcommitted
detect/krb5: move krb5.ticket_encryption to rust
Ticket: 8648
1 parent 8f0dbc0 commit a810315

6 files changed

Lines changed: 76 additions & 145 deletions

File tree

rust/src/krb/detect.rs

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ unsafe extern "C" fn krb5_sname_get_data(
8383
const KRB_TICKET_FASTARRAY_SIZE: usize = 256;
8484

8585
#[derive(Debug)]
86-
pub struct DetectKrb5TicketEncryptionList {
86+
struct DetectKrb5TicketEncryptionList {
8787
positive: [bool; KRB_TICKET_FASTARRAY_SIZE],
8888
negative: [bool; KRB_TICKET_FASTARRAY_SIZE],
8989
other: Vec<EncryptionType>,
@@ -96,7 +96,7 @@ impl Default for DetectKrb5TicketEncryptionList {
9696
}
9797

9898
impl DetectKrb5TicketEncryptionList {
99-
pub fn new() -> Self {
99+
fn new() -> Self {
100100
Self {
101101
positive: [false; KRB_TICKET_FASTARRAY_SIZE],
102102
negative: [false; KRB_TICKET_FASTARRAY_SIZE],
@@ -105,20 +105,20 @@ impl DetectKrb5TicketEncryptionList {
105105
}
106106
}
107107

108-
// Suppress large enum variant lint as the LIST is very large compared
108+
// Suppress large enum variant lint as the List is very large compared
109109
// to the boolean variant.
110110
#[derive(Debug)]
111111
#[allow(clippy::large_enum_variant)]
112-
pub enum DetectKrb5TicketEncryptionData {
113-
WEAK(bool),
114-
LIST(DetectKrb5TicketEncryptionList),
112+
enum DetectKrb5TicketEncryptionData {
113+
Weak(bool),
114+
List(DetectKrb5TicketEncryptionList),
115115
}
116116

117-
pub fn detect_parse_encryption_weak(i: &str) -> IResult<&str, DetectKrb5TicketEncryptionData> {
117+
fn detect_parse_encryption_weak(i: &str) -> IResult<&str, DetectKrb5TicketEncryptionData> {
118118
let (i, neg) = opt(char('!')).parse(i)?;
119119
let (i, _) = tag("weak").parse(i)?;
120120
let value = neg.is_none();
121-
return Ok((i, DetectKrb5TicketEncryptionData::WEAK(value)));
121+
return Ok((i, DetectKrb5TicketEncryptionData::Weak(value)));
122122
}
123123

124124
trait MyFromStr {
@@ -175,11 +175,11 @@ impl MyFromStr for EncryptionType {
175175
}
176176
}
177177

178-
pub fn is_alphanumeric_or_dash(chr: char) -> bool {
178+
fn is_alphanumeric_or_dash(chr: char) -> bool {
179179
return chr.is_alphanumeric() || chr == '-';
180180
}
181181

182-
pub fn detect_parse_encryption_item(i: &str) -> IResult<&str, EncryptionType> {
182+
fn detect_parse_encryption_item(i: &str) -> IResult<&str, EncryptionType> {
183183
let (i, _) = opt(is_a(" ")).parse(i)?;
184184
let (i, e) = map_res(take_while1(is_alphanumeric_or_dash), |s: &str| {
185185
EncryptionType::from_str(s)
@@ -190,7 +190,7 @@ pub fn detect_parse_encryption_item(i: &str) -> IResult<&str, EncryptionType> {
190190
return Ok((i, e));
191191
}
192192

193-
pub fn detect_parse_encryption_list(i: &str) -> IResult<&str, DetectKrb5TicketEncryptionData> {
193+
fn detect_parse_encryption_list(i: &str) -> IResult<&str, DetectKrb5TicketEncryptionData> {
194194
let mut l = DetectKrb5TicketEncryptionList::new();
195195
let (i, v) = many1(detect_parse_encryption_item).parse(i)?;
196196
for &val in v.iter() {
@@ -204,18 +204,17 @@ pub fn detect_parse_encryption_list(i: &str) -> IResult<&str, DetectKrb5TicketEn
204204
l.other.push(val);
205205
}
206206
}
207-
return Ok((i, DetectKrb5TicketEncryptionData::LIST(l)));
207+
return Ok((i, DetectKrb5TicketEncryptionData::List(l)));
208208
}
209209

210-
pub fn detect_parse_encryption(i: &str) -> IResult<&str, DetectKrb5TicketEncryptionData> {
210+
fn detect_parse_encryption(i: &str) -> IResult<&str, DetectKrb5TicketEncryptionData> {
211211
let (i, _) = opt(is_a(" ")).parse(i)?;
212212
let (i, parsed) = alt((detect_parse_encryption_weak, detect_parse_encryption_list)).parse(i)?;
213213
let (i, _) = all_consuming(take_while(|c| c == ' ')).parse(i)?;
214214
return Ok((i, parsed));
215215
}
216216

217-
#[no_mangle]
218-
pub unsafe extern "C" fn SCKrb5DetectEncryptionParse(
217+
unsafe extern "C" fn krb5_detect_encryption_parse(
219218
ustr: *const std::os::raw::c_char,
220219
) -> *mut DetectKrb5TicketEncryptionData {
221220
let ft_name: &CStr = CStr::from_ptr(ustr); //unsafe
@@ -228,18 +227,20 @@ pub unsafe extern "C" fn SCKrb5DetectEncryptionParse(
228227
return std::ptr::null_mut();
229228
}
230229

231-
#[no_mangle]
232-
pub unsafe extern "C" fn SCKrb5DetectEncryptionMatch(
233-
tx: &KRB5Transaction, ctx: &DetectKrb5TicketEncryptionData,
234-
) -> std::os::raw::c_int {
230+
unsafe extern "C" fn krb5_ticket_encryption_match(
231+
_de: *mut DetectEngineThreadCtx, _f: *mut Flow, _flags: u8, _state: *mut c_void,
232+
tx: *mut c_void, _sig: *const Signature, ctx: *const SigMatchCtx,
233+
) -> c_int {
234+
let tx = cast_pointer!(tx, KRB5Transaction);
235+
let ctx = cast_pointer!(ctx, DetectKrb5TicketEncryptionData);
235236
if let Some(x) = tx.ticket_etype {
236237
match ctx {
237-
DetectKrb5TicketEncryptionData::WEAK(w) => {
238+
DetectKrb5TicketEncryptionData::Weak(w) => {
238239
if (test_weak_encryption(x) && *w) || (!test_weak_encryption(x) && !*w) {
239240
return 1;
240241
}
241242
}
242-
DetectKrb5TicketEncryptionData::LIST(l) => {
243+
DetectKrb5TicketEncryptionData::List(l) => {
243244
let vali = x.0;
244245
if vali < 0 && ((-vali) as usize) < KRB_TICKET_FASTARRAY_SIZE {
245246
if l.negative[(-vali) as usize] {
@@ -262,12 +263,6 @@ pub unsafe extern "C" fn SCKrb5DetectEncryptionMatch(
262263
return 0;
263264
}
264265

265-
#[no_mangle]
266-
pub unsafe extern "C" fn SCKrb5DetectEncryptionFree(ctx: &mut DetectKrb5TicketEncryptionData) {
267-
// Just unbox...
268-
std::mem::drop(Box::from_raw(ctx));
269-
}
270-
271266
#[cfg(test)]
272267
mod tests {
273268

@@ -278,7 +273,7 @@ mod tests {
278273
match detect_parse_encryption(" weak ") {
279274
Ok((rem, ctx)) => {
280275
match ctx {
281-
DetectKrb5TicketEncryptionData::WEAK(w) => {
276+
DetectKrb5TicketEncryptionData::Weak(w) => {
282277
assert!(w);
283278
}
284279
_ => {
@@ -295,7 +290,7 @@ mod tests {
295290
match detect_parse_encryption("!weak") {
296291
Ok((rem, ctx)) => {
297292
match ctx {
298-
DetectKrb5TicketEncryptionData::WEAK(w) => {
293+
DetectKrb5TicketEncryptionData::Weak(w) => {
299294
assert!(!w);
300295
}
301296
_ => {
@@ -312,7 +307,7 @@ mod tests {
312307
match detect_parse_encryption(" des-cbc-crc , -128,2 257") {
313308
Ok((rem, ctx)) => {
314309
match ctx {
315-
DetectKrb5TicketEncryptionData::LIST(l) => {
310+
DetectKrb5TicketEncryptionData::List(l) => {
316311
assert!(l.positive[EncryptionType::DES_CBC_CRC.0 as usize]);
317312
assert!(l.negative[128]);
318313
assert!(l.positive[2]);
@@ -332,7 +327,7 @@ mod tests {
332327
}
333328
let ctx = detect_parse_encryption("-2147483648").unwrap().1;
334329
match ctx {
335-
DetectKrb5TicketEncryptionData::LIST(l) => {
330+
DetectKrb5TicketEncryptionData::List(l) => {
336331
assert_eq!(l.other.len(), 1);
337332
assert_eq!(l.other[0], EncryptionType(i32::MIN));
338333
}
@@ -348,6 +343,8 @@ static mut G_KRB5_GENERIC_BUFFER_ID: c_int = 0;
348343
static mut G_KRB5_ERR_CODE_KW_ID: u16 = 0;
349344
static mut G_KRB5_CNAME_BUFFER_ID: c_int = 0;
350345
static mut G_KRB5_SNAME_BUFFER_ID: c_int = 0;
346+
static mut G_KRB5_TICKET_ENCRYPTION_KW_ID: u16 = 0;
347+
static mut G_KRB5_TICKET_ENCRYPTION_BUFFER_ID: c_int = 0;
351348

352349
// We should apply the derive on the kerberos_parser MessageType
353350
#[repr(u32)]
@@ -501,6 +498,36 @@ unsafe extern "C" fn krb5_cname_setup(
501498
return 0;
502499
}
503500

501+
unsafe extern "C" fn krb5_ticket_encryption_setup(
502+
de: *mut DetectEngineCtx, s: *mut Signature, raw: *const libc::c_char,
503+
) -> c_int {
504+
if SCDetectSignatureSetAppProto(s, ALPROTO_KRB5 as AppProto) != 0 {
505+
return -1;
506+
}
507+
let ctx = krb5_detect_encryption_parse(raw) as *mut c_void;
508+
if ctx.is_null() {
509+
return -1;
510+
}
511+
if SCSigMatchAppendSMToList(
512+
de,
513+
s,
514+
G_KRB5_TICKET_ENCRYPTION_KW_ID,
515+
ctx as *mut SigMatchCtx,
516+
G_KRB5_TICKET_ENCRYPTION_BUFFER_ID,
517+
)
518+
.is_null()
519+
{
520+
krb5_ticket_encryption_free(std::ptr::null_mut(), ctx);
521+
return -1;
522+
}
523+
return 0;
524+
}
525+
526+
unsafe extern "C" fn krb5_ticket_encryption_free(_de: *mut DetectEngineCtx, ctx: *mut c_void) {
527+
let ctx = cast_pointer!(ctx, DetectKrb5TicketEncryptionData);
528+
std::mem::drop(Box::from_raw(ctx));
529+
}
530+
504531
#[no_mangle]
505532
pub unsafe extern "C" fn SCDetectKrb5Register() {
506533
let kw = SCSigTableAppLiteElmt {
@@ -570,4 +597,22 @@ pub unsafe extern "C" fn SCDetectKrb5Register() {
570597
krb5_cname_hdr_kw_id,
571598
b"krb5_cname\0".as_ptr() as *const libc::c_char,
572599
);
600+
601+
let kw = SCSigTableAppLiteElmt {
602+
name: b"krb5.ticket_encryption\0".as_ptr() as *const libc::c_char,
603+
desc: b"match Kerberos 5 ticket encryption\0".as_ptr() as *const libc::c_char,
604+
url: b"/rules/kerberos-keywords.html#krb5-ticket-encryption\0".as_ptr()
605+
as *const libc::c_char,
606+
AppLayerTxMatch: Some(krb5_ticket_encryption_match),
607+
Setup: Some(krb5_ticket_encryption_setup),
608+
Free: Some(krb5_ticket_encryption_free),
609+
flags: 0,
610+
};
611+
G_KRB5_TICKET_ENCRYPTION_KW_ID = SCDetectHelperKeywordRegister(&kw);
612+
G_KRB5_TICKET_ENCRYPTION_BUFFER_ID = SCDetectHelperBufferProgressRegister(
613+
b"krb5_ticket_encryption\0".as_ptr() as *const libc::c_char,
614+
ALPROTO_KRB5 as AppProto,
615+
STREAM_TOCLIENT,
616+
1,
617+
);
573618
}

src/Makefile.am

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ noinst_HEADERS = \
242242
detect-isdataat.h \
243243
detect-itype.h \
244244
detect-ja4-hash.h \
245-
detect-krb5-ticket-encryption.h \
246245
detect-l3proto.h \
247246
detect-lua-extensions.h \
248247
detect-lua.h \
@@ -820,7 +819,6 @@ libsuricata_c_a_SOURCES = \
820819
detect-isdataat.c \
821820
detect-itype.c \
822821
detect-ja4-hash.c \
823-
detect-krb5-ticket-encryption.c \
824822
detect-l3proto.c \
825823
detect-lua-extensions.c \
826824
detect-lua.c \

src/detect-engine-register.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@
196196
#include "detect-icmpv6-mtu.h"
197197
#include "detect-ipv4hdr.h"
198198
#include "detect-ipv6hdr.h"
199-
#include "detect-krb5-ticket-encryption.h"
200199
#include "detect-sip-method.h"
201200
#include "detect-sip-uri.h"
202201
#include "detect-target.h"
@@ -713,7 +712,6 @@ void SigTableSetup(void)
713712
DetectEtherhdrRegister();
714713
DetectIpv4hdrRegister();
715714
DetectIpv6hdrRegister();
716-
DetectKrb5TicketEncryptionRegister();
717715
DetectSipMethodRegister();
718716
DetectSipUriRegister();
719717
DetectTargetRegister();

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_TICKET_ENCRYPTION,
262-
263261
DETECT_SIP_METHOD,
264262
DETECT_SIP_URI,
265263
DETECT_TEMPLATE,

src/detect-krb5-ticket-encryption.c

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

src/detect-krb5-ticket-encryption.h

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

0 commit comments

Comments
 (0)