Advertisement
opexxx

torwget.py

May 5th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.36 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Copyright (C) 2010 Michael Ligh (the original code)
  3. # Modified by @Genuix/MMD
  4. # - modified: to save headers and cookie in the same
  5. #             directory
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. # [NOTES] -----------------------------------------------------------
  21. # 1) Tested on Linux (Ubuntu), Windows XP/7, and Mac OS X
  22. # 2) If the script hangs, make sure you have TOR running on the
  23. #        specified TOR_PORT
  24. #--------------------------------------------------------------------
  25.  
  26. from optparse import OptionParser
  27. import socket
  28. import httplib
  29. import urlparse
  30. import socks
  31. import random
  32. import time
  33. import os
  34. import cookielib
  35. import urllib2
  36.  
  37. user_agents = [
  38. 'Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17',
  39. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; )',
  40. 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-GB; rv:1.8.1.18) Gecko/20081029 Firefox/2.0.0.18',
  41. 'Opera/9.80 (Windows NT 5.1; U; cs) Presto/2.2.15 Version/10.00',
  42. 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/525.26.2 (KHTML, like Gecko) Version/3.2 Safari/525.26.12',
  43. 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Avant Browser; Avant Browser; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)',
  44. 'Opera/9.51 (Macintosh; Intel Mac OS X; U; en)',
  45. 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.2.1; MultiZilla v1.1.32 final) Gecko/20021130',
  46. ]
  47.  
  48. TOR_SERVER = "127.0.0.1"
  49. TOR_PORT = 9050
  50.  
  51. headers = {
  52. 'Accept' : '*/*',
  53. }
  54.  
  55. def main():
  56.     parser = OptionParser()
  57.     parser.add_option("-r", "--referrer", action="store", dest="referrer",
  58.                       type="string", help="use this Referrer")
  59.     parser.add_option("-u", "--useragent", action="store", dest="useragent",
  60.                       type="string", help="use this User Agent")
  61.     parser.add_option("-c", "--connect", action="store", dest="site",
  62.                       type="string", help="Connection string (i.e. www.sol.org/a.txt)")                      
  63.     parser.add_option("-z", "--randomize", action="store_true",
  64.                       dest="random", default=False,
  65.                       help="Choose a random User Agent")
  66.    
  67.     (opts, args) = parser.parse_args()
  68.    
  69.     if opts.site == None:
  70.         parser.print_help()
  71.         parser.error("You must supply a connection string!")
  72.        
  73.     if opts.referrer != None:
  74.         headers['Referrer'] = opts.referrer
  75.        
  76.     if opts.useragent != None:
  77.         headers['User-Agent'] = opts.useragent
  78.     elif opts.random:
  79.         random.seed(time.time())
  80.         headers['User-Agent'] = random.choice(user_agents)
  81.     else:
  82.         headers['User-Agent'] = user_agents[0]
  83.  
  84.     # Override the socket object with a Tor+Socks socket
  85.    
  86.     socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, TOR_SERVER, TOR_PORT)
  87.     socket.socket = socks.socksocket
  88.    
  89.     # Isolate the host name from the connection string
  90.    
  91.     if not opts.site.lower().startswith('http://'):
  92.         opts.site = 'http://' + opts.site
  93.        
  94.     url = urlparse.urlsplit(opts.site)
  95.        
  96.     print "Hostname: %s" % (url.hostname)
  97.     print "Path: %s" % (url.path)
  98.     print "Headers: %s" % (headers)
  99.    
  100.        
  101.     try:
  102.         conn = httplib.HTTPConnection(url.hostname)
  103.         conn.request('GET', url.path, None, headers)
  104.         data = conn.getresponse().read()
  105.  
  106.         ### Genuix ADD
  107.         cj = cookielib.LWPCookieJar()
  108.         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  109.         response = opener.open(opts.site)
  110.         infos = response.info()
  111.  
  112.         for index, cookie in enumerate(cj):
  113.             print index, '  :  ', cookie
  114.        
  115.         for indexx, inform in enumerate(infos):
  116.             print indexx, '  :  ', inform
  117.         ### END Genuix ADD
  118.  
  119.     except:
  120.         print "Cannot connect...network issue?"
  121.         return
  122.    
  123.     if len(data) == 0:
  124.         print "Got 0 bytes...try again later."
  125.         return
  126.    
  127.     outfile = url.hostname + os.sep + os.path.basename(opts.site)
  128.     ### Genuix ADD
  129.     if not os.path.isfile(outfile):
  130.         outfile = url.hostname + os.sep + "index.html"
  131.  
  132.     cookiefile = url.hostname + os.sep + "cookie.txt"
  133.     headfile = url.hostname + os.sep + "headers.txt"
  134.     ### END Genuix ADD
  135.     print "Saving %d bytes to %s" % (len(data), outfile)
  136.    
  137.     if not os.path.isdir(url.hostname):
  138.         os.mkdir(url.hostname)
  139.    
  140.     FILE = open(outfile, "wb")
  141.     FILE.write(data)
  142.     FILE.close()
  143.  
  144.     ### Genuix ADD
  145.     cj.save(cookiefile)
  146.  
  147.     HEAD = open(headfile, "wb")
  148.     for hdinf in infos:
  149.         HEAD.write( hdinf + os.linesep )
  150.     HEAD.close()
  151.     ### END Genuix ADD
  152.  
  153.     print "Done!"
  154.  
  155. if __name__ == '__main__':
  156.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement