Sunday, June 26, 2016

small web shell implement in python

directory strtucture
$ tree -r -a shell/
├── htbin
└── cgi-bin

1- code
from http.server import CGIHTTPRequestHandler, HTTPServer

handler = CGIHTTPRequestHandler
handler.cgi_directories = ['/cgi-bin', '/htbin']  # this is the default
server = HTTPServer(('localhost', 8080), handler)

2- code
#!/usr/bin/env python
import subprocess
import re
import os
import shlex

import cgi
print "Content-Type: text/html"
print """\

#print re.sub("\n", "<br>",
#print "exec form"
print "<br>"

if os.environ['REQUEST_METHOD'] == 'POST':
    #print "get posting"
    form = cgi.FieldStorage()
    inputs = form["username"].value
    command = shlex.split(inputs)
    lss = subprocess.Popen(command ,stdout=subprocess.PIPE, \


    print "$ " + " ".join(command) + "<br>"
    print re.sub("\n", "<br>",
    print "----------------<br>"
    print "error part<br>"
    print "----------------<br>"
    print re.sub("\n", "<br>",

print """<br>
<form method="post"  action="/cgi-bin/">
<input type="text" name="username">
<input type="submit" value="submit">
print """

2- usange
$ cd shell
$ python

to access the shell using url http://localhost:8080/cgi-bin/


- it also report the error message

No comments:

Post a Comment