Lab_PentestingFSR
Lab_PentestingFSR
Preparation
As in the Lab of Reverse engineering:
1) In order to decompile diva-beta.apk using JaDX, copy the archive .apk to /usr/share/jadx/bin
2) Decompile the apk archive into a folder named diva using the command: ( sudo ./jadx –d diva
diva-beta.apk) it is normal to see some errors, so ignore that errors
You will get a jar file named classes-dex2jar.jar (you may need to install dex2jar: sudo apt install
dex2jar)
Move the directory diva to your Desktop and continue this Lab
Insecure Logging
Android Applications may store some data as logs
Logs are stored in a central place and every Android application can access those logs
But in latest Android versions permissions for accessing that logs are changed.
Open your Kali Linux shell and go to: Desktop/diva/resources (cd Desktop/diva/resources)
Open the jar file classes_dex2jar.jar, already created in step (Analyzing Dex Files of the
Lab_ReverseEngineering), with jd-gui
Go to jakhar.aseem.divaLogActivity, this is the activity which is responsible for storing our data
as logs
open the DIVA application in your emulator and click on “Insecure logging”
find out the process ID of this application (adb shell ps | grep “diva”)
In my case PID=2420
Go back to DIVA and type a number then click “ckeck out” then verify in the logcat in Kali
What do you see? the credit card number is stored in clear as log
Hardcoding Issues
Sometimes developers made mistake in source code of an Android application
They explicitly define a constant value
If any confidential data is hardcoded in source code, then that is also a security issue
Attackers can decompile the application and see the hardcoded sensitive information
Hardcoded data might be –password, access token, authentication string etc.
analyse the code source of the access() method, what do you think?
The access method compares the input value with “vendorsecretkey” and grant access if are equal
now try to use “vendorsecretkey” to access the application, what do you get?
Analyse its code, what do you think? user and password are stored in Shared Preferences
in adb shell, change directory to: /data/data/jakhar.aseem.diva/shared_prefs, then list its content
what do you find? An xml file jakhar.aseem.diva_preferences.xml
in adb shell, change directory to: /data/data/jakhar.aseem.diva/databases and list its content
list the tables existing in this database using the command (.tables)
type the command: (select * from myuser;), what do you get? Username and password in clear
Analyse its code, what do you think? a temporary file named “uinfo” is used to store our data
in adb shell, change directory to: /data/data/jakhar.aseem.diva and list its content; what do you
see? A temporary file
show the content of this file using the cat command, what do you see?
Analyse its code, what do you think? there is a method called saveCredentials() which use the
method getExternalStorageDirectory() method to store credentials, in this method a hidden file
called .uinfo.txt is created
in adb shell, change directory to: /mnt/sdcard and list its content using the command (ls –a); what
do you see? We see our file .uinfo.txt
show the content of this file using the cat command, what do you see?
1’ or’1’=’1
admin’ or’1’=’1’
admin’ or’1’=’1’-
admin’ or’1’=’1’#
admin’ or’1’=’1’/*
admin’ or 1=1 or ‘’=’
admin’ or 1=1
admin’ or 1=1-
admin’ or 1=1#
admin’ or 1=1/*
admin’) or (‘1’=’1
Now go to jd-gui in Kali and open the activity SQLInjectionActivity and analyse its code
Enter a username to search, then click “Search”, in this step you can inject your query as below
http
https
ftp
smtp
file: throw which we can access internal system file, we will use this protocol to access
internal system files
in adb shell, change directory to: /mnt/sdcard and list its content using the command (ls –a); what
do you see? We see our file .uinfo.txt
Now we will enter the file system, type in the view the URL: file://mnt/sdcard/.uinfo.txt
Go to DIVA and open “Access control issues- Part 1”, then click “View API credentials”
we will try to access these secret API credentials without passing from these steps
Our objective is to open that API credentials without opening the DIVA Application
In Kali Linux, Change directory to /Desktop/diva/resources and list its content:
what do you deduce when analysing its content? There is one Activity called
AccessControlActivity which is responsible for the first view in “Access control issues- Part 1” and
contains the “View API credentials” button. If we click this button, another activity named
APICredsActivity will be called by using an intent filter named
“jakhar.aseem.diva.action.VIEW_CREDS”, this intent filter is responsible for invoking that activity
which contains our secret keys
we will use this intent filter to invoke our activity containing credentials
we will do this, from Kali, by the help of activity manager which allow to open any activity running
intent filter with the help of ADB.
Open adb shell, and the command: am what do you see? Activity manager
Go to your emulator, you will find the activity “APICredsActivity “ is opened containing secret
credentials
we can also invoke that activity without opening adb shell:
If we select “Register now” we should register online and to obtain a valid PIN which we could use
to get access to TVEETER API Credentials
we will do this, from Kali, by the help of activity manager which allow to open any activity running
intent filter with the help of ADB.
what do you deduce when analysing its content? There is one Activity called
AccessControl2Activity which is responsible for the first view in “Access control issues- Part 2” and
contains the two radio button and the “VIEW TVEETER API CREDENTIALS” button. If we select
“Register now” and click this button, another activity named APICreds2Activity will be called by using
an intent filter named “jakhar.aseem.diva.action.VIEW_CREDS2”, this intent filter is responsible for
invoking that activity which contains our secret keys
Try to get access to credentials by using the same steps as before, what do you deduce? if we try
the same method as in the previous step, we will be unable to get access to credentials because it is
protected by an authentication mechanism
In order to bypass this access control issue, lets analyse the source code
https://github.com/payatu/diva-
android/blob/master/app/src/main/java/jakhar/aseem/diva/APICreds2Activity.java
Looking at the above piece of code, it can be seen that there is an extra boolean type argument
required along with the ADB command we used to launch the intent.
Let’s check the chk_pin entry from strings.xml to see it’s actual value.
“chk_pin” is “check_pin”.
If the user is already registered “check_pin” is set to false, if not it is set to true. When the
“check_pin” button’s value is set to “false”, there are no other checks made by the application at
receiving side.
So, let’s try to pass this additional argument to the intent and see if it works.
Challenge cracked!
Content providers:
Content providers are used to store and query data within the application
All content providers have unique URI (Uniform Resource Identifier)
Content://
If our application wants some data from database, it will send a query to content provider
then this content provider itself sends that query to database
If an attacker requests the same data to that content provider and if the content provider is
not doing authentication check then it will end by sending the same data to the attacker
A look at the AndroidManifest.XML file reveals that there is a content provider registered, and it is
explicitly exported using android_exported=”true”.
Content Providers are represented using the URI that starts with content://. So, we need to find
the URI to access the data.
First navigate to the directory where the Java code is extracted using JaDX
We can find the strings using grep command. The following command recursively searches all
the files for the strings that contain “content://”.
$ grep -lr “content://” *
NotesProvider.java
$
Let’s open up the file and check for the content provider URI (vim NotesProvider.java)
content://jakhar.aseem.diva.provider.notesprovider/notes
As we saw in the AndroidManifest.XML file, this content provider is exported and hence we can
query it without any explicit permission.
This should query all the data from the application’s notes database.
Challenge cracked! We can query the data from the application without knowing the PIN.
Java Native Interface is a programming framework that enable java code running in JVM to
be interact with other libraries.
Other programming languages ie- C, C++ can also interact with java code with JNI.
Application
Click on “12. HARDCODING ISSUES – PART 2” in your DIVA application. The goal of this challenge is
to find out the vendor key and submit it to the application.
Following is the decompiled code that is associated with the activity. (Hardcode2Activity.java)
Looking at the above code at Hardcode2Activity.java, it appears that this activity is creating an
object of DivaJni class when it is loaded. Exploring other files reveal that there is a file called
DivaJni.java as shown below.
As we can see in the above excerpt, there are multiple instances of “libdivajni.so” files for various
architectures. Lets pull one of them and run strings command on that to see if we can find anything
interesting. This is shown below.
We can also find this hardcoded key in the source, which is available at the following link.
https://github.com/payatu/diva-android/blob/master/app/src/main/jni/divajni.c
Click on “13. INPUT VALIDATION ISSUES – PART 3” in your application. The goal is to crash the
application somehow.
Let’s first see how the application is responding when we enter some input. I have entered 4 As in the
input field as shown below.
After entering multiple inputs as such, the application responded with the same error message as
long as the input length is not greater than 20.
The following figure shows that the app is throwing the same error when we enter 20 As.
As you can see in the above figure, the application has been crashed.
Let’s see if we can find any information about this crash in “logcat”. Open up a new terminal and
type “adb logcat”
Looking at the above output from logcat, its clear that the crash is due to CPUs attempt to jump to
41414141. Basically A maps to 41 in hex. We have used large number of As in the input we have
supplied, and CPU is trying to jump to this location thus causing an error condition since this location
is something that is unknown.
https://github.com/payatu/diva-android/blob/master/app/src/main/jni/divajni.c
After exploring the source code available at the above link, it is clear that the application is processing
user supplied input using strcpy() function.
The buffer size allocated is 20. We can see it from the following figure.