This service is related to part two, so we won’t talk much about it in this write-up. The important part here is knowing that this service is running in backend:29092 and is not accessible to the host, at least from the information we have right now.
Let’s see the other container, docker-compose-chal.yml.
# CHAL_PORT=13337 docker-compose -f ./docker-compose-chal.yml -p flag_market_13337 up -d
Exposes port 19091 to the host and links it to the port passed in the ENV variable CHAL_PORT which will be 13337 if we choose so or run the deploy.sh script.
The file flag_market.Dockerfile will show it’s copying an elf executable and moving it to /home/flag_market and running a sh script named run.sh:
FROM ubuntu:20.04 MAINTAINER how2hack RUN apt-get update --fix-missing RUN apt-get upgrade -y RUN apt-get install -y xinetd RUN DEBIAN_FRONTEND=noninteractive apt-get install -y git libtool pkg-config make python3 python3-pip help2man RUN pip install -U pip pycrypto RUN useradd -m flag_market WORKDIR /home/flag_market RUN git clone https://github.com/frankmorgner/vsmartcard.git WORKDIR /home/flag_market/vsmartcard RUN git checkout 8b4aa3e7bfe891d986237759576b5ebf0e4ed42b COPY src/patch.diff /home/flag_market/vsmartcard/ RUN git apply patch.diff WORKDIR /home/flag_market/vsmartcard/virtualsmartcard RUN autoreconf --verbose --install RUN ./configure --sysconfdir=/etc --enable-libpcsclite RUN make RUN make install COPY src/flag_market /home/flag_market/ COPY src/run.sh /home/flag_market/ COPY src/flag3 /home/flag_market/ RUN chmod 774 /tmp RUN chmod -R 774 /var/tmp RUN chmod -R 774 /dev RUN chmod -R 774 /run RUN chmod 1733 /tmp /var/tmp /dev/shm RUN chown -R root:root /home/flag_market USER flag_market CMD ["/home/flag_market/run.sh"]
The src/run.sh file will start the ELF while preloading a special library:
# CHAL_PORT=13337 docker-compose -f ./docker-compose-chal.yml -p flag_market_13337 up -d
We could now either modify the Docker files to start the gdbserver automatically after the binary is run, or run commands after the instance is running.
I didn’t want to break anything or make the server slightly different from the server version, so to save time, after setting up the servers with ./deploy.sh, I just ran the following commands to install gdb:
1 2 3 4 5
$ sudo docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7e3450cc8dad flag_market_flag_market "/home/flag_market/r…" 10 hours ago Up 10 minutes 0.0.0.0:1337->1337/tcp, :::1337->1337/tcp, 0.0.0.0:13337->19091/tcp, :::13337->19091/tcp flag_market_flag_market_1 5ab9319711a0 flag_market_backend "/bin/sh -c '/usr/sb…" 2 days ago Up 2 days $ sudo docker exec -it --workdir /root --user root flag_market_flag_market_1 sh -c "apt update && apt install gdbserver"
After this we can attach the gdbserver with:
1 2 3 4 5 6 7 8 9 10
$ sudo docker exec -it flag_market_flag_market_1 ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND flag_ma+ 1 0.0 0.0 3984 2812 ? Ss 05:17 0:00 /bin/bash /ho flag_ma+ 7 0.0 0.0 2748 652 ? S 05:17 0:00 timeout 1800 flag_ma+ 8 0.0 0.0 2416 536 ? S 05:17 0:00 /home/flag_ma flag_ma+ 15 0.0 0.0 5900 2888 pts/0 Rs+ 05:29 0:00 ps -aux $ sudo docker exec -it flag_market_flag_market_1 \ sh -c "gdbserver :1337 --attach \$(ps -aux | grep ':00 /home/flag_market/flag_market' | head -n 1 | awk '{print \$2}')" Attached; pid = 8 Listening on port 1337
To attach with gdb from the host we can do this:
1 2 3
pwndbg> target remote :1337 ... pwndbg> n
Exploit
Since we don’t care right now about the flask server, ideally we would love to make the binary connect to the xinetd service to get the flag1. But to achieve this, we need to use an overflow.
We can find one in the sscanf:
1
n = sscanf(request, "%s /%s HTTP/1.1", method, path);
To overflow the port, we need to find the offset to the variable. One of the methods we could use is just trial and error (quite slow), but in my case I chose to use De Bruijn patterns:
The offset needed is 768 so we can do a oneliner to get the flag in the server (the port needs to be in this format, 31331 as a string due to the fact the binary uses atoi):