-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize.py
188 lines (156 loc) · 5.56 KB
/
initialize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-09-17 14:32:32
# @Author : giantbranch ([email protected])
# @Link : http://www.giantbranch.cn/
# @tags :
from config import *
import os
import uuid
import json
def getFileList():
filelist = []
for filename in os.listdir(PWN_BIN_PATH):
filelist.append(filename)
filelist.sort()
return filelist
def isExistBeforeGetFlagAndPort(filename, contentBefore):
filename_tmp = ""
tmp_dict = ""
ret = False
for line in contentBefore:
tmp_dict = json.loads(line)
filename_tmp = tmp_dict["filename"]
if filename == filename_tmp:
ret = [tmp_dict["flag"], tmp_dict["port"]]
return ret
def generateFlags(filelist):
tmp_flag = ""
contentBefore = []
if not os.path.exists(FLAG_BAK_FILENAME):
os.popen("touch " + FLAG_BAK_FILENAME)
with open(FLAG_BAK_FILENAME, 'r') as f:
while 1:
line = f.readline()
if not line:
break
contentBefore.append(line)
# bin's num != flags.txt's linenum, empty the flags.txt
if len(filelist) != len(contentBefore):
os.popen("echo '' > " + FLAG_BAK_FILENAME)
contentBefore = []
port = PORT_LISTEN_START_FROM + len(contentBefore)
flags = []
with open(FLAG_BAK_FILENAME, 'w') as f:
for filename in filelist:
flag_dict = {}
ret = isExistBeforeGetFlagAndPort(filename, contentBefore)
if ret == False:
tmp_flag = "flag{" + str(uuid.uuid4()) + "}"
flag_dict["port"] = port
port = port + 1
else:
tmp_flag = ret[0]
flag_dict["port"] = ret[1]
flag_dict["filename"] = filename
flag_dict["flag"] = tmp_flag
flag_json = json.dumps(flag_dict)
print flag_json
f.write(flag_json + "\n")
flags.append(tmp_flag)
return flags
def generateXinetd(filename):
contentBefore = []
with open(FLAG_BAK_FILENAME, 'r') as f:
while 1:
line = f.readline()
if not line:
break
contentBefore.append(line)
uid = 1000
# for filename in filelist:
conf = ""
port = isExistBeforeGetFlagAndPort(filename, contentBefore)[1]
conf += XINETD % (port, str(uid) + ":" + str(uid), filename, filename)
# uid = uid + 1
with open("pwn-{}{}".format(filename,XINETD_CONF_FILENAME[3:]), 'w') as f:
f.write(conf)
def generateDockerfile(filename, flag):
conf = ""
# useradd and put flag
runcmd="""RUN useradd -m %s && \\
echo '%s' > /home/%s/flag
"""
runcmd = runcmd % (filename,flag,filename)
# print runcmd
# copy bin
copybin = ""
copybin += "COPY " + PWN_BIN_PATH + "/" + filename + " /home/" + filename + "/" + filename + "\n"
copybin += "COPY ./catflag" + " /home/" + filename + "/bin/sh\n"
# print copybin
# chown & chmod
chown_chmod="""RUN chown -R root:%s /home/%s && \\
chmod -R 750 /home/%s && \\
chmod 740 /home/%s/flag
"""
chown_chmod = chown_chmod%(filename,filename,filename,filename)
# print chown_chmod
# copy lib,/bin
# 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/* && '''
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/* '''
if REPLACE_BINSH:
# ness_bin = '''mkdir /home/%s/bin && cp /bin/sh /home/%s/bin && cp /bin/ls /home/%s/bin && cp /bin/cat /home/%s/bin'''
ness_bin = '''&& \\
cp /bin/sh /home/%s/bin && \\
cp /bin/ls /home/%s/bin && \\
cp /bin/cat /home/%s/bin'''
# print ness_bin
copy_lib_bin_dev="""RUN cp -R /lib* /home/%s && cp -R /usr/lib* /home/%s && \\"""
copy_lib_bin_dev=copy_lib_bin_dev % (filename,filename)
copy_lib_bin_dev += dev % (filename, filename, filename, filename, filename, filename)
if REPLACE_BINSH:
copy_lib_bin_dev += ness_bin % (filename, filename, filename)
pass
# print copy_lib_bin_dev
conf = DOCKERFILE % (filename,runcmd, copybin, chown_chmod, copy_lib_bin_dev)
with open('pwn-{}.Dockerfile'.format(filename), 'w') as f:
f.write(conf)
def generateDockerCompose(filelist,length):
conf = '''version: '2'
services:
'''
for filename,index in zip(filelist,range(length)):
# print filename,index
ports = ""
port = PORT_LISTEN_START_FROM +index
ports += "- " + str(port) + ":" + str(port) + "\n"
conf += DOCKERCOMPOSE[23:] % (filename,filename,filename,filename,ports)
# print conf
with open("docker-compose.yml", 'w') as f:
f.write(conf)
# def generateBinPort(filelist):
# port = PORT_LISTEN_START_FROM
# tmp = ""
# for filename in filelist:
# tmp += filename + "'s port: " + str(port) + "\n"
# port = port + 1
# print tmp
# with open(PORT_INFO_FILENAME, 'w') as f:
# f.write(tmp)
filelist = getFileList()
flags = generateFlags(filelist)
# print flags
# generateBinPort(filelist)
for filename in filelist:
generateXinetd(filename)
for filename,flag in zip(filelist,flags):
# print flag
generateDockerfile(filename, flag)
# generateDockerfile(filelist[0], flags)
generateDockerCompose(filelist,len(filelist))