def proxy_ssl(self): host = '%s:%d' % (self.host, self.port) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((self.proxy, int(self.proxy_port))) except: raise sock.sendall("CONNECT %s HTTP/1.0\r\n" % host) sock.sendall("User-Agent: %s\r\n" % UserAgent) if self.proxy_user and self.proxy_pass: for k, v in self.get_proxy_auth_header().items(): sock.sendall("%s: %s\r\n" % (k, v)) sock.sendall("\r\n") resp = httplib.HTTPResponse(sock, strict=True) resp.begin() if resp.status != 200: # Fake a socket error, use a code that make it obvious it hasn't # been generated by the socket library raise socket.error( -71, "Error talking to HTTP proxy %s:%s: %s (%s)" % (self.proxy, self.proxy_port, resp.status, resp.reason)) # We can safely close the response, it duped the original socket resp.close() h = httplib.HTTPConnection(host) # Wrap the socket in an SSL socket if hasattr(httplib, 'ssl'): sslSock = httplib.ssl.SSLSocket(sock) else: # Old Python, no ssl module sslSock = socket.ssl(sock, None, None) sslSock = httplib.FakeSocket(sock, sslSock) # This is a bit unclean h.sock = sslSock return h
def connect(self): # TODO(frew): When we drop support for <2.6 (in the far distant future), # change this to socket.create_connection. self.sock = _create_connection((self.host, self.port)) if self._tunnel_host: self._tunnel() # ssl and FakeSocket got deprecated. Try for the new hotness of wrap_ssl, # with fallback. Note: Since can_validate_certs() just checks for the # ssl module, it's equivalent to attempting to import ssl from # the function, but doesn't require a dynamic import, which doesn't # play nicely with dev_appserver. if can_validate_certs(): context = ssl.SSLContext(ssl.PROTOCOL_TLS) context.options |= ssl.OP_NO_SSLv2 context.options |= ssl.OP_NO_SSLv3 context.options |= ssl.OP_NO_COMPRESSION context.verify_mode = self.cert_reqs if self.ca_certs: context.load_verify_locations(self.ca_certs) hostname = self.host.split(":", 0)[0] self.sock = context.wrap_socket(self.sock, server_hostname=hostname) if self.cert_reqs & ssl.CERT_REQUIRED: cert = self.sock.getpeercert() if not self._validate_certificate_hostname(cert, hostname): raise InvalidCertificateException( hostname, cert, "hostname mismatch") else: ssl_socket = socket.ssl(self.sock, keyfile=self.key_file, certfile=self.cert_file) self.sock = httplib.FakeSocket(self.sock, ssl_socket)
def _prepare_connection(self, url, headers): proxy_auth = _get_proxy_auth() if url.protocol == 'https': # destination is https proxy = os.environ.get('https_proxy') if proxy: # Set any proxy auth headers if proxy_auth: proxy_auth = 'Proxy-authorization: %s' % proxy_auth # Construct the proxy connect command. port = url.port if not port: port = '443' proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % (url.host, port) # Set the user agent to send to the proxy if headers and 'User-Agent' in headers: user_agent = 'User-Agent: %s\r\n' % (headers['User-Agent']) else: user_agent = '' proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth, user_agent) # Find the proxy host and port. proxy_url = atom.url.parse_url(proxy) if not proxy_url.port: proxy_url.port = '80' # Connect to the proxy server, very simple recv and error checking p_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) p_sock.connect((proxy_url.host, int(proxy_url.port))) p_sock.sendall(proxy_pieces) response = '' # Wait for the full response. while response.find("\r\n\r\n") == -1: response += p_sock.recv(8192) p_status = response.split()[1] if p_status != str(200): raise ProxyError('Error status=%s' % str(p_status)) # Trivial setup for ssl socket. ssl = socket.ssl(p_sock, None, None) fake_sock = httplib.FakeSocket(p_sock, ssl) # Initalize httplib and replace with the proxy socket. connection = httplib.HTTPConnection(proxy_url.host) connection.sock = fake_sock return connection else: # The request was HTTPS, but there was no https_proxy set. return HttpClient._prepare_connection(self, url, headers) else: proxy = os.environ.get('http_proxy') if proxy: # Find the proxy host and port. proxy_url = atom.url.parse_url(proxy) if not proxy_url.port: proxy_url.port = '80' if proxy_auth: headers['Proxy-Authorization'] = proxy_auth.strip() return httplib.HTTPConnection(proxy_url.host, proxy_url.port) else: # The request was HTTP, but there was no http_proxy set. return HttpClient._prepare_connection(self, url, headers)
def connect(self): ProxyHTTPConnection.connect(self) #make the sock ssl-aware ssl = socket.ssl(self.sock, self.key_file, self.cert_file) self.sock = httplib.FakeSocket(self.sock, ssl)
def open(self): """ Opens connection to the target. Make sure to call close! Returns: None """ # Create socket if self.proto == "tcp" or self.proto == "ssl": self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) elif self.proto == "udp": self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) if self.bind: self._sock.bind(self.bind) if self._udp_broadcast: self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True) elif self.proto == "raw-l2": self._sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW) elif self.proto == "raw-l3": self._sock = socket.socket(socket.AF_PACKET, socket.SOCK_DGRAM) else: raise exception.SullyRuntimeError( "INVALID PROTOCOL SPECIFIED: %s" % self.proto) self._sock.setsockopt( socket.SOL_SOCKET, socket.SO_SNDTIMEO, _seconds_to_second_microsecond_struct(self._send_timeout)) self._sock.setsockopt( socket.SOL_SOCKET, socket.SO_RCVTIMEO, _seconds_to_second_microsecond_struct(self._recv_timeout)) if self.server: self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self._sock.bind((self.host, self.port)) if self.proto == "tcp" or self.proto == "ssl": self._serverSock = self._sock self._serverSock.listen(1) self._sock, addr = self._serverSock.accept() # Connect is needed only for TCP protocols elif self.proto == "tcp" or self.proto == "ssl": try: self._sock.connect((self.host, self.port)) except socket.error as e: if e.errno == errno.ECONNREFUSED: raise exception.BoofuzzTargetConnectionFailedError( e.message) else: raise # if SSL is requested, then enable it. if self.proto == "ssl": if self.server: ssl_sock = ssl.wrap_socket(self._sock, keyfile=self.keyfile, certfile=self.certfile, server_side=True) self._sock = httplib.FakeSocket(self._sock, ssl_sock) else: ssl_sock = ssl.wrap_socket(self._sock) self._sock = httplib.FakeSocket(self._sock, ssl_sock)
def proxy_ssl(self): host = '%s:%d' % (self.host, self.port) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((self.proxy, int(self.proxy_port))) except: raise boto.log.debug("Proxy connection: CONNECT %s HTTP/1.0\r\n", host) sock.sendall("CONNECT %s HTTP/1.0\r\n" % host) sock.sendall("User-Agent: %s\r\n" % UserAgent) if self.proxy_user and self.proxy_pass: for k, v in self.get_proxy_auth_header().items(): sock.sendall("%s: %s\r\n" % (k, v)) # See discussion about this config option at # https://groups.google.com/forum/?fromgroups#!topic/boto-dev/teenFvOq2Cc if config.getbool('Boto', 'send_crlf_after_proxy_auth_headers', False): sock.sendall("\r\n") else: sock.sendall("\r\n") resp = httplib.HTTPResponse(sock, strict=True, debuglevel=self.debug) resp.begin() if resp.status != 200: # Fake a socket error, use a code that make it obvious it hasn't # been generated by the socket library raise socket.error( -71, "Error talking to HTTP proxy %s:%s: %s (%s)" % (self.proxy, self.proxy_port, resp.status, resp.reason)) # We can safely close the response, it duped the original socket resp.close() h = httplib.HTTPConnection(host) if self.https_validate_certificates and HAVE_HTTPS_CONNECTION: boto.log.debug( "wrapping ssl socket for proxied connection; " "CA certificate file=%s", self.ca_certificates_file) key_file = self.http_connection_kwargs.get('key_file', None) cert_file = self.http_connection_kwargs.get('cert_file', None) sslSock = ssl.wrap_socket(sock, keyfile=key_file, certfile=cert_file, cert_reqs=ssl.CERT_REQUIRED, ca_certs=self.ca_certificates_file) cert = sslSock.getpeercert() hostname = self.host.split(':', 0)[0] if not https_connection.ValidateCertificateHostname( cert, hostname): raise https_connection.InvalidCertificateException( hostname, cert, 'hostname mismatch') else: # Fallback for old Python without ssl.wrap_socket if hasattr(httplib, 'ssl'): sslSock = httplib.ssl.SSLSocket(sock) else: sslSock = socket.ssl(sock, None, None) sslSock = httplib.FakeSocket(sock, sslSock) # This is a bit unclean h.sock = sslSock return h
def _PrepareConnection(self, full_uri): (server, port, ssl, partial_uri) = self._ProcessUrl(full_uri) if ssl: # destination is https proxy = os.environ.get('https_proxy') if proxy: (p_server, p_port, p_ssl, p_uri) = self._ProcessUrl(proxy, True) proxy_username = os.environ.get('proxy-username') proxy_password = os.environ.get('proxy-password') if proxy_username: user_auth = base64.encodestring( '%s:%s' % (proxy_username, proxy_password)) proxy_authorization = ( 'Proxy-authorization: Basic %s\r\n' % (user_auth.strip())) else: proxy_authorization = '' proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % (server, port) user_agent = 'User-Agent: %s\r\n' % ( self.additional_headers['User-Agent']) proxy_pieces = (proxy_connect + proxy_authorization + user_agent + '\r\n') #now connect, very simple recv and error checking p_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) p_sock.connect((p_server, p_port)) p_sock.sendall(proxy_pieces) response = '' # Wait for the full response. while response.find("\r\n\r\n") == -1: response += p_sock.recv(8192) p_status = response.split()[1] if p_status != str(200): raise 'Error status=', str(p_status) # Trivial setup for ssl socket. ssl = socket.ssl(p_sock, None, None) fake_sock = httplib.FakeSocket(p_sock, ssl) # Initalize httplib and replace with the proxy socket. connection = httplib.HTTPConnection(server) connection.sock = fake_sock full_uri = partial_uri else: connection = httplib.HTTPSConnection(server, port) full_uri = partial_uri else: # destination is http proxy = os.environ.get('http_proxy') if proxy: (p_server, p_port, p_ssl, p_uri) = self._ProcessUrl(proxy, True) proxy_username = os.environ.get('proxy-username') proxy_password = os.environ.get('proxy-password') if proxy_username: self.UseBasicAuth(proxy_username, proxy_password, True) connection = httplib.HTTPConnection(p_server, p_port) if not full_uri.startswith("http://"): if full_uri.startswith("/"): full_uri = "http://%s%s" % (self.server, full_uri) else: full_uri = "http://%s/%s" % (self.server, full_uri) else: connection = httplib.HTTPConnection(server, port) full_uri = partial_uri return (connection, full_uri)
def _ssl_wrap_socket(sock, key_file, cert_file): ssl_sock = socket.ssl(sock, key_file, cert_file) return httplib.FakeSocket(sock, ssl_sock)
def secureSocket(sock): import socket import httplib s = socket.ssl(sock, None, None) newsock = httplib.FakeSocket(sock, s) return newsock
class session(pgraph.graph): def __init__(self, session_filename=None, skip=0, sleep_time=1.0, log_level=30, logfile=None, logfile_level=10, proto="tcp", bind=None, restart_interval=0, timeout=5.0, web_port=26000, crash_threshold=3, restart_sleep_time=300): ''' Extends pgraph.graph and provides a container for architecting protocol dialogs. @type session_filename: String @kwarg session_filename: (Optional, def=None) Filename to serialize persistant data to @type skip: Integer @kwarg skip: (Optional, def=0) Number of test cases to skip @type sleep_time: Float @kwarg sleep_time: (Optional, def=1.0) Time to sleep in between tests @type log_level: Integer @kwarg log_level: (Optional, def=30) Set the log level (CRITICAL : 50 / ERROR : 40 / WARNING : 30 / INFO : 20 / DEBUG : 10) @type logfile: String @kwarg logfile: (Optional, def=None) Name of log file @type logfile_level: Integer @kwarg logfile_level: (Optional, def=10) Log level for log file, default is debug @type proto: String @kwarg proto: (Optional, def="tcp") Communication protocol ("tcp", "udp", "ssl") @type bind: Tuple (host, port) @kwarg bind: (Optional, def=random) Socket bind address and port @type timeout: Float @kwarg timeout: (Optional, def=5.0) Seconds to wait for a send/recv prior to timing out @type restart_interval: Integer @kwarg restart_interval (Optional, def=0) Restart the target after n test cases, disable by setting to 0 @type crash_threshold: Integer @kwarg crash_threshold (Optional, def=3) Maximum number of crashes allowed before a node is exhaust @type restart_sleep_time: Integer @kwarg restart_sleep_time: Optional, def=300) Time in seconds to sleep when target can't be restarted ''' # run the parent classes initialization routine first. pgraph.graph.__init__(self) self.session_filename = session_filename self.skip = skip self.sleep_time = sleep_time self.proto = proto.lower() self.bind = bind self.ssl = False self.restart_interval = restart_interval self.timeout = timeout self.web_port = web_port self.crash_threshold = crash_threshold self.restart_sleep_time = restart_sleep_time # Initialize logger self.logger = logging.getLogger("Sulley_logger") self.logger.setLevel(logging.DEBUG) formatter = logging.Formatter( '[%(asctime)s] [%(levelname)s] -> %(message)s') if logfile != None: filehandler = logging.FileHandler(logfile) filehandler.setLevel(logfile_level) filehandler.setFormatter(formatter) self.logger.addHandler(filehandler) consolehandler = logging.StreamHandler() consolehandler.setFormatter(formatter) consolehandler.setLevel(log_level) self.logger.addHandler(consolehandler) self.total_num_mutations = 0 self.total_mutant_index = 0 self.fuzz_node = None self.targets = [] self.netmon_results = {} self.procmon_results = {} self.protmon_results = {} self.pause_flag = False self.crashing_primitives = {} if self.proto == "tcp": self.proto = socket.SOCK_STREAM elif self.proto == "ssl": self.proto = socket.SOCK_STREAM self.ssl = True elif self.proto == "udp": self.proto = socket.SOCK_DGRAM else: raise sex.error("INVALID PROTOCOL SPECIFIED: %s" % self.proto) # import settings if they exist. self.import_file() # create a root node. we do this because we need to start fuzzing from a single point and the user may want # to specify a number of initial requests. self.root = pgraph.node() self.root.name = "__ROOT_NODE__" self.root.label = self.root.name self.last_recv = None self.add_node(self.root) #################################################################################################################### def add_node(self, node): ''' Add a pgraph node to the graph. We overload this routine to automatically generate and assign an ID whenever a node is added. @type node: pGRAPH Node @param node: Node to add to session graph ''' node.number = len(self.nodes) node.id = len(self.nodes) if not self.nodes.has_key(node.id): self.nodes[node.id] = node return self #################################################################################################################### def add_target(self, target): ''' Add a target to the session. Multiple targets can be added for parallel fuzzing. @type target: session.target @param target: Target to add to session ''' # pass specified target parameters to the PED-RPC server. target.pedrpc_connect() # add target to internal list. self.targets.append(target) #################################################################################################################### def connect(self, src, dst=None, callback=None): ''' Create a connection between the two requests (nodes) and register an optional callback to process in between transmissions of the source and destination request. Leverage this functionality to handle situations such as challenge response systems. The session class maintains a top level node that all initial requests must be connected to. Example:: sess = sessions.session() sess.connect(sess.root, s_get("HTTP")) If given only a single parameter, sess.connect() will default to attaching the supplied node to the root node. This is a convenient alias and is identical to the second line from the above example:: sess.connect(s_get("HTTP")) If you register callback method, it must follow this prototype:: def callback(session, node, edge, sock) Where node is the node about to be sent, edge is the last edge along the current fuzz path to "node", session is a pointer to the session instance which is useful for snagging data such as sesson.last_recv which contains the data returned from the last socket transmission and sock is the live socket. A callback is also useful in situations where, for example, the size of the next packet is specified in the first packet. As another example, if you need to fill in the dynamic IP address of the target register a callback that snags the IP from sock.getpeername()[0]. @type src: String or Request (Node) @param src: Source request name or request node @type dst: String or Request (Node) @param dst: Destination request name or request node @type callback: Function @param callback: (Optional, def=None) Callback function to pass received data to between node xmits @rtype: pgraph.edge @return: The edge between the src and dst. ''' # if only a source was provided, then make it the destination and set the source to the root node. if not dst: dst = src src = self.root # if source or destination is a name, resolve the actual node. if type(src) is str: src = self.find_node("name", src) if type(dst) is str: dst = self.find_node("name", dst) # if source or destination is not in the graph, add it. if src != self.root and not self.find_node("name", src.name): self.add_node(src) if not self.find_node("name", dst.name): self.add_node(dst) # create an edge between the two nodes and add it to the graph. edge = connection(src.id, dst.id, callback) self.add_edge(edge) return edge #################################################################################################################### def export_file(self): ''' Dump various object values to disk. @see: import_file() ''' if not self.session_filename: return data = {} data["session_filename"] = self.session_filename data["skip"] = self.total_mutant_index data["sleep_time"] = self.sleep_time data["restart_sleep_time"] = self.restart_sleep_time data["proto"] = self.proto data["restart_interval"] = self.restart_interval data["timeout"] = self.timeout data["web_port"] = self.web_port data["crash_threshold"] = self.crash_threshold data["total_num_mutations"] = self.total_num_mutations data["total_mutant_index"] = self.total_mutant_index data["netmon_results"] = self.netmon_results data["procmon_results"] = self.procmon_results data['protmon_results'] = self.protmon_results data["pause_flag"] = self.pause_flag fh = open(self.session_filename, "wb+") fh.write(zlib.compress(cPickle.dumps(data, protocol=2))) fh.close() #################################################################################################################### def fuzz(self, this_node=None, path=[]): ''' Call this routine to get the ball rolling. No arguments are necessary as they are both utilized internally during the recursive traversal of the session graph. @type this_node: request (node) @param this_node: (Optional, def=None) Current node that is being fuzzed. @type path: List @param path: (Optional, def=[]) Nodes along the path to the current one being fuzzed. ''' # if no node is specified, then we start from the root node and initialize the session. if not this_node: # we can't fuzz if we don't have at least one target and one request. if not self.targets: raise sex.error("NO TARGETS SPECIFIED IN SESSION") if not self.edges_from(self.root.id): raise sex.error("NO REQUESTS SPECIFIED IN SESSION") this_node = self.root try: self.server_init() except: return # XXX - TODO - complete parallel fuzzing, will likely have to thread out each target target = self.targets[0] # step through every edge from the current node. for edge in self.edges_from(this_node.id): # the destination node is the one actually being fuzzed. self.fuzz_node = self.nodes[edge.dst] num_mutations = self.fuzz_node.num_mutations() # keep track of the path as we fuzz through it, don't count the root node. # we keep track of edges as opposed to nodes because if there is more then one path through a set of # given nodes we don't want any ambiguity. path.append(edge) current_path = " -> ".join( [self.nodes[e.src].name for e in path[1:]]) current_path += " -> %s" % self.fuzz_node.name self.logger.error("current fuzz path: %s" % current_path) self.logger.error( "fuzzed %d of %d total cases" % (self.total_mutant_index, self.total_num_mutations)) done_with_fuzz_node = False crash_count = 0 # loop through all possible mutations of the fuzz node. while not done_with_fuzz_node: # if we need to pause, do so. self.pause() # if we have exhausted the mutations of the fuzz node, break out of the while(1). # note: when mutate() returns False, the node has been reverted to the default (valid) state. if not self.fuzz_node.mutate(): self.logger.error( "all possible mutations for current fuzz node exhausted" ) done_with_fuzz_node = True continue # make a record in the session that a mutation was made. self.total_mutant_index += 1 # if we've hit the restart interval, restart the target. if self.restart_interval and self.total_mutant_index % self.restart_interval == 0: self.logger.error("restart interval of %d reached" % self.restart_interval) self.restart_target(target) # exception error handling routine, print log message and restart target. def error_handler(e, msg, target, sock=None): if sock: sock.close() msg += "\nException caught: %s" % repr(e) msg += "\nRestarting target and trying again" self.logger.critical(msg) self.restart_target(target) # if we don't need to skip the current test case. if self.total_mutant_index > self.skip: self.logger.error( "fuzzing %d of %d" % (self.fuzz_node.mutant_index, num_mutations)) # attempt to complete a fuzz transmission. keep trying until we are successful, whenever a failure # occurs, restart the target. while 1: # instruct the debugger/sniffer that we are about to send a new fuzz. if target.procmon: try: target.procmon.pre_send( self.total_mutant_index) except Exception, e: error_handler(e, "failed on procmon.pre_send()", target) continue if target.netmon: try: target.netmon.pre_send(self.total_mutant_index) except Exception, e: error_handler(e, "failed on netmon.pre_send()", target) continue try: # establish a connection to the target. sock = socket.socket(socket.AF_INET, self.proto) except Exception, e: error_handler(e, "failed creating socket", target) continue if self.bind: try: sock.bind(self.bind) except Exception, e: error_handler(e, "failed binding on socket", target, sock) continue try: sock.settimeout(self.timeout) # Connect is needed only for TCP stream if self.proto == socket.SOCK_STREAM: sock.connect((target.host, target.port)) except Exception, e: error_handler(e, "failed connecting on socket", target, sock) continue # if SSL is requested, then enable it. if self.ssl: try: ssl = socket.ssl(sock) sock = httplib.FakeSocket(sock, ssl) except Exception, e: error_handler(e, "failed ssl setup", target, sock) continue
user = '******' passwd = 'proxy_pass' host = 'login.yahoo.com' port = 443 phost = 'proxy_host' pport = 80 # setup basic authentication user_pass = base64.encodestring(user + ':' + passwd) proxy_authorization = 'Proxy-authorization: Basic ' + user_pass + '\r\n' proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % (host, port) user_agent = 'User-Agent: python\r\n' proxy_pieces = proxy_connect + proxy_authorization + user_agent + '\r\n' # connect to the proxy proxy_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) proxy_socket.connect((phost, pport)) proxy_socket.sendall(proxy_pieces + '\r\n') response = proxy_socket.recv(8192) status = response.split()[1] if status != '200': raise IOError, 'Connecting to proxy: status=%s' % status # trivial setup for SSL socket ssl = socket.ssl(proxy_socket, None, None) sock = httplib.FakeSocket(proxy_socket, ssl) # initialize httplib and replace the connection's socket with the SSL one h = httplib.HTTPConnection('localhost') h.sock = sock # and finally, use the now-HTTPS httplib connection as you wish h.request('GET', '/') r = h.getresponse() print r.read()
def post_multipart(self, host, selector, fields, files): """ Post fields to an https host as multipart/form-data. fields is a sequence of (name, value) elements for regular form fields. files is a sequence of (name, filename, value) elements for data to be uploaded as files. Return the server's response page. """ # HTTPS standard port to connect to the server port = 443 if (os.environ.has_key('https_proxy')): # Get the proxy details p = re.compile("(\S+)://(\S+):(\d+)") match = p.match(os.environ['https_proxy']) if (match): phost = match.group(2) pport = int(match.group(3)) #setup basic authentication user=self.user passwd="" user_pass=base64.encodestring(user+':'+passwd) proxy_authorization='Proxy-authorization: Basic '+user_pass+'\r\n' proxy_connect='CONNECT %s:%s HTTP/1.0\r\n'%(host,port) user_agent='User-Agent: python\r\n' proxy_pieces=proxy_connect+proxy_authorization+user_agent+'\r\n' #now connect, very simple recv and error checking proxy=socket.socket(socket.AF_INET,socket.SOCK_STREAM) proxy.connect((phost,pport)) proxy.sendall(proxy_pieces) response=proxy.recv(8192) status=response.split()[1] if status!=str(200): raise 'Error status=',str(status) try: # trivial setup for ssl socket ssl = socket.ssl(proxy, None, None) sock = httplib.FakeSocket(proxy, ssl) #initalize httplib and replace with your socket h=httplib.HTTPConnection('localhost') h.sock=sock except: # revert back to standard socket port = 80 h=httplib.HTTPConnection(phost,pport) selector = "http://%s:%d%s" % (host,port,selector) else: if (hasattr(socket,"ssl")): h = httplib.HTTPSConnection(host=host, port=port) else: port = 80 h = httplib.HTTPConnection(host=host, port=port) content_type, body = self.encode_multipart_formdata(fields, files) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() h.send(body) response = h.getresponse() return response.read()
def _get_connection(self, uri, headers=None): # Check to see if there are proxy settings required for this request. proxy = None if uri.scheme == 'https': proxy = os.environ.get('https_proxy') elif uri.scheme == 'http': proxy = os.environ.get('http_proxy') if not proxy: return HttpClient._get_connection(self, uri, headers=headers) # Now we have the URL of the appropriate proxy server. # Get a username and password for the proxy if required. proxy_auth = _get_proxy_auth() if uri.scheme == 'https': import socket if proxy_auth: proxy_auth = 'Proxy-authorization: %s' % proxy_auth # Construct the proxy connect command. port = uri.port if not port: port = 443 proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % (uri.host, port) # Set the user agent to send to the proxy user_agent = '' if headers and 'User-Agent' in headers: user_agent = 'User-Agent: %s\r\n' % (headers['User-Agent']) proxy_pieces = '%s%s%s\r\n' % (proxy_connect, proxy_auth, user_agent) # Find the proxy host and port. proxy_uri = Uri.parse_uri(proxy) if not proxy_uri.port: proxy_uri.port = '80' # Connect to the proxy server, very simple recv and error checking p_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) p_sock.connect((proxy_uri.host, int(proxy_uri.port))) p_sock.sendall(proxy_pieces) response = '' # Wait for the full response. while response.find("\r\n\r\n") == -1: response += p_sock.recv(8192) p_status = response.split()[1] if p_status != str(200): raise ProxyError('Error status=%s' % str(p_status)) # Trivial setup for ssl socket. sslobj = None if ssl is not None: sslobj = ssl.wrap_socket(p_sock, None, None) else: sock_ssl = socket.ssl(p_sock, None, Nonesock_) sslobj = httplib.FakeSocket(p_sock, sock_ssl) # Initalize httplib and replace with the proxy socket. connection = httplib.HTTPConnection(proxy_uri.host) connection.sock = sslobj return connection elif uri.scheme == 'http': proxy_uri = Uri.parse_uri(proxy) if not proxy_uri.port: proxy_uri.port = '80' if proxy_auth: headers['Proxy-Authorization'] = proxy_auth.strip() return httplib.HTTPConnection(proxy_uri.host, int(proxy_uri.port)) return None
def connect(self): log.debug('ProxyHTTPSConnection.connectt():Called... ') ProxyHTTPConnection.connect(self) #make the sock ssl-aware ssl = socket.ssl(self.sock, self.key_file, self.cert_file) self.sock = httplib.FakeSocket(self.sock, ssl)
def connect(self): sock = _create_connection((self.host, self.port), self.timeout) ssl = socket.ssl(sock, self.key_file, self.cert_file) self.sock = httplib.FakeSocket(sock, ssl)
def PrepareConnection(service, full_uri): """Opens a connection to the server based on the full URI. This method is deprecated, instead use atom.http.HttpClient.request. Examines the target URI and the proxy settings, which are set as environment variables, to open a connection with the server. This connection is used to make an HTTP request. Args: service: atom.AtomService or a subclass. It must have a server string which represents the server host to which the request should be made. It may also have a dictionary of additional_headers to send in the HTTP request. full_uri: str Which is the target relative (lacks protocol and host) or absolute URL to be opened. Example: 'https://www.google.com/accounts/ClientLogin' or 'base/feeds/snippets' where the server is set to www.google.com. Returns: A tuple containing the httplib.HTTPConnection and the full_uri for the request. """ deprecation('calling deprecated function PrepareConnection') (server, port, ssl, partial_uri) = ProcessUrl(service, full_uri) if ssl: # destination is https proxy = os.environ.get('https_proxy') if proxy: (p_server, p_port, p_ssl, p_uri) = ProcessUrl(service, proxy, True) proxy_username = os.environ.get('proxy-username') if not proxy_username: proxy_username = os.environ.get('proxy_username') proxy_password = os.environ.get('proxy-password') if not proxy_password: proxy_password = os.environ.get('proxy_password') if proxy_username: user_auth = base64.encodestring( '%s:%s' % (proxy_username, proxy_password)) proxy_authorization = ('Proxy-authorization: Basic %s\r\n' % (user_auth.strip())) else: proxy_authorization = '' proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % (server, port) user_agent = 'User-Agent: %s\r\n' % ( service.additional_headers['User-Agent']) proxy_pieces = (proxy_connect + proxy_authorization + user_agent + '\r\n') #now connect, very simple recv and error checking p_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) p_sock.connect((p_server, p_port)) p_sock.sendall(proxy_pieces) response = '' # Wait for the full response. while response.find("\r\n\r\n") == -1: response += p_sock.recv(8192) p_status = response.split()[1] if p_status != str(200): raise atom.http.ProxyError('Error status=%s' % p_status) # Trivial setup for ssl socket. ssl = socket.ssl(p_sock, None, None) fake_sock = httplib.FakeSocket(p_sock, ssl) # Initalize httplib and replace with the proxy socket. connection = httplib.HTTPConnection(server) connection.sock = fake_sock full_uri = partial_uri else: connection = httplib.HTTPSConnection(server, port) full_uri = partial_uri else: # destination is http proxy = os.environ.get('http_proxy') if proxy: (p_server, p_port, p_ssl, p_uri) = ProcessUrl(service.server, proxy, True) proxy_username = os.environ.get('proxy-username') if not proxy_username: proxy_username = os.environ.get('proxy_username') proxy_password = os.environ.get('proxy-password') if not proxy_password: proxy_password = os.environ.get('proxy_password') if proxy_username: UseBasicAuth(service, proxy_username, proxy_password, True) connection = httplib.HTTPConnection(p_server, p_port) if not full_uri.startswith("http://"): if full_uri.startswith("/"): full_uri = "http://%s%s" % (service.server, full_uri) else: full_uri = "http://%s/%s" % (service.server, full_uri) else: connection = httplib.HTTPConnection(server, port) full_uri = partial_uri return (connection, full_uri)
def connect(self): sock = TimeoutSocket(self.timeout) sock.connect((self.host, self.port)) realsock = getattr(sock.sock, '_sock', sock.sock) ssl = socket.ssl(realsock, self.key_file, self.cert_file) self.sock = httplib.FakeSocket(sock, ssl)
def makeSoapRequest(self, soapRequest, retry=True): common.debug('soap.manager makeSoapRequest(): %s' % soapRequest, 'soap') if soapRequest.host in self.msn.tokens: soapRequest.body = soapRequest.body.replace("&tickettoken;", self.msn.tokens[soapRequest.host]['security']\ .replace('&', '&')) # TODO: change to putheader headers = { "SOAPAction": soapRequest.action, "Content-Type": "text/xml; charset=utf-8", # "Cookie": "MSPAuth=" + self.msn.MSPAuth + ";MSPProf=" + # self.msn.MSPProf + soapRequest.extraCookie, "Host": soapRequest.host, "Content-Length": str(len(soapRequest.body)), "User-Agent": "MSN Explorer/9.0 (MSN 8.0; TmstmpExt)", "Connection": "Keep-Alive", "Cache-Control": "no-cache", "Accept-encoding": "gzip", # highly improves bandwidth usage } conn = None response = None if soapRequest.proxy and soapRequest.proxy.host: common.debug('>>> using proxy host: ' + soapRequest.proxy.host) proxy_connect = 'CONNECT %s:%s HTTP/1.0\r\n' % ( soapRequest.host, str(soapRequest.port)) user_agent = 'User-Agent: python\r\n' if soapRequest.proxy.user: common.debug('>>> using proxy auth user: '******':' + soapRequest.proxy.password).replace('\n', '') proxy_authorization = 'Proxy-authorization: Basic ' + user_pass + '\r\n' proxy_pieces = proxy_connect + proxy_authorization + user_agent + '\r\n' else: proxy_pieces = proxy_connect + user_agent + '\r\n' # now connect, very simple recv and error checking proxy = socket.socket(socket.AF_INET, socket.SOCK_STREAM) proxy.connect( (soapRequest.proxy.host, int(soapRequest.proxy.port))) proxy.sendall(proxy_pieces) response = proxy.recv(8192) status = response.split()[1] if status != str(200): raise ValueError, 'Error status=%s' % str(status) # trivial setup for ssl socket if HAVE_PY25: sslconn = socket.ssl(proxy, None, None) sock = httplib.FakeSocket(proxy, sslconn) else: sock = ssl.wrap_socket(proxy, None, None) conn = httplib.HTTPConnection('localhost') conn.sock = sock else: if soapRequest.port == 443: conn = httplib.HTTPSConnection(soapRequest.host, soapRequest.port) else: conn = httplib.HTTPConnection(soapRequest.host, soapRequest.port) if soapRequest.action == 'http://www.msn.com/webservices/storage/w10/CreateDocument' or \ soapRequest.action == 'http://www.msn.com/webservices/storage/w10/DeleteRelationships': #print soapRequest.path, soapRequest.body, headers if os.name == "nt": tempfile = os.environ[ 'TEMP'] + os.sep + "createdocumentsoap.txt" tempfile = unicode(tempfile) else: tempfile = '/tmp/createdocumentsoap.txt' f = open(tempfile, 'w') f.write(soapRequest.body) f.close() conn.request("POST", soapRequest.path, soapRequest.body, headers) response = conn.getresponse() data = response.read() isGzipd = response.getheader('Content-Encoding', '') if isGzipd == 'gzip': # data is gzipped, unzipit! cstream = StringIO.StringIO(data) gzpr = gzip.GzipFile(fileobj=cstream) data = gzpr.read() soapResponse = SoapRequest(soapRequest.proxy, soapRequest.action, soapRequest.host, soapRequest.port, soapRequest.path, data, soapRequest.callback, soapRequest.args) soapResponse.status = (response.status, response.reason) if soapResponse.body.count('TweenerChallenge') or \ soapResponse.body.count('LockKeyChallenge'): retry = False if retry and soapResponse.body.count('AuthenticationFailure') or \ soapResponse.body.count('PassportAuthFail'): self.msn.passportReAuth() soapResponse = self.makeSoapRequest(soapRequest, False) return soapResponse