Chapter 8. Client-Side Attacks – Dynamic Analysis Techniques

In the previous chapter, we covered client-side attacks associated with Android applications that we often see with Android apps from a static analysis perspective. In this chapter, we will cover same client-side attacks from a dynamic application security testing (DAST) perspective and will also see some automated tools. As mentioned in the previous chapter, to successfully execute most of the attacks covered in this chapter, an attacker needs to convince the victim to install a malicious application in his/her phone. Additionally, it is also possible for an attacker to successfully exploit the apps if he has physical access to the device.

Following are some of the major topics that we will discuss in this chapter:

We have seen the instructions to setup the Drozer tool in Chapter 1. Setting Up the Lab. This section covers some of the useful features that are available in Drozer to speed up the penetration testing process. Automated tools are always helpful when you have time constraints. Drozer is one of the best tools available for pen testing Android apps at the time of writing this book. To better understand this tool, we will discuss the same attacks that we discussed in Attacking application components section of Chapter 7, Client-Side Attacks – Static Analysis Techniques.

Before we dive into the attacks, let's see some of the useful Drozer commands.

The previous command shows the list of all Drozer modules that can be executed in the current session.

dz> list
app.activity.forintent                   Find activities that can handle the given intent                                                                      
app.activity.info                        Gets information about exported activities.                                                                           
app.activity.start                       Start an Activity                                                                                                     
app.broadcast.info                       Get information about broadcast receivers                                                                             
app.broadcast.send                       Send broadcast using an intent                                                                                        
app.package.attacksurface                Get attack surface of package                                                                                         
app.package.backup                       Lists packages that use the backup API (returns true on FLAG_ALLOW_BACKUP)                                            
app.package.debuggable                   Find debuggable packages                                                                                              
app.package.info                         Get information about installed packages                                                                              
app.package.launchintent                 Get launch intent of package                                                                                          
app.package.list                         List Packages                                                                                                         
app.package.manifest                     Get AndroidManifest.xml of package                                                                                    
app.package.native                       Find Native libraries embedded in the application.   
.
.
.
.                                                                 
scanner.provider.finduris                Search for content providers that can be queried from our context.                                                    
scanner.provider.injection               Test content providers for SQL injection vulnerabilities.                                                             
scanner.provider.sqltables               Find tables accessible through SQL injection vulnerabilities.                                                         
scanner.provider.traversal               Test content providers for basic directory traversal vulnerabilities.                                                 
shell.exec                               Execute a single Linux command.                                                                                       
shell.send                               Send an ASH shell to a remote listener.                                                                               
shell.start                              Enter into an interactive Linux shell.                                                                                
tools.file.download                      Download a File                                                                                                       
tools.file.md5sum                        Get md5 Checksum of file                                                                                              
tools.file.size                          Get size of file                                                                                                      
tools.file.upload                        Upload a File                                                                                                         
tools.setup.busybox                      Install Busybox.                                                                                                      
tools.setup.minimalsu                    Prepare 'minimal-su' binary installation on the device.                                                               

dz>

The preceding excerpt shows the list of modules that are available with Drozer.

It is often the case that we need the AndroidManifest.xml file for exploring more details about the application. Although Drozer can find out everything that we need from the AndroidManifest.xml file using different options, it is good to have the AndroidManifest.xml file with us. The following command dumps the complete AndroidManifest.xml file from the target application:

Running the preceding command will show us the following output (output truncated):

First, let's identify the attack surface of GoatDroid application that we used earlier.

The previous output shows that there are four activities exported. We can use the following Drozer command to see all the exported activities in an application:

Running the previous command, will show us the following output:

As you can see, we have got all the exported activities. The following activity is is the one we tested earlier using adb:

If you want to identify all the activities including the ones that are not exported, you can use the preceding command with the -u flag. This is shown following:

