Skip to content

Commit

Permalink
ssh: limit length for banner logs
Browse files Browse the repository at this point in the history
Ticket: 6770
(cherry picked from commit c4b8fb7)
  • Loading branch information
catenacyber authored and victorjulien committed Mar 18, 2024
1 parent f9ef96a commit c0b9ddb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
16 changes: 16 additions & 0 deletions rust/src/jsonbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,22 @@ impl JsonBuilder {
}
}

/// Set a key and a string value (from bytes) on an object, with a limited size
pub fn set_string_from_bytes_limited(&mut self, key: &str, val: &[u8], limit: usize) -> Result<&mut Self, JsonError> {
let mut valtrunc = Vec::new();
let val = if val.len() > limit {
valtrunc.extend_from_slice(&val[..limit]);
valtrunc.extend_from_slice(b"[truncated]");
&valtrunc
} else {
val
};
match std::str::from_utf8(val) {
Ok(s) => self.set_string(key, s),
Err(_) => self.set_string(key, &try_string_from_bytes(val)?),
}
}

/// Set a key and a string field as the base64 encoded string of the value.
pub fn set_base64(&mut self, key: &str, val: &[u8]) -> Result<&mut Self, JsonError> {
match self.current_state() {
Expand Down
10 changes: 5 additions & 5 deletions rust/src/ssh/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* 02110-1301, USA.
*/

use super::ssh::SSHTransaction;
use super::ssh::{SSHTransaction, SSH_MAX_BANNER_LEN};
use crate::jsonbuilder::{JsonBuilder, JsonError};

fn log_ssh(tx: &SSHTransaction, js: &mut JsonBuilder) -> Result<bool, JsonError> {
Expand All @@ -24,9 +24,9 @@ fn log_ssh(tx: &SSHTransaction, js: &mut JsonBuilder) -> Result<bool, JsonError>
}
if !tx.cli_hdr.protover.is_empty() {
js.open_object("client")?;
js.set_string_from_bytes("proto_version", &tx.cli_hdr.protover)?;
js.set_string_from_bytes_limited("proto_version", &tx.cli_hdr.protover, SSH_MAX_BANNER_LEN)?;
if !tx.cli_hdr.swver.is_empty() {
js.set_string_from_bytes("software_version", &tx.cli_hdr.swver)?;
js.set_string_from_bytes_limited("software_version", &tx.cli_hdr.swver, SSH_MAX_BANNER_LEN)?;
}
if !tx.cli_hdr.hassh.is_empty() || !tx.cli_hdr.hassh_string.is_empty() {
js.open_object("hassh")?;
Expand All @@ -42,9 +42,9 @@ fn log_ssh(tx: &SSHTransaction, js: &mut JsonBuilder) -> Result<bool, JsonError>
}
if !tx.srv_hdr.protover.is_empty() {
js.open_object("server")?;
js.set_string_from_bytes("proto_version", &tx.srv_hdr.protover)?;
js.set_string_from_bytes_limited("proto_version", &tx.srv_hdr.protover, SSH_MAX_BANNER_LEN)?;
if !tx.srv_hdr.swver.is_empty() {
js.set_string_from_bytes("software_version", &tx.srv_hdr.swver)?;
js.set_string_from_bytes_limited("software_version", &tx.srv_hdr.swver, SSH_MAX_BANNER_LEN)?;
}
if !tx.srv_hdr.hassh.is_empty() || !tx.srv_hdr.hassh_string.is_empty() {
js.open_object("hassh")?;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/ssh/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum SSHConnectionState {
SshStateFinished = 3,
}

const SSH_MAX_BANNER_LEN: usize = 256;
pub const SSH_MAX_BANNER_LEN: usize = 256;
const SSH_RECORD_HEADER_LEN: usize = 6;
const SSH_MAX_REASSEMBLED_RECORD_LEN: usize = 65535;

Expand Down

0 comments on commit c0b9ddb

Please sign in to comment.