1

hello guys i need some help when i run my proxy server on my browser it give me connection secure connection secure failed

#/!usr/bin/python
import socket
import os
import sys
import thread

try:
   listening_port = int(raw_input("enter the port of proxy: "))
except KeyboardInterrupt:
   print"\n[*] User Requested An Interrupt"
   print"[*] Alpication Exiting ..."
   sys.exit()

max_conn = 50
buffer_size = 4096

def start():
   try:
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.bind(("192.168.1.1",listening_port))
      s.listen(max_conn)
   except Exception, e:
      print "[*] Unabel To initialize Socket",e
      s.close()
      sys.exit(2)

   while 1:
      try:
         conn, addr = s.accept()
         data = conn.recv(buffer_size)
         thread.start_new_thread(conn_string,(conn, data, addr))

      except:
         sys.exit(1)
         s.close()

def conn_string(conn, data, addr):
    #client browser request apears here
    try:
       first_line = data.split('\n')[0]
       url = first_line.split(' ')[1]
       http_pos = url.find("://")
       if http_pos == -1:
           temp = url
       else:
           temp = url[(http_pos+3):]

       port_pos = temp.find(":")
       webserver_pos = temp.find("/")
       if webserver_pos == -1:
           webserver_pos = len(temp)
           webserver = ""
           port = -1
       if port_pos == -1 or webserver_pos < port_pos:
           port = 80
           webserver = temp[:webserver_pos]
       else:
          #specific port
          port = int((temp[(port_pos+1):])[:webserver_pos-port_pos-1])
          webserver = temp[:port_pos]
       proxy_server(webserver, port, conn, data, addr)
     except Exception, e:
        print e
        pass

def proxy_server(webserver, port, conn, data, addr):
    try:
       s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
       s.connect((webserver, 80))
       s.sendall(data)
       while 1:
          #read reply or data to from end webserver
          reply = s.recv(buffer_size)
          if (len(reply) > 0):
              conn.send(reply)
              #send notification to proxy server
              dar = float(len(reply))
              dar = float (dar / 1024)
              dar = "%.3s" % (str(dar))
              dar = "%s KB" % (dar)
              print "[*] Request Message For Request Complete"
              print "[*] Request Done: %s => %s <=" % (str(addr[0]),addr[1])
              print '\n',data,'\n'
          else:
              break
       s.close()
       conn.close()
    except socket.error, (value, message):
       print socket.error,value,message
       s.close()
       conn.close()
       sys.exit(1)

start()
mata
  • 67,110
  • 10
  • 163
  • 162
Sam Robert
  • 19
  • 2
  • You seem to connect to port 80, no matter wheather the original request was to a different port or protocol (https uses port 443, not 80), so it will fail for https urls. – mata Jan 30 '17 at 13:59
  • but when i replace 80 and put port that browser request need it give me that too – Sam Robert Jan 30 '17 at 14:17
  • thank u sir so much – Sam Robert Jan 30 '17 at 14:26
  • Try to print the `data` line you read from the initial request, for HTTPS connections probably CONNECT is used, in which case you are supposed to only open a channel to the server and return a 200 status code to the client to indicate that the channel is up, you're not supposed to forward the request headers. Your code will run into problems when the request is lager than buffer_size or not read fully by one single `conn.recv(buffer_size)` – mata Jan 30 '17 at 14:38
  • ook i will try but what if it need ssl cert but i can't build it could u tell me is that needed for https and thanks so much – Sam Robert Jan 30 '17 at 14:58
  • No, with CONNECT the proxy just opens the channel between client and server and forwards the traffic, in case of ssl/tsl the handhake then is performed normally between the client and the server. The proxy doesn't need a certificate because it isn't really involved besides supplying the connection. Unless of course it tries to do something like ssl inspection, then it would need to present a trusted certificate for the requested domain, basically perform a MITM attack, which defeats the purpose of tsl and opens a whole new set of problems. – mata Jan 30 '17 at 15:26
  • right bro but i need some help on building ssl cert – Sam Robert Jan 30 '17 at 15:28
  • Well, creating a [self signed certificate using openssl](http://stackoverflow.com/questions/10175812/how-to-create-a-self-signed-certificate-with-openssl) is not really that complicated, but that's only part of what's necessary. You could have a look at [mitmproxy](https://mitmproxy.org/). Its [how to](http://docs.mitmproxy.org/en/latest/howmitmproxy.html) page has a good overview of what the challenges in building an ssl inspection proxy are. – mata Jan 30 '17 at 16:07
  • but i need to use it as MITM to gather infos or use it as vpn or proxy so i need ur help to build it well for https and ssl – Sam Robert Jan 30 '17 at 22:00

0 Answers0