Skip to content

Commit

Permalink
Change services from unix to tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorNelson authored and mudongliang committed Jan 11, 2024
1 parent 86f3236 commit 37673ff
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 63 deletions.
8 changes: 4 additions & 4 deletions challenge/docker-entrypoint.d/20_start_code_server.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/bin/sh

mkdir /tmp/code-server
mkdir -p /tmp/.dojo/code-server
start-stop-daemon --start \
--pidfile /tmp/code-server/code-server.pid \
--pidfile /tmp/.dojo/code-server/code-server.pid \
--make-pidfile \
--background \
--no-close \
--startas /usr/bin/code-server \
-- \
--auth=none \
--socket=/home/hacker/.local/share/code-server/workspace.socket \
--bind-addr=dojo-user:6080 \
--extensions-dir=/opt/code-server/extensions \
--disable-telemetry \
</dev/null \
>>/tmp/code-server/code-server.log \
>>/tmp/.dojo/code-server/code-server.log \
2>&1
22 changes: 4 additions & 18 deletions challenge/docker-entrypoint.d/38_start_windows_gui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,18 @@

if ! find /challenge -name '*.exe' -exec false {} +
then
mkdir -p /tmp/vnc /home/hacker/.vnc
mkdir -p /tmp/.dojo/vnc /home/hacker/.vnc
start-stop-daemon --start \
--pidfile /tmp/vnc/websockify-windows.pid \
--pidfile /tmp/.dojo/vnc/websockify-windows.pid \
--make-pidfile \
--background \
--no-close \
--startas /usr/bin/websockify \
-- \
--web /usr/share/novnc/ \
24153 \
dojo-user:6082 \
localhost:5912 \
</dev/null \
>>/tmp/vnc/websockify-windows.log \
2>&1

rm -f /home/hacker/.vnc/novnc-windows.socket
start-stop-daemon --start \
--pidfile /tmp/vnc/socat-windows.pid \
--make-pidfile \
--background \
--no-close \
--startas /usr/bin/socat \
-- \
UNIX-LISTEN:/home/hacker/.vnc/novnc-windows.socket,fork \
TCP-CONNECT:localhost:24153 \
</dev/null \
>>/tmp/vnc/socat-windows.log \
>>/tmp/.dojo/vnc/websockify-windows.log \
2>&1
fi
33 changes: 11 additions & 22 deletions challenge/docker-entrypoint.d/39_start_gui.sh
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
#!/bin/sh

mkdir -p /tmp/vnc /home/hacker/.vnc
mkdir -p /tmp/.dojo/vnc /home/hacker/.vnc

echo "$(head -c32 /dev/urandom | md5sum | head -c8)" > /home/hacker/.vnc/pass-interact
echo "$(head -c32 /dev/urandom | md5sum | head -c8)" > /home/hacker/.vnc/pass-view
cat /home/hacker/.vnc/pass-interact /home/hacker/.vnc/pass-view | tigervncpasswd -f > /home/hacker/.vnc/vncpass

start-stop-daemon --start \
--pidfile /tmp/vnc/vncserver.pid \
--pidfile /tmp/.dojo/vnc/vncserver.pid \
--make-pidfile \
--background \
--no-close \
--startas /usr/bin/Xtigervnc \
-- \
:42 \
-localhost=0 \
-rfbunixpath /tmp/vnc/vnc_socket \
-rfbunixpath /tmp/.dojo/vnc/vnc_socket \
-rfbauth /home/hacker/.vnc/vncpass \
-nolisten tcp \
-geometry 1024x768 \
-depth 24 \
</dev/null \
>>/tmp/vnc/vncserver.log \
>>/tmp/.dojo/vnc/vncserver.log \
2>&1

start-stop-daemon --start \
--pidfile /tmp/vnc/websockify.pid \
--pidfile /tmp/.dojo/vnc/websockify.pid \
--make-pidfile \
--background \
--no-close \
--startas /usr/bin/websockify \
-- \
--web /usr/share/novnc/ \
24152 \
--unix-target=/tmp/vnc/vnc_socket \
</dev/null \
>>/tmp/vnc/websockify.log \
2>&1

rm -f /home/hacker/.vnc/novnc.socket
start-stop-daemon --start \
--pidfile /tmp/vnc/socat.pid \
--make-pidfile \
--background \
--no-close \
--startas /usr/bin/socat \
-- \
UNIX-LISTEN:/home/hacker/.vnc/novnc.socket,fork \
TCP-CONNECT:localhost:24152 \
dojo-user:6081 \
--unix-target=/tmp/.dojo/vnc/vnc_socket \
</dev/null \
>>/tmp/vnc/socat.log \
>>/tmp/.dojo/vnc/websockify.log \
2>&1

seq 1 50 | while read cnt; do sleep 0.1; [ -e /tmp/.X11-unix/X42 ] && break; done
Expand Down
4 changes: 2 additions & 2 deletions challenge/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh -e

exec >/tmp/.startup_log 2>&1
chmod 600 /tmp/.startup_log
mkdir -p /tmp/.dojo
exec >/tmp/.dojo/entrypoint.log 2>&1

