docker compose bugfix
This commit is contained in:
1 parent
6ea6f54150
commit
41a170543b
15 files changed
+183
-121
No files matched your search
@@ -0,0 +1,157 @@
|
|||||||
|
from .Challenge import Challenge
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
import time
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
|
||||||
|
class Carbeat(Challenge):
|
||||||
|
flag_location = 'flags/carbeat.txt'
|
||||||
|
history_location = 'history/carbeat.txt'
|
||||||
|
|
||||||
|
_CONTAINER = "carbeat_container"
|
||||||
|
_SERVICE_CMD = ["docker", "exec", "-i", _CONTAINER, "/home/ctf/chall/mybini"]
|
||||||
|
_HEX_RE = re.compile(r'^[0-9a-fA-F]+$')
|
||||||
|
|
||||||
|
def _read_container_flag(self) -> str:
|
||||||
|
out = subprocess.run(["docker", "exec", self._CONTAINER, "cat", "/flag.txt"],
|
||||||
|
capture_output=True, text=True)
|
||||||
|
if out.returncode != 0 or not out.stdout.strip():
|
||||||
|
raise FileNotFoundError("Flag not found in container (/flag.txt)")
|
||||||
|
return out.stdout.strip()
|
||||||
|
|
||||||
|
def _spawn(self):
|
||||||
|
return subprocess.Popen(
|
||||||
|
self._SERVICE_CMD,
|
||||||
|
stdin=subprocess.PIPE,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.STDOUT,
|
||||||
|
text=True,
|
||||||
|
bufsize=0,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _read_until(self, proc, token, timeout=5.0, max_bytes=1_000_000):
|
||||||
|
start = time.time()
|
||||||
|
buf = []
|
||||||
|
r = proc.stdout.read
|
||||||
|
while True:
|
||||||
|
if time.time() - start > timeout:
|
||||||
|
tail = ''.join(buf)[-500:]
|
||||||
|
raise TimeoutError(f"Timeout waiting for '{token}'. Got so far:\n{tail}")
|
||||||
|
ch = r(1)
|
||||||
|
if ch == "" and proc.poll() is not None:
|
||||||
|
raise RuntimeError(f"Process ended while waiting for '{token}'. Output:\n{''.join(buf)}")
|
||||||
|
buf.append(ch)
|
||||||
|
if len(buf) > max_bytes:
|
||||||
|
raise RuntimeError("Exceeded max read size")
|
||||||
|
if token in "".join(buf):
|
||||||
|
return "".join(buf)
|
||||||
|
|
||||||
|
def _send_line(self, proc, s: str):
|
||||||
|
proc.stdin.write(s + "\n")
|
||||||
|
proc.stdin.flush()
|
||||||
|
|
||||||
|
def _expect_hex_field(self, text: str, label: str) -> str:
|
||||||
|
m = re.search(rf"{re.escape(label)}\s*:\s*([0-9a-fA-F]+)", text)
|
||||||
|
assert m, f"Missing '{label}' in output. Tail:\n{text[-400:]}"
|
||||||
|
hx = m.group(1)
|
||||||
|
assert self._HEX_RE.match(hx), f"{label} is not hex"
|
||||||
|
return hx
|
||||||
|
|
||||||
|
def distribute(self, flag):
|
||||||
|
try:
|
||||||
|
os.makedirs(os.path.dirname(self.flag_location), exist_ok=True)
|
||||||
|
with open(self.flag_location, 'w') as f:
|
||||||
|
f.write(flag)
|
||||||
|
|
||||||
|
os.makedirs(os.path.dirname(self.history_location), exist_ok=True)
|
||||||
|
with open(self.history_location, 'a') as f:
|
||||||
|
f.write(flag + '\n')
|
||||||
|
|
||||||
|
self.logger.info(f'Flag {flag} written to {self.flag_location}')
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f'Could not write flag to {self.flag_location}: {e}')
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def register(self, proc, name, level, timeout=1.):
|
||||||
|
self._read_until(proc, "> ", timeout=timeout)
|
||||||
|
self._send_line(proc, "1")
|
||||||
|
self._read_until(proc, ": ", timeout=timeout)
|
||||||
|
self._send_line(proc, name)
|
||||||
|
self._read_until(proc, ": ", timeout=timeout)
|
||||||
|
self._send_line(proc, str(level))
|
||||||
|
|
||||||
|
def login(self, proc, name, timeout=1.):
|
||||||
|
self._read_until(proc, "> ", timeout=timeout)
|
||||||
|
self._send_line(proc, "2")
|
||||||
|
self._read_until(proc, ": ", timeout=timeout)
|
||||||
|
self._send_line(proc, name)
|
||||||
|
|
||||||
|
def list_user(self, proc, timeout=1.):
|
||||||
|
self._read_until(proc, "> ", timeout=timeout)
|
||||||
|
self._send_line(proc, "3")
|
||||||
|
return self._read_until(proc, "\n1. register")
|
||||||
|
|
||||||
|
def bini(self, proc, id, data, timeout=1.0):
|
||||||
|
self._read_until(proc, "> ", timeout=timeout)
|
||||||
|
self._send_line(proc, str(id))
|
||||||
|
self._read_until(proc, ": ", timeout=timeout)
|
||||||
|
self._send_line(proc, data)
|
||||||
|
|
||||||
|
def logout(self, proc, timeout=1.):
|
||||||
|
self._read_until(proc, "> ", timeout=timeout)
|
||||||
|
self._send_line(proc, "4")
|
||||||
|
|
||||||
|
def exit(self, proc, timeout=1.):
|
||||||
|
self._read_until(proc, "> ", timeout=timeout)
|
||||||
|
self._send_line(proc, "3")
|
||||||
|
|
||||||
|
def check(self):
|
||||||
|
try:
|
||||||
|
with open(self.flag_location, 'r') as f:
|
||||||
|
host_flag = f.read().strip()
|
||||||
|
container_flag = self._read_container_flag()
|
||||||
|
assert host_flag == container_flag, 'Flag mismatch between host and container'
|
||||||
|
self.logger.info('[ok] flag parity (carbeat)')
|
||||||
|
|
||||||
|
proc = self._spawn()
|
||||||
|
|
||||||
|
self.register(proc, "karbit", 1337)
|
||||||
|
|
||||||
|
name = self.list_user(proc)
|
||||||
|
assert "karbit" in name
|
||||||
|
|
||||||
|
self.login(proc, "karbit")
|
||||||
|
self.bini(proc, 1, "waguri")
|
||||||
|
self.bini(proc, 2, "0")
|
||||||
|
waguri = self._read_until(proc, "0. edit", timeout=1.)
|
||||||
|
assert "waguri" in waguri
|
||||||
|
|
||||||
|
self.bini(proc, 3, "0")
|
||||||
|
self.logout(proc)
|
||||||
|
self.exit(proc)
|
||||||
|
|
||||||
|
try:
|
||||||
|
proc.wait(timeout=2.0)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
proc.kill()
|
||||||
|
raise AssertionError("Program did not exit after option 3")
|
||||||
|
self.logger.info("[ok] service exit on 3")
|
||||||
|
|
||||||
|
proc_alarm = self._spawn()
|
||||||
|
self._read_until(proc_alarm, "zzz", timeout=190.0) # 180s + slack
|
||||||
|
try:
|
||||||
|
proc_alarm.wait(timeout=5.0)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
proc_alarm.kill()
|
||||||
|
raise AssertionError("Alarm fired but process did not exit")
|
||||||
|
self.logger.info("[ok] alarm fired ('zzz') and process self-terminated")
|
||||||
|
|
||||||
|
self.logger.info('Check passed for carbeat')
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.error(f'Could not check carbeat: {e}')
|
||||||
|
return False
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
from .Challenge import Challenge
|
|
||||||
|
|
||||||
import io
|
|
||||||
import requests
|
|
||||||
import random
|
|
||||||
import subprocess
|
|
||||||
import requests
|
|
||||||
import re
|
|
||||||
|
|
||||||
class Naraka(Challenge):
|
|
||||||
flag_location = 'flags/naraka.txt'
|
|
||||||
history_location = 'history/naraka.txt'
|
|
||||||
|
|
||||||
def distribute(self, flag):
|
|
||||||
try:
|
|
||||||
with open(self.flag_location, 'w') as f:
|
|
||||||
f.write(flag)
|
|
||||||
|
|
||||||
with open(self.history_location, 'a') as f:
|
|
||||||
f.write(flag + '\n')
|
|
||||||
|
|
||||||
self.logger.info(f'Flag {flag} written to {self.flag_location}')
|
|
||||||
return True
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
self.logger.error(f'Could not write flag to {self.flag_location}: {e}')
|
|
||||||
return False
|
|
||||||
|
|
||||||
def check(self):
|
|
||||||
random_name = [
|
|
||||||
"nael", "mufat", "rayhan", "wicak", "Kingggg",
|
|
||||||
"osdie", "udin", "memet", "entong", "krabby",
|
|
||||||
"mew", "charizard", "pidgey", "rattata", "vulpix",
|
|
||||||
"eevee", "snorlax", "magikarp", "growlithe", "abra"
|
|
||||||
]
|
|
||||||
|
|
||||||
math_expression = "+-*"
|
|
||||||
|
|
||||||
python_programs = [
|
|
||||||
"a = 10;b = 5;print(a+b)", "a = 'add'; b='string';print(a+b)","x = [1,2,3,4,5,6,7,8];print(max(x))"
|
|
||||||
]
|
|
||||||
|
|
||||||
programs_result = ["b'15","b'addstring","b'8"]
|
|
||||||
try:
|
|
||||||
# Step 1: Chekc Greetings return name
|
|
||||||
name = random.choice(random_name)
|
|
||||||
url = f'http://localhost:{self.port}/render?name={name}'
|
|
||||||
r = requests.get(url, timeout=5)
|
|
||||||
assert name in r.text, 'Greetings is not greetings my name :('
|
|
||||||
|
|
||||||
# Step 2: Check eval calculator using random math expression
|
|
||||||
math = f'{random.randint(1,100)}{random.choice(math_expression)}{random.randint(1,100)}'
|
|
||||||
math_result = eval(math)
|
|
||||||
data = {'chall': '2','input':math}
|
|
||||||
url = f'http://localhost:{self.port}/chall'
|
|
||||||
r = requests.post(url, data=data, timeout=5)
|
|
||||||
assert str(math_result) in r.text, 'Eval calculator is not working :('
|
|
||||||
|
|
||||||
# Step 3: Check exec that execute python program
|
|
||||||
ind = random.randint(0,2)
|
|
||||||
pythononeline = python_programs[ind]
|
|
||||||
pythonresult = programs_result[ind]
|
|
||||||
data = {'chall': '1','input':pythononeline}
|
|
||||||
url = f'http://localhost:{self.port}/chall'
|
|
||||||
r = requests.post(url, data=data, timeout=5)
|
|
||||||
assert pythonresult in r.text, 'My code line is not execute :('
|
|
||||||
|
|
||||||
# Step 4: Check execute FLAG declaration execute
|
|
||||||
url = f'http://localhost:{self.port}/sourcecode/1'
|
|
||||||
r = requests.get(url, timeout=5)
|
|
||||||
flag_assignments = re.findall(r'^\s*FLAG\s*=\s*sys\.argv\[2\]\s*$', r.text, re.MULTILINE)
|
|
||||||
flag_assignments_num = flag_assignments = re.findall(r'^\s*FLAG\s*=\s*.+$', r.text, re.MULTILINE)
|
|
||||||
assert flag_assignments != 1 or flag_assignments_num != 1, "execute FLAG declared incorrectly"
|
|
||||||
|
|
||||||
# Step 5: Check evaluate FLAG declaration execute
|
|
||||||
url = f'http://localhost:{self.port}/sourcecode/1'
|
|
||||||
r = requests.get(url, timeout=5)
|
|
||||||
flag_assignments = re.findall(r'^\s*FLAG\s*=\s*sys\.argv\[2\]\s*$', r.text, re.MULTILINE)
|
|
||||||
flag_assignments_num = flag_assignments = re.findall(r'^\s*FLAG\s*=\s*.+$', r.text, re.MULTILINE)
|
|
||||||
assert flag_assignments != 1 or flag_assignments_num != 1, "evaluate FLAG declared incorrectly"
|
|
||||||
|
|
||||||
# Step 6: Check flag
|
|
||||||
with open(self.flag_location, 'r') as f:
|
|
||||||
host_flag = f.read().strip()
|
|
||||||
|
|
||||||
container_flag = subprocess.run(
|
|
||||||
["docker", "exec", "naraka_container", "cat", "/flag.txt"],
|
|
||||||
capture_output=True,
|
|
||||||
text=True
|
|
||||||
).stdout.strip()
|
|
||||||
|
|
||||||
assert host_flag == container_flag, 'Flag mismatch between host and container'
|
|
||||||
|
|
||||||
self.logger.info('Check passed for naraka')
|
|
||||||
return True
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
self.logger.error(f'Could not check naraka: {e}')
|
|
||||||
return False
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
GEMASTIK18{PLACEHOLDER}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
GEMASTIK18{PLACEHOLDER}
|
||||||
@@ -4,7 +4,7 @@ FROM ubuntu:24.04
|
|||||||
# Build-time args
|
# Build-time args
|
||||||
ARG PASSWORD=root
|
ARG PASSWORD=root
|
||||||
ENV DEBIAN_FRONTEND=noninteractive
|
ENV DEBIAN_FRONTEND=noninteractive
|
||||||
WORKDIR /home/ctf/chall
|
WORKDIR /home/ctfuser/chall
|
||||||
|
|
||||||
RUN apt-get update && \
|
RUN apt-get update && \
|
||||||
apt-get install -y --no-install-recommends \
|
apt-get install -y --no-install-recommends \
|
||||||
@@ -18,15 +18,15 @@ RUN apt-get update && \
|
|||||||
vim \
|
vim \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
RUN useradd -m -d /home/ctf -s /bin/bash ctf && echo "ctf:${PASSWORD}" | chpasswd
|
RUN useradd -m -d /home/ctfuser -s /bin/bash ctfuser && echo "ctfuser:${PASSWORD}" | chpasswd
|
||||||
|
|
||||||
RUN mkdir -p /var/run/sshd
|
RUN mkdir -p /var/run/sshd
|
||||||
|
|
||||||
COPY chall/ /home/ctf/chall
|
COPY chall/ /home/ctfuser/chall
|
||||||
COPY ./entrypoint.sh /entrypoint.sh
|
COPY ./entrypoint.sh /entrypoint.sh
|
||||||
|
|
||||||
RUN chmod +x /entrypoint.sh /home/ctf/chall/run.sh && \
|
RUN chmod +x /entrypoint.sh /home/ctfuser/chall/run.sh && \
|
||||||
chown -R root:root /home/ctf/chall && chmod -R 555 /home/ctf/chall
|
chown -R root:root /home/ctfuser/chall && chmod -R 555 /home/ctfuser/chall
|
||||||
|
|
||||||
EXPOSE 9000 22
|
EXPOSE 9000 22
|
||||||
CMD ["/entrypoint.sh"]
|
CMD ["/entrypoint.sh"]
|
||||||
@@ -11,7 +11,7 @@ grep -q "^PasswordAuthentication" /etc/ssh/sshd_config && \
|
|||||||
sed -i "s/^PasswordAuthentication.*/PasswordAuthentication yes/" /etc/ssh/sshd_config || \
|
sed -i "s/^PasswordAuthentication.*/PasswordAuthentication yes/" /etc/ssh/sshd_config || \
|
||||||
echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
|
echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
|
||||||
|
|
||||||
echo "AllowUsers ctf" >> /etc/ssh/sshd_config
|
echo "AllowUsers ctfuser" >> /etc/ssh/sshd_config
|
||||||
|
|
||||||
/usr/sbin/sshd
|
/usr/sbin/sshd
|
||||||
|
|
||||||
@@ -21,4 +21,4 @@ if [ -n "$FLAG" ]; then
|
|||||||
chown root:root /flag.txt
|
chown root:root /flag.txt
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exec su -c "cd /home/ctf/chall && ./run.sh" -s /bin/bash ctf
|
exec su -c "cd /home/ctfuser/chall && ./run.sh" -s /bin/bash ctfuser
|
||||||
@@ -2,8 +2,8 @@ FROM python:3.12-slim
|
|||||||
|
|
||||||
ARG PASSWORD=root
|
ARG PASSWORD=root
|
||||||
ENV DEBIAN_FRONTEND=noninteractive
|
ENV DEBIAN_FRONTEND=noninteractive
|
||||||
ENV HOME=/home/ctf
|
ENV HOME=/home/ctfuser
|
||||||
WORKDIR /home/ctf/chall
|
WORKDIR /home/ctfuser/chall
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
openssh-server \
|
openssh-server \
|
||||||
@@ -11,22 +11,23 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|||||||
libffi-dev \
|
libffi-dev \
|
||||||
libssl-dev \
|
libssl-dev \
|
||||||
python3-dev \
|
python3-dev \
|
||||||
|
socat \
|
||||||
bash \
|
bash \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
RUN useradd -m -d /home/ctf -s /bin/bash ctf && \
|
RUN useradd -m -d /home/ctfuser -s /bin/bash ctfuser && \
|
||||||
echo "ctf:${PASSWORD}" | chpasswd
|
echo "ctfuser:${PASSWORD}" | chpasswd
|
||||||
|
|
||||||
RUN mkdir -p /var/run/sshd
|
RUN mkdir -p /var/run/sshd
|
||||||
|
|
||||||
COPY requirements.txt /tmp/requirements.txt
|
COPY requirements.txt /tmp/requirements.txt
|
||||||
RUN pip install --no-cache-dir -r /tmp/requirements.txt
|
RUN pip install --no-cache-dir -r /tmp/requirements.txt
|
||||||
|
|
||||||
COPY ./src /home/ctf/chall/src
|
COPY ./src /home/ctfuser/chall/src
|
||||||
COPY ./start.sh /start.sh
|
COPY ./start.sh /start.sh
|
||||||
RUN chmod +x /start.sh /home/ctf/chall/src/run.sh
|
RUN chmod +x /start.sh /home/ctfuser/chall/src/run.sh
|
||||||
|
|
||||||
RUN chown -R root:root /home/ctf/chall && chmod -R 555 /home/ctf/chall
|
RUN chown -R root:root /home/ctfuser/chall && chmod -R 555 /home/ctfuser/chall
|
||||||
|
|
||||||
EXPOSE 8000 22
|
EXPOSE 8000 22
|
||||||
CMD ["/start.sh"]
|
CMD ["/start.sh"]
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -12,7 +12,7 @@ grep -q "^PasswordAuthentication" /etc/ssh/sshd_config && \
|
|||||||
sed -i "s/^PasswordAuthentication.*/PasswordAuthentication yes/" /etc/ssh/sshd_config || \
|
sed -i "s/^PasswordAuthentication.*/PasswordAuthentication yes/" /etc/ssh/sshd_config || \
|
||||||
echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
|
echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config
|
||||||
|
|
||||||
echo "AllowUsers ctf" >> /etc/ssh/sshd_config
|
echo "AllowUsers ctfuser" >> /etc/ssh/sshd_config
|
||||||
|
|
||||||
/usr/sbin/sshd
|
/usr/sbin/sshd
|
||||||
|
|
||||||
@@ -22,4 +22,4 @@ if [ -n "$FLAG" ]; then
|
|||||||
chown root:root /flag.txt
|
chown root:root /flag.txt
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exec su -c "cd /home/ctf/chall/src && ./run.sh" -s /bin/bash ctf
|
exec su -c "cd /home/ctfuser/chall/src && ./run.sh" -s /bin/bash ctfuser
|
||||||
@@ -11,6 +11,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|||||||
libffi-dev \
|
libffi-dev \
|
||||||
libssl-dev \
|
libssl-dev \
|
||||||
python3-dev \
|
python3-dev \
|
||||||
|
socat \
|
||||||
bash \
|
bash \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
|||||||
File renamed without changes.
File renamed without changes.
+6
-6
@@ -35,12 +35,12 @@ def main():
|
|||||||
os.chdir(os.path.join(cwd, 'services'))
|
os.chdir(os.path.join(cwd, 'services'))
|
||||||
os.system(f'docker compose -f docker-compose.yml up --build -d {args.challenges}')
|
os.system(f'docker compose -f docker-compose.yml up --build -d {args.challenges}')
|
||||||
|
|
||||||
os.chdir(os.path.join(cwd, 'receiver'))
|
# os.chdir(os.path.join(cwd, 'receiver'))
|
||||||
os.system('apt-get install -y gcc python3-dev libgmp3-dev libssl-dev libffi-dev build-essential python3-venv')
|
# os.system('apt-get install -y gcc python3-dev libgmp3-dev libssl-dev libffi-dev build-essential python3-venv')
|
||||||
os.system('python3 -m venv .')
|
# os.system('python3 -m venv .')
|
||||||
os.system('sudo ./bin/activate')
|
# os.system('sudo ./bin/activate')
|
||||||
os.system('sudo ./bin/python3 -m pip install -r requirements.txt')
|
# os.system('sudo ./bin/python3 -m pip install -r requirements.txt')
|
||||||
os.system('sudo ./bin/python3 -m uvicorn main:app --reload --host 0.0.0.0 --port 80')
|
# os.system('sudo ./bin/python3 -m uvicorn main:app --reload --host 0.0.0.0 --port 80')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
Reference in new issue
Block a user