Exemple #1
0
    def sendAndReceive(self):
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect((ORIGINAL_IP, 80))
            sock.sendall(str(self))
        except:
            return GlobalTools.FailedHTTPResponse(), ''

        res = HTTPResponse(sock)
        res.begin()
        res_body = res.read()
        res.close()

        if 'transfer-encoding' in res.msg:
            # httplib.HTTPResponse automatically concatenate chunked response
            # but do not delete 'transfer-encoding' header
            # so the header must be deleted
            res.msg.__delitem__('transfer-encoding')
        compmeth = res.msg.getheader('content-encoding', '').lower()
        if compmeth and compmeth.find('identity') != 0:
            # response body is compressed with some method
            offset = 0
            if compmeth.find('gzip') != -1:
                # if body is gziped, header offset value is 47
                # if not, offset value is 0
                # this server does not support sdch...
                offset += 47
            res_body = decompress(res_body, offset)
            res.msg['content-encoding'] = 'identity'

        return res, res_body
Exemple #2
0
    def send_and_recv(self):
        try:
            # because www.dream-pro.info is tlanslated to 127.0.0.1 using hosts' entry,
            # send message to www.dream-pro.info with socket.socket to make
            # http connection
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect((WWW_DREAM_PRO_INFO, 80))
            sock.sendall(str(self))
        except:
            print 'SocketError'
            return

        res = HTTPResponse(sock)
        res.begin()
        res_body = res.read()
        res.close()

        if 'transfer-encoding' in res.msg:
            # httplib.HTTPResponse automatically concatenate chunked response
            # but do not delete 'transfer-encoding' header
            # so the header must be deleted
            res.msg.__delitem__('transfer-encoding')
        compmeth = res.msg.getheader('content-encoding', '').lower()
        if compmeth and compmeth.find('identity') != 0:
            # response body is compressed with some method
            offset = 0
            if compmeth.find('gzip') != -1:
                # if body is gziped, header offset value is 47
                # if not, offset value is 0
                # this server does not support sdch...
                offset += 47
            res_body = decompress(res_body, offset)
            res.msg['content-encoding'] = 'identity'

        return res, res_body
    def _get_response(self):

        # Parse response
        h = HTTPResponse(self._proxy_sock)
        h.begin()
        res_d = {
            'status':
            h.status,
            'reason':
            h.reason,
            'req_version':
            self.request_version,
            'msg':
            h.msg,
            'trans_enc': (h.msg['Transfer-Encoding']
                          if 'Transfer-Encoding' in h.msg else '[NO-ENCODING]')
        }
        # Get rid of the pesky header
        del h.msg['Transfer-Encoding']

        res = '%(req_version)s %(status)s %(reason)s\r\n%(msg)s\r\n' % res_d
        res_d['data'] = h.read()
        res_d['time'] = time()
        res += res_d['data']
        # Let's close off the remote end
        h.close()
        self._proxy_sock.close()
        # Relay the message
        self.request.sendall(self.mitm_response(res))
        print res
        return res_d
Exemple #4
0
    def send_and_recv(self):
        try:
            # because www.dream-pro.info is tlanslated to 127.0.0.1 using hosts' entry,
            # send message to www.dream-pro.info with socket.socket to make
            # http connection
            sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
            sock.connect((WWW_DREAM_PRO_INFO,80))
            sock.sendall(str(self))
        except:
            print 'SocketError'
            return

        res = HTTPResponse(sock)
        res.begin()
        res_body = res.read()
        res.close()

        if 'transfer-encoding' in res.msg:
            # httplib.HTTPResponse automatically concatenate chunked response
            # but do not delete 'transfer-encoding' header
            # so the header must be deleted
            res.msg.__delitem__('transfer-encoding')
        compmeth = res.msg.getheader('content-encoding','').lower()
        if compmeth and compmeth.find('identity') != 0 :
            # response body is compressed with some method
            offset = 0
            if compmeth.find('gzip') != -1:
                # if body is gziped, header offset value is 47
                # if not, offset value is 0
                # this server does not support sdch...
                offset += 47
            res_body = decompress(res_body,offset)
            res.msg['content-encoding'] = 'identity'

        return res, res_body
Exemple #5
0
 def __init__(self, response):
     r = HTTPResponse(self._FakeSocket(response))
     r.begin()
     self.location = r.getheader("location")
     self.usn      = r.getheader("usn")
     self.st       = r.getheader("st")
     self.cache    = r.getheader("cache-control").split("=")[1]
     try: r.close()
     except: pass
Exemple #6
0
        self._proxy_sock.sendall(self.mitm_request(req))

        # Parse response
        h = HTTPResponse(self._proxy_sock)
        h.begin()

        # Get rid of the pesky header
        del h.msg['Transfer-Encoding']

        #  Time to relay the message across
        res = '%s %s %s\r\n' % (self.request_version, h.status, h.reason)
        res += '%s\r\n' % h.msg
        res += h.read()

        # Let's close off the remote end
        h.close()
        self._proxy_sock.close()

        # Relay the message
        self.request.sendall(self.mitm_response(res))

    def mitm_request(self, data):
        for p in self.server._req_plugins:
            data = p(self.server, self).do_request(data)
        return data

    def mitm_response(self, data):
        for p in self.server._res_plugins:
            data = p(self.server, self).do_response(data)
        return data
