def send_request(self, ssl_client: SslClient) -> str: """Send an HTTP GET to the server and return the HTTP status code. """ try: ssl_client.write(HttpRequestGenerator.get_request(self._hostname)) # Parse the response and print the Location header http_response = HttpResponseParser.parse_from_ssl_connection(ssl_client) if http_response.version == 9: # HTTP 0.9 => Probably not an HTTP response result = self.ERR_NOT_HTTP else: redirect = '' if 300 <= http_response.status < 400: redirect_location = http_response.getheader('Location') if redirect_location: # Add redirection URL to the result redirect = f' - {redirect_location}' result = self.GET_RESULT_FORMAT.format(http_response.status, http_response.reason, redirect) except socket.timeout: result = self.ERR_HTTP_TIMEOUT except IOError: result = self.ERR_GENERIC return result
def send_request(self, ssl_client: SslClient) -> str: """Send an HTTP GET to the server and return the HTTP status code. """ try: ssl_client.write(HttpRequestGenerator.get_request(self._hostname)) # Parse the response and print the Location header http_response = HttpResponseParser.parse_from_ssl_connection( ssl_client) if http_response.version == 9: # HTTP 0.9 => Probably not an HTTP response result = self.ERR_NOT_HTTP else: redirect = "" if 300 <= http_response.status < 400: redirect_location = http_response.getheader("Location") if redirect_location: # Add redirection URL to the result redirect = f" - {redirect_location}" result = self.GET_RESULT_FORMAT.format(http_response.status, http_response.reason, redirect) except socket.timeout: result = self.ERR_HTTP_TIMEOUT except IOError: result = self.ERR_GENERIC return result
def post_handshake_check(self): # type: () -> Text try: # Send an HTTP GET to the server and store the HTTP Status Code self.write(HttpRequestGenerator.get_request(self._hostname)) # Parse the response and print the Location header http_response = HttpResponseParser.parse_from_ssl_connection(self) if http_response.version == 9: # HTTP 0.9 => Probably not an HTTP response result = self.ERR_NOT_HTTP else: redirect = '' if 300 <= http_response.status < 400: if http_response.getheader('Location', None): # Add redirection URL to the result redirect = ' - ' + http_response.getheader( 'Location', None) result = self.GET_RESULT_FORMAT.format(http_response.status, http_response.reason, redirect) except socket.timeout: result = self.ERR_HTTP_TIMEOUT except IOError: result = self.ERR_GENERIC return result
def _get_security_headers(cls, server_info): hpkp_report_only = False # Perform the SSL handshake ssl_connection = server_info.get_preconfigured_ssl_connection() ssl_connection.connect() certificate_chain = [ load_pem_x509_certificate(x509_cert.as_pem().encode('ascii'), backend=default_backend()) for x509_cert in ssl_connection.ssl_client.get_peer_cert_chain() ] # Send an HTTP GET request to the server ssl_connection.write(HttpRequestGenerator.get_request(host=server_info.hostname)) http_resp = HttpResponseParser.parse_from_ssl_connection(ssl_connection) ssl_connection.close() if http_resp.version == 9: # HTTP 0.9 => Probably not an HTTP response raise ValueError('Server did not return an HTTP response') else: hsts_header = http_resp.getheader('strict-transport-security', None) hpkp_header = http_resp.getheader('public-key-pins', None) expect_ct_header = http_resp.getheader('expect-ct', None) if hpkp_header is None: hpkp_report_only = True hpkp_header = http_resp.getheader('public-key-pins-report-only', None) # We do not follow redirections because the security headers must be set on the first page according to # https://hstspreload.appspot.com/: # "If you are serving an additional redirect from your HTTPS site, that redirect must still have the HSTS # header (rather than the page it redirects to)." return hsts_header, hpkp_header, expect_ct_header, hpkp_report_only, certificate_chain
def process_task( self, server_info: ServerConnectivityInfo, scan_command: PluginScanCommand ) -> 'HttpHeadersScanResult': if not isinstance(scan_command, HttpHeadersScanCommand): raise ValueError('Unexpected scan command') if server_info.tls_wrapped_protocol not in [TlsWrappedProtocolEnum.PLAIN_TLS, TlsWrappedProtocolEnum.HTTPS]: raise ValueError('Cannot test for HTTP headers on a StartTLS connection.') # Perform the SSL handshake mozilla_store = TrustStoresRepository.get_default().get_main_store() ssl_connection = server_info.get_preconfigured_ssl_connection(ssl_verify_locations=mozilla_store.path) try: ssl_connection.connect() try: verified_chain_as_pem = ssl_connection.ssl_client.get_verified_chain() except CouldNotBuildVerifiedChain: verified_chain_as_pem = None # Send an HTTP GET request to the server ssl_connection.ssl_client.write(HttpRequestGenerator.get_request(host=server_info.hostname)) # We do not follow redirections because the security headers must be set on the first page according to # https://hstspreload.appspot.com/: # "If you are serving an additional redirect from your HTTPS site, that redirect must still have the HSTS # header (rather than the page it redirects to)." http_response = HttpResponseParser.parse_from_ssl_connection(ssl_connection.ssl_client) finally: ssl_connection.close() if http_response.version == 9: # HTTP 0.9 => Probably not an HTTP response raise ValueError('Server did not return an HTTP response') # Parse the certificate chain verified_chain = [ load_pem_x509_certificate(cert_as_pem.encode('ascii'), backend=default_backend()) for cert_as_pem in verified_chain_as_pem ] if verified_chain_as_pem else None # Parse each header hsts_header = StrictTransportSecurityHeader.from_http_response(http_response) expect_ct_header = ExpectCtHeader.from_http_response(http_response) hpkp_header = PublicKeyPinsHeader.from_http_response(http_response) hpkp_report_only_header = PublicKeyPinsReportOnlyHeader.from_http_response(http_response) return HttpHeadersScanResult( server_info, scan_command, hsts_header, hpkp_header, hpkp_report_only_header, expect_ct_header, verified_chain )
def _get_security_headers( cls, server_info: ServerConnectivityInfo ) -> Tuple[Optional[str], Optional[str], Optional[str], bool, List[Certificate]]: hpkp_report_only = False # Perform the SSL handshake ssl_connection = server_info.get_preconfigured_ssl_connection() try: ssl_connection.connect() certificate_chain = [ load_pem_x509_certificate(x509_cert.as_pem().encode('ascii'), backend=default_backend()) for x509_cert in ssl_connection.ssl_client.get_peer_cert_chain() ] # Send an HTTP GET request to the server ssl_connection.ssl_client.write(HttpRequestGenerator.get_request(host=server_info.hostname)) http_resp = HttpResponseParser.parse_from_ssl_connection(ssl_connection.ssl_client) finally: ssl_connection.close() if http_resp.version == 9: # HTTP 0.9 => Probably not an HTTP response raise ValueError('Server did not return an HTTP response') else: hsts_header = http_resp.getheader('strict-transport-security', None) hpkp_header = http_resp.getheader('public-key-pins', None) expect_ct_header = http_resp.getheader('expect-ct', None) if hpkp_header is None: hpkp_report_only = True hpkp_header = http_resp.getheader('public-key-pins-report-only', None) # We do not follow redirections because the security headers must be set on the first page according to # https://hstspreload.appspot.com/: # "If you are serving an additional redirect from your HTTPS site, that redirect must still have the HSTS # header (rather than the page it redirects to)." return hsts_header, hpkp_header, expect_ct_header, hpkp_report_only, certificate_chain
def process_task( self, server_info: ServerConnectivityInfo, scan_command: PluginScanCommand) -> "HttpHeadersScanResult": if not isinstance(scan_command, HttpHeadersScanCommand): raise ValueError("Unexpected scan command") if server_info.tls_wrapped_protocol not in [ TlsWrappedProtocolEnum.PLAIN_TLS, TlsWrappedProtocolEnum.HTTPS ]: raise ValueError( "Cannot test for HTTP headers on a StartTLS connection.") # Perform the SSL handshake mozilla_store = TrustStoresRepository.get_default().get_main_store() ssl_connection = server_info.get_preconfigured_ssl_connection( ssl_verify_locations=mozilla_store.path) try: ssl_connection.connect() try: verified_chain_as_pem = ssl_connection.ssl_client.get_verified_chain( ) except CouldNotBuildVerifiedChain: verified_chain_as_pem = None # Send an HTTP GET request to the server ssl_connection.ssl_client.write( HttpRequestGenerator.get_request(host=server_info.hostname)) # We do not follow redirections because the security headers must be set on the first page according to # https://hstspreload.appspot.com/: # "If you are serving an additional redirect from your HTTPS site, that redirect must still have the HSTS # header (rather than the page it redirects to)." http_response = HttpResponseParser.parse_from_ssl_connection( ssl_connection.ssl_client) finally: ssl_connection.close() if http_response.version == 9: # HTTP 0.9 => Probably not an HTTP response raise ValueError("Server did not return an HTTP response") # Parse the certificate chain verified_chain = ([ load_pem_x509_certificate(cert_as_pem.encode("ascii"), backend=default_backend()) for cert_as_pem in verified_chain_as_pem ] if verified_chain_as_pem else None) # Parse each header hsts_header = StrictTransportSecurityHeader.from_http_response( http_response) expect_ct_header = ExpectCtHeader.from_http_response(http_response) hpkp_header = PublicKeyPinsHeader.from_http_response(http_response) hpkp_report_only_header = PublicKeyPinsReportOnlyHeader.from_http_response( http_response) return HttpHeadersScanResult( server_info, scan_command, hsts_header, hpkp_header, hpkp_report_only_header, expect_ct_header, verified_chain, )