Operators
Overview
Operators in DuckyScript instruct the payload to perform a given mathematical, relational or logical operation.
Math Operators
Math operators may be used to change the value of a variable.
=
Assignment
+
Add
-
Subtract
*
Multiply
/
Divide
%
Modulus
^
Exponent
Examples
Consider how the variable $FOO
changes with each math operation.
Comparison Operators
Comparison operators (or relational operators) will compare two values to evaluate a single boolean value.
==
Equal to
!=
Not equal to
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
Examples
Consider how the different comparison operators evaluate down to a single boolean value for the following variables:
The comparison
( $FOO == $BAR )
evaluates to the booleanFALSE
.The comparison
( $FOO != $BAR )
evaluates to the booleanTRUE
.The comparison
( $FOO > $BAR )
evaluates to the booleanFALSE
.The comparison
( $FOO < $BAR )
evaluates to the booleanTRUE
.The comparison
( $FOO >= $BAR )
evaluates to the booleanFALSE
.The comparison
( $FOO <= $BAR )
evaluates to the booleanTRUE
.
Order of Operations
The order of operations (order precedence) are a set of rules that define which procedures are performed first in order to evaluate an expression, similar to that of mathematics.
In DuckyScript, parentheses ( ) are required to define the precedence conventions.
If multiple pairs of parentheses are required, the parentheses can be nested.
Logical Operators
Logical operators are important as they allow us to make decisions based on certain conditions. For example, when combining the result of more than one condition, the logical AND or OR logical operators will make the final determination.
These logical operators may be used to connect two or more expressions.
&&
Logical AND. If both the operands are non-zero, the condition is TRUE
.
||
Logical OR. If any of the two operands is non-zero, the condition is TRUE
.
Examples
Considering the values of the two variables:
The expression ( $FOO < $BAR )
evaluates to TRUE
.
The expression ( $FOO > $BAR )
evaluates to FALSE
.
Combining these expressions, we can evaluate:
( $FOO < $BAR ) && ( $BAR > $FOO )
Evalues down to
TRUE && TRUE
Because 42 is less than 1337 is TRUE AND 1337 is greater than 42 is TRUE.
Both operands are non-zero (
TRUE
), therefore the final condition isTRUE
.
( $FOO < $BAR ) || ( $BAR == $FOO )
Evaluates as
TRUE || FALSE
Because 42 is less than 1337 is TRUE OR 1337 is equal to 42 is FALSE.
Any of the operands are non-zero (
TRUE
), therefore the final condition isTRUE
.
Augmented Assignment Operators
When assigning a value to a variable, the variable itself may be referenced.
Example
Result
The variable
$FOO
is initiated as1337
.$FOO
is incremented by1
(itself plus 1).$FOO
will then equal1338
.
Bitwise Operators
Bitwise operators are operators which operate on the uint16 values at the binary level.
&
Bitwise AND. If the corresponding bits of the two operands is 1, will result in 1. Otherwise if either bit of an operand is 0, the result of the corresponding bit is evaluated as 0.
|
Bitwise OR. If at least one corresponding bit of the two operands is 1, will result in 1.
>>
Right Shift. Accepts two numbers. Right shifts the bits of the first operand. The second operand determines the number of places to shift.
<<
Left Shift. Accepts two numbers. Left shifts the bits of the first operand. The second operand decides the number of places to shift.
Example
Result
The value of
$_CURRENT_VID
is saved into the variable$FOO
asAC05
.Using bitwise operators its endianness is swapped to
05AC
.
Last updated