Skip to content

Commit

Permalink
Reduce code duplication and remove some unused files/functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrnrd committed Nov 25, 2023
1 parent b0e71a1 commit 22ad839
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 273 deletions.
1 change: 1 addition & 0 deletions client/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def handle_builder(args: List[str], config: OperatorConfig) -> None:
"""
if len(args) < 1:
logger.error("Please provide an action and a field")
return

if args[0] == "set":
if len(args) < 2:
Expand Down
75 changes: 25 additions & 50 deletions client/comms.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ def check_auth_token(config: OperatorConfig) -> bool:
"""
Check if the auth token is valid
"""
url = f"http://{config.c2}:{config.c2_port}/op/auth/token/status"
headers = {
"Authentication": f"Bearer {config.auth_token}",
}

response = requests.get(url, headers=headers)
return response.json()["status"]
return send_authenticated_request("GET", "/op/auth/token/status", config).json().get("status")


def server_auth(ip: str, port: int, name: str, login_secret: str) -> ServerAuthResponse:
Expand Down Expand Up @@ -102,19 +96,29 @@ def ensure_token(config: OperatorConfig) -> None:
config.auth_token = handle_server_auth(config)


def send_authenticated_request(method: str, endpoint: str, config: OperatorConfig, **request_kwargs) -> requests.Response:
"""
Build and send an authenticated response to the given endpoint. The C2 and authentication information
will be extracted from `config`.
"""
url = f"http://{config.c2}:{config.c2_port}{endpoint}"
headers = {
"Authorization": f"Bearer {config.auth_token}",
}

return requests.request(
method=method,
url=url,
headers=headers,
**request_kwargs)

def list_implants(config: OperatorConfig) -> list:
"""
List all the implants
"""
try:
ensure_token(config)

url = f"http://{config.c2}:{config.c2_port}/op/implant/list"
headers = {
"Authorization": f"Bearer {config.auth_token}",
}

response = requests.get(url, headers=headers)
response = send_authenticated_request("GET", "/op/implant/list", config)
return response.json()["implants"]
except Exception as e:
logger.error("Failed to list implants")
Expand Down Expand Up @@ -146,12 +150,7 @@ def get_tasks(config: OperatorConfig) -> List[Dict[Any, Any]]:
try:
ensure_token(config)

url = f"http://{config.c2}:{config.c2_port}/op/tasks/list"
headers = {
"Authorization": f"Bearer {config.auth_token}",
}

response = requests.get(url, headers=headers, timeout=120)
response = send_authenticated_request("GET", "/op/tasks/list", config, timeout=120)
if response.json()["status"] != True:
logger.error("Failed to get tasks")
return []
Expand All @@ -168,18 +167,13 @@ def add_task(
try:
ensure_token(config)

url = f"http://{config.c2}:{config.c2_port}/op/tasks/add"
headers = {
"Authorization": f"Bearer {config.auth_token}",
}

data = {
"opcode": opcode,
"implant_id": implant_id,
"args": args,
}

response = requests.post(url, headers=headers, json=data)
response = send_authenticated_request("POST", "/op/tasks/add", config, json=data)
if response.json()["status"] != True:
logger.error("Failed to add task")
return {}
Expand All @@ -194,12 +188,7 @@ def get_task_result(config: OperatorConfig, task_id: str) -> Optional[str]:
try:
ensure_token(config)

url = f"http://{config.c2}:{config.c2_port}/op/tasks/results/{task_id}"
headers = {
"Authorization": f"Bearer {config.auth_token}",
}

response = requests.get(url, headers=headers)
response = send_authenticated_request("GET", f"/op/tasks/results/{task_id}", config)
if response.json()["status"] != True:
logger.error("Failed to get task result")
return None
Expand All @@ -220,12 +209,7 @@ def get_implant_profile(config: OperatorConfig, implant_id: str) -> Dict[str, An

ensure_token(config)

url = f"http://{config.c2}:{config.c2_port}/op/implant/config/{implant_id}"
headers = {
"Authorization": f"Bearer {config.auth_token}",
}

response = requests.get(url, headers=headers)
response = send_authenticated_request("GET", f"/op/implant/config/{implant_id}", config)
if response.json()["status"] != True:
logger.error("Failed to get implant config")
return {}
Expand All @@ -242,6 +226,7 @@ def update_implant_profile(config: OperatorConfig, implant_id: str, changes: Dic
}

response = requests.post(url, headers=headers, json=changes)
response = send_authenticated_request("POST", f"/op/implant/config/{implant_id}", config, json=changes)
if response.json()["status"] != True:
logger.error("Failed to update implant config")
return
Expand All @@ -251,12 +236,7 @@ def kill_implant(config: OperatorConfig, implant_id: str) -> None:

ensure_token(config)

url = f"http://{config.c2}:{config.c2_port}/op/implant/kill/{implant_id}"
headers = {
"Authorization": f"Bearer {config.auth_token}",
}

response = requests.delete(url, headers=headers)
response = send_authenticated_request("DELETE", f"/op/implant/kill/{implant_id}", config)
if response.json()["status"] != True:
logger.error("Failed to kill implant")
return
Expand All @@ -270,12 +250,7 @@ def build_implant(config: OperatorConfig, build_options: dict) -> str:

ensure_token(config)

url = f"http://{config.c2}:{config.c2_port}/op/implant/build"
headers = {
"Authorization": f"Bearer {config.auth_token}",
}

response = requests.post(url, headers=headers, json=build_options)
response = send_authenticated_request("POST", "/op/implant/build", config, json=build_options)
if response.status_code != 200:
logger.error("Failed to build implant")
logger.error(f"Server response: {response.text}")
Expand Down
5 changes: 5 additions & 0 deletions server/config/admin/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ auth_token_status:
path: /auth/token/status
methods:
- GET

admin_revoke_operator:
path: /admin/revoke_access
methods:
- POST
9 changes: 9 additions & 0 deletions server/create_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def main():
)

other = parser.add_option_group("Other")
other.add_option(
"-r",
"--role",
action="store",
dest="role",
default="operator",
help="The role to give to the new user, if applicable. One of \"admin\", \"operator\"."
)
other.add_option(
"-v",
"--verbose",
Expand Down Expand Up @@ -114,6 +122,7 @@ def main():
auth_token_expiry=None,
created_at=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
rmq_queue=operator_config["rmq_queue"],
role=options.role
)
db.session.add(operator)
db.session.commit()
Expand Down
10 changes: 5 additions & 5 deletions server/maliketh/builder/builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass, asdict, field
from typing import Optional
from typing import Any, Generator, Optional
from maliketh.crypto.utils import random_hex
import subprocess
import os
Expand Down Expand Up @@ -60,14 +60,14 @@ def operator_name(self):
def BuilderOptions(self):
return self._builder_options

def operator(self, operator_name: str):
def operator(self, name: str):
"""
Set the operator name
"""
self.operator_name = operator_name
self._operator_name = name
return self

def with_options(self, options: BuilderOptions):
def with_options(self, options: "BuilderOptions"):
"""
Set the build options
"""
Expand Down Expand Up @@ -148,7 +148,7 @@ def build(self) -> Optional[bytes]:
return None


def __create_compiler_flags(self) -> str:
def __create_compiler_flags(self) -> Generator[str, Any, Any]:
"""
Create the string of compiler arguments (-D) to pass to the compiler
"""
Expand Down
93 changes: 0 additions & 93 deletions server/maliketh/crypto/aes.py

This file was deleted.

20 changes: 0 additions & 20 deletions server/maliketh/crypto/x509.py

This file was deleted.

Loading

0 comments on commit 22ad839

Please sign in to comment.