dz> run app.activity.info -a org.owasp.goatdroid.fourgoats -u
Package: org.owasp.goatdroid.fourgoats
  Exported Activities:
    org.owasp.goatdroid.fourgoats.activities.Main
    org.owasp.goatdroid.fourgoats.activities.ViewCheckin
    org.owasp.goatdroid.fourgoats.activities.ViewProfile
    org.owasp.goatdroid.fourgoats.activities.SocialAPIAuthentication
  Hidden Activities:
    org.owasp.goatdroid.fourgoats.activities.Login
    org.owasp.goatdroid.fourgoats.activities.Register
    org.owasp.goatdroid.fourgoats.activities.Home
    org.owasp.goatdroid.fourgoats.fragments.DoCheckin
    org.owasp.goatdroid.fourgoats.activities.Checkins
    org.owasp.goatdroid.fourgoats.activities.Friends
    org.owasp.goatdroid.fourgoats.fragments.HistoryFragment
    org.owasp.goatdroid.fourgoats.activities.History
    org.owasp.goatdroid.fourgoats.activities.Rewards
    org.owasp.goatdroid.fourgoats.activities.AddVenue
    org.owasp.goatdroid.fourgoats.fragments.MyFriends
    org.owasp.goatdroid.fourgoats.fragments.SearchForFriends
    org.owasp.goatdroid.fourgoats.fragments.PendingFriendRequests
    org.owasp.goatdroid.fourgoats.activities.ViewFriendRequest
    org.owasp.goatdroid.fourgoats.fragments.MyRewards
    org.owasp.goatdroid.fourgoats.fragments.AvailableRewards
    org.owasp.goatdroid.fourgoats.activities.Preferences
    org.owasp.goatdroid.fourgoats.activities.About
    org.owasp.goatdroid.fourgoats.activities.SendSMS
    org.owasp.goatdroid.fourgoats.activities.DoComment
    org.owasp.goatdroid.fourgoats.activities.UserHistory
    org.owasp.goatdroid.fourgoats.activities.DestinationInfo
    org.owasp.goatdroid.fourgoats.activities.AdminHome
    org.owasp.goatdroid.fourgoats.activities.AdminOptions
    org.owasp.goatdroid.fourgoats.fragments.ResetUserPasswords
    org.owasp.goatdroid.fourgoats.fragments.DeleteUsers
    org.owasp.goatdroid.fourgoats.activities.DoAdminPasswordReset
    org.owasp.goatdroid.fourgoats.activities.DoAdminDeleteUser
    org.owasp.goatdroid.fourgoats.activities.GenericWebViewActivity

dz>

Now, let's launch the private activity using Drozer without entering valid credentials since it is exported.

Following is the activity when we launch the GoatDroid application:

Attacks on activities

Running the following command will launch the activity:

If you notice the emulator after running the preceding command, you should see the following activity launched:

Attacks on activities

Similar to activities and services, we can invoke broadcast receivers using Drozer. The following command lists all the exported broadcast receivers from the target application:

Running the previous command on the GoatDroid application will result in the following:

As we can see in the preceding output, the application has got one broadcast receiver exported.

We can also list the broadcast receivers that are not exported using the –u flag. This is shown as follows:

As you can see in the preceding excerpt, this application doesn't have any broadcast receivers that are not exported.

Now, we can use the following Drozer command to launch a broadcast intent:

The previous command will trigger the broadcast receiver similar to what we saw with the adb method earlier. This is shown in the following figure:

Broadcast receivers

This section shows how we can use Drozer to perform various attacks on content providers. We are going to use the previously shown Sony Notes application as our target.

We can find out the package name of our target application using the command shown as follows:

We knew that there is an exported content provider in this app. But, let's find it out using Drozer. The following command can be used to list the exported components:

At this stage, we used the grep command to figure out the actual content provider URI when we were doing this using the adb method. Drozer makes our life easier is by automating the whole process of finding out the content provider URIs. This can be done using the following command:

As you can see in the preceding excerpt, we have got four accessible content provider URIs.

We can now query these content providers using the app.provider.query module as shown following:

Running the preceding command will result in the following output:

As we can see, we are able to query the content from the application's provider without any errors.

Alternatively, we can also use the following command to display the results in a vertical format:

Running the previous command will display the results in a nicely formatted way as follows:

Let us see how we can find SQL Injection vulnerabilities in content provider URIs. We can use scanner.provider.injection module:

Scanner is one of the nice modules in Drozer that can automatically find Injection and path traversal vulnerabilities. We will discuss path traversal attacks later in this section.

Running the following command will tell us if there are any injection vulnerabilities in the content providers:

As we can see in the preceding excerpt, all the four URIs have got injection vulnerabilities in selection.

