SSL/TLS

Working with SSL/TLS connections is a very important job and it comes in tow shapes. (1) Secure HTTP connection. (2) Secure Socket. To reduce the redundancy, I’ll deal with both in this section, instead of putting the http part under Web Kung Fu section.

Certificate Validation

Validate HTTPS Certificate

validate_https_cert.rb

  1. #!/usr/bin/env ruby
  2. #
  3. # KING SABRI | @KINGSABRI
  4. #
  5. require 'open-uri'
  6. def validate_https_cert(target) begin
  7. open("https://#{target}")
  8. puts '[+] Valid SSL Certificate!'
  9. rescue OpenSSL::SSL::SSLError
  10. puts '[+] Invalid SSL Certificate!'
  11. end
  12. end
  13. good_ssl = 'google.com'
  14. bad_ssl = 'expired.badssl.com'
  15. validate_https_cert good_ssl
  16. validate_https_cert bad_ssl

Validate Secure Socket Certificate

validate_socket_cert.rb

  1. #!/usr/bin/env ruby
  2. #
  3. # KING SABRI | @KINGSABRI
  4. #
  5. require 'socket'
  6. require 'openssl'
  7. def validate_socket_cert(target)
  8. ssl_context = OpenSSL::SSL::SSLContext.new
  9. ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
  10. cert_store = OpenSSL::X509::Store.new
  11. cert_store.set_default_paths
  12. ssl_context.cert_store = cert_store
  13. socket = TCPSocket.new(target, 443)
  14. ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
  15. begin
  16. ssl_socket.connect
  17. puts '[+] Valid SSL Certificate!'
  18. rescue OpenSSL::SSL::SSLError
  19. puts '[+] Invalid SSL Certificate!'
  20. end
  21. end
  22. good_ssl = 'google.com'
  23. bad_ssl = 'expired.badssl.com'
  24. validate_socket_cert good_ssl
  25. validate_socket_cert bad_ssl

Putting all together

ssl_validator.rb

  1. #!/usr/bin/env ruby
  2. #
  3. # SSL/TLS validator
  4. # KING SABRI | @KINGSABRI
  5. #
  6. def validate_ssl(target, conn_type=:web)
  7. case conn_type
  8. # Web Based SSL
  9. when :web
  10. require 'open-uri'
  11. begin
  12. open("https://#{target}")
  13. puts '[+] Valid SSL Certificate!'
  14. rescue OpenSSL::SSL::SSLError
  15. puts '[+] Invalid SSL Certificate!'
  16. end
  17. # Socked Based SSL
  18. when :socket
  19. require 'socket'
  20. require 'openssl'
  21. ssl_context = OpenSSL::SSL::SSLContext.new
  22. ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
  23. cert_store = OpenSSL::X509::Store.new
  24. cert_store.set_default_paths
  25. ssl_context.cert_store = cert_store
  26. socket = TCPSocket.new(target, 443)
  27. ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
  28. begin
  29. ssl_socket.connect
  30. puts '[+] Valid SSL Certificate!'
  31. rescue OpenSSL::SSL::SSLError
  32. puts '[+] Invalid SSL Certificate!'
  33. end
  34. else
  35. puts '[!] Unknown connection type!'
  36. end
  37. end
  38. good_ssl = 'google.com'
  39. bad_ssl = 'expired.badssl.com'
  40. validate_ssl(bad_ssl, :web)
  41. validate_ssl(bad_ssl, :socket)
  42. validate_ssl(good_ssl, :web)
  43. validate_ssl(good_ssl, :socket)

Run it

  1. ruby ssl_validator.rb
  2. [+] Invalid SSL Certificate!
  3. [+] Invalid SSL Certificate!
  4. [+] Valid SSL Certificate!
  5. [+] Valid SSL Certificate!