Welcome to the Remote Code Execution (RCE) Challenge! This lab provides a real-world scenario where you’ll explore vulnerabilities in popular software. Your mission is to exploit a path traversal vulnerability combined with dynamic code loading to achieve remote code execution.
privatefinalvoidloadProLibrary() {
try {
String abi = Build.SUPPORTED_ABIS[0];
File libraryFolder =new File(getApplicationContext().getFilesDir(), "native-libraries/"+ abi);
File libraryFile =new File(libraryFolder, "libdocviewer_pro.so");
System.load(libraryFile.getAbsolutePath());
this.proFeaturesEnabled=true;
} catch (UnsatisfiedLinkError e) {
Log.e(TAG, "Unable to load library with Pro version features! (You can ignore this error if you are using the Free version)", e);
this.proFeaturesEnabled=false;
}
}
As for the loadProLibrary function, it seems to be trying to load a native library called libdocviewer_pro.so in the specific path. this could be my endpoint of performing RCE if I could write a file in that endpoint.
Moving to copyFileFromUri function, it will download the file from provided URI to externalStoragePublicDirectory which is the Download folder of the android. Based on the code, it is possible to save file to another file directory by abusing the getLastPathSegment function. The getLastPathSegment function will take the word on the last / and I could bypass this using url encode method %2f. From my current understanding, I believe it is possible to write a file anywhere with this copyFileFromUri function. I then proceed to dynamic analysis to test out the idea.
04-07 23:36:52.050 1936719367 E Companion: Unable to load library with Pro version features! (You can ignore this error if you are using the Free version)04-07 23:36:52.050 1936719367 E Companion: java.lang.UnsatisfiedLinkError: dlopen failed: library "/data/user/0/com.mobilehackinglab.documentviewer/files/native-libraries/arm64-v8a/libdocviewer_pro.so" not found
Looking into the logcat, I noticed that it is trying to run loadProLibrary function. It also provide me the path that it is looking at /data/user/0/com.mobilehackinglab.documentviewer/files/native-libraries/arm64-v8a/libdocviewer_pro.so.
After looking into my Download folder, the dummy.pdf is downloaded and saved inside the Download folder. I then tried to see if it’s possible to save the file in other place. To do so, I started a simple web server that will just provide the same content even the file name is different.
It is possible to write file into another directory. By abusing this vulnerability, it is possible for me to write a shared library into the specific directory but to do so, I will need to create one shared library.
This is the command for me to compile an amd64 shared library. To make life easier, it is better to use Android studio to compile it instead of using my method. After compiling it, I use the simple http server from python to host my shared library and abuse the path traversal vulnerability to write the shared library into the specific directory.
04-08 00:06:05.242 2433524335 E AndroidRuntime: FATAL EXCEPTION: main
04-08 00:06:05.242 2433524335 E AndroidRuntime: Process: com.mobilehackinglab.documentviewer, PID: 2433504-08 00:06:05.242 2433524335 E AndroidRuntime: java.lang.UnsatisfiedLinkError: No implementation found for void com.mobilehackinglab.documentviewer.MainActivity.initProFeatures()(tried Java_com_mobilehackinglab_documentviewer_MainActivity_initProFeatures and Java_com_mobilehackinglab_documentviewer_MainActivity_initProFeatures__)
This basically means the shared library looking for a function initProFeatures which is not provided in the shared library but it is loaded in onCreate function in MainActivity. Although it has error, the command execution is still successful due to the onLoad function in shared library.
This means that my RCE is successful despite having some minor error which could be easily fixed by adding the required function. It is also possible to write a POC app to send the intent which exploit the path traversal write and restart the application.