-->
当前位置:首页 > DayDayUp > 正文内容

pwn题目批量部署脚本(一题一容器)

Luz4年前 (2019-12-18)DayDayUp3915

一题一个docker,优点是便于单个题目的维护,缺点比较占用资源

多题目部署在一个docker的见这里,但是为了避免权限过高带来的跨题目读取flag等问题,这个大佬锁死了可执行的命令,以至于部分题目直接使用"cat flag"等命令直接报错

#coding:utf-8
# by : Luz
import os
import sys
import random
import time
def creatflag():
 random.seed(time.time())
 fl='flag{'
 for j in range(32):
  fl=fl+chr(random.randint(0,25)+ord('a'))
 fl=fl+'}'
 return fl
print(sys.path[0])
po=open(sys.path[0]+'/port')
port=int(po.readline(),10)
print port
po.close()
PWN_path=sys.path[0]+'/pwn/'
pwn_names=[name for name in os.listdir(PWN_path)]
flag=""
print pwn_names
for i in pwn_names:
 port=port+1
 os.system("mkdir "+sys.path[0]+'/'+i)
 flag=creatflag()
 print flag
 #fd = open(sys.path[0]+'/'+i+'/'+'flag', mode="w")
 #fd.write(str(flag))
 os.system("echo "+flag+" > "+sys.path[0]+'/'+str(i)+'/'+'flag')
 #fd.close()
 os.system("cp "+sys.path[0]+'/pwn/'+i+' '+sys.path[0]+'/'+i+'/'+'pwn')#copy the challenge to the challenge document and set the challname to pwn ,then copy the dockerfile and ctf.xinetd to the challenge document
 os.system("cp "+sys.path[0]+'/docker/ctf.xinetd'+' '+sys.path[0]+'/'+i+'/ctf.xinetd')
 os.system("cp "+sys.path[0]+'/docker/Dockerfile'+' '+sys.path[0]+'/'+i+'/Dockerfile')
 os.system("cd "+sys.path[0]+'/'+str(i)+'/'+' '+'&& '+"docker build -t "+str(i)+'auto .')#build docker 
 os.system("cd "+sys.path[0]+'/'+str(i)+'/'+' '+'&& '+"docker run -p "+str(port)+':8888 -v /data:/data -d '+i+'auto')#run docker
 print i+' '+'port:'+str(port)
 ag=open(sys.path[0]+'/flag',mode="a")
 ag.write(i+' '+'port:'+str(port)+'flag: '+flag +' \n')
 os.system("mkdir "+sys.path[0]+'/output/'+i)
 os.system("cp "+sys.path[0]+'/pwn/'+i+'  '+sys.path[0]+'/output/'+i)
 wenjian=open(sys.path[0]+'/output/'+i+'/meta.txt',mode="w")
 wenjian.write('<message>'+'nc '+'www.hutc.xyz :'+str(port)+' \n'+'<flag>'+flag)#creat meta.txt
 os.system("rm -f "+sys.path[0]+"/pwn/"+i)#delete the challenge if it have been setted
 os.system("rm -rf "+sys.path[0]+'/'+i)#delete the docker document
po=po=open(sys.path[0]+'/port',"w")
po.write(str(port))


依赖两个文件:


Dockerfile 文件:


FROM ubuntu:16.04
RUN sed -i "s/http:\/\/archive.ubuntu.com/http:\/\/mirrors.aliyun.com/g" /etc/apt/sources.list
RUN apt-get update && apt-get -y dist-upgrade
RUN apt-get install -y lib32z1 xinetd build-essential ssh
RUN useradd -m ctf
COPY ./flag /flag
COPY ./pwn /pwn/pwn
COPY ./ctf.xinetd /etc/xinetd.d/ctf
RUN chown root:ctf /pwn/pwn && chmod 750 /pwn/pwn
RUN echo 'ctf - nproc 1500' >>/etc/security/limits.conf
CMD exec /bin/bash -c "/etc/init.d/xinetd start; trap : TERM INT; sleep infinity & wait"
EXPOSE 8888

ctf.xinetd 文件:


service ctf
{
    disable = no
    socket_type = stream
    protocol    = tcp
    wait        = no
    user        = root
    type        = UNLISTED
    port        = 8888
    bind        = 0.0.0.0
    server      = /usr/sbin/chroot
    server_args = --userspec=1000:1000 / timeout 50 ./pwn/pwn
    banner_fail = /etc/banner_fail
    # safety options
    per_source   = 10 # the maximum instances of this service per source IP address
    rlimit_cpu   = 120 # the maximum number of CPU seconds that the service may use
    rlimit_as     = 1024M # the Address Space resource limit for the service
    #access_times = 2:00-9:00 12:00-24:00
    #Instances   = 20 #process limit
    #per_source  = 5 #link ip limit
    #log warning die
    log_on_success  = PID HOST EXIT DURATION
    log_on_failure  = HOST ATTEMPT 
    log_type =FILE /var/log/myservice.log 8388608 15728640
}


在Ubuntu16.04测试通过

把题目放置于pwn文件夹中,两个依赖文件放置在docker文件夹中

自动生成flag,docker开启后自动删除pwn文件夹中的题目文件

在output文件夹中生成题目文件夹放置题目、题目的mete.txt(包括题目的地址、题目的flag,使用标签隔开方便使用正则表达式做匹配)

自动删除创建题目docker使用的临时文件夹

自动记录已经部署过题目的端口,存放在port文件中,下次运行自动读入上次部署结束的端口,避免出现端口占用

每次部署至于要把需要新增的题目放置在pwn文件夹中





发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。