Skip to content

Instantly share code, notes, and snippets.

@jflefebvre
Last active December 15, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jflefebvre/5281070 to your computer and use it in GitHub Desktop.
Save jflefebvre/5281070 to your computer and use it in GitHub Desktop.
Allows to automate the use of apktool and dex2jar to reverse engineer an apk file.
#!/usr/bin/env php
<?php
/**
* Allows to automate the use of apktool and dex2jar to reverse engineer an apk file.
*
* Usage:
*
* reverse <file-apk>
* or
* reverse d <file-apk> --> display more detailed infos
*
* Copy the script in file named reverse in a directory containing (give execute right with: chmod +x reverse)
* ---> the application apk to be decoded
* ---> apktool (http://code.google.com/p/android-apktool/)
* ---> dex2jar (http://code.google.com/p/dex2jar/)
*
* Let say that we have an application apk named: be.evolution.myapp.apk
* Run: reverse be.evolution.myapp.apk
*
* Two directories will be created :
* be.evolution.myapp --> containing the resources and android manifest decoded
* be.evolution.myapp- --> containing the java classes in a classes-dex2jar folder
*
* To reverse the code, use jd-gui (http://java.decompiler.free.fr/?q=jdgui) or jdeclipse plugin.
*
* Author : Jean-François Lefebvre
* E-mail : lefebvre.jf@gmail.com
*
*/
$options = getopt("d::");
$debug = isset($options['d']);
if ($argc==1) {
echo 'Usage:' . PHP_EOL;
echo 'reverse application-name.apk' . PHP_EOL;
echo 'reverse -d application-name.apk' . PHP_EOL;
die();
}
$apkFilename = ($debug) ? $argv[2] : $argv[1];
if (strpos($apkFilename, 'apk') ===FALSE) {
echo 'the argument must be an apk file' . PHP_EOL;
die();
}
echo 'Run apktool ...' . PHP_EOL;
$result = shell_exec("apktool d $apkFilename");
if ($debug) {
echo $result . PHP_EOL;
}
echo 'Copy apk to zip ...' . PHP_EOL;
$zipFile = str_replace('.apk', '-.zip', $apkFilename);
$result = shell_exec("cp $apkFilename $zipFile");
$targetDirectory = str_replace('.zip', '', $zipFile);
echo 'Unzip archive ...' . PHP_EOL;
$result = shell_exec("unzip -o $zipFile -d $targetDirectory");
if ($debug) {
echo $result . PHP_EOL;
}
echo 'Run dex2jar ...' . PHP_EOL;
$result = shell_exec("./d2j-dex2jar.sh --force $targetDirectory/classes.dex");
if ($debug) {
echo $result . PHP_EOL;
}
echo 'Move classes-dex2jar ...' . PHP_EOL;
$result = shell_exec("mv classes-dex2jar.jar $targetDirectory/classes-dex2jar.zip");
if ($debug) {
echo $result . PHP_EOL;
}
echo "Unzip classes to $targetDirectory" . PHP_EOL;
$result = shell_exec("unzip -o $targetDirectory/classes-dex2jar.zip -d $targetDirectory/classes-dex2jar");
if ($debug) {
echo $result . PHP_EOL;
}
echo "DONE !" . PHP_EOL;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment