This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathdocker_utils.py
174 lines (144 loc) · 7.78 KB
/
docker_utils.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import json
import random
import traceback
import uuid
from collections import OrderedDict
import docker
from .db_utils import DBUtils
from .models import DynamicDockerChallenge
from .redis_utils import RedisUtils
class DockerUtils:
@staticmethod
def add_new_docker_container(app, user_id, challenge_id, flag, uuid_code):
configs = DBUtils.get_all_configs()
dynamic_docker_challenge = DynamicDockerChallenge.query \
.filter(DynamicDockerChallenge.id == challenge_id) \
.first_or_404()
dns = configs.get("docker_dns", "").split(",")
nodes = configs.get("docker_swarm_nodes", "").split(",")
win_nodes = []
linux_nodes = []
for node in nodes:
if node.startswith("windows"):
win_nodes.append(node)
else:
linux_nodes.append(node)
client = docker.DockerClient(base_url=configs.get("docker_api_url"))
if dynamic_docker_challenge.docker_image.startswith("{"):
images = json.loads(dynamic_docker_challenge.docker_image, object_pairs_hook=OrderedDict)
redis_util = RedisUtils(app)
range_prefix = redis_util.get_available_network_range()
ipam_pool = docker.types.IPAMPool(
subnet=range_prefix
)
ipam_config = docker.types.IPAMConfig(driver='default', pool_configs=[ipam_pool])
network_name = str(user_id) + '-' + uuid_code
network = client.networks.create(network_name, internal=True, ipam=ipam_config, attachable=True,
labels={'prefix': range_prefix}, driver="overlay", scope="swarm")
dns = []
containers = configs.get("docker_auto_connect_containers", "").split(",")
for c in containers:
if c.find("dns") != -1:
network.connect(c)
network.reload()
for name in network.attrs['Containers']:
if network.attrs['Containers'][name]['Name'] == c:
dns.append(network.attrs['Containers'][name]['IPv4Address'].split('/')[0])
else:
network.connect(c)
has_processed_main = False
for name in images:
if not has_processed_main:
image = images[name]
container_name = str(user_id) + '-' + uuid_code
node = DockerUtils.choose_node(image, win_nodes, linux_nodes)
client.services.create(image=image, name=container_name, networks=[
docker.types.NetworkAttachmentConfig(network_name, aliases=[name])],
env={'FLAG': flag}, dns_config=docker.types.DNSConfig(nameservers=dns),
resources=docker.types.Resources(
mem_limit=DockerUtils.convert_readable_text(
dynamic_docker_challenge.memory_limit),
cpu_limit=int(
dynamic_docker_challenge.cpu_limit * 1e9)),
labels={str(user_id) + '-' + uuid_code: str(user_id) + '-' + uuid_code},
hostname=name, constraints=['node.labels.name==' + node],
endpoint_spec=docker.types.EndpointSpec(mode='dnsrr', ports={}))
has_processed_main = True
continue
image = images[name]
container_name = str(user_id) + '-' + str(uuid.uuid4())
client.services.create(image=image, name=container_name, networks=[
docker.types.NetworkAttachmentConfig(network_name, aliases=[name])],
env={'FLAG': flag}, dns_config=docker.types.DNSConfig(nameservers=dns),
resources=docker.types.Resources(mem_limit=DockerUtils.convert_readable_text(
dynamic_docker_challenge.memory_limit),
cpu_limit=int(
dynamic_docker_challenge.cpu_limit * 1e9)),
labels={str(user_id) + '-' + uuid_code: str(user_id) + '-' + uuid_code},
hostname=name, constraints=['node.labels.name==' + node],
endpoint_spec=docker.types.EndpointSpec(mode='dnsrr', ports={}))
else:
node = DockerUtils.choose_node(dynamic_docker_challenge.docker_image, win_nodes, linux_nodes)
client.services.create(image=dynamic_docker_challenge.docker_image, name=str(user_id) + '-' + uuid_code,
env={'FLAG': flag}, dns_config=docker.types.DNSConfig(nameservers=dns),
networks=[configs.get("docker_auto_connect_network", "ctfd_frp-containers")],
resources=docker.types.Resources(mem_limit=DockerUtils.convert_readable_text(
dynamic_docker_challenge.memory_limit),
cpu_limit=int(
dynamic_docker_challenge.cpu_limit * 1e9)),
constraints=['node.labels.name==' + node],
endpoint_spec=docker.types.EndpointSpec(mode='dnsrr', ports={}))
@staticmethod
def choose_node(image, win_nodes, linux_nodes):
is_win = False
image_split = image.split(":")
if len(image_split) > 1:
if image_split[1].startswith("windows"):
is_win = True
if is_win:
node = random.choice(win_nodes)
else:
node = random.choice(linux_nodes)
return node
@staticmethod
def convert_readable_text(text):
lower_text = text.lower()
if lower_text.endswith("k"):
return int(text[:-1]) * 1024
if lower_text.endswith("m"):
return int(text[:-1]) * 1024 * 1024
if lower_text.endswith("g"):
return int(text[:-1]) * 1024 * 1024 * 1024
return 0
@staticmethod
def remove_current_docker_container(app, user_id, is_retry=False):
configs = DBUtils.get_all_configs()
container = DBUtils.get_current_containers(user_id=user_id)
auto_containers = configs.get("docker_auto_connect_containers", "").split(",")
if container is None:
return False
try:
client = docker.DockerClient(base_url=configs.get("docker_api_url"))
networks = client.networks.list(names=[str(user_id) + '-' + container.uuid])
if len(networks) == 0:
services = client.services.list(filters={'name': str(user_id) + '-' + container.uuid})
for s in services:
s.remove()
else:
redis_util = RedisUtils(app)
services = client.services.list(filters={'label': str(user_id) + '-' + container.uuid})
for s in services:
s.remove()
for n in networks:
for ac in auto_containers:
try:
n.disconnect(ac, force=True)
except:
pass
n.remove()
redis_util.add_available_network_range(n.attrs['Labels']['prefix'])
except:
traceback.print_exc()
if not is_retry:
DockerUtils.remove_current_docker_container(app, user_id, True)
return True