Skip to content
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
1 change: 1 addition & 0 deletions doc/userguide/rules/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Suricata Rules
quic-keywords
nfs-keywords
smtp-keywords
websocket-keywords
app-layer
xbits
thresholding
Expand Down
1 change: 1 addition & 0 deletions doc/userguide/rules/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ you can pick from. These are:
* snmp
* tftp
* sip
* websocket

The availability of these protocols depends on whether the protocol
is enabled in the configuration file, suricata.yaml.
Expand Down
50 changes: 50 additions & 0 deletions doc/userguide/rules/websocket-keywords.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
WebSocket Keywords
==================

websocket.payload
-----------------

A sticky buffer on the unmasked payload,
limited by suricata.yaml config value ``websocket.max-payload-size``.

Examples::

websocket.payload; pcre:"/^123[0-9]*/";
websocket.payload content:"swordfish";

``websocket.payload`` is a 'sticky buffer' and can be used as ``fast_pattern``.

websocket.fin
-------------

A boolean to tell if the payload is complete.

Examples::

websocket.fin:true;
websocket.fin:false;

websocket.mask
--------------

Matches on the websocket mask if any.
It uses a 32-bit unsigned integer as value (big-endian).

Examples::

websocket.mask:123456;
websocket.mask:>0;

websocket.opcode
----------------

Matches on the websocket opcode.
It uses a 8-bit unsigned integer as value.
Only 16 values are relevant.
It can also be specified by text from the enumeration

Examples::

