Skip to content
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

detect/bytemath: Support out of order options #7509

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1,007 changes: 1,007 additions & 0 deletions rust/src/detect_parser/byte_math.rs

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions rust/src/detect_parser/error.rs
@@ -0,0 +1,37 @@
/* Copyright (C) 2022 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::error::{ErrorKind, ParseError};

/// Custom rule parse errors.
///
/// Implemented based on the Nom example for implementing custom errors.
#[derive(Debug, PartialEq)]
pub enum RuleParseError<I> {
InvalidByteMath(String),

Nom(I, ErrorKind),
}
impl<I> ParseError<I> for RuleParseError<I> {
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
RuleParseError::Nom(input, kind)
}

fn append(_: I, _: ErrorKind, other: Self) -> Self {
other
}
}
20 changes: 20 additions & 0 deletions rust/src/detect_parser/mod.rs
@@ -0,0 +1,20 @@
/* Copyright (C) 2022 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.
*/

pub mod byte_math;
pub mod error;
pub mod parser;
38 changes: 38 additions & 0 deletions rust/src/detect_parser/parser.rs
@@ -0,0 +1,38 @@
/* Copyright (C) 2022 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 crate::detect_parser::error::RuleParseError;

use nom7::bytes::complete::is_not;
use nom7::character::complete::multispace0;
use nom7::sequence::preceded;
use nom7::IResult;

static WHITESPACE: &str = " \t\r\n";
/// Parse all characters up until the next whitespace character.
pub fn take_until_whitespace(input: &str) -> IResult<&str, &str, RuleParseError<&str>> {
nom7::bytes::complete::is_not(WHITESPACE)(input)
}

/// Parse the next token ignoring leading whitespace.
///
/// A token is the next sequence of chars until a terminating character. Leading whitespace
/// is ignore.
pub fn parse_token(input: &str) -> IResult<&str, &str, RuleParseError<&str>> {
let terminators = "\n\r\t,;: ";
preceded(multispace0, is_not(terminators))(input)
}
1 change: 1 addition & 0 deletions rust/src/lib.rs
Expand Up @@ -113,6 +113,7 @@ pub mod smb;
pub mod krb;
pub mod dcerpc;
pub mod modbus;
pub mod detect_parser;

pub mod ike;
pub mod snmp;
Expand Down
2 changes: 2 additions & 0 deletions src/detect-byte.c
Expand Up @@ -25,6 +25,8 @@
#include "detect-byte.h"
#include "detect-byte-extract.h"
#include "detect-bytemath.h"
#include "detect-engine.h"
#include "rust-bindings.h"

/**
* \brief Used to retrieve args from BM.
Expand Down