From fa140446531f0e4c234c799fc1a62114f18ffa62 Mon Sep 17 00:00:00 2001 From: Rahul Binjve Date: Mon, 10 Apr 2017 13:53:01 -0400 Subject: [PATCH 1/4] Refactored code, fix for #4 and #5. --- apk2java | 77 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/apk2java b/apk2java index 80f485d..166ab1b 100644 --- a/apk2java +++ b/apk2java @@ -2,6 +2,7 @@ # this requires executables besides JRE # apktool,enjarify,jad, jadx # Script designed and Tested on Android Tamer (https://androidtamer.com) + echo "APK TO JAVA source code extraction script" echo "This is a script created by Anant Shrivastava" echo "http://anantshri.info" @@ -10,70 +11,72 @@ echo "This script will work on automating the work of extracting the source code if [[ $# -eq 0 || $1 == "--help" || $1 == "-help" ]] then echo "" - echo "$0 APK_FILE_NAME" + echo "$0 APK_FILE_NAME DESTINATION" exit fi -echo "Starting APK Decompile" +echo "[*] Starting APK Decompile" # JAR KEEP is a variable which allows you to keep jar file converted using dex2jar. # 0 is off # 1 is on JAR_KEEP=1 -echo $0 $1 -echo "$@" -echo "APK to JAVA/SRC conversion Utility" -APK_NAME=`basename $1` -c=`basename $APK_NAME .apk` -FULL_PTH=`readlink -f $1` +APK_NAME=`basename "$1"` +c=`basename "$APK_NAME" .apk` +FULL_PTH=`readlink -f "$1"` CDIR=`dirname "$FULL_PTH"` -SRC_DIR=$CDIR"/"$APK_NAME"_src/" -SRC_PATH=$CDIR"/"$APK_NAME"_src/" -echo $c -echo $APK_NAME -echo "FULL_PATH"$FULL_PTH -echo "CDIR"$CDIR -if [ $c == `basename $APK_NAME` ] + +if [[ $# -eq 2 ]] +then + if [[ "$2" != /* ]] + then + # Relative path + SRC_DIR="$CDIR"/"$2"/"$APK_NAME""_src/" + else + SRC_DIR=$2"/"$APK_NAME"_src/" + fi +else + SRC_DIR=$CDIR"/"$APK_NAME"_src/" +fi + +if [[ "$c" == `basename "$APK_NAME"` ]] then - echo "Only APK's allowed" + echo "[-] Only APK's allowed. Exiting." exit fi #Checks if the file exists. if [ ! -f "$FULL_PTH" ] then - echo "File not found." + echo "[-] File not found. Exiting." exit fi -echo "Creating Output Directory" +echo "[*] Creating Output Directory" mkdir -p "$SRC_DIR" -ls -l -echo "Extracting files via APKTool" + +echo "[*] Extracting files via APKTool" apktool decode -f "$CDIR/$APK_NAME" -o "$SRC_DIR" + +echo "[*] Enjarify for decoding back to java classes." mkdir -p "$SRC_DIR"jar -# Dex2 JAR was used earlier -#echo "Dex2jar conversion and extraction" -#$PTH/dex2jar.sh $CDIR"/"$APK_NAME -#Removing dex2jar -#d2j-dex2jar.sh $CDIR"/"$APK_NAME -#echo "jar file name is "$c"_dex2jar.jar" -#mv $CDIR"/"$c"_dex2jar.jar" $SRC_DIR"/jar/" -echo "Enjarify for decoding back to java classes" -JAR_FILE=$SRC_DIR"/jar/"$c"_enjarify.jar" -enjarify -f "$CDIR/$APK_NAME" -o "$JAR_FILE" -echo "Decompiling via JADX Decompiler" +JAR_FILE="$SRC_DIR""/jar/""$c""_enjarify.jar" +enjarify -f "$CDIR""/$APK_NAME" -o "$JAR_FILE" + +echo "[*] Decompiling via JADX Decompiler" cd "$SRC_DIR"/jar mkdir -p ../src/jadx -jadx -d ../src/jadx $SRC_DIR"/jar/"$c"_enjarify.jar" -jar -xf $c"_enjarify.jar" -echo "Source Extraction via JAD from class files" +jadx -d ../src/jadx "$SRC_DIR""/jar/""$c""_enjarify.jar" +jar -xf "$c""_enjarify.jar" + +echo "[*] Source Extraction via JAD from class files" jad -o -r -sjava -d../src/jad './**/*.class' + if [ $JAR_KEEP -eq 0 ] then - echo "removing jar file" - rm -rf $c"_enjarify.jar" + echo "[*] Removing jar file." + rm -rf "$c""_enjarify.jar" cd .. rm -rf ./jar fi ls -l "$SRC_DIR" cd "$CDIR" -echo "All Done" +echo "[*] All Done" \ No newline at end of file From 092f0eec54ace3454bfc38159351b1dd09fccf4d Mon Sep 17 00:00:00 2001 From: Rahul Binjve Date: Mon, 10 Apr 2017 14:06:55 -0400 Subject: [PATCH 2/4] Updated documentation. --- README.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b629f07..4a1e422 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,24 @@ # apk2java -A simple Shell script which allows for one stp decompilation of apk file via various decompilers. +A simple Shell script which allows for one step decompilation of apk file via various decompilers. This script was written and tested on Android Tamer project. + +## Requirements +Apart from the JRE, following tools should be in your PATH: +* apktool +* enjarify +* jad +* jadx + +## Usage +Syntax for running the tool: + +```bash +./apk2java +``` + +Example: + +```bash +./apk2java com.whatsapp.apk /tmp/ +``` + +If everything goes fine in above example, this tool will save the source in `/tmp/com.whatsapp.apk_src/` directory. In case the 2nd argument is skipped, the source is saved in current directory. From 070e29eb4fd77b53aad2d8e0c172504d8e45b775 Mon Sep 17 00:00:00 2001 From: Rahul Binjve Date: Wed, 12 Apr 2017 06:42:44 -0400 Subject: [PATCH 3/4] Fixed issue with current working directory. --- apk2java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apk2java b/apk2java index 166ab1b..c620866 100644 --- a/apk2java +++ b/apk2java @@ -29,12 +29,12 @@ then if [[ "$2" != /* ]] then # Relative path - SRC_DIR="$CDIR"/"$2"/"$APK_NAME""_src/" + SRC_DIR="$PWD"/"$2"/"$APK_NAME""_src/" else SRC_DIR=$2"/"$APK_NAME"_src/" fi else - SRC_DIR=$CDIR"/"$APK_NAME"_src/" + SRC_DIR="$PWD/$APK_NAME""_src/" fi if [[ "$c" == `basename "$APK_NAME"` ]] From b93aa417f93a647867dceec2a29a38af0b7a982d Mon Sep 17 00:00:00 2001 From: Rahul Binjve Date: Wed, 12 Apr 2017 06:45:23 -0400 Subject: [PATCH 4/4] Renamed variable for better readability. --- apk2java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apk2java b/apk2java index c620866..03bc282 100644 --- a/apk2java +++ b/apk2java @@ -22,7 +22,7 @@ JAR_KEEP=1 APK_NAME=`basename "$1"` c=`basename "$APK_NAME" .apk` FULL_PTH=`readlink -f "$1"` -CDIR=`dirname "$FULL_PTH"` +APK_DIR_PATH=`dirname "$FULL_PTH"` if [[ $# -eq 2 ]] then @@ -54,12 +54,12 @@ echo "[*] Creating Output Directory" mkdir -p "$SRC_DIR" echo "[*] Extracting files via APKTool" -apktool decode -f "$CDIR/$APK_NAME" -o "$SRC_DIR" +apktool decode -f "$APK_DIR_PATH/$APK_NAME" -o "$SRC_DIR" echo "[*] Enjarify for decoding back to java classes." mkdir -p "$SRC_DIR"jar JAR_FILE="$SRC_DIR""/jar/""$c""_enjarify.jar" -enjarify -f "$CDIR""/$APK_NAME" -o "$JAR_FILE" +enjarify -f "$APK_DIR_PATH""/$APK_NAME" -o "$JAR_FILE" echo "[*] Decompiling via JADX Decompiler" cd "$SRC_DIR"/jar @@ -78,5 +78,5 @@ then rm -rf ./jar fi ls -l "$SRC_DIR" -cd "$CDIR" +cd "$APK_DIR_PATH" echo "[*] All Done" \ No newline at end of file