3: Writing the agent handler

Now that we have a basic agent, we need to create a python script to handle our agent callbacks. The first step to creating a handler for our custom agent is downloading the official Havoc-py python library from the github repo. This provides a way for us to control how the server handles callbacks coming from our third party agent.

As of now, it we are still going to be using built-in C2 channels such as HTTP/s to communicate with the server, but our callback requests may not necessarily be in the same structure as that of the default Demon agent that comes with Havoc. Therefore, we will be using the official Havoc-py library to write a python script to parse and process the requests and responses for the teamserver in a way that suits our agent.

from havoc.service import HavocService
from havoc.agent import *

Defining the new agent

First, we have to define the new agent. This is done by defining it as a python class. Here we can define the basic information about our agent such as its name, author, version and description.

# =======================
# ===== Agent Class =====
# =======================
class python(AgentType):
    Name = "Python"
    Author = "@codex_tf2"
    Version = "0.1"
    Description = f"""python 3rd party agent for Havoc"""

Next, we have to define a magic value for our agent. This is a 4 byte value that tells the teamserver what agent type the request is coming from, so that it can be processed accordingly. In this case, we use 0x41414141

# =======================
# ===== Agent Class =====
# =======================
class python(AgentType):
    Name = "My first agent"
    Author = "@codex_tf2"
    Version = "0.1"
    Description = f"""python 3rd party agent for Havoc"""
    MagicValue = 0x41414141

Now we need to specify the rest of the basic information about the agent such as its supported archs and file formats. These fields should be self explanatory.

We also have to add the BuildingConfig value to define the values the user can set in the agent building GUI, along with its default values. We won't be doing the automatic generation just yet, but here's an example of what defining it would look like.

Commands

The most important part of our agent is the commands it can run.

Each command must be defined as a class in the following structure:

Following this structure, we can add our exit command as such:

Now, we just need to register these commands into our agent. Let's go back to the agent class and register them as such:

Our code now should look like this:

Now we need to tell Havoc how to handle our agent callbacks. We do this by defining a response() method in the agent class. This function takes in the callback and performs actions accordingly.

A few functions to take note of are:

Now we just have to register the new agent type in Havoc. This can be done by connecting to the Havoc service using HavocService() and calling the register_agent() method.

We can now test our custom agent!

Last updated