# Stage Class

### Stage Class

The `Stage` class is responsible for managing the agent's initial communication with the command and control server, including the setup of encryption keys and system information exchange. It acts as the bootstrap mechanism for the agent, setting up all necessary configurations for secure and covert operations.

#### Attributes

* **staging\_key**: A pre-shared key used for initial secure communications during the staging process.
* **profile**: The communication profile string that defines how the agent should communicate (headers, user-agents, etc.).
* **server**: The base URL of the command and control server.
* **kill\_date**: The date on which the agent will automatically cease operations.
* **working\_hours**: A time window during which the agent is allowed to operate.
* **session\_id**: A randomly generated session identifier for the agent.
* **key**: The encryption key that will be derived from the Diffie-Hellman key exchange.
* **headers**: The HTTP headers that the agent will use, derived from the communication profile.
* **packet\_handler**: An instance of the packet handler (likely `ExtendedPacketHandler`) which will handle the packet-level operations like encryption, routing, etc.
* **taskURIs**: A list of potential URIs the agent can use to fetch tasking or communicate results.

### Dependencies

The agent incorporates multiple external Python functionalities, sourced via Jinja2 templates:

```python
{% include 'common/aes.py' %}
{% include 'common/chacha.py' %}
{% include 'common/diffiehellman.py' %}
{% include 'common/get_sysinfo.py' %}
{% include 'http/comms.py' %}
```

These functionalities provide:

* AES & ChaCha20poly1305 Encryption: For encrypted communications.
* Diffie-Hellman Key Exchange: Secure establishment of a shared secret key.
* System Information: Gather details about the host system.
* HTTP Communication Methods: Communication methods tailored for HTTP. (Can be customized with other listener options)

### Staging Process

Staging is the agent's initial phase, where it communicates with the server and prepares for secure interactions. During the staging process initial staging information is provided and used to create a secure communication channel. This information is provided through a jinja profile such as:

```python
self.staging_key = b'{{ staging_key }}'
self.profile = '{{ profile }}'
self.server = '{{ host }}'
self.kill_date = '{{ kill_date }}'
self.working_hours = '{{ working_hours }}'
```

#### Methods

**`generate_session_id()`**

Generates a random session identifier for the agent. This ensures each agent instance has a unique identifier during its operation.

**`initialize_headers(profile)`**

Parses the communication profile string to extract and set up the HTTP headers for the agent.

**`execute()`**

The main method responsible for:

1. Initiating the Diffie-Hellman key exchange with the server.
2. Sending system information to the server.
3. Decrypting the agent code received from the server.
4. Executing the agent code and initializing the main agent operations.

#### Usage Example

To use the `Stage` class, instantiate it and then call the `execute` method. This will initiate the staging process:

```python
stager = Stage()
stager.execute()
```


---

# Agent Instructions: 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://bc-security.gitbook.io/empire-wiki/agents/python/stageclass.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.
