此篇文章已经废弃,相关内容请见最新文章:CTF-nc-docker配置指南

缘起

为了之后为微软社的同学们演示一些CTF题目的解法,需要在自己电脑中部署几个可以用来进行简单nc交互的docker,记录一些这个过程中遇到的问题以及坑。

最开始是pull了hackergame的docker:hackergame-challenge-docker,但其实我并不需要那么复杂的功能,而且这个docker在windows下build会出现一些问题,比如js文件夹的链接等,于是我考虑自己写一个最简单的,不需要过多关心安全性和功能性,能跑就行(

在github上搜索相关的内容,我知道了xinetd这个服务可以利用,也看到了一些封装好的项目,比如ctf-xinetd,但依旧是build不成功。

于是我开始自己摸索,从一道题目开始,这道题目只需要当有连接时候利用python3运行program.py,这里其实我用的是build_yourself_in那道题目。

遇坑过程

先堆出来几个最开始的配置文件:

  • docker-compose.yml

    version: '2'
    services:
    challenge:
        build: .
        ports:
        - 65114:2333
        restart: always
  • xinetd

    service ctf
    {
        disable       = no
        socket_type   = stream
        server        = /usr/bin/python3
        server_args   = -u /home/ctf/program.py
        port          = 2333
        protocol      = tcp
        user          = root
        wait          = no
        flags         = NODELAY
        type          = UNLISTED
    }
  • Dockerfile

    FROM ubuntu:20.04
    
    RUN apt update && apt -y upgrade
    RUN apt install -y xinetd python3
    
    COPY xinetd /etc/xinetd.d/ctf
    
    RUN mkdir /home/ctf/
    
    COPY ./program.py /home/ctf/program.py
    COPY ./flag /home/ctf/flag
    
    CMD ["xinetd","-dontfork"]

Problem 1: Linux与Windows下文件行尾序列不一致

build成功之后无法运行,最开始考虑是不是docker-desktop的问题,但是种种迹象都表明端口的映射是没什么问题的。然后突然想起来可以用xinetd -d输出调试信息,来看看到底发生了什么,然后发现:

21/5/21@12:43:29: DEBUG: 13 {handle_includedir} Reading included configuration file: /etc/xinetd.d/ctf [file=/etc/xinetd.d/ctf] [line=67]
 [file=/etc/xinetd.d/ctf] [line=3]sable_parser} Bad value: no
21/5/21@12:43:29: ERROR: 13 {identify_attribute} Error parsing attribute disable - DISABLING SERVICE [file=/etc/xinetd.d/ctf] [line=3]
 [file=/etc/xinetd.d/ctf] [line=4]t_type_parser} Bad socket type: stream
21/5/21@12:43:29: ERROR: 13 {identify_attribute} Error parsing attribute socket_type - DISABLING SERVICE [file=/etc/xinetd.d/ctf] [line=4]
 is not executable [file=/etc/xinetd.d/ctf] [line=5]usr/bin/python3
21/5/21@12:43:29: ERROR: 13 {identify_attribute} Error parsing attribute server - DISABLING SERVICE [file=/etc/xinetd.d/ctf] [line=5]
 not in /etc/protocols [file=/etc/xinetd.d/ctf] [line=8]cp
21/5/21@12:43:29: ERROR: 13 {identify_attribute} Error parsing attribute protocol - DISABLING SERVICE [file=/etc/xinetd.d/ctf] [line=8]
 [file=/etc/xinetd.d/ctf] [line=9]parser} Unknown user: root
21/5/21@12:43:29: ERROR: 13 {identify_attribute} Error parsing attribute user - DISABLING SERVICE [file=/etc/xinetd.d/ctf] [line=9]
 [file=/etc/xinetd.d/ctf] [line=10]arser} Bad value for wait: no
21/5/21@12:43:29: ERROR: 13 {identify_attribute} Error parsing attribute wait - DISABLING SERVICE [file=/etc/xinetd.d/ctf] [line=10]
 [file=/etc/xinetd.d/ctf] [line=11]e_value_list} Bad service flag: NODELAY
21/5/21@12:43:29: ERROR: 13 {identify_attribute} Error parsing attribute flags - DISABLING SERVICE [file=/etc/xinetd.d/ctf] [line=11]
 [file=/etc/xinetd.d/ctf] [line=12]e_value_list} Bad service type: UNLISTED
21/5/21@12:43:29: ERROR: 13 {identify_attribute} Error parsing attribute type - DISABLING SERVICE [file=/etc/xinetd.d/ctf] [line=12]

为什么会读取失败呢,之后突然反应过来,linux下是LF行尾序列,而这里因为我在Windows下编辑,默认的行尾序列都是CRLF,因此造成了xinetd的读取错误。

解决方案:将xinetd配置文件行尾序列更改为LF

Problem 2: docker正常运行,但是无法正常访问

这种情况下多是端口绑定问题,包括有些时候ubuntu下的bind failed报错均可能由于此。

解决方案:换个映射端口

附录

相关文件和代码:CTF-nc-docker