The byte_math Keyword

The byte_math keyword performs a mathematical operation on an extracted value and a specified value or existing variable, and stores the outcome in a new resulting variable. You can then use the resulting variable as an argument in other keywords.

You can use multiple byte_math keywords in a rule to perform multiple byte_math operations.

The following table describes the arguments required by the byte_math keyword.

Required byte_math Arguments

Argument

Description

Bytes

The number of bytes to calculate from the packet.

If used without DCE/RPC, the allowed values are 1 to 10:

  • Bytes can be 1 to 10 when the operator is +, -. *, or /.

  • Bytes can be 1 to 4 when the operator is << or >>.

  • If you specify a number of bytes other than 1, 2, or 4, you must specify a Number Type (hexadecimal, octal, or decimal.)

If used with DCE/RPC, allowed values are 1, 2, and 4.

Offset

The number of bytes into the payload to start processing. The offset counter starts at byte 0, so calculate the offset value by subtracting 1 from the number of bytes you want to jump forward from the beginning of the packet payload or (if you specified Relative) from the last successful content match.

You can specify -65535 to 65535 bytes.

You can also specify the byte_extract variable here.

Operator

+, -, *, /, <<, or >>

RValue

The value following the operator. This can be an unsigned integer or a variable passed from byte_extract.

Result Variable

The name of the variable into which the result of the byte_math calculation will be stored. You can use this variable as an argument in other keywords.

This value is stored as an unsigned integer.

This variable name:

  • Must use alphanumeric characters

  • Must not begin with a number

  • May include special characters supported by the Microsoft filename/variable name convention

  • Cannot consist entirely of special characters

The following table describes options you can use to define how the system interprets the values you specified for the required arguments.

Additional Optional byte_math Arguments

Argument

Description

Relative

Makes the offset relative to the last pattern found in the last successful content match instead of the beginning of the payload.

Bitmask

Applies the specified hexadecimal bitmask using the AND operator to the bytes extracted from the Bytes argument.

A bitmask can be 1 to 4 bytes.

The result will be right-shifted by the number of bits equal to the number of trailing zeros in the mask.

You can specify only one of DCE/RPC, Endian, or Number Type.

If you want to define how the byte_math keyword calculates the bytes, you can choose from the arguments described in the following table. If you do not select a byte-ordering argument, the rules engine uses big endian byte order.

Byte-Ordering byte_math Arguments

Argument

Description

Big Endian

Processes data in big endian byte order, which is the default network byte order.

Little Endian

Processes data in little endian byte order.

DCE/RPC

Specifies a byte_math keyword for traffic processed by the DCE/RPC preprocessor.

The DCE/RPC preprocessor determines big endian or little endian byte order, and the Number Type and Endian arguments do not apply.

When you enable this argument, you can also use byte_math in conjunction with other specific DCE/RPC keywords.

Define how the system views string data in a packet by using one of the arguments in the following table.

Number Type Arguments

Argument

Description

Hexadecimal String

Represents string data in hexadecimal format.

Decimal String

Represents string data in decimal format.

Octal String

Represents string data in octal format.

For example, if the values you set for byte_math are as follows:

  • Bytes = 2

  • Offset = 0

  • Operator = *

  • RValue = height

  • Result Variable = area

the rules engine extracts the number described in the first two bytes in the packet and multiplies it by the RValue (which uses the existing variable, height) to create the new variable, area.

Arguments Accepting a byte_math Variable

Keyword

Argument

byte_jump

Offset

byte_test

Offset, Value

byte_extract

Offset

isdataat

Offset