-
-
Save y11en/c1e047261114ed4eb558be234c4d71d3 to your computer and use it in GitHub Desktop.
Basic WebDAV server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, binascii | |
from flask import Flask, Response, abort | |
def random_etag(): | |
return "1000-" + binascii.b2a_hex(os.urandom(6)) | |
app = Flask(__name__) | |
PORT = 80 | |
DLL_ETAG = random_etag() | |
# | |
# Serve a very simple WebDAV server to load remote DLL from share \\IP\X\X.dll | |
# The path must be in this form: \\IP\*\*.dll | |
DLL = 'XXX.dll' # Real file served | |
EXE_XML = """<?xml version="1.0" encoding="utf-8"?> | |
<D:multistatus xmlns:D="DAV:"> | |
<D:response xmlns:lp1="DAV:" xmlns:lp2="http://apache.org/dav/props/"> | |
<D:href>{}</D:href> | |
<D:propstat> | |
<D:prop> | |
<lp1:resourcetype/> | |
<lp1:creationdate>2018-04-21T02:47:28Z</lp1:creationdate> | |
<lp1:getcontentlength>{}</lp1:getcontentlength> | |
<lp1:getlastmodified>Sat, 21 Apr 2018 02:47:28 GMT</lp1:getlastmodified> | |
<lp1:getetag>"{}"</lp1:getetag> | |
<lp2:executable>T</lp2:executable> | |
<D:supportedlock> | |
<D:lockentry> | |
<D:lockscope><D:exclusive/></D:lockscope> | |
<D:locktype><D:write/></D:locktype> | |
</D:lockentry> | |
<D:lockentry> | |
<D:lockscope><D:shared/></D:lockscope> | |
<D:locktype><D:write/></D:locktype> | |
</D:lockentry> | |
</D:supportedlock> | |
<D:lockdiscovery/> | |
<D:getcontenttype>application/x-msdos-program</D:getcontenttype> | |
</D:prop> | |
<D:status>HTTP/1.1 200 OK</D:status> | |
</D:propstat> | |
</D:response> | |
</D:multistatus>""" # path size etag | |
@app.route('/', defaults={'path': ''}, methods=['OPTIONS']) | |
@app.route('/<path:path>', methods=['OPTIONS']) | |
def option(path): | |
print("Received OPTION with path '{}'".format(path)) | |
if path and path.lower().endswith('.dll'): | |
resp = Response('', mimetype = "application/x-msdos-program") | |
else: | |
resp = Response('') | |
resp.headers['Allow'] = 'OPTIONS,GET,HEAD,POST,DELETE,TRACE,PROPFIND,PROPPATCH,COPY,MOVE,PUT,LOCK,UNLOCK' | |
resp.headers['DAV'] = '<http://apache.org/dav/propset/fs/1>' | |
resp.headers['MS-Author-Via'] = 'DAV' | |
return resp | |
@app.route('/', defaults={'path': ''}, methods=['PROPFIND']) | |
@app.route('/<path:path>', methods=['PROPFIND']) | |
def propfind(path): | |
print("Received PROPFIND with path '{}'".format(path)) | |
if path and path.lower().endswith('.dll'): | |
xml = EXE_XML.format(path, os.path.getsize(DLL), DLL_ETAG) | |
resp = Response(xml, status=207, mimetype='text/xml; charset="utf-8"') | |
else: | |
abort(404) | |
return resp | |
@app.route('/', defaults={'path': ''}, methods=['GET']) | |
@app.route('/<path:path>', methods=['GET']) | |
def get(path): | |
print("Received GET with path '{}'".format(path)) | |
if path and path.lower().endswith('.dll'): | |
dllcontent = open(DLL, 'rb').read() | |
resp = Response(dllcontent, mimetype='application/x-msdos-program') | |
resp.headers['Last-Modified'] = 'Sat, 21 Apr 2018 02:47:28 GMT' | |
resp.headers['ETag'] = DLL_ETAG | |
resp.headers['Accept-Ranges'] = 'bytes' | |
else: | |
abort(404) | |
return resp | |
if __name__ == '__main__': | |
app.run('0.0.0.0', port=PORT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment