Skip to content

Commit

Permalink
snmp: register protocol dynamically
Browse files Browse the repository at this point in the history
Ticket: 5053
  • Loading branch information
catenacyber committed Jan 17, 2025
1 parent b99c0a6 commit f9314c5
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
20 changes: 15 additions & 5 deletions rust/src/snmp/snmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use crate::snmp::snmp_parser::*;
use crate::core::{self, *};
use crate::applayer::{self, *};
use super::log::rs_snmp_log_json_response;
use super::detect::ScDetectSNMPRegister;
use std;
use std::ffi::CString;

Expand Down Expand Up @@ -410,20 +412,28 @@ pub unsafe extern "C" fn rs_register_snmp_parser() {
get_frame_name_by_id: None,
};
let ip_proto_str = CString::new("udp").unwrap();
ALPROTO_SNMP = AppProtoNewProtoFromString(PARSER_NAME.as_ptr() as *const std::os::raw::c_char);
let reg_data = EveJsonTxLoggerRegistrationData {
confname: b"eve-log.snmp\0".as_ptr() as *const std::os::raw::c_char,
logname: b"JsonSNMPLog\0".as_ptr() as *const std::os::raw::c_char,
alproto: ALPROTO_SNMP,
dir: OutputJsonLogDirection::LOG_DIR_PACKET as u8,
LogTx: rs_snmp_log_json_response,
};
OutputPreRegisterLogger(reg_data);
SigTablePreRegister(ScDetectSNMPRegister);
if AppLayerProtoDetectConfProtoDetectionEnabled(ip_proto_str.as_ptr(), parser.name) != 0 {
// port 161
let alproto = AppLayerRegisterProtocolDetection(&parser, 1);
// store the allocated ID for the probe function
ALPROTO_SNMP = alproto;
_ = AppLayerRegisterProtocolDetection(&parser, 1);
if AppLayerParserConfParserEnabled(ip_proto_str.as_ptr(), parser.name) != 0 {
let _ = AppLayerRegisterParser(&parser, alproto);
let _ = AppLayerRegisterParser(&parser, ALPROTO_SNMP);
}
// port 162
let default_port_traps = CString::new("162").unwrap();
parser.default_port = default_port_traps.as_ptr();
let _ = AppLayerRegisterProtocolDetection(&parser, 1);
if AppLayerParserConfParserEnabled(ip_proto_str.as_ptr(), parser.name) != 0 {
let _ = AppLayerRegisterParser(&parser, alproto);
let _ = AppLayerRegisterParser(&parser, ALPROTO_SNMP);
}
AppLayerParserRegisterLogger(IPPROTO_UDP, ALPROTO_SNMP);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/app-layer-protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ enum AppProtoEnum {
ALPROTO_KRB5,
ALPROTO_QUIC,
ALPROTO_DHCP,
ALPROTO_SNMP,
ALPROTO_SIP,
ALPROTO_RFB,
ALPROTO_MQTT,
Expand All @@ -78,6 +77,7 @@ enum AppProtoEnum {
/* keep last */
ALPROTO_MAX_STATIC,
// After this ALPROTO_MAX_STATIC can come dynamic alproto ids
// For example, ALPROTO_SNMP is now dynamic
};
// NOTE: if ALPROTO's get >= 256, update SignatureNonPrefilterStore

Expand Down
1 change: 0 additions & 1 deletion src/app-layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,6 @@ static void AppLayerNamesSetup(void)
AppProtoRegisterProtoString(ALPROTO_KRB5, "krb5");
AppProtoRegisterProtoString(ALPROTO_QUIC, "quic");
AppProtoRegisterProtoString(ALPROTO_DHCP, "dhcp");
AppProtoRegisterProtoString(ALPROTO_SNMP, "snmp");
AppProtoRegisterProtoString(ALPROTO_SIP, "sip");
AppProtoRegisterProtoString(ALPROTO_RFB, "rfb");
AppProtoRegisterProtoString(ALPROTO_MQTT, "mqtt");
Expand Down
1 change: 0 additions & 1 deletion src/detect-engine-register.c
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,6 @@ void SigTableSetup(void)
DetectVlanIdRegister();
DetectVlanLayersRegister();

ScDetectSNMPRegister();
ScDetectDHCPRegister();
ScDetectWebsocketRegister();
ScDetectEnipRegister();
Expand Down
7 changes: 0 additions & 7 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -885,8 +885,6 @@ void OutputRegisterRootLoggers(void)
ALPROTO_KRB5, (EveJsonSimpleTxLogFunc)rs_krb5_log_json_response, NULL);
RegisterSimpleJsonApplayerLogger(ALPROTO_QUIC, rs_quic_to_json, NULL);
// ALPROTO_DHCP TODO missing
RegisterSimpleJsonApplayerLogger(
ALPROTO_SNMP, (EveJsonSimpleTxLogFunc)rs_snmp_log_json_response, NULL);
RegisterSimpleJsonApplayerLogger(ALPROTO_SIP, (EveJsonSimpleTxLogFunc)rs_sip_log_json, NULL);
RegisterSimpleJsonApplayerLogger(ALPROTO_RFB, rs_rfb_logger_log, NULL);
RegisterSimpleJsonApplayerLogger(ALPROTO_MQTT, JsonMQTTAddMetadata, NULL);
Expand Down Expand Up @@ -1084,12 +1082,7 @@ void OutputRegisterLoggers(void)
SCLogDebug("quic json logger registered.");
/* DHCP JSON logger. */
JsonDHCPLogRegister();
/* SNMP JSON logger. */
OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonSNMPLog", "eve-log.snmp",
OutputJsonLogInitSub, ALPROTO_SNMP, JsonGenericDirPacketLogger, JsonLogThreadInit,
JsonLogThreadDeinit);

SCLogDebug("SNMP JSON logger registered.");
/* SIP JSON logger. */
OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonSIPLog", "eve-log.sip",
OutputJsonLogInitSub, ALPROTO_SIP, JsonGenericDirPacketLogger, JsonLogThreadInit,
Expand Down

0 comments on commit f9314c5

Please sign in to comment.