-
Notifications
You must be signed in to change notification settings - Fork 1.7k
app-layer: websockets protocol support #10075
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* 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}; | ||
|
|
||
| pub fn derive_enum_string_u8(input: TokenStream) -> TokenStream { | ||
| let input = parse_macro_input!(input as DeriveInput); | ||
| let name = transform_name(&input.ident.to_string()); | ||
| let mut values = Vec::new(); | ||
| let mut names = Vec::new(); | ||
|
|
||
| if let syn::Data::Enum(ref data) = input.data { | ||
| for (_, v) in (&data.variants).into_iter().enumerate() { | ||
| let fname = transform_name(&v.ident.to_string()); | ||
| names.push(fname); | ||
| if let Some((_, val)) = &v.discriminant { | ||
| if let syn::Expr::Lit(l) = val { | ||
| if let syn::Lit::Int(li) = &l.lit { | ||
| if let Ok(value) = li.base10_parse::<u8>() { | ||
| values.push(value); | ||
| } else { | ||
| panic!("EnumString requires explicit u8"); | ||
| } | ||
| } 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 stringer = syn::Ident::new(&(name.clone() + "_string"), proc_macro2::Span::call_site()); | ||
| let parser = syn::Ident::new(&(name + "_parse"), proc_macro2::Span::call_site()); | ||
|
|
||
| let expanded = quote! { | ||
| fn #stringer(v: u8) -> Option<&'static str> { | ||
| match v { | ||
| #( #values => Some(#names) ,)* | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn #parser(v: &str) -> Option<u8> { | ||
| match v { | ||
| #( #names => Some(#values) ,)* | ||
| _ => None, | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| proc_macro::TokenStream::from(expanded) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* 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::logger::web_socket_opcode_parse; | ||
| use super::websocket::WebSocketTransaction; | ||
| use crate::detect::uint::{detect_parse_uint, DetectUintData, DetectUintMode}; | ||
| use std::ffi::CStr; | ||
|
|
||
| #[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 Ok((_, ctx)) = detect_parse_uint::<u8>(s) { | ||
| let boxed = Box::new(ctx); | ||
| return Box::into_raw(boxed) as *mut _; | ||
| } | ||
| if let Some(arg1) = web_socket_opcode_parse(s) { | ||
| let ctx = DetectUintData::<u8> { | ||
| arg1, | ||
| arg2: 0, | ||
| mode: DetectUintMode::DetectUintModeEqual, | ||
| }; | ||
| let boxed = Box::new(ctx); | ||
| return Box::into_raw(boxed) as *mut _; | ||
| } | ||
| } | ||
| return std::ptr::null_mut(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* 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::jsonbuilder::{JsonBuilder, JsonError}; | ||
| use std; | ||
| use suricata_derive::EnumStringU8; | ||
|
|
||
| #[derive(EnumStringU8)] | ||
| pub enum WebSocketOpcode { | ||
| Continuation = 0, | ||
| Text = 1, | ||
| Binary = 2, | ||
| Ping = 8, | ||
| Pong = 9, | ||
| } | ||
|
|
||
| fn log_websocket(tx: &WebSocketTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> { | ||
| js.open_object("websocket")?; | ||
| js.set_bool("fin", tx.pdu.fin)?; | ||
| if let Some(xorkey) = tx.pdu.mask { | ||
| js.set_uint("mask", xorkey.into())?; | ||
| } | ||
| if let Some(val) = web_socket_opcode_string(tx.pdu.opcode) { | ||
| js.set_string("opcode", val)?; | ||
| } else { | ||
| js.set_string("opcode", &format!("unknown-{}", tx.pdu.opcode))?; | ||
| } | ||
| js.close()?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[no_mangle] | ||
| pub unsafe extern "C" fn rs_websocket_logger_log( | ||
| tx: *mut std::os::raw::c_void, js: &mut JsonBuilder, | ||
| ) -> bool { | ||
| let tx = cast_pointer!(tx, WebSocketTransaction); | ||
| log_websocket(tx, js).is_ok() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* 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. | ||
| */ | ||
|
|
||
| //! Application layer websocket parser and logger module. | ||
|
|
||
| pub mod detect; | ||
| pub mod logger; | ||
| mod parser; | ||
| pub mod websocket; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be more idiomatic if the derive was done as a
FromorFromStrimplementation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So rather
ToStringandFromStr?You are my reference for what is idiomatic in rust :-p
Is there a performance cost to do type conversion from integer to enum ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I misread this.. So it essentially goes from u8 -> WebSocketOpCode -> &str? My first feeling is the derive macro does seem like a little overkill since
WebSocketOpcodedoesn't seem to be used at all in the code, other than behind the derive macro? But that aside..I think the derived code should implement a trait, or an impl block rather than a make function. Normally you might add a
to_str()method...Of course this assumes that you have a constructed
WebSocketOpcodealready, which it probably makes sense thattx.pdu.opcodemight be an instance of, which could be done with an implementation ofI guess it feels odd that this enum exists only to generate some bare functions, but is never actually used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, not using
WebSocketOpCodeitselfI want to code only once the match between integer value and string...
How do I achieve that without overkill ?
And without risking a typo if I code 2 functions (one stringer and one from str)
I do not
It does not seem to fit for me as
WebSocketOpcodeenum has a limited number of values, and I still want opcode 3 that is unknown to be parsed...Can/should I improve my enum ? Like adding a case
Unknown(u8)?I agree.
Sum up :
Unknown(u8)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense.
Implement the methods on the enum, like
from_strandto_str?This looks interesting as well: https://github.com/Peternator7/strum
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no standard
to_strright ?Should I use https://doc.rust-lang.org/std/string/trait.ToString.html ? That is
to_string(&self) -> Stringso that allocates even if it is a static string... I think notThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No there isn't, but that doesn't mean you can implement it/derive it directly on the
WebSocketOpCode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't look at it that closely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, they do.
derive(AppLayerEvent)was added to implement theAppLayerEventtrait we use in some generic functions and has some additional Suri only methods likeget_event_info.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can check next version of the PR ;-)