-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Websockets 2695 v8 #10093
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
Websockets 2695 v8 #10093
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,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;) |
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,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) | ||
| } |
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
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,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(); | ||
| } |
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.
So you should now be able to do something like:
instead of having this method as part of the trait.
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.
Alternatively naming.. This is an enum specifically for use in detection with a direct mapping from a name to an integer value?
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 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 stringThe enum is also used in logging (not only for alerts). Was it your question ?
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.
It just seems out of place, with respect to naming. We have a rather generic trait named
Enum, but then we have this methodto_detect_ctxthat doesn't even take&self.Some more comments in next PR.
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.
Maybe the trait can be better named indeed.
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 ?
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.
In the gist I posted that has the from traits, maybe a generic helper function?
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.
Thans Jason, tried in d5f1bf2