Changeset 850:aafbb3d3b3eb


Ignore:
Timestamp:
02/04/12 23:14:59 (4 months ago)
Author:
xeraph
Branch:
default
Children:
852:5a74670b3de6, 853:27b5e31d681d
Message:

implemented simple ssl cipher suite scanning feature

Location:
kraken-sslscan
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • kraken-sslscan/pom.xml

    r844 r850  
    2626                                                <Export-Package>org.krakenapps.sslscan</Export-Package> 
    2727                                                <Import-Package>*</Import-Package> 
     28                                                <Main-Class>org.krakenapps.sslscan.SslScanner</Main-Class> 
    2829                                        </instructions> 
    2930                                </configuration> 
  • kraken-sslscan/src/main/java/org/krakenapps/sslscan/SslScanner.java

    r844 r850  
    22 
    33import java.io.IOException; 
    4 import java.io.InputStream; 
    5 import java.net.InetAddress; 
    6 import java.net.MalformedURLException; 
    74import java.net.Socket; 
    8 import java.net.URL; 
    9 import java.net.URLConnection; 
    10 import java.net.UnknownHostException; 
    115import java.security.NoSuchAlgorithmException; 
    126import java.security.SecureRandom; 
     
    148import java.security.cert.X509Certificate; 
    159 
    16 import javax.net.ssl.HostnameVerifier; 
    17 import javax.net.ssl.HttpsURLConnection; 
     10import javax.net.SocketFactory; 
     11import javax.net.ssl.HandshakeCompletedEvent; 
     12import javax.net.ssl.HandshakeCompletedListener; 
    1813import javax.net.ssl.SSLContext; 
    19 import javax.net.ssl.SSLSession; 
    20 import javax.net.ssl.SSLSocketFactory; 
     14import javax.net.ssl.SSLHandshakeException; 
     15import javax.net.ssl.SSLParameters; 
     16import javax.net.ssl.SSLSocket; 
    2117import javax.net.ssl.TrustManager; 
    2218import javax.net.ssl.X509TrustManager; 
    2319 
     20import sun.security.validator.ValidatorException; 
     21 
     22@SuppressWarnings("restriction") 
    2423public class SslScanner { 
     24        private SSLContext ctx; 
     25 
    2526        public static void main(String[] args) throws Exception { 
    26                 HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { 
     27                new SslScanner().run(args); 
     28        } 
     29 
     30        public void run(String[] args) throws Exception { 
     31                if (args.length < 2) { 
     32                        System.out.println("SSL Cipher Suite Scanner, xeraph@nchovy.com"); 
     33                        System.out.println("Usage: java -jar kraken-sslscan.jar [hostname] [port]"); 
     34                        return; 
     35                } 
     36 
     37                ctx = SSLContext.getDefault(); 
     38                String hostname = args[0]; 
     39                Integer port = Integer.valueOf(args[1]); 
     40 
     41                checkCertificate(hostname, port); 
     42                checkAllCipherSuites(ctx, hostname, port); 
     43        } 
     44 
     45        private void checkCertificate(String hostname, int port) throws Exception { 
     46                try { 
     47                        checkCipherSuite(hostname, port, null); 
     48                } catch (SSLHandshakeException e) { 
     49                        if (e.getCause() instanceof ValidatorException) { 
     50                                System.out.println("Warning: Invalid Certificate, Ignoring.."); 
     51                                System.out.println(">> " + e.getCause().getMessage()); 
     52 
     53                                ctx = SSLContext.getInstance("SSL"); 
     54                                ctx.init(null, trustAllCerts, new SecureRandom()); 
     55                        } 
     56                } 
     57        } 
     58 
     59        private void checkAllCipherSuites(SSLContext ctx, String hostname, Integer port) throws NoSuchAlgorithmException { 
     60                SSLParameters sslParams = ctx.getSupportedSSLParameters(); 
     61                for (String cipher : sslParams.getCipherSuites()) { 
     62                        try { 
     63                                checkCipherSuite(hostname, port, cipher); 
     64                                System.out.println("PASS " + cipher); 
     65                        } catch (IOException e) { 
     66                                System.out.println("FAIL " + cipher); 
     67                        } 
     68                } 
     69        } 
     70 
     71        public void checkCipherSuite(String hostname, int port, String cipher) throws IOException { 
     72                SocketFactory socketFactory = ctx.getSocketFactory(); 
     73                Socket socket = socketFactory.createSocket(hostname, port); 
     74                SSLSocket sslSocket = (SSLSocket) socket; 
     75 
     76                if (cipher != null) 
     77                        sslSocket.setEnabledCipherSuites(new String[] { cipher }); 
     78 
     79                sslSocket.addHandshakeCompletedListener(new HandshakeCompletedListener() { 
    2780                        @Override 
    28                         public boolean verify(String hostname, SSLSession session) { 
    29                                 System.out.println(hostname); 
    30                                 System.out.println(session); 
    31                                 System.out.println("CIPHER SUITE: " + session.getCipherSuite()); 
    32                                 return true; 
     81                        public void handshakeCompleted(HandshakeCompletedEvent e) { 
    3382                        } 
    3483                }); 
    35                  
    36                 TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { 
    37                         @Override 
    38                         public X509Certificate[] getAcceptedIssuers() { 
    39                                 return null; 
    40                         } 
    4184 
    42                         @Override 
    43                         public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
    44                         } 
     85                sslSocket.startHandshake(); 
     86        } 
    4587 
    46                         @Override 
    47                         public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
    48                         } 
    49                 } }; 
    50  
    51                 SSLContext sc = SSLContext.getInstance("SSL"); 
    52                 sc.init(null, trustAllCerts, new SecureRandom()); 
    53                 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
    54                 URLConnection con = new URL("https://xtm8000").openConnection(); 
    55                 byte[] b = new byte[8096]; 
    56  
    57                 StringBuilder sb = new StringBuilder(); 
    58                 InputStream is = con.getInputStream(); 
    59                 while (true) { 
    60                         int readBytes = is.read(b); 
    61                         if (readBytes <= 0) 
    62                                 break; 
    63  
    64                         sb.append(new String(b, 0, readBytes)); 
     88        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { 
     89                @Override 
     90                public X509Certificate[] getAcceptedIssuers() { 
     91                        return null; 
    6592                } 
    6693 
    67                 System.out.println("read " + sb.toString().length() + " chars"); 
    68         } 
     94                @Override 
     95                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
     96                } 
     97 
     98                @Override 
     99                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
     100                } 
     101        } }; 
     102 
    69103} 
Note: See TracChangeset for help on using the changeset viewer.