#!/usr/bin/python
#
from spf_core import *
#
# file-utils.py
# Makes files for various SPF exploits, can probably be cleaned up and
# merged with the exploits that require this, for more modularity.
#

import ConfigParser

def make_files2(path):
    config = ConfigParser.ConfigParser()
    config.read('config.cfg')

    webroot = config.get('Web','webroot')

    fullpath      = "%s%s" % (webroot,path)
    command1      = "mkdir %s" % fullpath
    system(command1)

    connectfile   = "%s/connect" % fullpath
    command2      = "touch %s" % connectfile
    system(command2)

    command3      = "chmod 777 %s" % connectfile
    system(command3)

    picturefile   = "%s/picture.jpg" % fullpath
    command4      = "touch %s" % picturefile
    system(command4)

    command5      = "chmod 777 %s" % picturefile
    system(command5)

    textfile      = "%s/text.txt" % fullpath
    command6      = "touch %s" % textfile
    system(command6)

    command7      = "chmod 777 %s" % textfile
    system(command7)

    textfile2     = "%s/text2.txt" % fullpath
    command77     = "touch %s" % textfile2
    system(command77)

    command7777   = "chmod 777 %s" % textfile2
    system(command7777)

    pictureupload = "%s/pictureupload.php" % fullpath
    command8      = "touch %s" % pictureupload
    system(command8)

    command9      = "chmod 777 %s" % pictureupload
    system(command9)

    pictureuploadtext =("<?php\n"+
                        "$base=$_REQUEST['picture'];\n"+
                        "echo $base;\n"+
                        "$binary=base64_decode($base);\n"+
                        "header('Content-Type: bitmap; charset=utf-8');\n"+
                        "$file = fopen('picture.jpg', 'wb');\n"+
                        "fwrite($file, $binary);\n"+
                        "fclose($file);\n"+
                        "?>")

    file = open(pictureupload, 'w')
    file.write(pictureuploadtext)
    file.close()

    textupload = "%s/textuploader.php" % fullpath
    command10  = "touch %s" % textupload
    system(command10)

    command11  = "chmod 777 %s" % textupload
    system(command11)

    textuploadtext =("<?php\n"+
                     "$base=$_REQUEST['text'];\n"+
                     "header('Content-Type: text; charset=utf-8');\n"+
                     "$file = fopen('text.txt', 'wb');\n"+
                     "fwrite($file, $base);\n"+
                     "?>")

    file = open(textupload, 'w')
    file.write(textuploadtext)
    file.close()

    text2upload     = "%s/text2uploader.php" % fullpath
    command100      = "touch %s" % text2upload
    system(command100)

    command110      = "chmod 777 %s" % text2upload
    system(command110)

    text2uploadtext =("<?php\n"+
                      "$base=$_REQUEST['text'];\n"+
                      "header('Content-Type: text; charset=utf-8');\n"+
                      "$file = fopen('text2.txt', 'wb');\n"+
                      "fwrite($file, $base);\n"+
                      "?>")

    file = open(text2upload, 'w')
    file.write(text2uploadtext)
    file.close()

    connectupload   = "%s/connectuploader.php" % fullpath
    command12       = "touch %s" % connectupload;
    system(command12)

    command13       = "chmod 777 %s" % connectupload
    system(command13)

    connectuploadtext =("<?php\n"+
                        "$base=$_REQUEST['text'];\n"+
                        "header('Content-Type: text; charset=utf-8');\n"+
                        "$file = fopen('connect','wb');\n"+
                        "fwrite($file, $base);\n"+
                        "?>")

    file = open(connectupload, 'w')
    file.write(connectuploadtext)
    file.close(CONNECTFILE)

    getfuncfile   = "%s/getfunc" % fullpath
    command6      = "touch %s" % getfuncfile
    system(command6)

    command7      = "chmod 777 %s" % getfuncfile
    system(command7)

    putfuncfile   = "%s/putfunc" % fullpath
    command6      = "touch %s" % putfuncfile
    system(command6)

    command7      = "chmod 777 %s" % putfuncfile
    system(command7)

    getfuncupload = "%s/getfuncuploader.php" % fullpath
    command10     = "touch %s" % getfuncupload
    system(command10)

    command11     = "chmod 777 %s" % getfuncupload
    system(command11)

    getfuncuploadtext =("<?php\n"+
                        "$base=$_REQUEST['text'];\n"+
                        "header('Content-Type: text; charset=utf-8');\n"+
                        "\$file = fopen('getfunc', 'wb');\n"+
                        "fwrite($file, $base);\n"
                        "?>")

    file = open(getfuncupload, 'w')
    file.write(getfuncuploadtext)
    file.close()

    putfuncupload = "%s/putfuncuploader.php" % fullpath
    command10     = "touch %s" % putfuncupload
    system(command10)

    command11     = "chmod 777 %s" % putfuncupload
    system(command11)

    putfuncuploadtext =("<?php\n"+
                        "$base=$_REQUEST['text'];\n"+
                        "header('Content-Type: text; charset=utf-8');\n"+
                        "$file = fopen('putfunc', 'wb');\n"+
                        "fwrite($file, $base);\n"+
                        "?>")

    file = open(putfuncupload, 'w')
    file.write(putfuncuploadtext)
    file.close()

