Skip to content

Commit

Permalink
increase coverage by optimizing conftest and removed unused migration…
Browse files Browse the repository at this point in the history
… scripts. add tag filters to credentials (BC-SECURITY#728)
  • Loading branch information
vinnybod authored Nov 11, 2023
1 parent 2fb8865 commit 4e87067
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 263 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Upgrade Pydantic to v2 (@Vinnybod)
- Update common FastAPI Dependencies to use 'Annotated' types for simpler code (@Vinnybod)
- Add tags search to credentials endpoints (@Vinnybod)
- Remove unused migration scripts (@Vinnybod)
- Simplify TestClient setup (@Vinnybod)

## [5.8.0] - 2023-11-06

Expand Down
47 changes: 26 additions & 21 deletions empire/server/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def load_starkiller(v2App, ip, port):
log.info(f"Starkiller served at http://localhost:{port}/index.html")


def initialize(secure: bool = False, ip: str = "0.0.0.0", port: int = 1337):
def initialize(
secure: bool = False, ip: str = "0.0.0.0", port: int = 1337, run: bool = True
):
# Not pretty but allows us to use main_menu by delaying the import
from empire.server.api.v2.agent import agent_api, agent_file_api, agent_task_api
from empire.server.api.v2.bypass import bypass_api
Expand Down Expand Up @@ -151,23 +153,26 @@ def shutdown_event():

cert_path = os.path.abspath("./empire/server/data/")

if not secure:
uvicorn.run(
v2App,
host=ip,
port=port,
log_config=None,
lifespan="on",
# log_level="info",
)
else:
uvicorn.run(
v2App,
host=ip,
port=port,
log_config=None,
lifespan="on",
ssl_keyfile=f"{cert_path}/empire-priv.key",
ssl_certfile=f"{cert_path}/empire-chain.pem",
# log_level="info",
)
if run:
if not secure:
uvicorn.run(
v2App,
host=ip,
port=port,
log_config=None,
lifespan="on",
# log_level="info",
)
else:
uvicorn.run(
v2App,
host=ip,
port=port,
log_config=None,
lifespan="on",
ssl_keyfile=f"{cert_path}/empire-priv.key",
ssl_certfile=f"{cert_path}/empire-chain.pem",
# log_level="info",
)

return v2App
6 changes: 4 additions & 2 deletions empire/server/api/v2/credential/credential_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import Depends, HTTPException
from fastapi import Depends, HTTPException, Query
from starlette.responses import Response
from starlette.status import HTTP_204_NO_CONTENT

Expand All @@ -14,6 +14,7 @@
from empire.server.api.v2.shared_dependencies import CurrentSession
from empire.server.api.v2.shared_dto import BadRequestResponse, NotFoundResponse
from empire.server.api.v2.tag import tag_api
from empire.server.api.v2.tag.tag_dto import TagStr
from empire.server.core.db import models
from empire.server.server import main

Expand Down Expand Up @@ -54,11 +55,12 @@ async def read_credentials(
db: CurrentSession,
search: str | None = None,
credtype: str | None = None,
tags: list[TagStr] | None = Query(None),
):
credentials = list(
map(
lambda x: domain_to_dto_credential(x),
credential_service.get_all(db, search, credtype),
credential_service.get_all(db, search, credtype, tags),
)
)

Expand Down
104 changes: 0 additions & 104 deletions empire/server/common/converter/convert_authors.py

This file was deleted.

96 changes: 0 additions & 96 deletions empire/server/common/converter/module_converter.py

This file was deleted.

13 changes: 12 additions & 1 deletion empire/server/core/credential_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def __init__(self, main_menu):
self.main_menu = main_menu

@staticmethod
def get_all(db: Session, search: str = None, credtype: str = None):
def get_all(
db: Session, search: str = None, credtype: str = None, tags: list[str] = None
):
query = db.query(models.Credential)

if search:
Expand All @@ -23,6 +25,15 @@ def get_all(db: Session, search: str = None, credtype: str = None):
)
)

if tags:
tags_split = [tag.split(":", 1) for tag in tags]
query = query.join(models.Credential.tags).filter(
and_(
models.Tag.name.in_([tag[0] for tag in tags_split]),
models.Tag.value.in_([tag[1] for tag in tags_split]),
)
)

if credtype:
query = query.filter(models.Credential.credtype == credtype)

Expand Down
43 changes: 4 additions & 39 deletions empire/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from pathlib import Path

import pytest
from fastapi import FastAPI
from starlette.testclient import TestClient

from empire.client.src.utils.data_util import get_random_string
Expand Down Expand Up @@ -50,48 +49,14 @@ def client():
args = arguments.parent_parser.parse_args()

import empire.server.server
from empire.server.api.app import initialize
from empire.server.common.empire import MainMenu

empire.server.server.main = MainMenu(args)

from empire.server.api.v2.agent import agent_api, agent_file_api, agent_task_api
from empire.server.api.v2.bypass import bypass_api
from empire.server.api.v2.credential import credential_api
from empire.server.api.v2.download import download_api
from empire.server.api.v2.host import host_api, process_api
from empire.server.api.v2.listener import listener_api, listener_template_api
from empire.server.api.v2.meta import meta_api
from empire.server.api.v2.module import module_api
from empire.server.api.v2.obfuscation import obfuscation_api
from empire.server.api.v2.plugin import plugin_api, plugin_task_api
from empire.server.api.v2.profile import profile_api
from empire.server.api.v2.stager import stager_api, stager_template_api
from empire.server.api.v2.tag import tag_api
from empire.server.api.v2.user import user_api

v2App = FastAPI()
v2App.include_router(listener_template_api.router)
v2App.include_router(listener_api.router)
v2App.include_router(stager_template_api.router)
v2App.include_router(stager_api.router)
v2App.include_router(agent_task_api.router)
v2App.include_router(agent_file_api.router)
v2App.include_router(agent_api.router)
v2App.include_router(module_api.router)
v2App.include_router(bypass_api.router)
v2App.include_router(obfuscation_api.router)
v2App.include_router(profile_api.router)
v2App.include_router(plugin_api.router)
v2App.include_router(plugin_task_api.router)
v2App.include_router(credential_api.router)
v2App.include_router(host_api.router)
v2App.include_router(user_api.router)
v2App.include_router(process_api.router)
v2App.include_router(download_api.router)
v2App.include_router(meta_api.router)
v2App.include_router(tag_api.router)

yield TestClient(v2App)
app = initialize(ip="localhost", run=False)

yield TestClient(app)

from empire.server.server import main

Expand Down

0 comments on commit 4e87067

Please sign in to comment.