Skip to content

Commit edaa62e

Browse files
committed
first commit
0 parents  commit edaa62e

File tree

8 files changed

+278
-0
lines changed

8 files changed

+278
-0
lines changed

README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# pwn_deploy_chroot
2+
3+
> A project for deploying ctf pwn challenge use chroot
4+
5+
中文请点击:
6+
7+
[README_CN.md](https://github.com/giantbranch/pwn_deploy_chroot/blob/master/README_CN.md)
8+
9+
## Before
10+
11+
```
12+
# Install the latest version docker
13+
curl -s https://get.docker.com/ | sh
14+
# Install docker compose
15+
apt install docker-compose
16+
```
17+
18+
## Configuration
19+
20+
Put your pwn bin to ./bin (**Note that the filename should not contain special characters.**
21+
22+
Listen port start from 10000, you can change in config.py
23+
24+
## Run
25+
26+
```
27+
python initialize.py
28+
# please run as root
29+
docker-compose up --build -d
30+
```
31+
32+
## Attention
33+
34+
The flag will be generated by the initialize.py and it store in flags.txt
35+
36+
The port information corresponding to the pwn program is also inside flags.txt.
37+
38+
## Reference
39+
40+
https://github.com/Eadom/ctf_xinetd

README_CN.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# pwn_deploy_chroot
2+
3+
> 可以方便地部署多个pwn题到一个docker容器中(使用chroot)
4+
5+
## 前置
6+
7+
```
8+
# 安装 docker
9+
curl -s https://get.docker.com/ | sh
10+
# 安装 docker-compose
11+
apt install docker-compose
12+
```
13+
14+
## 配置
15+
16+
将你的pwn二进制程序放到`./bin` 目录(注意文件名不要含有特殊字符,因为后面会这个文件名创建用户名)
17+
18+
监听端口从10000开始,每多一个pwn就加1,你可以在`config.py`中修改起始监听端口
19+
20+
## 启动
21+
22+
```
23+
python initialize.py
24+
# 请用root用户启动
25+
docker-compose up --build -d
26+
```
27+
28+
## 注意
29+
30+
flag会由`initialize.py`生成,并写入flags.txt中,并且pwn程序对应的端口信息也在里面
31+
32+
## 参考
33+
34+
https://github.com/Eadom/ctf_xinetd
35+
36+
37+
38+

bin/pwn1

5.46 KB
Binary file not shown.

bin/pwn1_copy1

5.46 KB
Binary file not shown.

bin/pwn1_copy2

5.46 KB
Binary file not shown.

config.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2018-09-17 14:01:13
4+
# @Author : giantbranch ([email protected])
5+
# @Link : http://www.giantbranch.cn/
6+
# @tags :
7+
8+
FLAG_BAK_FILENAME = "flags.txt"
9+
PWN_BIN_PATH = "./bin"
10+
XINETD_CONF_FILENAME = "pwn.xinetd"
11+
PORT_LISTEN_START_FROM = 10000
12+
13+
XINETD = '''service ctf
14+
{
15+
disable = no
16+
socket_type = stream
17+
protocol = tcp
18+
wait = no
19+
user = root
20+
type = UNLISTED
21+
port = %d
22+
bind = 0.0.0.0
23+
server = /usr/sbin/chroot
24+
server_args = --userspec=%s /home/%s ./%s
25+
# safety options
26+
per_source = 10 # the maximum instances of this service per source IP address
27+
rlimit_cpu = 20 # the maximum number of CPU seconds that the service may use
28+
rlimit_as = 100M # the Address Space resource limit for the service
29+
#access_times = 2:00-9:00 12:00-24:00
30+
}
31+
32+
'''
33+
34+
DOCKERFILE = '''FROM ubuntu:16.04
35+
36+
RUN sed -i 's/archive.ubuntu.com/asia-east1.gce.archive.ubuntu.com/g' /etc/apt/sources.list && apt update && apt-get install -y lib32z1 xinetd && rm -rf /var/lib/apt/lists/ && rm -rf /root/.cache && apt-get autoclean && rm -rf /tmp/* /var/lib/apt/* /var/cache/* /var/log/*
37+
#apt update && apt-get install -y lib32z1 xinetd && rm -rf /var/lib/apt/lists/ && rm -rf /root/.cache && apt-get autoclean && rm -rf /tmp/* /var/lib/apt/* /var/cache/* /var/log/*
38+
39+
COPY ./'''+ XINETD_CONF_FILENAME +''' /etc/xinetd.d/pwn
40+
41+
COPY ./service.sh /service.sh
42+
43+
RUN chmod +x /service.sh
44+
45+
# useradd and put flag
46+
%s
47+
48+
# copy bin
49+
%s
50+
51+
# chown & chmod
52+
%s
53+
54+
# copy lib,/bin
55+
%s
56+
57+
CMD ["/service.sh"]
58+
'''
59+
60+
DOCKERCOMPOSE = '''version: '2'
61+
services:
62+
pwn_deploy_chroot:
63+
image: pwn_deploy_chroot:latest
64+
build: .
65+
container_name: pwn_deploy_chroot
66+
ports:
67+
%s
68+
'''
69+

initialize.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# @Date : 2018-09-17 14:32:32
4+
# @Author : giantbranch ([email protected])
5+
# @Link : http://www.giantbranch.cn/
6+
# @tags :
7+
8+
from config import *
9+
import os
10+
import uuid
11+
12+
def getFileList():
13+
filelist = []
14+
for filename in os.listdir(PWN_BIN_PATH):
15+
filelist.append(filename)
16+
filelist.sort()
17+
return filelist
18+
19+
def generateFlags(filelist):
20+
tmp = ""
21+
flags = []
22+
if os.path.exists(FLAG_BAK_FILENAME):
23+
os.remove(FLAG_BAK_FILENAME)
24+
with open(FLAG_BAK_FILENAME, 'a') as f:
25+
for filename in filelist:
26+
tmp = "flag{" + str(uuid.uuid4()) + "}"
27+
f.write(filename + ": " + tmp + "\n")
28+
flags.append(tmp)
29+
return flags
30+
31+
def generateXinetd(filelist):
32+
port = PORT_LISTEN_START_FROM
33+
conf = ""
34+
uid = 1000
35+
for filename in filelist:
36+
conf += XINETD % (port, str(uid) + ":" + str(uid), filename, filename)
37+
port = port + 1
38+
uid = uid + 1
39+
with open(XINETD_CONF_FILENAME, 'w') as f:
40+
f.write(conf)
41+
42+
def generateDockerfile(filelist, flags):
43+
conf = ""
44+
# useradd and put flag
45+
runcmd = "RUN "
46+
47+
for filename in filelist:
48+
runcmd += "useradd -m " + filename + " && "
49+
50+
for x in xrange(0, len(filelist)):
51+
if x == len(filelist) - 1:
52+
runcmd += "echo '" + flags[x] + "' > /home/" + filelist[x] + "/flag.txt"
53+
else:
54+
runcmd += "echo '" + flags[x] + "' > /home/" + filelist[x] + "/flag.txt" + " && "
55+
# print runcmd
56+
57+
# copy bin
58+
copybin = ""
59+
for filename in filelist:
60+
copybin += "COPY " + PWN_BIN_PATH + "/" + filename + " /home/" + filename + "/" + filename + "\n"
61+
# print copybin
62+
63+
# chown & chmod
64+
chown_chmod = "RUN "
65+
for x in xrange(0, len(filelist)):
66+
chown_chmod += "chown -R root:" + filelist[x] + " /home/" + filelist[x] + " && "
67+
chown_chmod += "chmod -R 750 /home/" + filelist[x] + " && "
68+
if x == len(filelist) - 1:
69+
chown_chmod += "chmod 740 /home/" + filelist[x] + "/flag.txt"
70+
else:
71+
chown_chmod += "chmod 740 /home/" + filelist[x] + "/flag.txt" + " && "
72+
# print chown_chmod
73+
74+
# copy lib,/bin
75+
dev = '''mkdir /home/%s/dev && mknod /home/%s/dev/null c 1 3 && mknod /home/%s/dev/zero c 1 5 && mknod /home/%s/dev/random c 1 8 && mknod /home/%s/dev/urandom c 1 9 && chmod 666 /home/%s/dev/* && '''
76+
ness_bin = '''mkdir /home/%s/bin && cp /bin/sh /home/%s/bin && cp /bin/ls /home/%s/bin && cp /bin/cat /home/%s/bin'''
77+
copy_lib_bin_dev = "RUN "
78+
for x in xrange(0, len(filelist)):
79+
copy_lib_bin_dev += "cp -R /lib* /home/" + filelist[x] + " && "
80+
copy_lib_bin_dev += dev % (filelist[x], filelist[x], filelist[x], filelist[x], filelist[x], filelist[x])
81+
if x == len(filelist) - 1:
82+
copy_lib_bin_dev += ness_bin % (filelist[x], filelist[x], filelist[x], filelist[x])
83+
else:
84+
copy_lib_bin_dev += ness_bin % (filelist[x], filelist[x], filelist[x], filelist[x]) + " && "
85+
86+
# print copy_lib_bin_dev
87+
88+
conf = DOCKERFILE % (runcmd, copybin, chown_chmod, copy_lib_bin_dev)
89+
90+
with open("Dockerfile", 'w') as f:
91+
f.write(conf)
92+
93+
def generateDockerCompose(length):
94+
conf = ""
95+
ports = ""
96+
port = PORT_LISTEN_START_FROM
97+
for x in xrange(0,length):
98+
ports += "- " + str(port) + ":" + str(port) + "\n "
99+
port = port + 1
100+
101+
conf = DOCKERCOMPOSE % ports
102+
# print conf
103+
with open("docker-compose.yml", 'w') as f:
104+
f.write(conf)
105+
106+
def generateBinPort(filelist):
107+
port = PORT_LISTEN_START_FROM
108+
tmp = "\n"
109+
for filename in filelist:
110+
tmp += filename + "'s port: " + str(port) + "\n"
111+
port = port + 1
112+
print tmp
113+
with open(FLAG_BAK_FILENAME, 'a') as f:
114+
f.write(tmp)
115+
116+
117+
filelist = getFileList()
118+
flags = generateFlags(filelist)
119+
generateBinPort(filelist)
120+
generateXinetd(filelist)
121+
generateDockerfile(filelist, flags)
122+
generateDockerCompose(len(filelist))
123+
124+
125+

service.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
# Add your startup script
3+
4+
# DO NOT DELETE
5+
/etc/init.d/xinetd start;
6+
sleep infinity;

0 commit comments

Comments
 (0)