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

Dev/ldap v0.3: small improvements to the LDAP parser #12152

Closed
wants to merge 2 commits into from
Closed
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
34 changes: 32 additions & 2 deletions rust/src/ldap/ldap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static mut LDAP_MAX_TX: usize = LDAP_MAX_TX_DEFAULT;

static mut ALPROTO_LDAP: AppProto = ALPROTO_UNKNOWN;

const STARTTLS_OID: &str = "1.3.6.1.4.1.1466.20037";

#[derive(AppLayerFrameType)]
pub enum LdapFrameType {
Pdu,
Expand Down Expand Up @@ -92,6 +94,8 @@ pub struct LdapState {
response_frame: Option<Frame>,
request_gap: bool,
response_gap: bool,
request_tls: bool,
has_starttls: bool,
}

impl State<LdapTransaction> for LdapState {
Expand All @@ -115,6 +119,8 @@ impl LdapState {
response_frame: None,
request_gap: false,
response_gap: false,
request_tls: false,
has_starttls: false,
}
}

Expand Down Expand Up @@ -182,6 +188,13 @@ impl LdapState {
return AppLayerResult::ok();
}

if self.has_starttls {
unsafe {
AppLayerRequestProtocolTLSUpgrade(flow);
}
return AppLayerResult::ok();
}

if self.request_gap {
match ldap_parse_msg(input) {
Ok((_, msg)) => {
Expand Down Expand Up @@ -216,6 +229,12 @@ impl LdapState {
let mut tx = self.new_tx();
let tx_id = tx.id();
let request = LdapMessage::from(msg);
// check if STARTTLS was requested
if let ProtocolOp::ExtendedRequest(request) = &request.protocol_op {
if request.request_name.0 == STARTTLS_OID {
self.request_tls = true;
}
}
tx.complete = tx_is_complete(&request.protocol_op, Direction::ToServer);
tx.request = Some(request);
self.transactions.push_back(tx);
Expand Down Expand Up @@ -275,6 +294,17 @@ impl LdapState {
match ldap_parse_msg(start) {
Ok((rem, msg)) => {
let response = LdapMessage::from(msg);
// check if STARTTLS was requested
if self.request_tls {
if let ProtocolOp::ExtendedResponse(response) = &response.protocol_op
{
if response.result.result_code == ResultCode(0) {
SCLogDebug!("LDAP: STARTTLS detected");
self.has_starttls = true;
}
self.request_tls = false;
}
}
if let Some(tx) = self.find_request(response.message_id) {
tx.complete = tx_is_complete(&response.protocol_op, Direction::ToClient);
let tx_id = tx.id();
Expand Down Expand Up @@ -617,7 +647,7 @@ const PARSER_NAME: &[u8] = b"ldap\0";

#[no_mangle]
pub unsafe extern "C" fn SCRegisterLdapTcpParser() {
let default_port = CString::new("389").unwrap();
let default_port = CString::new("[389, 3268]").unwrap();
let parser = RustParser {
name: PARSER_NAME.as_ptr() as *const c_char,
default_port: default_port.as_ptr(),
Expand Down Expand Up @@ -674,7 +704,7 @@ pub unsafe extern "C" fn SCRegisterLdapTcpParser() {

#[no_mangle]
pub unsafe extern "C" fn SCRegisterLdapUdpParser() {
let default_port = CString::new("389").unwrap();
let default_port = CString::new("[389, 3268]").unwrap();
let parser = RustParser {
name: PARSER_NAME.as_ptr() as *const c_char,
default_port: default_port.as_ptr(),
Expand Down
4 changes: 2 additions & 2 deletions suricata.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -1188,11 +1188,11 @@ app-layer:
tcp:
enabled: yes
detection-ports:
dp: 389
dp: 389, 3268
udp:
enabled: yes
detection-ports:
dp: 389
dp: 389, 3268
# Maximum number of live LDAP transactions per flow
# max-tx: 1024

Expand Down
Loading