0

I need to setup a demo Android app that can help learn how ignoring certificate validation could lead to MITM.

At the moment, my test server has a self signed certificate, created as follows (see here) :

 sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt

Now to test an Android app with this server (which has a self signed certificate), I made the code work by using this reference for setting up keystore and sending http post/get to my server.

A sample of my HTTP POST code (MyHttpClient is as per reference) :

HttpClient httpclient = new MyHttpClient(getApplicationContext());

HttpPost httpPost = new HttpPost("https://www.testwebsite.com/api/rest/json/?");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("method", "gettoken"));
nvps.add(new BasicNameValuePair("username",<Username> ));
nvps.add(new BasicNameValuePair("password", <Password>));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpPost);

If I understand correctly, the keystore step (in the reference), specifically :

 final KeyStore ks = KeyStore.getInstance("BKS");
 final InputStream inputStream = appContext.getResources().openRawResource(R.raw.certs);
 ks.load(inputStream, appContext.getString(R.string.store_pass).toCharArray());
 inputStream.close();

 ret = new SSLSocketFactory(ks);

is trusting exactly the certificate I want. So here I don't have control over the CA of the certificate (correct me if I'm wrong).

To demo MITM, I need to have certificate signed by same CA, but for a different domain, and so without certificate validation, the app will talk to the wrong server.

I have a thought on how to correct my demo, and want to confirm whether it would be the right direction.

What I'm thinking is that on my Apache server, I will first create a CA, then create a certificate for the benign domain with that CA.

In my app, I'm thinking to replace the self-signed certificate file with the CA's file and change the code above to refer to the CA's certificate kept in app's raw resources (I presume the SSL handshake will still work - need some confirmation here .. ).

I think with this change, if I send down a certificate signed by the same CA but for a malicious domain, the app without any certificate validation, will accept the certificate and start communicating with that.

If any one could please comment on my thought, it would be appreciated.

Community
  • 1
  • 1
Jake
  • 16,329
  • 50
  • 126
  • 202

1 Answers1

1

So here I don't have control over the CA of the certificate (correct me if I'm wrong).

It is a self-signed certificate; there is no CA.

To demo MITM, I need to have certificate signed by same CA, but for a different domain, and so without certificate validation, the app will talk to the wrong server.

No. A real man-in-the-middle attack involves a proxy, one that pretends to be your target server and intercepts the communications.

You are welcome to confirm that your certificate validation fails by testing against a second, different self-signed certificate for the same domain. That would be more realistic for a MITM attack than a different self-signed certificate for a different domain.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • First question .. relating to self-signed .. regarding the keystore step, is it actually adding trust for the public key of the domain in the certificate, so as you pointed out in the suggested test, different key of the same domain will fail ? – Jake Jun 18 '14 at 16:48
  • Second .. I do plan to add a proxy, which returns certificate with same CA, but signed for different domain, will the app code with keystore containing CA's certificate notice the different domain (given my http post code) when it has no certificate validation ? Thanks for your time ! – Jake Jun 18 '14 at 16:50
  • @Jake: I do not understand your questions, sorry. You keep saying "CA", when there is no CA involved with self-signed certificates. – CommonsWare Jun 18 '14 at 17:18
  • Oh sorry. My first question relates to self signed certificate (basically the code I reference in the question). The second question refers to the change I suggest in my question - no more self signed certificate - and I would sign certificate with a my own CA. – Jake Jun 18 '14 at 17:32
  • @Jake: I have never tried creating my own CA, so I don't have any experience in that area. Since a MITM proxy specifically would *not* be using a different domain for its certificate, though, I fail to see why you are trying to use a different domain. – CommonsWare Jun 18 '14 at 17:35
  • Okay understood. Could you please also answer the first one ? I have the code working (following other's tutorials) as I describe initially in my question. What I want to understand is that what does creating that custom key store do ? My first question relates to that. Thank you. – Jake Jun 18 '14 at 17:39
  • @Jake: "What I want to understand is that what does creating that custom key store do ?" -- it's a file. It contains the key information used for validation purposes. http://javarevisited.blogspot.com/2012/09/difference-between-truststore-vs-keyStore-Java-SSL.html – CommonsWare Jun 18 '14 at 17:55