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
3 changes: 2 additions & 1 deletion doc/userguide/configuration/suricata-yaml.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1761,7 +1761,8 @@ incompatible with ``decode-mime``. If both are enabled,
Maximum transactions
~~~~~~~~~~~~~~~~~~~~

MQTT, FTP, PostgreSQL, SMB, DCERPC and NFS have each a `max-tx` parameter that can be customized.
MQTT, FTP, PostgreSQL, SMB, DCERPC, ENIP and NFS have each a `max-tx`
parameter that can be customized.
`max-tx` refers to the maximum number of live transactions for each flow.
An app-layer event `protocol.too_many_transactions` is triggered when this value is reached.
The point of this parameter is to find a balance between the completeness of analysis
Expand Down
137 changes: 120 additions & 17 deletions doc/userguide/rules/enip-keyword.rst
Original file line number Diff line number Diff line change
@@ -1,40 +1,143 @@
ENIP/CIP Keywords
=================

The enip_command and cip_service keywords can be used for matching on various properties of
ENIP requests.
enip_command
------------

There are three ways of using this keyword:
For the ENIP command, we are matching against the command field found in the ENIP encapsulation.

Examples::

* matching on ENIP command with the setting "enip_command";
* matching on CIP Service with the setting "cip_service".
* matching both the ENIP command and the CIP Service with "enip_command" and "cip_service" together
enip_command:99;
enip_command:ListIdentity;


For the ENIP command, we are matching against the command field found in the ENIP encapsulation.
cip_service
-----------

For the CIP Service, we use a maximum of 3 comma separated values representing the Service, Class and Attribute.
These values are described in the CIP specification. CIP Classes are associated with their Service, and CIP Attributes
are associated with their Service. If you only need to match up until the Service, then only provide the Service value.
If you want to match to the CIP Attribute, then you must provide all 3 values.


Syntax::

enip_command:<value>
cip_service:<value(s)>
enip_command:<value>, cip_service:<value(s)>


Examples::

enip_command:99
cip_service:75
cip_service:16,246,6
enip_command:111, cip_service:5


