Operators
Last updated
Last updated
Operators in DuckyScript instruct the payload to perform a given mathematical, relational or logical operation.
Math operators may be used to change the value of a variable.
Operator | Meaning |
---|---|
Consider how the variable $FOO
changes with each math operation.
Comparison operators (or relational operators) will compare two values to evaluate a single boolean value.
Consider how the different comparison operators evaluate down to a single boolean value for the following variables:
The comparison ( $FOO == $BAR )
evaluates to the boolean FALSE
.
The comparison ( $FOO != $BAR )
evaluates to the boolean TRUE
.
The comparison ( $FOO > $BAR )
evaluates to the boolean FALSE
.
The comparison ( $FOO < $BAR )
evaluates to the boolean TRUE
.
The comparison ( $FOO >= $BAR )
evaluates to the boolean FALSE
.
The comparison ( $FOO <= $BAR )
evaluates to the boolean TRUE
.
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 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.
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 is TRUE
.
( $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 is TRUE
.
When assigning a value to a variable, the variable itself may be referenced.
The variable $FOO
is initiated as 1337
.
$FOO
is incremented by 1
(itself plus 1).
$FOO
will then equal 1338
.
Bitwise operators are operators which operate on the uint16 values at the binary level.
The value of $_CURRENT_VID
is saved into the variable $FOO
as AC05
.
Using bitwise operators its endianness is swapped to 05AC
.
Operator | Meaning |
---|---|
Operator | Description |
---|---|
Operator | Description |
---|---|
=
Assignment
+
Add
-
Subtract
*
Multiply
/
Divide
%
Modulus
^
Exponent
==
Equal to
!=
Not equal to
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
&&
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
.
&
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.