DuckyScript™ Quick Reference
DuckyScript™ is the programming language of the USB Rubber Ducky™, Hak5® hotplug attack gear and officially licensed devices (Trademark Hak5 LLC. Copyright © 2010 Hak5 LLC. All rights reserved.)
DuckyScript™ officially licensed and supported devices are backwards compatible with previous versions, excluding any device specific functionality.
E.g. DuckyScript™ 1.0 payloads written for the USB Rubber Ducky are valid DuckyScript™ 3.0 and will function on the New USB Rubber Ducky without modification
DuckyScript™ includes commands/syntax which only work on some devices. For example, MATCH
and SAVEKEYS
are DuckyScript™ commands which are only for Key Croc payloads and will not work on other devices.
Hak5 does NOT guarantee payload functionality for unlicensed device's, or payloads NOT compiled using Hak5 PayloadStudio
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.
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.
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.
The STRINGLN
command, like STRING
, will inject a series of keystrokes then terminate with a carriage return (ENTER
).
STRING
blocks can be used effectively to convert multiple lines into one without needing to prepend each line with STRING
STRING
blocks strip leading white space and ignore new lines!
is the equivalent of
Or in this case: STRING abc
STRINGLN
blocks can be used like here-doc; allowing you to inject multiple lines as they are written in the payload.
STRINGLN
blocks strip the first tab but will preserve all other formatting
is the equivalent of
Result
Deploying this payload will produce the following keystroke injection on the target machine:
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 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
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.
SHIFT
ALT
CONTROL
orCTRL
COMMAND
WINDOWS
orGUI
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
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.
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
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.
The minimum delay value is 20.
The DELAY
command may also accept an integer variable.
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.
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".
Halts payload execution until a button press is detected. When this command is reached in the payload, no further execution will occur.
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.
The DISABLE_BUTTON
command prevents the button from calling the BUTTON_DEF
.
The ENABLE_BUTTON
command allows pressing the button to call the BUTTON_DEF
.
The USB Rubber Ducky includes an LED which may be helpful when deploying certain payloads where feedback is important.
The LED_OFF
command will disable all LED modes.
The LED_R
command will enable the red LED.
The LED_G
command will enable the green LED.
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
Optional Parameters
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.
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.
The RESTORE_ATTACKMODE
command will restore a previously saved ATTACKMODE
state.
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.
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.
Operators instruct the payload to perform a given mathematical, relational or logical operation.
Will compare two values to evaluate a single boolean value.
Parentheses ( )
are required to define the precedence conventions.
Logical operators may be used to connect two or more expressions.
When assigning a value to a variable, the variable itself may be referenced.
Operate on the uint16 values at the binary level.
Conditional statements, loops and functions allow for dynamic execution.
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".
The ELSE
statement is an optional component of the IF
statement which will only execute when the IF
statement condition is FALSE
.
Loops are flow control statements that can be used to repeat instructions until a specific condition is reached.
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
.
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.
A function may return a integer or boolean value which may also be evaluated.
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.
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
.
The ATTACKMODE
command may accept random values for the optional parameters.
Use caution when using random VID
and PID
values as unexpected results are likely.
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 may be held.
These simple commands exist to control the execution of a payload.
The RESTART_PAYLOAD
command ceases execution, restarting the payload from the beginning.
The STOP_PAYLOAD
command ceases and further execution.
The RESET
command clears the keystroke buffer, useful for debugging complex hold key states.
Jitter randomly varies the delay between individual key presses based on the seed.bin
value.
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
.
Hides the inject.bin and seed.bin files from the MicroSD card.
Restores the inject.bin and seed.bin files to the MicroSD card.
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.
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.
Exfiltration is the unauthorized transfer of information from a system. Typically performed over a physical medium (copying to a USB flash disk such as the USB Rubber Ducky while using ATTACKMODE STORAGE
) or a network medium such as email, ftp, smb, http, etc.
Physical Exfiltration Example
Network Exfiltration Example
By taking advantage of the HID OUT endpoint as described in the lock keys 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
on the root of the MicroSD card. For a detailed example, see the section on Keystroke Reflection.
Similarly, arbitrary variable data may be saved to the loot.bin
file using the EXFIL
command.
Internal Variables
Last updated