Cop1r3

SSL Request

Jul 24th, 2015
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. import SocketServer,urllib,httplib,gzip,StringIO
  2.  
  3. def decode_gzip(compresseddata):
  4.         compressedstream = StringIO.StringIO(compresseddata)
  5.         gzipper = gzip.GzipFile(fileobj = compressedstream)
  6.         return gzipper.read()  
  7. def craft_res(code,reason,ver,res_head,res_body):
  8.     if ver == 10:
  9.         http_ver = "HTTP/1.0"
  10.     if ver == 11:
  11.         http_ver = "HTTP/1.1"
  12.     head_buff = ''
  13.     for i in range(0,len(res_head)):
  14.             e1 =  res_head[i]
  15.         if e1[1] == "gzip":
  16.             res_body = decode_gzip(res_body)
  17.             head_buff += e1[0]+": "+e1[1]+"\r\n"
  18.     head_buff = head_buff.replace('\r\ncontent-encoding: gzip','')
  19.     final_res = http_ver+" "+str(code)+" "+reason+"\r\n"+head_buff+"\r\n\r\n"+res_body
  20.     return final_res
  21.  
  22. def fire_request(HOST,method,path,headers,params):
  23.     print method,HOST,path,params
  24.     conn = httplib.HTTPSConnection(HOST)
  25.         conn.request(method, path, params, headers)
  26.         response = conn.getresponse()
  27.         return craft_res(response.status,response.reason,response.version,response.getheaders(),response.read())
  28.  
  29. def parsebody(bdy):            
  30.     bodypart = bdy.split('&',bdy.count('&'))
  31.     encode_body = {}           
  32.     for i in range(0, bdy.count('&')+1):               
  33.         encode_body[bodypart[i].split('=',1)[0]] = bodypart[i].split('=',1)[1]
  34.     return encode_body
  35. def parseheader(head,body):
  36.     c1 = head.split('\n',head.count('\n'))
  37.     method = c1[0].split(' ',2)[0]             
  38.     path = c1[0].split(' ',2)[1]
  39.     headers = {}
  40.     for i in range(1, head.count('\n')+1):
  41.         c1[i] = c1[i].replace('\r','')
  42.         slice1 = c1[i].split(': ',1)
  43.         headers[slice1[0]] = slice1[1]
  44.     if body != "":
  45.         body = parsebody(body)
  46.         body = urllib.urlencode(body)
  47.     HOST = headers['Host']
  48.     return HOST,method,path,headers,body
  49. def send(r_header,r_body):
  50.     info = parseheader(r_header,r_body)
  51.     return fire_request(info[0],info[1],info[2],info[3],info[4])
  52.  
  53. class MyTCPHandler(SocketServer.BaseRequestHandler):
  54.     def handle(self):
  55.             self.data = self.request.recv(1024).strip()
  56.             rcvd = self.data
  57.         if rcvd[:3] == "POS":
  58.             post = rcvd.split('\r\n\r\n',1)
  59.             all = send(post[0],post[1])
  60.         else:
  61.             all = send(rcvd,"")
  62.         self.request.sendall(all)
  63. if __name__ == "__main__":
  64.     HOST, PORT = '', 8080
  65.         server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
  66.     print "[*] Proxy Server Started(Tunneling HTTP to HTTPS).Listening on",PORT
  67.     server.serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment