-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
initialize.py
182 lines (158 loc) · 6.14 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2018-09-17 14:32:32
# @Author : giantbranch (giantbranch@gmail.com)
# @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(filelist):
contentBefore = []
with open(FLAG_BAK_FILENAME, 'r') as f:
while 1:
line = f.readline()
if not line:
break
contentBefore.append(line)
conf = ""
uid = 1000
for filename in filelist:
port = isExistBeforeGetFlagAndPort(filename, contentBefore)[1]
conf += XINETD % (port, str(uid) + ":" + str(uid), filename, filename)
uid = uid + 1
with open(XINETD_CONF_FILENAME, 'w') as f:
f.write(conf)
def generateDockerfile(filelist, flags):
conf = ""
# useradd and put flag
runcmd = "RUN "
for filename in filelist:
runcmd += "useradd -m " + filename + " && "
for x in xrange(0, len(filelist)):
if x == len(filelist) - 1:
runcmd += "echo '" + flags[x] + "' > /home/" + filelist[x] + "/flag.txt"
else:
runcmd += "echo '" + flags[x] + "' > /home/" + filelist[x] + "/flag.txt" + " && "
# print runcmd
# copy bin
copybin = ""
for filename in filelist:
copybin += "COPY " + PWN_BIN_PATH + "/" + filename + " /home/" + filename + "/" + filename + "\n"
if REPLACE_BINSH:
copybin += "COPY ./catflag" + " /home/" + filename + "/bin/sh\n"
else:
copybin += "COPY ./catflag" + " /home/" + filename + "/bin/sh\n"
# print copybin
# chown & chmod
chown_chmod = "RUN "
for x in xrange(0, len(filelist)):
chown_chmod += "chown -R root:" + filelist[x] + " /home/" + filelist[x] + " && "
chown_chmod += "chmod -R 750 /home/" + filelist[x] + " && "
if x == len(filelist) - 1:
chown_chmod += "chmod 740 /home/" + filelist[x] + "/flag.txt"
else:
chown_chmod += "chmod 740 /home/" + filelist[x] + "/flag.txt" + " && "
# 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 not 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'''
copy_lib_bin_dev = "RUN "
for x in xrange(0, len(filelist)):
copy_lib_bin_dev += "cp -R /lib* /home/" + filelist[x] + " && "
copy_lib_bin_dev += "cp -R /usr/lib* /home/" + filelist[x] + " && "
copy_lib_bin_dev += dev % (filelist[x], filelist[x], filelist[x], filelist[x], filelist[x], filelist[x])
if x == len(filelist) - 1:
if not REPLACE_BINSH:
copy_lib_bin_dev += ness_bin % (filelist[x], filelist[x], filelist[x])
pass
else:
if not REPLACE_BINSH:
copy_lib_bin_dev += ness_bin % (filelist[x], filelist[x], filelist[x]) + " && "
else:
copy_lib_bin_dev += " && "
# print copy_lib_bin_dev
conf = DOCKERFILE % (runcmd, copybin, chown_chmod, copy_lib_bin_dev)
with open("Dockerfile", 'w') as f:
f.write(conf)
def generateDockerCompose(length):
conf = ""
ports = ""
port = PORT_LISTEN_START_FROM
for x in xrange(0,length):
ports += "- " + str(port) + ":" + str(port) + "\n "
port = port + 1
conf = DOCKERCOMPOSE % 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)
# generateBinPort(filelist)
generateXinetd(filelist)
generateDockerfile(filelist, flags)
generateDockerCompose(len(filelist))