Loops
Overview
Loops are flow control statements that can be used to repeat instructions until a specific condition is reached.
WHILE
A block of code can be executed repeatedly a specified number of times (called iterations) using a WHILE statement. The code within the WHILE statement will continue to execute for as long as the condition of the WHILE statement is TRUE.
A WHILE statement is similar to an IF statement, however it behaves differently when at the statements end. Whereas at the end of an IF statement the payload will continue, when the end of a WHILE statement is reached the payload execution will jump back to the beginning of the WHILE statement and reevaluate the condition. One way to interpret a WHILE statement is to read it as “IF this condition is true, THEN do that until it isn’t true anymore” — hence it being called a while loop.
Syntax
The WHILE statement consists of four parts
- The
WHILEkeyword. - The condition, or expression that evaluates to
TRUEorFALSE. - One or more newlines containing the block of code to execute.
- The
END_WHILEkeyword.
Example
REM Example while loop - blink LED 42 times
VAR $FOO = 42
WHILE ( $FOO > 0 )
LED_G
DELAY 500
LED_OFF
DELAY 500
$FOO = ( $FOO - 1 )
END_WHILE
LED_R
Result
- The variable
$FOOis set to 42. - The
WHILEloop begins, evaluating the condition “is $FOO greater than 0”. - Every time the condition is
TRUE, the block of code betweenWHILEandEND_WHILEwill run.- The LED will blink green: half a second on, half a second off.
- The variable
$FOOwill decrement by one.
- Once
$FOOreaches zero, theWHILEcondition will no longer evaluate toTRUE. The payload will continue execution after theEND_WHILEstatement, where the LED will light red. - If the button is pressed at any time during the payload execution, the
WHILEloop will end and the USB Rubber Ducky will enterATTACKMODE STORAGEsince that is the default behavior when noBUTTON_DEFhas been initiated.
Example
REM Example while loop - press the button 5 times
VAR $FOO = 5
WHILE ( $FOO > 0 )
STRINGLN Press the button...
WAIT_FOR_BUTTON_PRESS
$FOO = ( $FOO - 1 )
END_WHILE
STRINGLN You pressed the button 5 times!
Result
- The variable
$FOOis set to 5. - The code block within the
WHILEloop will be repeated until the expression evaluates toFALSE. - For each run of the code block, the message “
Press the button...” is typed. The payload then waits until it detects the button is pressed, at which point the variable$FOOis decremented.
Infinite Loop
The syntax of WHILE states that in nearly all cases the expression should be surrounded by parenthesis ( ). The exception is when initiating an infinite loop. The condition of the expression TRUE will always evaluate to TRUE. While it is not necessary to omit the parenthesis, it is technically more efficient. This is because it directly references TRUE, reducing the number of instructions and removing the step of first reducing the order of precedence.
This is loop that will execute endlessly, until intervention occurs. This may either come in the form of a button press, or simply by unplugging the USB Rubber Ducky.
Example
REM Example Infinite Loop
BUTTON_DEF
WHILE TRUE
LED_R
DELAY 500
LED_OFF
DELAY 500
END_WHILE
END_BUTTON
WHILE TRUE
LED_G
DELAY 500
LED_OFF
DELAY 500
END_WHILE
Result
- Because a button definition has been initiated with
BUTTON_DEF, the default behavior will no longer apply when the button is pressed. - The LED will blink green: half a second on, half a second off.
- Pressing the button will stop the currently infinite loop of blinking the LED green and execute the button definition, thus blinking the LED red.