for SCRIPT in /opt/pwn.college/docker-entrypoint.d/*
do
Expand Down
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ services:
- ./nginx-proxy/etc/passwd:/etc/passwd:ro
- ./data/homes:/var/homes:shared
- /var/run/docker.sock:/tmp/${DOCKER_PSLR}/docker.sock:ro
networks:
default:
user_network:
aliases:
- nginx
ipv4_address: 10.0.0.3

nginx-certs:
container_name: nginx_certs
Expand Down
12 changes: 2 additions & 10 deletions dojo_plugin/api/v1/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from ...config import HOST_DATA_PATH, INTERNET_FOR_ALL, WINDOWS_VM_ENABLED
from ...models import Dojos, DojoModules, DojoChallenges
from ...utils import serialize_user_flag, simple_tar, random_home_path, SECCOMP, USER_FIREWALL_ALLOWED, module_challenges_visible
from ...utils import serialize_user_flag, simple_tar, random_home_path, SECCOMP, USER_FIREWALL_ALLOWED, module_challenges_visible, user_ipv4
from ...utils.dojo import dojo_accessible, get_current_dojo_challenge


Expand Down Expand Up @@ -117,6 +117,7 @@ def start_container(user, dojo_challenge, practice):
f"vm_{hostname}": "127.0.0.1",
"challenge.localhost": "127.0.0.1",
"hacker.localhost": "127.0.0.1",
"dojo-user": user_ipv4(user),
**USER_FIREWALL_ALLOWED,
},
init=True,
Expand All @@ -130,15 +131,6 @@ def start_container(user, dojo_challenge, practice):
auto_remove=True,
)

def user_ipv4(user):
# Subnet: 10.0.0.0/8
# Reserved: 10.0.0.0/24, 10.255.255.0/24
# Gateway: 10.0.0.1
# User IPs: 10.0.1.0 - 10.255.254.255
user_ip = (10 << 24) + (1 << 8) + user.id
assert user_ip < (10 << 24) + (255 << 16) + (255 << 8)
return f"{user_ip >> 24 & 0xff}.{user_ip >> 16 & 0xff}.{user_ip >> 8 & 0xff}.{user_ip & 0xff}"

user_network = docker_client.networks.get("user_network")
user_network.connect(container, ipv4_address=user_ipv4(user), aliases=[f"user_{user.id}"])

Expand Down
6 changes: 3 additions & 3 deletions dojo_plugin/pages/desktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@ def forward_desktop_res(route, socket_path, user_id, path=""):
if not can_connect_to(user):
abort(403)

return redirect_user_socket(user, socket_path, f"/{path}")
return redirect_user_socket(user, socket_path, path)


@desktop.route("/desktop/<int:user_id>/")
@desktop.route("/desktop/<int:user_id>/<path:path>")
@authed_only
def forward_desktop(user_id, path=""):
return forward_desktop_res("desktop", ".vnc/novnc.socket", user_id, path)
return forward_desktop_res("desktop", 6081, user_id, path)


@desktop.route("/desktop-win/<int:user_id>/")
@desktop.route("/desktop-win/<int:user_id>/<path:path>")
@authed_only
def forward_desktop_win(user_id, path=""):
return forward_desktop_res("desktop-win", ".vnc/novnc-windows.socket", user_id, path)
return forward_desktop_res("desktop-win", 6082, user_id, path)

@desktop.route("/admin/desktops", methods=["GET"])
@admins_only
Expand Down
2 changes: 1 addition & 1 deletion dojo_plugin/pages/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def forward_workspace(path=""):
prefix = "/workspace/"
assert request.full_path.startswith(prefix)
path = request.full_path[len(prefix):]
return redirect_user_socket(get_current_user(), ".local/share/code-server/workspace.socket", f"/{path}")
return redirect_user_socket(get_current_user(), 6080, path)


def redirect_workspace_referers():
Expand Down
13 changes: 11 additions & 2 deletions dojo_plugin/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ def serialize_user_flag(account_id, challenge_id, *, secret=None):
user_flag = serializer.dumps(data)[::-1]
return user_flag

def user_ipv4(user):
# Subnet: 10.0.0.0/8
# Reserved: 10.0.0.0/24, 10.255.255.0/24
# Gateway: 10.0.0.1
# User IPs: 10.0.1.0 - 10.255.254.255
user_ip = (10 << 24) + (1 << 8) + user.id
assert user_ip < (10 << 24) + (255 << 16) + (255 << 8)
return f"{user_ip >> 24 & 0xff}.{user_ip >> 16 & 0xff}.{user_ip >> 8 & 0xff}.{user_ip & 0xff}"

def redirect_internal(redirect_uri, auth=None):
response = Response()
if auth:
Expand All @@ -150,9 +159,9 @@ def redirect_internal(redirect_uri, auth=None):
response.headers["redirect_uri"] = redirect_uri
return response

def redirect_user_socket(user, socket_path, url_path):
def redirect_user_socket(user, port, url_path):
assert user is not None
return redirect_internal(f"http://unix:/var/homes/nosuid/{random_home_path(user)}/{socket_path}:{url_path}")
return redirect_internal(f"http://user_{user.id}:{port}/{url_path}")

def render_markdown(s):
raw_html = build_markdown(s or "")
Expand Down
3 changes: 2 additions & 1 deletion script/container-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ iptables -I DOCKER-USER -i user_network -j DROP
for host in $(cat $DOJO_DIR/user_firewall.allowed); do
iptables -I DOCKER-USER -i user_network -d $(host $host | awk '{print $NF; exit}') -j ACCEPT
done

iptables -I DOCKER-USER -i user_network -s 10.0.0.0/24 -m conntrack --ctstate NEW -j ACCEPT
iptables -I DOCKER-USER -i user_network -d 10.0.0.0/8 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

0 comments on commit 37673ff

Please sign in to comment.