|
| 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 | + |
0 commit comments