As we discussed earlier, the traditional way of confirming SQL Injection is to pass a single quote and break the query. Let us pass a single quote (') in selection and see the response.

This can be done as shown following:

If we observe the preceding response, the single quote has been sent to the query and it is throwing an error along with the broken query.

Now, let us form a proper query by passing id=1:

The preceding query has been executed as expected and returned the row associated with id 1. As we did with the adb method, let's write a new select statement with UNION as follows:

As we can see in the preceding output, we are able to see the numbers from 1 to 14. We can now replace any of these numbers to extract the content from the database.

Replacing column number 5 with sqlite_version() will print the version of the database as shown following:

Now, getting the table names using Drozer is as simple as replacing the column number 5 with tbl_name. This is shown is the following command. Please note that we are querying sqlite_master to get the table names:

As we can see in the previous output, we have extracted the following tables:

Content providers can also be implemented as file backed providers. This means a developer can write a content provider that allows another application to access its private files. When an application is accessing these files via the content provider, it may be able to read arbitrary files in the context of a vulnerable app if no proper validation is done on what files are being read. This is usually done by traversing through the directories.

Implementing file backed content providers is done by writing the method public ParcelFileDescriptor openFile(Uri uri, String mode) within the class extending the ContentProvider class.

A nice tutorial on how this can be implemented in an app is discussed at:

http://blog.evizija.si/android-contentprovider/Drozer has a module scanner.provider.traversal to scan content providers for such traversal vulnerabilities.

This section shows how Drozer can be used to identify and exploit path traversal vulnerabilities in Android apps. We will use the Adobe Reader app for Android.

Original advisory information associated with this app was published at the following link:

http://blog.seguesec.com/2012/09/path-traversal-vulnerability-on-adobe-reader-android-application/

According to the original advisory, all versions of Adobe <= 10.3.1 are vulnerable to this attack.

We are using Adobe 10.3.1 with the package name com.adobe.reader in this example.

Installing the application is done using adb as shown following:

$ adb install Adobe_Reader_10.3.1.apk 
1453 KB/s (6165978 bytes in 4.143s)
  pkg: /data/local/tmp/Adobe_Reader_10.3.1.apk
Success
$

Once installed, we should see the Adobe Reader app icon on the device which looks like this:

Path traversal attacks in content providers

Finding out the package name is done in a similar way as to how we did with other apps using the following command:

Let's find out the attack surface.

Interestingly, there is a content provider exported. The next step is to find out the content provider URI. This can be done using scanner.provider.finduris.

If you notice, Drozer says that there are no accessible content URIs found. No surprise, as it is trying to read data from the database and it is a file-based provider. Let's see if there are any traversal vulnerabilities in the application. This can be done using the following command:

Running the preceding command will result in the following:

As you can see in the preceding excerpt, there is a content provider URI vulnerable to path traversal. This vulnerability allows an attacker to read arbitrary files from the device as described in the next section.

Android apps have a flag known as android:debuggable in their AndroidManifest.xml file. This is set to true during the app development stage and by default set to false once the app is exported for distribution. This flag is used for debugging purposes during the development process and it is not supposed to be set to true in production. If a developer explicitly sets the value of the debuggable flag to true it becomes vulnerable. If an application running in its VM is debuggable, it exposes a unique port on which we can connect to it using a little tool called JDB. This is possible in Dalvik Virtual Machines with the support of JDWP (short for Java Debug Wire Protocol). An attacker with physical access to the device can connect to the app through the exposed UNIX socket and running arbitrary code in the context of the target app is possible.

This section shows the easiest way to exploit debuggable apps.

The following command lists out all the PIDs on which we can connect and debug:

In order to find out the exact PID associated with our target application, make sure that the target app is not running when you run the previous command. This looks as shown following:

Exploiting debuggable apps

Now, launch the application and run the preceding command once again. The idea is to bring the application into an active state as the pid is visible only when the application is active. Running the preceding command after launching the app will show us an extra pid as shown following:

Exploiting debuggable apps

Although there are a few other extra ports in the listing, we can find the one associated with our target application using the ps command as shown below:

Exploiting debuggable apps

As you can see in the preceding output, the pid 1062 is associated with our target app. We can also see the package name of this application. Make a note of it as this is required in the next step.

Before we see how we can make use of the debuggable flag to abuse an app, let's see if we can access the app specific data without root privileges.

Exploiting debuggable apps

As you can see, we are getting a Permission denied error, when we tried to list the files and folders inside the app's private directory.

Now, let's get a shell once again and use run-as binary as shown following:

Exploiting debuggable apps

If you notice the above output, we are able to see the private contents of the vulnerable application.