(cf. http://read.pudn.com/downloads166/ebook/763211/EIP-CIP-V1-1.0.pdf)

Information on the protocol can be found here:
`<http://literature.rockwellautomation.com/idc/groups/literature/documents/wp/enet-wp001_-en-p.pdf>`_

enip.status
-----------

For the ENIP status, we are matching against the status field found in the ENIP encapsulation.
It uses a 32-bit unsigned integer as value.

Examples::

enip.status:100;
enip.status:>106;

enip.protocol_version
---------------------

Match on the protocol version in identity message.
It uses a 16-bit unsigned integer as value.

Examples::

enip.protocol_version:1;
enip.protocol_version:>1;

enip.revision
---------------------

Match on the revision in identity message.
It uses a 16-bit unsigned integer as value.

Examples::

enip.revision:1;
enip.revision:>1;

enip.identity_status
--------------------

Match on the status in identity message (not in ENIP header).
It uses a 16-bit unsigned integer as value.

Examples::

enip.identity_status:1;
enip.identity_status:>1;

enip.state
----------

Match on the state in identity message.
It uses a 8-bit unsigned integer as value.

Examples::

enip.state:1;
enip.state:>1;

enip.serial
-----------

Match on the serial in identity message.
It uses a 32-bit unsigned integer as value.

Examples::

enip.serial:1;
enip.serial:>1;

enip.product_code
-----------------

Match on the product code in identity message.
It uses a 16-bit unsigned integer as value.

Examples::

enip.product_code:1;
enip.product_code:>1;

enip.device_type
----------------

Match on the device type in identity message.
It uses a 16-bit unsigned integer as value.

Examples::

enip.device_type:1;
enip.device_type:>1;

enip.vendor_id
--------------

Match on the vendor id in identity message.
It uses a 16-bit unsigned integer as value.

Examples::

enip.vendor_id:1;
enip.vendor_id:>1;

enip.product_name
-----------------

Match on the product name in identity message.

Examples::

enip.product_name; pcre:"/^123[0-9]*/";
enip.product_name; content:"swordfish";

``enip.product_name`` is a 'sticky buffer' and can be used as ``fast_pattern``.
66 changes: 66 additions & 0 deletions etc/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,72 @@
},
"additionalProperties": false
},
"enip": {
"type": "object",
"properties": {
"request": {
"type": "object",
"properties": {
"command": {
"type": "string"
},
"status": {
"type": "string"
},
"length": {
"type": "integer"
}
},
"additionalProperties": false
},
"response": {
"type": "object",
"properties": {
"command": {
"type": "string"
},
"status": {
"type": "string"
},
"identity": {
"type": "object",
"properties": {
"protocol_version": {
"type": "integer"
},
"revision": {
"type": "string"
},
"vendor_id": {
"type": "string"
},
"device_type": {
"type": "string"
},
"product_code": {
"type": "integer"
},
"status": {
"type": "integer"
},
"serial": {
"type": "integer"
},
"product_name": {
"type": "string"
},
"state": {
"type": "integer"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
"ether": {
"type": "object",
"properties": {
Expand Down
1 change: 1 addition & 0 deletions rules/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ decoder-events.rules \
dhcp-events.rules \
dnp3-events.rules \
dns-events.rules \
enip-events.rules \
files.rules \
ftp-events.rules \
http-events.rules \
Expand Down
8 changes: 8 additions & 0 deletions rules/enip-events.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ENIP app layer event rules
#
# SID's fall in the 2223000+ range. See https://redmine.openinfosecfoundation.org/projects/suricata/wiki/AppLayer
#
# These sigs fire at most once per connection.
#
alert enip any any -> any any (msg:"SURICATA ENIP too many transactions"; app-layer-event:enip.too_many_transactions; classtype:protocol-command-decode; sid:2234000; rev:1;)
alert enip any any -> any any (msg:"SURICATA ENIP invalid PDU"; app-layer-event:enip.invalid_pdu; classtype:protocol-command-decode; sid:2234001; rev:1;)
1 change: 1 addition & 0 deletions rust/src/applayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ extern {
pub fn AppLayerParserStateIssetFlag(state: *mut c_void, flag: u16) -> u16;
pub fn AppLayerParserSetStreamDepth(ipproto: u8, alproto: AppProto, stream_depth: u32);
pub fn AppLayerParserConfParserEnabled(ipproto: *const c_char, proto: *const c_char) -> c_int;
pub fn AppLayerParserRegisterParserAcceptableDataDirection(ipproto: u8, alproto: AppProto, dir: u8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it have its own commit despite being just one line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, why so ?

My logic is to put in the commit, the line using it, ie calling AppLayerParserRegisterParserAcceptableDataDirection from rust

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your logic is undoubtedly sound.
However, considering that you're introducing a new function for the app layer, which can be reused in other protocols, I thought that creating a specific commit would provide a clear history in the git log.
This way, it can be easily traced back to its introduction if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is not new.

It makes it merely accessible to rust app-layers...

}

#[repr(C)]
Expand Down
43 changes: 36 additions & 7 deletions rust/src/detect/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use nom7::branch::alt;
use nom7::bytes::complete::{is_a, tag, tag_no_case, take_while};
use nom7::character::complete::digit1;
use nom7::character::complete::{digit1, hex_digit1};
use nom7::combinator::{all_consuming, map_opt, opt, value, verify};
use nom7::error::{make_error, ErrorKind};
use nom7::Err;
Expand Down Expand Up @@ -73,8 +73,25 @@ pub fn detect_parse_uint_unit(i: &str) -> IResult<&str, u64> {
return Ok((i, unit));
}

pub fn detect_parse_uint_value_hex<T: DetectIntType>(i: &str) -> IResult<&str, T> {
let (i, _) = tag("0x")(i)?;
let (i, arg1s) = hex_digit1(i)?;
match T::from_str_radix(arg1s, 16) {
Ok(arg1) => Ok((i, arg1)),
_ => Err(Err::Error(make_error(i, ErrorKind::Verify))),
}
}

pub fn detect_parse_uint_value<T: DetectIntType>(i: &str) -> IResult<&str, T> {
let (i, arg1) = alt((
detect_parse_uint_value_hex,
map_opt(digit1, |s: &str| s.parse::<T>().ok()),
))(i)?;
Ok((i, arg1))
}

pub fn detect_parse_uint_with_unit<T: DetectIntType>(i: &str) -> IResult<&str, T> {
let (i, arg1) = map_opt(digit1, |s: &str| s.parse::<T>().ok())(i)?;
let (i, arg1) = detect_parse_uint_value::<T>(i)?;
let (i, unit) = opt(detect_parse_uint_unit)(i)?;
if arg1 >= T::one() {
if let Some(u) = unit {
Expand Down Expand Up @@ -107,11 +124,11 @@ pub fn detect_parse_uint_start_equal<T: DetectIntType>(
pub fn detect_parse_uint_start_interval<T: DetectIntType>(
i: &str,
) -> IResult<&str, DetectUintData<T>> {
let (i, arg1) = map_opt(digit1, |s: &str| s.parse::<T>().ok())(i)?;
let (i, arg1) = detect_parse_uint_value(i)?;
let (i, _) = opt(is_a(" "))(i)?;
let (i, _) = alt((tag("-"), tag("<>")))(i)?;
let (i, _) = opt(is_a(" "))(i)?;
let (i, arg2) = verify(map_opt(digit1, |s: &str| s.parse::<T>().ok()), |x| {
let (i, arg2) = verify(detect_parse_uint_value, |x| {
x > &arg1 && *x - arg1 > T::one()
})(i)?;
Ok((
Expand All @@ -127,13 +144,13 @@ pub fn detect_parse_uint_start_interval<T: DetectIntType>(
fn detect_parse_uint_start_interval_inclusive<T: DetectIntType>(
i: &str,
) -> IResult<&str, DetectUintData<T>> {
let (i, arg1) = verify(map_opt(digit1, |s: &str| s.parse::<T>().ok()), |x| {
let (i, arg1) = verify(detect_parse_uint_value::<T>, |x| {
*x > T::min_value()
})(i)?;
let (i, _) = opt(is_a(" "))(i)?;
let (i, _) = alt((tag("-"), tag("<>")))(i)?;
let (i, _) = opt(is_a(" "))(i)?;
let (i, arg2) = verify(map_opt(digit1, |s: &str| s.parse::<T>().ok()), |x| {
let (i, arg2) = verify(detect_parse_uint_value::<T>, |x| {
*x > arg1 && *x < T::max_value()
})(i)?;
Ok((
Expand Down Expand Up @@ -162,7 +179,7 @@ pub fn detect_parse_uint_mode(i: &str) -> IResult<&str, DetectUintMode> {
fn detect_parse_uint_start_symbol<T: DetectIntType>(i: &str) -> IResult<&str, DetectUintData<T>> {
let (i, mode) = detect_parse_uint_mode(i)?;
let (i, _) = opt(is_a(" "))(i)?;
let (i, arg1) = map_opt(digit1, |s: &str| s.parse::<T>().ok())(i)?;
let (i, arg1) = detect_parse_uint_value(i)?;

match mode {
DetectUintMode::DetectUintModeNe => {}
Expand Down Expand Up @@ -407,6 +424,18 @@ pub unsafe extern "C" fn rs_detect_u16_free(ctx: &mut DetectUintData<u16>) {
mod tests {
use super::*;

#[test]
fn test_parse_uint_hex() {
match detect_parse_uint::<u64>("0x100") {
Ok((_, val)) => {
assert_eq!(val.arg1, 0x100);
}
Err(_) => {
assert!(false);
}
}
}

#[test]
fn test_parse_uint_unit() {
match detect_parse_uint::<u64>(" 2kb") {
Expand Down
Loading