forked from its-a-feature/Mythic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_payload_types.sh
executable file
·131 lines (129 loc) · 3.91 KB
/
start_payload_types.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
RED='\033[1;31m'
NC='\033[0m' # No Color
GREEN='\033[1;32m'
BLUE='\033[1;34m'
if [ "$EUID" -ne 0 ]
then echo -e "${RED}[-]${NC} Please run as root"
exit
fi
containsElement () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
startContainer(){
p="$1"
realpath=$(realpath "$p")
if [ $? -ne 0 ]
then
echo -e "${RED}[-]${NC} Failed to find 'realpath' command. Aborting"
exit 1
fi
p=$(echo "${p/.\/Payload_Types\//}")
tag=$(echo "$p" | tr '[:upper:]' '[:lower:]')
tag=$(echo "${tag/' '/}")
tag=$(echo "${tag/'_'/}")
if [ -d "$realpath" ]
then
# only try to do this if the specified directory actually exists
echo -e "${BLUE}[*]${NC} Building $p's docker"
docker build -f "$realpath/Dockerfile" --rm -t "$tag" "$realpath"
if [ $? -ne 0 ]
then
echo -e "${RED}[-]${NC} Failed to build $p's container. Aborting"
exit 1
else
echo -e "${GREEN}[+]${NC} Successfully built $p's container"
fi
docker container prune --filter label=name="$tag" -f
output=`docker run --network host --hostname "$p" -d -v "$realpath:/Mythic/" --name "$tag" "$tag" 2>&1`
if [ $? -ne 0 ]
then
echo -e "${BLUE}[*]${NC} Payload Type, $p, is already running. Stopping it..."
# if we got an error while trying to run the container, stop any current ones first and try again
output=`docker stop "$tag" 2>/dev/null`
output=`docker container rm $(docker container ps -aq --filter name="$tag") 2>/dev/null`
echo -e "${BLUE}[*]${NC} Now trying to start it again..."
docker run --network host --hostname "$p" -d -v "$realpath:/Mythic/" --name "$tag" "$tag"
if [ $? -ne 0 ]
then
echo -e "${RED}[-]${NC} Failed to start $p's container. Aborting"
exit 1
else
echo -e "${GREEN}[+]${NC} Successfully started $p's container"
fi
else
echo -e "${GREEN}[+]${NC} Successfully started $p's container"
fi
fi
}
if [ $# -eq 0 ]
then
# if no arguments supplied, try to build and start all payload types
exclude=()
include=()
else
exclude=()
include=()
while (( "$#" )); do
case "$1" in
-e|--exclude)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
exclude+=("$2")
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
include+=("$1")
shift
;;
esac
done
#echo "exclude:"
#echo "${exclude[*]}"
#echo "include:"
#echo "${include[*]}"
# if any arguments supplied, add them to an exclusion list
exclude=("${exclude[@]/#/.\/Payload_Types\/}")
include=("${include[@]/#/.\/Payload_Types\/}")
fi
payloads=(./Payload_Types/*)
# build out the standard image for building payload types
# can use the next two lines to build a local "payload_type_base" image that's python3.6 and has the necessary files
#echo "Building golden payload_type_base image..."
#docker build -f "./Payload_Types/Dockerfile" --rm -t "payload_type_base" "./Payload_Types"
# now loop through the profiles to build out their variations
echo -e "${BLUE}[*]${NC} Looping through payload types..."
for p in "${payloads[@]}"
do
containsElement "${p}" "${exclude[@]}"
# 0 is success, the element is contained
if [[ $? == 1 ]]
then
# payload type not excluded
if [[ ${#include[@]} > 0 ]]
then
# it wasn't explicitly excluded, but we do have an inclusion list, so only start those
containsElement "${p}" "${include[@]}"
if [[ $? == 0 ]]
then
startContainer "$p"
continue
fi
echo -e "${BLUE}[*]${NC} skipping $p's container"
continue
fi
startContainer "$p"
else
echo -e "${BLUE}[*]${NC} skipping $p's container"
fi
done