Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next/198/20231121/v1 #9853

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rust/src/applayertemplate/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ use crate::jsonbuilder::{JsonBuilder, JsonError};
use std;

fn log_template(tx: &TemplateTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> {
js.open_object("template")?;
if let Some(ref request) = tx.request {
js.set_string("request", request)?;
}
if let Some(ref response) = tx.response {
js.set_string("response", response)?;
}
js.close()?;
Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions rust/src/bittorrent_dht/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn print_ip_addr(addr: &[u8]) -> std::string::String {
fn log_bittorrent_dht(
tx: &BitTorrentDHTTransaction, js: &mut JsonBuilder,
) -> Result<(), JsonError> {
js.open_object("bittorrent_dht")?;
js.set_hex("transaction_id", &tx.transaction_id)?;
if let Some(client_version) = &tx.client_version {
js.set_hex("client_version", client_version)?;
Expand Down Expand Up @@ -125,6 +126,7 @@ fn log_bittorrent_dht(
}
js.close()?;
};
js.close()?;
Ok(())
}

Expand Down
5 changes: 3 additions & 2 deletions rust/src/http2/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ fn log_http2_frames(frames: &[HTTP2Frame], js: &mut JsonBuilder) -> Result<bool,
}

fn log_http2(tx: &HTTP2Transaction, js: &mut JsonBuilder) -> Result<bool, JsonError> {
js.open_object("http")?;
js.set_string("version", "2")?;

let mut common: HashMap<HeaderName, &Vec<u8>> = HashMap::new();
Expand Down Expand Up @@ -261,8 +262,8 @@ fn log_http2(tx: &HTTP2Transaction, js: &mut JsonBuilder) -> Result<bool, JsonEr
let has_response = log_http2_frames(&tx.frames_tc, js)?;
js.close()?;

// Close http2.
js.close()?;
js.close()?; // http2
js.close()?; // http

return Ok(has_request || has_response || has_headers);
}
Expand Down
4 changes: 3 additions & 1 deletion rust/src/krb/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::krb::krb5::{KRB5Transaction,test_weak_encryption};

fn krb5_log_response(jsb: &mut JsonBuilder, tx: &mut KRB5Transaction) -> Result<(), JsonError>
{
jsb.open_object("krb5")?;
match tx.error_code {
Some(c) => {
jsb.set_string("msg_type", &format!("{:?}", tx.msg_type))?;
Expand Down Expand Up @@ -63,12 +64,13 @@ fn krb5_log_response(jsb: &mut JsonBuilder, tx: &mut KRB5Transaction) -> Result<
jsb.set_string("ticket_encryption", &refs)?;
jsb.set_bool("ticket_weak_encryption", test_weak_encryption(x))?;
}
jsb.close()?;

return Ok(());
}

#[no_mangle]
pub extern "C" fn rs_krb5_log_json_response(jsb: &mut JsonBuilder, tx: &mut KRB5Transaction) -> bool
pub extern "C" fn rs_krb5_log_json_response(tx: &mut KRB5Transaction, jsb: &mut JsonBuilder) -> bool
{
krb5_log_response(jsb, tx).is_ok()
}
4 changes: 2 additions & 2 deletions rust/src/quic/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn quic_tls_extension_name(e: u16) -> Option<String> {
}
}

fn log_template(tx: &QuicTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> {
fn log_quic(tx: &QuicTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> {
js.open_object("quic")?;
if tx.header.ty != QuicType::Short {
js.set_string("version", String::from(tx.header.version).as_str())?;
Expand Down Expand Up @@ -153,5 +153,5 @@ pub unsafe extern "C" fn rs_quic_to_json(
tx: *mut std::os::raw::c_void, js: &mut JsonBuilder,
) -> bool {
let tx = cast_pointer!(tx, QuicTransaction);
log_template(tx, js).is_ok()
log_quic(tx, js).is_ok()
}
4 changes: 3 additions & 1 deletion rust/src/snmp/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn str_of_pdu_type(t:&PduType) -> Cow<str> {

fn snmp_log_response(jsb: &mut JsonBuilder, tx: &mut SNMPTransaction) -> Result<(), JsonError>
{
jsb.open_object("snmp")?;
jsb.set_uint("version", tx.version as u64)?;
if tx.encrypted {
jsb.set_string("pdu_type", "encrypted")?;
Expand Down Expand Up @@ -71,11 +72,12 @@ fn snmp_log_response(jsb: &mut JsonBuilder, tx: &mut SNMPTransaction) -> Result<
}
}

jsb.close()?;
return Ok(());
}

#[no_mangle]
pub extern "C" fn rs_snmp_log_json_response(jsb: &mut JsonBuilder, tx: &mut SNMPTransaction) -> bool
pub extern "C" fn rs_snmp_log_json_response(tx: &mut SNMPTransaction, jsb: &mut JsonBuilder) -> bool
{
snmp_log_response(jsb, tx).is_ok()
}
2 changes: 2 additions & 0 deletions rust/src/ssh/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::ssh::SSHTransaction;
use crate::jsonbuilder::{JsonBuilder, JsonError};

fn log_ssh(tx: &SSHTransaction, js: &mut JsonBuilder) -> Result<bool, JsonError> {
js.open_object("ssh")?;
if tx.cli_hdr.protover.is_empty() && tx.srv_hdr.protover.is_empty() {
return Ok(false);
}
Expand Down Expand Up @@ -58,6 +59,7 @@ fn log_ssh(tx: &SSHTransaction, js: &mut JsonBuilder) -> Result<bool, JsonError>
}
js.close()?;
}
js.close()?;
return Ok(true);
}

Expand Down
2 changes: 2 additions & 0 deletions rust/src/tftp/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ fn tftp_log_request(tx: &mut TFTPTransaction,
jb: &mut JsonBuilder)
-> Result<(), JsonError>
{
jb.open_object("tftp")?;
match tx.opcode {
1 => jb.set_string("packet", "read")?,
2 => jb.set_string("packet", "write")?,
_ => jb.set_string("packet", "error")?
};
jb.set_string("file", tx.filename.as_str())?;
jb.set_string("mode", tx.mode.as_str())?;
jb.close()?;
Ok(())
}

Expand Down
4 changes: 4 additions & 0 deletions scripts/setup-app-layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ def logger_patch_output_c(proto):
output = io.StringIO()
inlines = open(filename).readlines()
for i, line in enumerate(inlines):
if line.find("ALPROTO_TEMPLATE") > -1:
new_line = line.replace("TEMPLATE", proto.upper()).replace(
"template", proto.lower())
output.write(new_line)
if line.find("output-json-template.h") > -1:
output.write(line.replace("template", proto.lower()))
if line.find("/* Template JSON logger.") > -1:
Expand Down
11 changes: 5 additions & 6 deletions src/app-layer-ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1405,13 +1405,10 @@ uint16_t JsonGetNextLineFromBuffer(const char *buffer, const uint16_t len)
return c == NULL ? len : (uint16_t)(c - buffer + 1);
}

void EveFTPDataAddMetadata(const Flow *f, JsonBuilder *jb)
bool EveFTPDataAddMetadata(void *vtx, JsonBuilder *jb)
{
const FtpDataState *ftp_state = NULL;
if (f->alstate == NULL)
return;

ftp_state = (FtpDataState *)f->alstate;
const FtpDataState *ftp_state = (FtpDataState *)vtx;
jb_open_object(jb, "ftp_data");

if (ftp_state->file_name) {
jb_set_string_from_bytes(jb, "filename", ftp_state->file_name, ftp_state->file_len);
Expand All @@ -1426,6 +1423,8 @@ void EveFTPDataAddMetadata(const Flow *f, JsonBuilder *jb)
default:
break;
}
jb_close(jb);
return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/app-layer-ftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ uint64_t FTPMemuseGlobalCounter(void);
uint64_t FTPMemcapGlobalCounter(void);

uint16_t JsonGetNextLineFromBuffer(const char *buffer, const uint16_t len);
void EveFTPDataAddMetadata(const Flow *f, JsonBuilder *jb);
bool EveFTPDataAddMetadata(void *vtx, JsonBuilder *jb);

#endif /* __APP_LAYER_FTP_H__ */

21 changes: 8 additions & 13 deletions src/detect-app-layer-event.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,19 @@ static int DetectAppLayerEventSetup(DetectEngineCtx *de_ctx, Signature *s, const
}
SCLogDebug("data->event_id %u", data->event_id);

SigMatch *sm = SigMatchAlloc();
if (sm == NULL)
goto error;

sm->type = DETECT_AL_APP_LAYER_EVENT;
sm->ctx = (SigMatchCtx *)data;

if (event_type == APP_LAYER_EVENT_TYPE_PACKET) {
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_MATCH);
if (SigMatchAppendSMToList(de_ctx, s, DETECT_AL_APP_LAYER_EVENT, (SigMatchCtx *)data,
DETECT_SM_LIST_MATCH) == NULL) {
goto error;
}
} else {
if (DetectSignatureSetAppProto(s, data->alproto) != 0)
goto error;

SigMatchAppendSMToList(s, sm, g_applayer_events_list_id);
if (SigMatchAppendSMToList(de_ctx, s, DETECT_AL_APP_LAYER_EVENT, (SigMatchCtx *)data,
g_applayer_events_list_id) == NULL) {
goto error;
}
s->flags |= SIG_FLAG_APPLAYER;
}

Expand All @@ -301,10 +300,6 @@ static int DetectAppLayerEventSetup(DetectEngineCtx *de_ctx, Signature *s, const
if (data) {
DetectAppLayerEventFree(de_ctx, data);
}
if (sm) {
sm->ctx = NULL;
SigMatchFree(de_ctx, sm);
}
return -1;
}

Expand Down
11 changes: 3 additions & 8 deletions src/detect-app-layer-protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ static int DetectAppLayerProtocolSetup(DetectEngineCtx *de_ctx,
Signature *s, const char *arg)
{
DetectAppLayerProtocolData *data = NULL;
SigMatch *sm = NULL;

if (s->alproto != ALPROTO_UNKNOWN) {
SCLogError("Either we already "
Expand Down Expand Up @@ -169,14 +168,10 @@ static int DetectAppLayerProtocolSetup(DetectEngineCtx *de_ctx,
}
}

sm = SigMatchAlloc();
if (sm == NULL)
if (SigMatchAppendSMToList(de_ctx, s, DETECT_AL_APP_LAYER_PROTOCOL, (SigMatchCtx *)data,
DETECT_SM_LIST_MATCH) == NULL) {
goto error;

sm->type = DETECT_AL_APP_LAYER_PROTOCOL;
sm->ctx = (void *)data;

SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_MATCH);
}
return 0;

error:
Expand Down
11 changes: 2 additions & 9 deletions src/detect-asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,12 @@ static int DetectAsn1Setup(DetectEngineCtx *de_ctx, Signature *s, const char *as
if (ad == NULL)
return -1;

/* Okay so far so good, lets get this into a SigMatch
* and put it in the Signature. */
SigMatch *sm = SigMatchAlloc();
if (sm == NULL) {
if (SigMatchAppendSMToList(de_ctx, s, DETECT_ASN1, (SigMatchCtx *)ad, DETECT_SM_LIST_MATCH) ==
NULL) {
DetectAsn1Free(de_ctx, ad);
return -1;
}

sm->type = DETECT_ASN1;
sm->ctx = (SigMatchCtx *)ad;

SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_MATCH);

return 0;
}

Expand Down
8 changes: 2 additions & 6 deletions src/detect-base64-decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ static int DetectBase64DecodeSetup(DetectEngineCtx *de_ctx, Signature *s,
uint8_t relative = 0;
DetectBase64Decode *data = NULL;
int sm_list;
SigMatch *sm = NULL;
SigMatch *pm = NULL;

if (str != NULL) {
Expand Down Expand Up @@ -226,13 +225,10 @@ static int DetectBase64DecodeSetup(DetectEngineCtx *de_ctx, Signature *s,
}
}

sm = SigMatchAlloc();
if (sm == NULL) {
if (SigMatchAppendSMToList(de_ctx, s, DETECT_BASE64_DECODE, (SigMatchCtx *)data, sm_list) ==
NULL) {
goto error;
}
sm->type = DETECT_BASE64_DECODE;
sm->ctx = (SigMatchCtx *)data;
SigMatchAppendSMToList(s, sm, sm_list);

if (!data->bytes) {
data->bytes = BASE64_DECODE_MAX;
Expand Down
9 changes: 2 additions & 7 deletions src/detect-bsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ static int SigParseGetMaxBsize(DetectU64Data *bsz)
static int DetectBsizeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *sizestr)
{
SCEnter();
SigMatch *sm = NULL;

if (DetectBufferGetActiveList(de_ctx, s) == -1)
SCReturnInt(-1);
Expand All @@ -212,13 +211,9 @@ static int DetectBsizeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *
if (bsz == NULL)
goto error;

sm = SigMatchAlloc();
if (sm == NULL)
if (SigMatchAppendSMToList(de_ctx, s, DETECT_BSIZE, (SigMatchCtx *)bsz, list) == NULL) {
goto error;
sm->type = DETECT_BSIZE;
sm->ctx = (void *)bsz;

SigMatchAppendSMToList(s, sm, list);
}

SCReturnInt(0);

Expand Down
9 changes: 2 additions & 7 deletions src/detect-bypass.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,16 @@ void DetectBypassRegister(void)

static int DetectBypassSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
{
SigMatch *sm = NULL;

if (s->flags & SIG_FLAG_FILESTORE) {
SCLogError("bypass can't work with filestore keyword");
return -1;
}
s->flags |= SIG_FLAG_BYPASS;

sm = SigMatchAlloc();
if (sm == NULL)
if (SigMatchAppendSMToList(de_ctx, s, DETECT_BYPASS, NULL, DETECT_SM_LIST_POSTMATCH) == NULL) {
return -1;

sm->type = DETECT_BYPASS;
sm->ctx = NULL;
SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_POSTMATCH);
}

return 0;
}
Expand Down
11 changes: 3 additions & 8 deletions src/detect-byte-extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ static inline DetectByteExtractData *DetectByteExtractParse(DetectEngineCtx *de_
*/
static int DetectByteExtractSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
{
SigMatch *sm = NULL;
SigMatch *prev_pm = NULL;
DetectByteExtractData *data = NULL;
int ret = -1;
Expand Down Expand Up @@ -609,14 +608,10 @@ static int DetectByteExtractSetup(DetectEngineCtx *de_ctx, Signature *s, const c
if (data->local_id > de_ctx->byte_extract_max_local_id)
de_ctx->byte_extract_max_local_id = data->local_id;


sm = SigMatchAlloc();
if (sm == NULL)
if (SigMatchAppendSMToList(de_ctx, s, DETECT_BYTE_EXTRACT, (SigMatchCtx *)data, sm_list) ==
NULL) {
goto error;
sm->type = DETECT_BYTE_EXTRACT;
sm->ctx = (void *)data;
SigMatchAppendSMToList(s, sm, sm_list);

}

if (!(data->flags & DETECT_BYTE_EXTRACT_FLAG_RELATIVE))
goto okay;
Expand Down
8 changes: 2 additions & 6 deletions src/detect-bytejump.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,6 @@ static DetectBytejumpData *DetectBytejumpParse(

static int DetectBytejumpSetup(DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
{
SigMatch *sm = NULL;
SigMatch *prev_pm = NULL;
DetectBytejumpData *data = NULL;
char *offset = NULL;
Expand Down Expand Up @@ -569,12 +568,9 @@ static int DetectBytejumpSetup(DetectEngineCtx *de_ctx, Signature *s, const char
offset = NULL;
}

sm = SigMatchAlloc();
if (sm == NULL)
if (SigMatchAppendSMToList(de_ctx, s, DETECT_BYTEJUMP, (SigMatchCtx *)data, sm_list) == NULL) {
goto error;
sm->type = DETECT_BYTEJUMP;
sm->ctx = (SigMatchCtx *)data;
SigMatchAppendSMToList(s, sm, sm_list);
}

if (!(data->flags & DETECT_BYTEJUMP_RELATIVE))
goto okay;
Expand Down
Loading