websocket.opcode:1;
websocket.opcode:>8;
websocket.opcode:ping;
24 changes: 24 additions & 0 deletions etc/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3833,6 +3833,9 @@
},
"tls": {
"$ref": "#/$defs/stats_applayer_error"
},
"websocket": {
"$ref": "#/$defs/stats_applayer_error"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -3950,6 +3953,9 @@
},
"tls": {
"type": "integer"
},
"websocket": {
"type": "integer"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -4061,6 +4067,9 @@
},
"tls": {
"type": "integer"
},
"websocket": {
"type": "integer"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -5498,6 +5507,21 @@
}
},
"additionalProperties": false
},
"websocket": {
"type": "object",
"properties": {
"fin": {
"type": "boolean"
},
"mask": {
"type": "integer"
},
"opcode": {
"type": "string"
}
},
"additionalProperties": false
}
},
"$defs": {
Expand Down
3 changes: 2 additions & 1 deletion rules/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ smb-events.rules \
smtp-events.rules \
ssh-events.rules \
stream-events.rules \
tls-events.rules
tls-events.rules \
websocket-events.rules
8 changes: 8 additions & 0 deletions rules/websocket-events.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# WebSocket app-layer event rules.
#
# These SIDs fall in the 2235000+ range. See:
# http://doc.emergingthreats.net/bin/view/Main/SidAllocation and
# https://redmine.openinfosecfoundation.org/projects/suricata/wiki/AppLayer

alert websocket any any -> any any (msg:"SURICATA Websocket skipped end of payload"; app-layer-event:websocket.skip_end_of_payload; classtype:protocol-command-decode; sid:2235000; rev:1;)
alert websocket any any -> any any (msg:"SURICATA Websocket reassembly limit reached"; app-layer-event:websocket.reassembly_limit_reached; classtype:protocol-command-decode; sid:2235001; rev:1;)
6 changes: 6 additions & 0 deletions rust/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use proc_macro::TokenStream;

mod applayerevent;
mod applayerframetype;
mod stringenum;

/// The `AppLayerEvent` derive macro generates a `AppLayerEvent` trait
/// implementation for enums that define AppLayerEvents.
Expand Down Expand Up @@ -50,3 +51,8 @@ pub fn derive_app_layer_event(input: TokenStream) -> TokenStream {
pub fn derive_app_layer_frame_type(input: TokenStream) -> TokenStream {
applayerframetype::derive_app_layer_frame_type(input)
}

#[proc_macro_derive(EnumStringU8, attributes(name))]
pub fn derive_enum_string_u8(input: TokenStream) -> TokenStream {
stringenum::derive_enum_string::<u8>(input, "u8")
}
111 changes: 111 additions & 0 deletions rust/derive/src/stringenum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* Copyright (C) 2023 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

extern crate proc_macro;
use super::applayerevent::transform_name;
use proc_macro::TokenStream;
use quote::quote;
use syn::{self, parse_macro_input, DeriveInput};
use std::str::FromStr;

pub fn derive_enum_string<T: std::str::FromStr + quote::ToTokens>(input: TokenStream, ustr: &str) -> TokenStream where <T as FromStr>::Err: std::fmt::Display {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;
let mut values = Vec::new();
let mut names = Vec::new();
let mut fields = Vec::new();

if let syn::Data::Enum(ref data) = input.data {
for (_, v) in (&data.variants).into_iter().enumerate() {
if let Some((_, val)) = &v.discriminant {
let fname = transform_name(&v.ident.to_string());
names.push(fname);
fields.push(v.ident.clone());
if let syn::Expr::Lit(l) = val {
if let syn::Lit::Int(li) = &l.lit {
if let Ok(value) = li.base10_parse::<T>() {
values.push(value);
} else {
panic!("EnumString requires explicit {}", ustr);
}
} else {
panic!("EnumString requires explicit literal integer");
}
} else {
panic!("EnumString requires explicit literal");
}
} else {
panic!("EnumString requires explicit values");
}
}
} else {
panic!("EnumString can only be derived for enums");
}

let is_suricata = std::env::var("CARGO_PKG_NAME").map(|var| var == "suricata").unwrap_or(false);
let crate_id = if is_suricata {
syn::Ident::new("crate", proc_macro2::Span::call_site())
} else {
syn::Ident::new("suricata", proc_macro2::Span::call_site())
};

let utype_str = syn::Ident::new(&ustr, proc_macro2::Span::call_site());

let expanded = quote! {
impl #crate_id::detect::Enum<#utype_str> for #name {
fn from_u(v: #utype_str) -> Option<Self> {
match v {
#( #values => Some(#name::#fields) ,)*
_ => None,
}
}
fn into_u(&self) -> #utype_str {
match *self {
#( #name::#fields => #values ,)*
}
}
fn to_str(&self) -> &'static str {
match *self {
#( #name::#fields => #names ,)*
}
}
fn from_str(s: &str) -> Option<Self> {
match s {
#( #names => Some(#name::#fields) ,)*
_ => None
}
}
fn to_detect_ctx(s: &str) -> Option<DetectUintData<#utype_str>> {
if let Ok((_, ctx)) = detect_parse_uint::<#utype_str>(s) {
return Some(ctx);
}
if let Some(arg1) = #name::from_str(s) {
let arg1 = #name::into_u(&arg1);
let ctx = DetectUintData::<#utype_str> {
arg1,
arg2: 0,
mode: DetectUintMode::DetectUintModeEqual,
};
return Some(ctx);
}
return None;
}
}
};

proc_macro::TokenStream::from(expanded)
}
21 changes: 21 additions & 0 deletions rust/src/detect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,24 @@ pub mod stream_size;
pub mod uint;
pub mod uri;
pub mod requires;

use crate::detect::uint::DetectUintData;

/// Enum trait that will be implemented on enums that
/// derive StringEnum.
pub trait Enum<T> {
/// Return the enum variant of the given numeric value.
fn from_u(v: T) -> Option<Self> where Self: Sized;

/// Convert the enum variant to the numeric value.
fn into_u(&self) -> T;

/// Return the string for logging the enum value.
fn to_str(&self) -> &'static str;

/// Get an enum variant from parsing a string.
fn from_str(s: &str) -> Option<Self> where Self: Sized;

/// Get a detect context for integer keyword.
fn to_detect_ctx(s: &str) -> Option<DetectUintData<T>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you should now be able to do something like:

impl<T> TryFrom<&dyn Enum<T>> for DetectUintData<T> {
    type Error = ();

    fn try_from(value: &dyn Enum<T>) -> Result<Self, Self::Error> {
        todo!()
    }
}

instead of having this method as part of the trait.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively naming.. This is an enum specifically for use in detection with a direct mapping from a name to an integer value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you should now be able to do something like:

impl<T> TryFrom<&dyn Enum<T>> for DetectUintData<T> {
    type Error = ();

