Skip to content

Commit 6e8eb58

Browse files
Cx01Nvinnybod
andauthored
Updated to startup and install (EmpireProject#790)
* Added install and pull for submodules * fixed pytest * updated submodules code to run as non-root * removed comments * Update empire/server/api/app.py Co-authored-by: Vincent Rose <[email protected]> * Update ps-empire Co-authored-by: Vincent Rose <[email protected]> * Update ps-empire Co-authored-by: Vincent Rose <[email protected]> * Update ps-empire Co-authored-by: Vincent Rose <[email protected]> * Update ps-empire Co-authored-by: Vincent Rose <[email protected]> * Update ps-empire Co-authored-by: Vincent Rose <[email protected]> * updated starkiller pull to use non root * updated submodule fetch to occur before check * updated changelog * updated docs --------- Co-authored-by: Vincent Rose <[email protected]>
1 parent 174b692 commit 6e8eb58

File tree

12 files changed

+110
-21
lines changed

12 files changed

+110
-21
lines changed

CHANGELOG.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
## [Unreleased]
1616

17-
### Fixed
18-
19-
- Fixed issue loading `openapi.json` (@Vinnybod)
20-
2117
### Added
2218

2319
- Added dependabot for github actions dependencies (@Vinnybod)
20+
- Added install option to ./ps-empire file (@Cx01N)
21+
- Added auto pull options for submodules on startup (@Cx01N)
2422
- Added hook and socket message to receive callback messages for individual agents (@AaronVigal)
2523

2624
### Changed
2725

2826
- Updated all dependencies (@Vinnybod)
2927
- Updated Dockerfile and install script to Python 3.12.2 (@Vinnybod)
28+
- Updated starkiller snyc to no longer require root (@Cx01N)
29+
30+
### Fixed
31+
32+
- Fixed issue loading `openapi.json` (@Vinnybod)
3033

3134
## [5.9.5] - 2024-02-22
3235

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ Empire is a post-exploitation and adversary emulation framework that is used to
5353
- [ProcessInjection](https://github.com/3xpl01tc0d3r/ProcessInjection)
5454
- And Many More
5555

56+
<!---
5657
## Sponsors
5758
<div align="center">
5859
5960
[<img src="https://github.com/BC-SECURITY/Empire/assets/9831420/f273f4b0-400c-49ce-b62f-521239a86754" width="100"/>](https://www.cybrary.it/)
6061
6162
[<img src="https://github.com/BC-SECURITY/Empire/assets/9831420/d14af000-80d2-4f67-b70c-b62ac42b6a52" width="100"/>](https://twitter.com/joehelle)
62-
6363
</div>
64+
--->
6465

6566
## Release Notes
6667

@@ -80,7 +81,7 @@ After cloning the repo, you can checkout the latest stable release by running th
8081
git clone --recursive https://github.com/BC-SECURITY/Empire.git
8182
cd Empire
8283
./setup/checkout-latest-tag.sh
83-
./setup/install.sh
84+
./ps-empire install -y
8485
```
8586

8687
If you are using the sponsors version of Empire, it will pull the sponsors version of Starkiller.

docs/quickstart/configuration/server.md

+7
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,10 @@ directories:
9090
```
9191
9292
* **logging** - See [Logging](../../logging/logging.md) for more information on logging configuration.
93+
94+
* **submodules** - Control if submodules wil be auto updated on startup.
95+
96+
```
97+
submodules:
98+
auto_update: true
99+
```

docs/quickstart/installation/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Note: The `main` branch is a reflection of the latest changes and may not always
1919
git clone --recursive https://github.com/BC-SECURITY/Empire.git
2020
cd Empire
2121
./setup/checkout-latest-tag.sh
22-
./setup/install.sh
22+
./ps-empire install -y
2323
```
2424

2525
**Sponsors:**
@@ -28,7 +28,7 @@ cd Empire
2828
git clone --recursive https://github.com/BC-SECURITY/Empire-Sponsors.git
2929
cd Empire-Sponsors
3030
./setup/checkout-latest-tag.sh sponsors
31-
./setup/install.sh
31+
./ps-empire install -y
3232
```
3333

3434
If you are using the sponsors version of Empire, it will pull the sponsors version of Starkiller.

empire/arguments.py

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
sync_starkiller_parser = subparsers.add_parser(
1515
"sync-starkiller", help="Sync Starkiller submodule with the config"
1616
)
17+
install_parser = subparsers.add_parser("install", help="Install the Empire framework")
18+
install_parser.add_argument(
19+
"-y",
20+
action="store_true",
21+
help="Automatically say yes to all prompts during installation",
22+
)
1723

1824
# Client Args
1925
client_parser.add_argument(

empire/scripts/sync_starkiller.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import logging
2-
import subprocess
32
from pathlib import Path
43

4+
from empire.server.utils.file_util import run_as_user
5+
56
log = logging.getLogger(__name__)
67

78

@@ -26,23 +27,18 @@ def sync_starkiller(empire_config):
2627

2728

2829
def _clone_starkiller(starkiller_config: dict, starkiller_dir: str):
29-
subprocess.run(
30-
["git", "clone", starkiller_config["repo"], starkiller_dir],
31-
check=True,
32-
)
30+
run_as_user(["git", "clone", starkiller_config["repo"], starkiller_dir])
3331

3432

3533
def _fetch_checkout_pull(remote_repo, ref, cwd):
36-
subprocess.run(
34+
run_as_user(
3735
["git", "remote", "set-url", "origin", remote_repo],
3836
cwd=cwd,
39-
check=True,
4037
)
4138

42-
subprocess.run(["git", "fetch"], cwd=cwd, check=True)
43-
subprocess.run(
39+
run_as_user(["git", "fetch"], cwd=cwd)
40+
run_as_user(
4441
["git", "checkout", ref],
4542
cwd=cwd,
46-
check=True,
4743
)
48-
subprocess.run(["git", "pull", "origin", ref], cwd=cwd)
44+
run_as_user(["git", "pull", "origin", ref], cwd=cwd)

empire/server/config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ starkiller:
4949
# Can be a branch, tag, or commit hash
5050
ref: sponsors-main
5151
auto_update: true
52+
submodules:
53+
auto_update: true
5254
plugins:
5355
# Auto-load plugin with defined settings
5456
csharpserver:

empire/server/core/config.py

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class ApiConfig(EmpireBaseModel):
2222
cert_path: Path = "empire/server/data"
2323

2424

25+
class SubmodulesConfig(EmpireBaseModel):
26+
auto_update: bool = True
27+
28+
2529
class StarkillerConfig(EmpireBaseModel):
2630
repo: str = "bc-security/starkiller"
2731
directory: Path = "empire/server/api/v2/starkiller"
@@ -96,6 +100,7 @@ class EmpireConfig(EmpireBaseModel):
96100
)
97101
api: ApiConfig | None = ApiConfig()
98102
starkiller: StarkillerConfig
103+
submodules: SubmodulesConfig
99104
database: DatabaseConfig
100105
plugins: dict[str, dict[str, str]] = {}
101106
directories: DirectoriesConfig

empire/server/server.py

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from empire.server.core.config import empire_config
1616
from empire.server.core.db import base
1717
from empire.server.utils import file_util
18+
from empire.server.utils.file_util import run_as_user
1819
from empire.server.utils.log_util import LOG_FORMAT, SIMPLE_LOG_FORMAT, ColorFormatter
1920

2021
log = logging.getLogger(__name__)
@@ -124,6 +125,11 @@ def check_submodules():
124125
exit(1)
125126

126127

128+
def fetch_submodules():
129+
command = ["git", "submodule", "update", "--init", "--recursive"]
130+
run_as_user(command)
131+
132+
127133
def check_recommended_configuration():
128134
log.info(f"Using {empire_config.database.use} database.")
129135
if empire_config.database.use == "sqlite":
@@ -135,6 +141,13 @@ def check_recommended_configuration():
135141

136142
def run(args):
137143
setup_logging(args)
144+
145+
if empire_config.submodules.auto_update:
146+
log.info("Submodules auto update enabled. Loading.")
147+
fetch_submodules()
148+
else:
149+
log.info("Submodules auto update disabled. Not fetching.")
150+
138151
check_submodules()
139152
check_recommended_configuration()
140153

empire/server/utils/file_util.py

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import logging
12
import os
23
import shutil
4+
import subprocess
5+
6+
log = logging.getLogger(__name__)
37

48

59
def remove_dir_contents(path: str) -> None:
@@ -22,3 +26,28 @@ def remove_file(path: str) -> None:
2226
"""
2327
if os.path.exists(path):
2428
os.remove(path)
29+
30+
31+
def run_as_user(command, user=None, cwd=None):
32+
"""
33+
Runs a command as a specified user or the user who invoked sudo.
34+
If no user is specified and the script is not run with sudo, it runs as the current user.
35+
36+
Args:
37+
command (list): The command to run, specified as a list of strings.
38+
user (str, optional): The username to run the command as. Defaults to None.
39+
"""
40+
try:
41+
if user is None:
42+
user = os.getenv("SUDO_USER")
43+
44+
command_with_user = ["sudo", "-u", user, *command] if user else command
45+
46+
subprocess.run(command_with_user, check=True, cwd=cwd)
47+
48+
log.debug("Command executed successfully: %s", " ".join(command))
49+
50+
except subprocess.CalledProcessError as e:
51+
# Log the error details
52+
log.error("Failed to execute command: %s", e, exc_info=True)
53+
log.error("Try running the command manually: %s", " ".join(command))

empire/test/test_server_config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ starkiller:
5151
# for the downstream main branches.
5252
use_temp_dir: false
5353
auto_update: true
54+
submodules:
55+
auto_update: true
5456
directories:
5557
downloads: empire/test/downloads/
5658
module_source: empire/server/data/module_source/

ps-empire

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
1-
#! /bin/bash
2-
sudo -E poetry run python empire.py ${@}
1+
#!/bin/bash
2+
3+
INSTALL=0
4+
YES_OPTION=""
5+
EMPIRE_ARGS=()
6+
7+
# Parse command-line options manually, without using getopts, to allow non-standard options
8+
for arg in "$@"; do
9+
case $arg in
10+
install)
11+
INSTALL=1
12+
;;
13+
-y)
14+
YES_OPTION="-y"
15+
;;
16+
*)
17+
EMPIRE_ARGS+=("$arg")
18+
;;
19+
esac
20+
done
21+
22+
# Check if install option was given
23+
if [ $INSTALL -eq 1 ]; then
24+
./setup/install.sh $YES_OPTION
25+
fi
26+
27+
sudo -E poetry run python empire.py "${EMPIRE_ARGS[@]}"

0 commit comments

Comments
 (0)