Commands and Hooks
Command data
When a user enters a command in the agent console, it is passed to the Commander handler. This handler matches the entered command with the registered AxCommand object, and then parses and converts the commandline into a JSON object:
The argument name is the JSON key and the value is the JSON value.
The JSON value type will match the argument type. The exception is the FILE type, as the file will be read and encoded in base64.
Example:
Registered command:
let _cmd_start = ax.create_command("start", "Description");
_cmd_start.addArgBool("-b");
_cmd_start.addArgInt("arg_int", true);
_cmd_start.addArgString("arg_str", true);
_cmd_start.addArgFlagString("-flag", "arg_str_flag", true);
_cmd_start.addArgFile("content", true);
let _cmd_stop = ax.create_command("stop", "Description");
_socks_stop.addArgInt("i", true);
let cmd = ax.create_command("cmd", "Description");
cmd.addSubCommands([_cmd_start, _cmd_stop]);CommandLine:
JSON:
PreHook
You can set a PreHook for a command. PreHook is an AxScript function that will process the command data between the command entry and sending its data to the server.
handler(id, cmdline, parsed_json, ...parsed_lines) - is an AxScript function that takes 4 parameters:
id- identifier of the agent for which the command is specifiedcmdline- command sent from the agent consoleparsed_json- the JSON object that the command was converted to after processing.parsed_lines- is an array of strings that the command was converted to.
Example
This code creates the screenshot_bof command
If the command is sent from the agent console: screenshot_bof -n "Test bof" -p 608
cmdline:
parsed_lines:
parsed_json:
In this example, setPreHook installs a handler that packages arguments into BOF format and executes the execute bof command via the ax.execute_alias function.
PostHook
A PostHook is used to allow the user to access intermediate results and perform data transformations before displaying and saving. AxScript uses the concept of hooks due to its asynchronous behavior: sending a task to a beacon and receiving a response at a later time, depending on the current wait time.
After executing an asynchronous hook, you can perform the necessary operations to transform the result according to your use case (for each message in the task). Hooks can be used to process the task result, start an additional task, or format the result before outputting it to the agent console.
PostHook can be set via the ax.execute_alias_hook or ax.execute_command_hook functions.
hooktask handler(hooktask task) - is an AxScript function that takes hooktask parameter and return hooktask.
task- ahooktaskstructure that contains the result of the task execution.
Both the client and teamserver save requests that have associated callbacks in a queue. The request is deleted when the initiating client disconnects from TeamServer. This deletes the queue managed by the client, as it is tied to each TeamServer connection. The queue on the teamserver will see the originating client has disconnected and flag any requests for that client to be removed. This means the originating client needs to stay connected to the teamserver until the command with a PostHook has completed. Otherwise, any responses from Beacon after a disconnection from the originating client will be lost.
Example
Hashdump BOF output without posthook:

This posthook removes "BOF output" messages, parses names and hashes, and stores them in Credentials Manager.


Handler
A handler is used to allow the user to access and process the final result.
Handlers can be used to process a task's result, start an additional task, or transfer control within a task flow.
Handlers can be set via the ax.execute_alias_handler or ax.execute_command_handler functions.
void handler(handlertask task) - is an AxScript function that takes handlertask parameter.
task- ahandlertaskstructure that contains the result of the task execution.
Both the client and the command server store requests with their corresponding callbacks in a queue. The request will remain stored, but will not be processed by the handler, when the initiating client disconnects from the command server. This means that the initiating client must remain connected to the command server until the command from the handler is completed.
Example


AxScript Execute command
execute_command
The function passes the specified command to the Commander handler for the agent with the specified id. This will work as if the user entered the command in the agent console.
id- agent IDcommand- command linehook- PostHookhandler- Handler
Example:
execute_browser
The function passes the specified command to the Commander handler for the agent with the specified id, but marks the task as TYPE_BROWSER, so the command and its result are not saved and displayed in the agent console.
id- agent IDcommand- command line
Example:
execute_alias
The function passes the specified command to the Commander handler, but overrides the comandline and message for the agent console with the specified id. So if execute_command outputs the original comandline and message, execute_alias will replace them after processing and before output to the agent console.
id- agent IDcommand- command linecmdline- new command line for agent consolemessage- new message for agent consolehook- PostHookhandler- Handler
Example:
Last updated