Exemple #7
0
        self._proxy_sock.sendall(self.mitm_request(req))

        # Parse response
        h = HTTPResponse(self._proxy_sock)
        h.begin()

        # Get rid of the pesky header
        del h.msg['Transfer-Encoding']

        # Time to relay the message across
        res = '%s %s %s\r\n' % (self.request_version, h.status, h.reason)
        res += '%s\r\n' % h.msg
        res += h.read()

        # Let's close off the remote end
        h.close()
        self._proxy_sock.close()

        # Relay the message
        self.request.sendall(self.mitm_response(res))

    def mitm_request(self, data):
        for p in self.server._req_plugins:
            data = p(self.server, self).do_request(data)
        return data

    def mitm_response(self, data):
        for p in self.server._res_plugins:
            data = p(self.server, self).do_response(data)
        return data
    def do_COMMAND(self):



        global allCache
        global dumpToDisk

        cacheHit = False
        sendNotModifiesResponse = False
        cacheurl = self.path
        print cacheurl
        # checking if we have a cache hit

        if not ('?' in cacheurl) and (cacheurl in allCache):

            #in all cache array add two more fields max-age and date,time when added, and check if the object is valid before
            #making cachehit true
            cacheHit = True

            #------ code below uncomment

            # if 'If-Modified-Since' in self.headers:

            #     last_modified_date = self.headers['If-Modified-Since']
            #     formatter_string = "%a, %d %b %Y %H:%M:%S %Z"
            #     in_req_modified_date = datetime.strptime(last_modified_date, formatter_string)

            #     #get the other date from cache
            #     in_cache_modified_date = allCache[cacheurl][3]

            #     if in_req_modified_date > in_cache_modified_date:
            #         #can be served from cache
            sendNotModifiesResponse = False




        # No cache hit, fetch from Internet
        if not cacheHit:
            # Is this an SSL tunnel?
            if not self.is_connect:
                try:
                    # Connect to destination
                    self._connect_to_host()
                except Exception, e:
                    self.send_error(500, str(e))
                    return
                # Extract path

            # Build request
            req = '%s %s %s\r\n' % (self.command, self.path, self.request_version)

            # Add headers to the request
            req += '%s\r\n' % self.headers

            # Append message body if present to the request
            if 'Content-Length' in self.headers:
                req += self.rfile.read(int(self.headers['Content-Length']))

            # Send it down the pipe!
            self._proxy_sock.sendall(self.mitm_request(req))

            # Parse response
            h = HTTPResponse(self._proxy_sock)
            h.begin()

            # Get rid of the pesky header
            del h.msg['Transfer-Encoding']

            # Time to relay the message across
            res = '%s %s %s\r\n' % (self.request_version, h.status, h.reason)
            res += '%s\r\n' % h.msg
            toWrite=h.read()
            res += toWrite

            # fetched an object from the Internet, need to add it to memory and dump it to disk
            if not ("?" in cacheurl):
                fileName=cacheFileName()
                tempObject=CacheObject(cacheurl, h.getheaders(), h.status, h.reason, toWrite,fileName)
                #
                current_datetime = datetime.now()
                #
                allCache[cacheurl]=['memory', tempObject, fileName, current_datetime]
                dumpToDisk.append(tempObject)

            h.close()
            self._proxy_sock.close()
Exemple #9
0
        if 'content-encoding' in http_response.msg.keys():
            if http_response.msg['Content-Encoding'] == 'gzip':
                compress_data = StringIO.StringIO(content)
                gzipper = gzip.GzipFile(fileobj=compress_data)
                del http_response.msg['Content-Encoding']
                content = gzipper.read()

        # Reenviar mensaje obtenido por proxy
        response = '%s %s %s\r\n' % (
            self.request_version, http_response.status, http_response.reason)
        response += '%s\r\n' % http_response.msg
        response += content

        # y ahora a cerrar la conexion remota quien nos dio info para enviar al cliente
        http_response.close()
        self._proxy_sock.close()

        # transmitir respuesta al MITM
        self.request.sendall(self.mitm_response(response))

    def mitm_request(self, data):
        data = self.do_tampering(data)
        times = 1
        for p in self.server._req_plugins:
            for tampering_value in pen_tester['ddos']:
                if tampering_value['url'] in self.path:
                    times = tampering_value['times']
            for x in range(0, times):
                data = p(self.server, self).do_request(data)
        if times > 1:
                self.send_error(500, str(e))
                return

        request = "%s %s %s\r\n" % (self.command, self.path, self.request_version)
        request += "%s\r\n" % self.headers
        if "Content-Length" in self.headers:
            request += self.rfile.read(int(self.headers["Content-Length"]))
        self._pipe_socket.sendall(request)

        http_response = HTTPResponse(self._pipe_socket)
        http_response.begin()
        response = "%s %s %s\r\n" % (self.request_version, http_response.status, http_response.reason)
        response += "%s\r\n" % http_response.msg
        response += http_response.read()

        http_response.close()
        self._pipe_socket.close()

        try:
            self.request.sendall(response)
        except Exception, e:
            print "ERROR request relay: ", e

    def do_GET(self):
        self.do_RELAY()

    def do_POST(self):
        self.do_RELAY()

    def finish(self):
        try: