fetch_url.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. """
  2. Fetches a URL from a web-server supporting NTLM authentication
  3. eg, IIS.
  4. If no arguments are specified, a default of http://localhost/localstart.asp
  5. is used. This script does follow simple 302 redirections, so pointing at the
  6. root of an IIS server is should work.
  7. """
  8. import sys
  9. import urllib
  10. import httplib
  11. import urlparse
  12. from base64 import encodestring, decodestring
  13. from sspi import ClientAuth
  14. import optparse # sorry, this demo needs 2.3+
  15. options = None # set to optparse options object
  16. def open_url(host, url):
  17. h = httplib.HTTPConnection(host)
  18. # h.set_debuglevel(9)
  19. h.putrequest('GET', url)
  20. h.endheaders()
  21. resp = h.getresponse()
  22. print "Initial response is", resp.status, resp.reason
  23. body = resp.read()
  24. if resp.status == 302: # object moved
  25. url = "/" + resp.msg["location"]
  26. resp.close()
  27. h.putrequest('GET', url)
  28. h.endheaders()
  29. resp = h.getresponse()
  30. print "After redirect response is", resp.status, resp.reason
  31. if options.show_headers:
  32. print "Initial response headers:"
  33. for name, val in resp.msg.items():
  34. print " %s: %s" % (name, val)
  35. if options.show_body:
  36. print body
  37. if resp.status == 401:
  38. # 401: Unauthorized - here is where the real work starts
  39. auth_info = None
  40. if options.user or options.domain or options.password:
  41. auth_info = options.user, options.domain, options.password
  42. ca = ClientAuth("NTLM", auth_info=auth_info)
  43. auth_scheme = ca.pkg_info['Name']
  44. data = None
  45. while 1:
  46. err, out_buf = ca.authorize(data)
  47. data = out_buf[0].Buffer
  48. # Encode it as base64 as required by HTTP
  49. auth = encodestring(data).replace("\012", "")
  50. h.putrequest('GET', url)
  51. h.putheader('Authorization', auth_scheme + ' ' + auth)
  52. h.putheader('Content-Length', '0')
  53. h.endheaders()
  54. resp = h.getresponse()
  55. if options.show_headers:
  56. print "Token dance headers:"
  57. for name, val in resp.msg.items():
  58. print " %s: %s" % (name, val)
  59. if err==0:
  60. break
  61. else:
  62. if resp.status != 401:
  63. print "Eeek - got response", resp.status
  64. cl = resp.msg.get("content-length")
  65. if cl:
  66. print repr(resp.read(int(cl)))
  67. else:
  68. print "no content!"
  69. assert resp.status == 401, resp.status
  70. assert not resp.will_close, "NTLM is per-connection - must not close"
  71. schemes = [s.strip() for s in resp.msg.get("WWW-Authenticate", "").split(",")]
  72. for scheme in schemes:
  73. if scheme.startswith(auth_scheme):
  74. data = decodestring(scheme[len(auth_scheme)+1:])
  75. break
  76. else:
  77. print "Could not find scheme '%s' in schemes %r" % (auth_scheme, schemes)
  78. break
  79. resp.read()
  80. print "Final response status is", resp.status, resp.reason
  81. if resp.status == 200:
  82. # Worked!
  83. # Check we can read it again without re-authenticating.
  84. if resp.will_close:
  85. print "EEEK - response will close, but NTLM is per connection - it must stay open"
  86. body = resp.read()
  87. if options.show_body:
  88. print "Final response body:"
  89. print body
  90. h.putrequest('GET', url)
  91. h.endheaders()
  92. resp = h.getresponse()
  93. print "Second fetch response is", resp.status, resp.reason
  94. if options.show_headers:
  95. print "Second response headers:"
  96. for name, val in resp.msg.items():
  97. print " %s: %s" % (name, val)
  98. resp.read(int(resp.msg.get("content-length", 0)))
  99. elif resp.status == 500:
  100. print "Error text"
  101. print resp.read()
  102. else:
  103. if options.show_body:
  104. cl = resp.msg.get("content-length")
  105. print resp.read(int(cl))
  106. if __name__=='__main__':
  107. parser = optparse.OptionParser(description=__doc__)
  108. parser.add_option("", "--show-body", action="store_true",
  109. help="print the body of each response as it is received")
  110. parser.add_option("", "--show-headers", action="store_true",
  111. help="print the headers of each response as it is received")
  112. parser.add_option("", "--user", action="store",
  113. help="The username to login with")
  114. parser.add_option("", "--password", action="store",
  115. help="The password to login with")
  116. parser.add_option("", "--domain", action="store",
  117. help="The domain to login to")
  118. options, args = parser.parse_args()
  119. if not args:
  120. print "Run with --help for usage details"
  121. args = ["http://localhost/localstart.asp"]
  122. for url in args:
  123. scheme, netloc, path, params, query, fragment = urlparse.urlparse(url)
  124. if (scheme != "http") or params or query or fragment:
  125. parser.error("Scheme must be http, URL must be simple")
  126. print "Opening '%s' from '%s'" % (path, netloc)
  127. r = open_url(netloc, path)