How can I automate the pen testing on my APK app installed in my device?
Assistant
To automate penetration testing on an APK app installed on your device using Mobile Security Framework (MobSF), you can leverage MobSF's dynamic analysis capabilities. Here's a step-by-step guide to automate the process:
1. Set Up MobSF
Ensure you have MobSF installed and running. You can set it up using Docker for ease:
bash
docker pull opensecurity/mobile-security-framework-mobsf:latest
docker run -it --rm -p 8000:8000 opensecurity/mobile-security-framework-mobsf:latest
Access the MobSF web interface at http://localhost:8000.
2. Upload and Analyze the APK
Upload your APK file to MobSF through the web interface.
MobSF will perform a static analysis on the APK and provide a detailed report.
3. Dynamic Analysis Setup
Ensure your Android device is connected and recognized by MobSF. You can check this in the "Dynamic Analyzer" section.
In the Dynamic Analyzer, select the APK you want to test and click on "Start Dynamic Analysis".
4. Automate Dynamic Analysis with [Redacted Person]
MobSF uses Frida for dynamic instrumentation. You can automate the dynamic analysis by writing Frida scripts or using the provided Frida scripts in MobSF.
The [Redacted Person] scripts are located in the mobsf/DynamicAnalyzer/views/android/frida_scripts.py file. You can customize these scripts or write your own to automate specific tests.
5. Run Dynamic Analysis
Once the dynamic analysis environment is set up, MobSF will automatically instrument the app using Frida and start monitoring the app's runtime behavior.
You can automate the interaction with the app by using the "Touch" and "ScreenCast" features in MobSF to simulate user interactions.
6. Automate API Monitoring
MobSF can monitor API calls made by the app. This is useful for detecting sensitive data leakage or insecure API usage.
You can automate this by enabling the API monitor in the Dynamic Analyzer and letting MobSF capture and analyze the API calls.
7. Automate with REST API
MobSF provides a REST API that you can use to automate the entire process. You can upload the APK, start the dynamic analysis, and retrieve the results programmatically.
Example API endpoints:
Upload APK: POST /api/v1/upload
Start Dynamic Analysis: POST /api/v1/dynamic/start_analysis
Get Report: GET /api/v1/report/json
8. Integrate with CI/CD
You can integrate MobSF into your CI/CD pipeline using the REST API or CLI tools. This allows you to automate security testing as part of your build process.
Example CI/CD integration:
[Redacted
- name: Run MobSF Security Scan
run: |
curl -X POST -F "file=@app-release.apk" http://localhost:8000/api/v1/upload
curl -X POST http://localhost:8000/api/v1/dynamic/start_analysis -d "hash=<APK_HASH>"
curl -X GET http://localhost:8000/api/v1/report/json -d "hash=<APK_HASH>"
9. Review and Analyze Results
After the dynamic analysis is complete, review the results in the MobSF web interface or via the API.
The results will include details on vulnerabilities, API calls, and other security issues detected during the analysis.
10. Automate Remediation
Based on the analysis results, you can automate the remediation process by integrating security fixes into your CI/CD pipeline.
By following these steps, you can automate the penetration testing of your APK app using MobSF, ensuring that your app is secure before deployment.
docker run -it --rm -p 8000:8000 opensecurity/mobile-security-framework-mobsf:latest
MobSF is configured to use default credentials for the first login. These credentials are:
Username: [Redacted Person]
Password: mobsf
After logging in for the first time, you should change the password for security reasons. You can do this by navigating to the Settings or Profile section in the MobSF web interface.
Steps to Log In:
Go to http://localhost:8000/login/?next=/.
Enter the default credentials:
Username: [Redacted Person]
Password: mobsf
Click Login.
Changing the Password:
After logging in, go to the Settings or Profile section (usually accessible from the top-right corner of the interface).
Update the password to a secure one of your choice.
Troubleshooting:
If the default credentials don't work, ensure you are using the correct Docker image (opensecurity/mobile-security-framework-mobsf:latest).
If you are using a custom setup or have modified the Docker image, check the env file or Docker environment variables for any changes to the default credentials.
Let me know if you encounter any issues!
User
OK. I managed to run a first analysis by uploading my APK. Its a long report. I havent connected my phone although.. Security Score 48/100
Trackers Detection 2/432
I got 2/15 exported SERVICES and 2/11 exported receivers.
7/16 exported activities. How to interpret the report? I dont know. What should I do next? How to increase the security Score? Is Trackers Detection 2/432 safe?
Assistant
Great job on running your first analysis! Let’s break down the report and what you should focus on to improve your app's security. Here's a step-by-step guide to interpreting the report and taking actionable steps:
1. Understanding the Security Score (48/100)
The Security Score is a summary of how secure your app is based on various factors like exported components, permissions, trackers, and vulnerabilities. A score of 48/100 indicates there are significant security issues that need attention.
Key Areas to Improve:
Exported Components: These are components that can be accessed by other apps or the system. If not properly secured, they can be exploited.
2/15 exported SERVICES: 2 services are exposed to other apps. Check if these services need to be exported or if they should be restricted.
2/11 exported RECEIVERS: 2 broadcast receivers are exposed. Ensure they are not open to abuse.
7/16 exported ACTIVITIES: 7 activities are exposed. Review if all these activities need to be exported or if they can be made private.
Action:
Add the android:exported="false" attribute in the AndroidManifest.xml for components that do not need to be exposed.
Use permissions or intent filters to restrict access to exported components if they must remain exported.
Permissions: Check the permissions your app requests. Remove unnecessary permissions and ensure sensitive permissions are properly justified.
Vulnerabilities: Look for vulnerabilities like insecure storage, hardcoded secrets, or improper use of cryptography.
2. Trackers Detection (2/432)
Trackers are third-party libraries or SDKs that collect user data. A detection of 2/432 means MobSF identified 2 trackers in your app out of a possible 432 known trackers.
Is 2/432 safe?
It depends on the trackers detected. Some trackers are benign (e.g., analytics), while others may collect sensitive data or pose privacy risks.
Action:
Review the Trackers section in the report to identify which trackers are present.
Check if these trackers are necessary. If not, remove the associated SDKs or libraries.
User
OK. I stopped the docker env for the program, how do I run it again?
Assistant
To restart the MobSF Docker environment after stopping it, follow these steps:
1. Restart MobSF with Docker
Run the same Docker command you used initially to start MobSF:
bash
docker run -it --rm -p 8000:8000 opensecurity/mobile-security-framework-mobsf:latest
This command will start MobSF and make it accessible at http://localhost:8000.
2. Persist Data (Optional)
If you want to persist your analysis data (e.g., uploaded APKs, reports) between Docker sessions, you can use a volume to store the data on your host machine.
Steps to Use a Volume:
Create a directory on your host machine to store MobSF data:
bash
mkdir ~/mobsf_data
Start MobSF with the volume mounted:
bash
docker run -it --rm -p 8000:8000 -v ~/mobsf_data:/home/mobsf/.MobSF opensecurity/mobile-security-framework-mobsf:latest
The -v ~/mobsf_data:/home/mobsf/.MobSF flag mounts the ~/mobsf_data directory on your host machine to the MobSF data directory inside the container.
Your data (e.g., uploaded APKs, reports) will now persist in ~/mobsf_data even if you stop and restart the container.
3. Access MobSF
Once the container is running, open your browser and go to:
code
http://localhost:8000
Log in with the default credentials:
Username: [Redacted Person]
Password: mobsf
4. Stopping the Container
If you need to stop the container:
Press Ctrl+C in the terminal where the container is running.
Alternatively, find the container ID using:
bash
docker ps
Then stop the container with:
bash
docker stop <container_id>
5. Restarting with Persistent Data
If you used a volume, simply run the same command again to restart MobSF with your persisted data:
bash
docker run -it --rm -p 8000:8000 -v ~/mobsf_data:/home/mobsf/.MobSF opensecurity/mobile-security-framework-mobsf:latest
Let me know if you need further assistance! 😊
User
Yes i want persistent data here "H:\Mobile-Security-Framework-MobSF"
Assistant
To ensure persistent data storage for MobSF in the directory H:\Mobile-Security-Framework-MobSF, you can mount this directory as a volume when running the Docker container. Here's how to do it:
Steps to Run MobSF with Persistent Data
Ensure the Directory Exists:
Make sure the directory H:\Mobile-Security-Framework-MobSF exists on your system. If it doesn't, create it:
bash
mkdir H:\Mobile-Security-Framework-MobSF
Run MobSF with Volume Mounting:
Use the -v flag to mount the directory as a volume. Here's the command:
bash
docker run -it --rm -p 8000:8000 -v H:\Mobile-Security-Framework-MobSF:/home/mobsf/.MobSF opensecurity/mobile-security-framework-mobsf:latest
Explanation:
-v H:\Mobile-Security-Framework-MobSF:/home/mobsf/.MobSF: This mounts the H:\Mobile-Security-Framework-MobSF directory on your host machine to the /home/mobsf/.MobSF directory inside the container, where MobSF stores its data.
-p 8000:8000: Maps port 8000 on your host to port 8000 in the container, so you can access MobSF at http://localhost:8000.
Access MobSF:
Open your browser and go to:
code
http://localhost:8000
Log in with the default credentials:
Username: [Redacted Person]
Password: mobsf
Verify Persistent Data:
Any APKs you upload, reports generated, or other data will now be stored in H:\Mobile-Security-Framework-MobSF on your host machine.
You can stop and restart the container, and your data will persist.
Restarting MobSF with Persistent Data
To restart MobSF with the same persistent data:
Run the same command again:
bash
docker run -it --rm -p 8000:8000 -v H:\Mobile-Security-Framework-MobSF:/home/mobsf/.MobSF opensecurity/mobile-security-framework-mobsf:latest
Your data will still be available in H:\Mobile-Security-Framework-MobSF.
Stopping the Container
To stop the container, press Ctrl+C in the terminal where it's running.
Alternatively, find the container ID using:
bash
docker ps
Then stop the container with:
bash
docker stop <container_id>
Important Notes
Ensure Docker has the necessary permissions to access the H:\Mobile-Security-Framework-MobSF directory.
If you’re on Windows, ensure Docker Desktop is configured to allow access to the H: drive:
Open Docker Desktop.
Go to Settings > Resources > File Sharing.
Add H:\ to the list of shared drives.
Let me know if you encounter any issues or need further assistance! 😊
User
Ok but i cannot run a dynamic one: Android Runtime not found!
MobSF cannot find android instance identifier. Make sure that an android instance is running and refresh this page.
I alraedy plugged my USB so its connected through the app of mine. Its in debug mode so i am using npx react-native start. Does it need a release version? I dont know
Assistant
The error "Android Runtime not found! MobSF cannot find android instance identifier" typically occurs when MobSF cannot detect your Android device or emulator. Let’s troubleshoot and fix this step by step:
1. Ensure Your Device is Properly Connected
USB Debugging: Make sure USB debugging is enabled on your Android device.
Go to Settings > About phone and tap Build number 7 times to enable Developer Options.
Go to Settings > Developer Options and enable USB Debugging.
Authorize the Device: When you connect your device via USB, ensure you authorize the connection on your device by accepting the Allow USB Debugging prompt.
2. Check ADB Connection
MobSF uses ADB (Android Debug Bridge) to communicate with your Android device. Ensure ADB is working properly.
Steps to Verify ADB:
Open a terminal and check if your device is detected by ADB:
bash
adb devices
If your device is listed, it means ADB can communicate with it.
Example output:
code
List of devices attached
ABC1234567890 device
If your device is not listed:
Reconnect the USB cable.
Restart the ADB server:
bash
adb kill-server
adb start-server
Check again with adb devices.
3. Run MobSF with ADB Access
When running MobSF in Docker, you need to ensure the container has access to ADB and your connected device.
Steps to Grant ADB Access to Docker:
Find ADB on Your Host:
Locate the adb executable on your system. It’s usually in the Android SDK platform-tools directory.
Example path: ~/Android/Sdk/platform-tools/adb
Run MobSF with ADB Access:
Use the flag to pass your device to the Docker container:
User
OK since with USB plugged it cannot detect my phone and perform dynamic analysis, i will use android emulator. how? " Is your Android VM/emulator running? MobSF cannot find the android device identifier. Please read official documentation. If this error persists, set ANALYZER_IDENTIFIER in /home/mobsf/.MobSF/config.py or via environment variable MOBSF_ANALYZER_IDENTIFIER" so i will just run it through the emulator
Assistant
Using an Android emulator is a great alternative if your physical device isn't being detected. Let’s walk through setting up and using an Android emulator with MobSF for dynamic analysis.
During installation, ensure the Android Emulator and Android SDK Tools are selected.
Create an Android Virtual Device (AVD)
Open Android Studio and go to Tools > Device Manager.
Click Create Device.
Select a device definition (e.g., Pixel 5) and click Next.
Choose a system image (e.g., Android 12, API 31) and click Next.
Name your AVD and click Finish.
Start the Emulator
In the Device Manager, select your AVD and click Launch.
2. Verify Emulator is Running
Ensure the emulator is running and visible on your screen.
Open a terminal and check if the emulator is detected by ADB:
bash
adb devices
You should see an entry like:
code
List of devices attached
emulator-5554 device
3. Configure MobSF to Use the Emulator
MobSF needs to know the identifier of your emulator. This is typically emulator-5554 (or similar).
Option 1: Set Analyzer Identifier in MobSF
Open the MobSF Docker container shell:
bash
docker exec -it <container_id> /bin/bash
Edit the MobSF configuration file:
bash
nano /home/mobsf/.MobSF/config.py
Add the following line:
python
ANALYZER_IDENTIFIER = "emulator-5554"
Replace with the actual identifier from .
User
OK but when i click dynamic analysis i get 'Error
Description
Cannot Connect to host.docker.internal:5555'
[INFO] 25/Jan/2025 20:01:25 - Creating Dynamic Analysis Environment for com.anonymous.NutriApp
[INFO] 25/Jan/2025 20:01:25 - ADB Restarted
[INFO] 25/Jan/2025 20:01:25 - Waiting for 2 seconds...
[INFO] 25/Jan/2025 20:01:27 - Connecting to Android host.docker.internal:5555
[INFO] 25/Jan/2025 20:01:37 - Waiting for 2 seconds...
[ERROR] 25/Jan/2025 20:01:39 - failed to connect to host.docker.internal:5555
[ERROR] 25/Jan/2025 20:01:39 - Cannot Connect to host.docker.internal:5555
[ERROR] 25/Jan/2025 20:01:39 - Internal Server Error: /android_dynamic/1dc05994a20b6490751225dec9e0620d
[ERROR] 25/Jan/2025 20:01:39 - Internal Server Error: /android_dynamic/1dc05994a20b6490751225dec9e0620d
Assistant
The error "Cannot Connect to host.docker.internal:5555" occurs because MobSF inside the Docker container cannot connect to your Android emulator. This is a common issue when using Docker with Android emulators, as Docker containers and emulators run in separate networking environments.
Let’s fix this step by step:
1. Identify the Emulator’s ADB Address
By default, the Android emulator uses localhost:5555 for ADB connections.
However, Docker containers cannot directly access localhost on the host machine. Instead, Docker uses host.docker.internal to refer to the host machine.
2. Configure MobSF to Use the Correct ADB Address
You need to ensure MobSF can access the emulator via ADB. Here’s how:
Option 1: Use host.docker.internal (Windows/Mac)
On Windows and macOS, Docker provides a special DNS name host.docker.internal to refer to the host machine.
Update MobSF’s configuration to use this address.
Open the MobSF Docker container shell:
bash
docker exec -it <container_id> /bin/bash
Edit the MobSF configuration file:
bash
nano /home/mobsf/.MobSF/config.py
Add the following line:
python
ANALYZER_IDENTIFIER = "host.docker.internal:5555"
Save and exit (Ctrl+O, Enter, Ctrl+X).
Restart the MobSF container:
bash
docker restart <container_id>
Option 2: Use Host Machine’s IP Address
Find your host machine’s IP address:
On Windows: Open Command Prompt and run:
cmd
ipconfig
Look for the IPv4 address under your active network adapter.
On macOS/Linux: Open a terminal and run:
Look for the address under your active network adapter.
Dataset releases
Not yet included in a published release.
If you need the trackers, ensure they comply with privacy regulations (e.g., GDPR, CCPA) and disclose their use in your privacy policy.
3. Exported Components
Exported Services, Receivers, and Activities are potential attack vectors if not properly secured.
Why it matters:
Attackers can exploit exported components to perform unauthorized actions, such as accessing sensitive data or executing malicious code.
Action:
Review the Exported Components section in the report.
For each exported component, ask:
Does this component need to be exported?
If yes, is it properly protected with permissions or intent filters?
Restrict access to components that do not need to be exported.
4. Next Steps to Improve Security
Here’s a checklist to improve your app's security score and overall security posture:
a. Restrict Exported Components
Add android:exported="false" to components that do not need to be exported.
Use permissions or intent filters to control access to exported components.
b. Remove Unnecessary Permissions
Review the Permissions section in the report.
Remove any permissions that are not essential for your app's functionality.
c. Secure Sensitive Data
Check for hardcoded secrets (e.g., API keys, passwords) in the code or resources.
Use secure storage mechanisms like Android Keystore for sensitive data.
d. Update Dependencies
Review the Libraries section in the report.
Update all third-party libraries to their latest versions to avoid known vulnerabilities.
e. Address Vulnerabilities
Look for vulnerabilities like insecure storage, improper use of cryptography, or insecure network communication.
Follow secure coding practices to fix these issues.
f. Test with Dynamic Analysis
Connect your phone to MobSF and perform Dynamic Analysis.
Dynamic analysis will help you identify runtime issues like insecure API calls, data leakage, or insecure storage.
5. Dynamic Analysis (Connect Your Phone)
Dynamic analysis is crucial for identifying runtime vulnerabilities and ensuring your app behaves securely in real-world scenarios.
Steps to Perform Dynamic Analysis:
Connect Your Phone:
Enable USB Debugging on your Android device.
Connect your phone to your computer via USB.
Ensure MobSF detects your device in the Dynamic Analyzer section.
Start Dynamic Analysis:
Upload your APK if you haven’t already.
Click Start Dynamic Analysis in the MobSF interface.
MobSF will install the app on your device and start monitoring its behavior.
Analyze Results:
Check for runtime issues like insecure API calls, sensitive data leakage, or insecure storage.
Use the API Monitor and Logcat to track app behavior.
6. Re-run the Analysis
After making the necessary changes (e.g., restricting exported components, removing trackers, fixing vulnerabilities), re-upload your APK and run the analysis again. Aim for a higher security score (ideally above 80/100).
Summary
Security Score (48/100): Focus on restricting exported components, removing unnecessary permissions, and fixing vulnerabilities.
Trackers (2/432): Review and remove unnecessary trackers or ensure they comply with privacy regulations.
Dynamic Analysis: Connect your phone and perform dynamic analysis to identify runtime issues.
By following these steps, you can significantly improve your app's security and user trust. Let me know if you need further clarification or help!
--device
bash
docker run -it --rm -p 8000:8000 -v H:\Mobile-Security-Framework-MobSF:/home/mobsf/.MobSF --device=/dev/bus/usb opensecurity/mobile-security-framework-mobsf:latest
If you’re on Windows, you may need to use -v to mount the ADB executable: