Android: how to use adb commands to block/unblock specific app from using the Internet, on rooted device if needed?
2
votes
0
answers
2112
views
Background
--
I want to temporarily block a specific app from using the Internet on my Android device, using a batch file. The device is rooted, so it should be possible via the PC.
The problem
--
I actually found something over the Internet that seems to work fine for most apps:
@echo off
set package_name="com.android.chrome"
c:
cd\
cd C:\Users\User\AppData\Local\Android\Sdk\platform-tools
for /f "delims=" %%A in ('adb shell dumpsys package %package_name% ^| findstr "userId="') do (
for /f "tokens=2 delims== " %%B in ("%%A") do (
echo Extracted UID: %%B
adb shell "su -c 'iptables -A OUTPUT -m owner --uid-owner %%B -j DROP'"
)
)
And to unblock:
@echo off
set package_name="com.android.chrome"
c:
cd\
cd C:\Users\User\AppData\Local\Android\Sdk\platform-tools
for /f "delims=" %%A in ('adb shell dumpsys package %package_name% ^| findstr "userId="') do (
for /f "tokens=2 delims== " %%B in ("%%A") do (
echo Extracted UID: %%B
adb shell "su -c 'iptables -D OUTPUT -m owner --uid-owner %%B -j DROP'"
)
)
Thing is, as I wrote, it doesn't always work for all apps.
I also tried this instead:
...
adb shell "su -c 'iptables -I OUTPUT -m owner --uid-owner %%B -j REJECT'"
What I've found
--
I've found an open sourced app called "AFWall+ " , and inside of it, I've noticed a file that seems to call many functions to block the app. When launching the app and applying a blocking rule, it indeed takes some time, which means maybe it's worth checking out:
https://github.com/ukanth/afwall/blob/beta/app/src/main/java/dev/ukanth/ufirewall/Api.java#L660
I've tried to launch it and debug, and I think it created more than 90 adb commands in the process of blocking a single app that I chose...
But, it seems it worked.
The questions
--
1. Is there a minimal way to block an app completely from the Internet using adb ?
2. How come there are so many commands that were used on "AFWall+"?
Asked by android developer
(615 rep)
Jun 24, 2023, 08:14 PM
Last activity: Jun 30, 2023, 10:06 PM
Last activity: Jun 30, 2023, 10:06 PM