    fn try_from(value: &dyn Enum<T>) -> Result<Self, Self::Error> {
        todo!()
    }
}

instead of having this method as part of the trait.

So I think the prototype fn to_detect_ctx(s: &str) -> Option<DetectUintData<T>> is right as it takes a string as input, and resorts to generic integer parsing if this is not an enum string

This is an enum specifically for use in detection with a direct mapping from a name to an integer value?

The enum is also used in logging (not only for alerts). Was it your question ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just seems out of place, with respect to naming. We have a rather generic trait named Enum, but then we have this method to_detect_ctx that doesn't even take &self.

Some more comments in next PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the trait can be better named indeed.

method to_detect_ctx that doesn't even take &self.

But I do not want to use the enumeration : I just want a unique association between integers and strings that goes both ways, and some helper functions around that.
Is derive+trait+enum the best way to do so ? Or do you see another way ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the gist I posted that has the from traits, maybe a generic helper function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thans Jason, tried in d5f1bf2

}
4 changes: 4 additions & 0 deletions rust/src/http2/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,8 @@ pub enum HTTP2SettingsId {
InitialWindowSize = 4,
MaxFrameSize = 5,
MaxHeaderListSize = 6,
EnableConnectProtocol = 8, // rfc8441
NoRfc7540Priorities = 9, // rfc9218
}

impl fmt::Display for HTTP2SettingsId {
Expand All @@ -716,6 +718,8 @@ impl std::str::FromStr for HTTP2SettingsId {
"SETTINGS_INITIAL_WINDOW_SIZE" => Ok(HTTP2SettingsId::InitialWindowSize),
"SETTINGS_MAX_FRAME_SIZE" => Ok(HTTP2SettingsId::MaxFrameSize),
"SETTINGS_MAX_HEADER_LIST_SIZE" => Ok(HTTP2SettingsId::MaxHeaderListSize),
"SETTINGS_ENABLE_CONNECT_PROTOCOL" => Ok(HTTP2SettingsId::EnableConnectProtocol),
"SETTINGS_NO_RFC7540_PRIORITIES" => Ok(HTTP2SettingsId::NoRfc7540Priorities),
_ => Err(format!("'{}' is not a valid value for HTTP2SettingsId", s)),
}
}
Expand Down
1 change: 1 addition & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub mod rfb;
pub mod mqtt;
pub mod pgsql;
pub mod telnet;
pub mod websocket;
pub mod applayertemplate;
pub mod rdp;
pub mod x509;
Expand Down
66 changes: 66 additions & 0 deletions rust/src/websocket/detect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* Copyright (C) 2023 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

use super::websocket::WebSocketTransaction;
use crate::detect::uint::DetectUintData;
use crate::websocket::parser::WebSocketOpcode;
use std::ffi::CStr;
use crate::detect::Enum;

#[no_mangle]
pub unsafe extern "C" fn SCWebSocketGetOpcode(tx: &mut WebSocketTransaction) -> u8 {
return tx.pdu.opcode;
}

#[no_mangle]
pub unsafe extern "C" fn SCWebSocketGetFin(tx: &mut WebSocketTransaction) -> bool {
return tx.pdu.fin;
}

#[no_mangle]
pub unsafe extern "C" fn SCWebSocketGetPayload(
tx: &WebSocketTransaction, buffer: *mut *const u8, buffer_len: *mut u32,
) -> bool {
*buffer = tx.pdu.payload.as_ptr();
*buffer_len = tx.pdu.payload.len() as u32;
return true;
}

#[no_mangle]
pub unsafe extern "C" fn SCWebSocketGetMask(
tx: &mut WebSocketTransaction, value: *mut u32,
) -> bool {
if let Some(xorkey) = tx.pdu.mask {
*value = xorkey;
return true;
}
return false;
}

#[no_mangle]
pub unsafe extern "C" fn SCWebSocketParseOpcode(
ustr: *const std::os::raw::c_char,
) -> *mut DetectUintData<u8> {
let ft_name: &CStr = CStr::from_ptr(ustr); //unsafe
if let Ok(s) = ft_name.to_str() {
if let Some(ctx) = WebSocketOpcode::to_detect_ctx(s) {
let boxed = Box::new(ctx);
return Box::into_raw(boxed) as *mut _;
}
}
return std::ptr::null_mut();
}
Loading