def make_files(path):
    config = ConfigParser.ConfigParser()
    config.read('config.cfg')

    webroot = config.get('Web','webroot')

    fullpath = webroot+path
    command1 = "mkdir "+fullpath
    system(command1)

    controlfile = fullpath+"/control"
    command2 = "touch "+controlfile
    system(command2)

    command3 = "chmod 777 "+controlfile
    system(command3)

    picturefile = fullpath+"/picture.jpg"
    command4 = "touch "+picturefile
    system(command4)

    command5 = "chmod 777 "+picturefile
    system(command5)

    textfile = fullpath+"/text.txt"
    command6 = "touch "+textfile
    system(command6)

    command7 = "chmod 777 "+textfile
    system(command7)

    pictureupload = fullpath+"/pictureupload.php"
    command8 = "touch "+pictureupload
    system(command8)

    command9 = "chmod 777 "+pictureupload
    system(command9);

    pictureuploadtext =("<?php\n"+
                        "$base=$_REQUEST['picture'];\n"+
                        "echo $base;\n"+
                        "$binary=base64_decode($base);\n"+
                        "header('Content-Type: bitmap; charset=utf-8');\n"+
                        "$file = fopen('picture.jpg', 'wb');\n"+
                        "fwrite($file, $binary);\n"+
                        "fclose($file);\n"+
                        "?>")

    file = open(pictureupload, "w")
    file.write(pictureuploadtext)
    file.close()

    textupload = fullpath+"/textuploader.php"
    command10 = "touch "+textupload
    system(command10)

    command11 = "chmod 777 "+textupload
    system(command11)

    textuploadtext =("<?php\n"+
                     "$base=$_REQUEST['text'];\n"+
                     "header('Content-Type: text; charset=utf-8');\n"+
                     "$file = fopen('text.txt', 'wb');\n"+
                     "fwrite($file, $base);\n"+
                     "?>")
    file = open(textupload, "w")
    file.write(textuploadtext)
    file.close()

    controlupload = fullpath+"/controluploader.php"
    command12 = "touch "+controlupload
    system(command12)

    command13 = "chmod 777 "+controlupload
    system(command13)

    controluploadtext =("<?php\n"+
                        "$base=$_REQUEST['text'];\n"+
                        "header('Content-Type: text; charset=utf-8');\n"+
                        "$file = fopen('control','wb');\n"+
                        "fwrite($file, $base);\n"+
                        "?>")
    file = open(controlupload, "w")
    file.write(controluploadtext)
    file.close()

    putfile = fullpath+"/putfunc"
    command14 = "touch "+putfile
    system(command14)

    command15 = "chmod 777 "+putfile
    system(command15)

