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
57 changes: 57 additions & 0 deletions doc/userguide/rules/websocket-keywords.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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.flags
---------------

Matches on the websocket flags.
It uses a 8-bit unsigned integer as value.
Only the four upper bits are used.

The value can also be a list of strings (comma-separated),
where each string is the name of a specific bit like `fin` and `comp`,
and can be prefixed by `!` for negation.

Examples::

websocket.flags:128;
websocket.flags:&0x40=0x40;
websocket.flags:fin,!comp;

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 @@ -5501,6 +5510,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>>;
}
Loading