-
Notifications
You must be signed in to change notification settings - Fork 1.7k
app-layer: websockets protocol support #10014
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,26 @@ | ||
| WebSocket Keywords | ||
| ================== | ||
|
|
||
| websocket.mask | ||
| -------------- | ||
|
|
||
| A boolean to tell if the payload is masked. | ||
|
|
||
| Examples:: | ||
|
|
||
| websocket.mask:true; | ||
| websocket.mask:false; | ||
|
|
||
| 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
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,54 @@ | ||
| /* 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 super::logger::web_socket_opcode_parse; | ||
| 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 SCWebSocketGetMask(tx: &mut WebSocketTransaction) -> bool { | ||
| return tx.pdu.mask; | ||
| } | ||
|
|
||
| #[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,50 @@ | ||
| /* 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("mask", tx.pdu.mask)?; | ||
| 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; |
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,63 @@ | ||
| /* 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 nom7::bytes::streaming::take; | ||
| use nom7::combinator::cond; | ||
| use nom7::number::streaming::{be_u16, be_u64, be_u8}; | ||
| use nom7::IResult; | ||
|
|
||
| #[derive(Clone, Debug, Default)] | ||
| pub struct WebSocketPdu { | ||
| pub fin: bool, | ||
| pub opcode: u8, | ||
| pub mask: bool, | ||
| pub payload: Vec<u8>, | ||
| } | ||
|
|
||
| // cf rfc6455#section-5.2 | ||
| pub fn parse_message(i: &[u8]) -> IResult<&[u8], WebSocketPdu> { | ||
| let (i, fin_op) = be_u8(i)?; | ||
| let fin = (fin_op & 0x80) != 0; | ||
| let opcode = fin_op & 0xF; | ||
| let (i, mask_plen) = be_u8(i)?; | ||
| let mask = (mask_plen & 0x80) != 0; | ||
| let (i, payload_len) = match mask_plen & 0x7F { | ||
| 126 => { | ||
| let (i, val) = be_u16(i)?; | ||
| Ok((i, val.into())) | ||
| } | ||
| 127 => be_u64(i), | ||
| _ => Ok((i, (mask_plen & 0x7F).into())), | ||
| }?; | ||
| let (i, xormask) = cond(mask, take(4usize))(i)?; | ||
| let (i, payload_raw) = take(payload_len)(i)?; | ||
| let mut payload = payload_raw.to_vec(); | ||
| if let Some(xorkey) = xormask { | ||
| for i in 0..payload.len() { | ||
| payload[i] ^= xorkey[i % 4]; | ||
| } | ||
| } | ||
| Ok(( | ||
| i, | ||
| WebSocketPdu { | ||
| fin, | ||
| opcode, | ||
| mask, | ||
| payload, | ||
| }, | ||
| )) | ||
| } |
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.
Ah I see this is a bool. But why not the mask 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.
This does not seem meaningful to me.
What seems meaningful is if the payload is masked or not.
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 general, keys are often used to identify malware / malware families, so I think we should make it available.
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.
ok