Constants

Overview

A constant is like a variable, except that its value cannot change throughout the program.

DEFINE

In DuckyScript, a constant is initiated using the DEFINE command. One may consider the use of a DEFINE within a payload like a find-and-replace at time of compile.
A constant may be an integer, boolean or string.

Example Boolean

REM Example Boolean (TRUE/FALSE or 1/0). May be expressed as either of:
DEFINE BLINK_ON_FINISH TRUE
DEFINE BLINK_ON_FINISH 1
DuckyScript developers may find it useful to include a boolean define at the top of their payload which determines whether or not a function will run. This makes it easier for the end-user to customize a shared payload. In this example, a conditional statement may determine whether or not to execute a function which will blink the LED upon completion of a task based on the value.
The boolean FALSE and 0 (zero) may be used interchangeably.
The boolean TRUE equates to any non-zero number (best practice is to use "1")

Example Integer

REM Integer
DEFINE DELAY_SPEED 2000
In this example, one may imagine the DELAY_SPEED constant will be used in conjunction with one or more DELAY commands.

Example String

When using a constant with the STRING command, the defined keyword must be on a line of its own and cannot be combined with other characters.

Valid Usage

DEFINE MESSAGE example.com
STRING https://
STRING MESSAGE
This will result in "https://example.com" being typed.

Invalid Usage

DEFINE MESSAGE example.com
STRING https://MESSAGE
This will result in "https://MESSAGE" being typed because the constant was combined with additional characters.
DEFINE MY_MESSAGE example.com
STRING https://
STRING MESSAGE
This will result in "https://MESSAGE" being typed because the constant "MESSAGE" is not defined. Consistency is key when naming constants and variables.

Example

REM Example constants using DEFINE
ATTACKMODE HID STORAGE
DEFINE SPEED 2000
DEFINE MESSAGE1 Hello,
DEFINE MESSAGE2 World!
DELAY SPEED
STRING MESSAGE1
DELAY SPEED
SPACE
STRING MESSAGE2

Result

  • The payload will begin with a 2 second delay, then type "Hello, World!" with a 2 second delay in between MESSAGE1 and MESSAGE2.
  • Changing the string values of MESSAGE1 and MESSAGE2 will change the outcome of the payload.
  • Changing the integer value of SPEED will change the delay between the first and second message.

Avoiding Errors

  • Constant names can only contain letters, numbers and underscore ("_").
  • They may begin with a letter or an underscore, but not a number.
  • Internal variables begin with an underscore, so it is best practice to avoid this style.
  • Spaces cannot be used in naming a constant — however underscore makes for a suitable replacement. For example: DEFINE REMOTE_HOST 192.168.1.100.
  • Constants should be short and descriptive. For example, RHOST is better than R, and REMOTE_HOST is better than RHOST.
  • Be careful when using the uppercase letter O or lowercase letter l as they may be confused with the numbers 0 and 1.
  • Avoid using the names of commands or internal variables (e.g. ATTACKMODE, STRING, WINDOWS, MAC, $_BUTTON_ENABLED). See the full command and variable reference.

Best Practices

Configurable payload options should be specified in variables or defines at the top of the payload.
When writing a payload that calls external resources which may vary depending on the operator, such as a website to open or address to establish a reverse shell with, it is best to use DEFINE.
In addition to comment blocks (like the REM title/author/description lines in the above example), putting your DEFINE commands at the top of your payload makes it easier for someone else to use your payload effectively. Even more so if the constants are commented!

Example

REM This payload targets Windows systems and will open the defined website.
DEFINE WEBSITE http://example.com
DELAY 2000
GUI r
DELAY 500
STRING WEBSITE
ENTER
Copy link
On this page
Overview
DEFINE
Example Boolean
Example Integer
Example String
Example
Avoiding Errors
Best Practices
Example