Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.
Python adbadblockandroid-tvandroid-tv-launcherapple-tvbloatware-removalclaude-codedebloatfire-tvgoogle-tvlauncherprivacysmart-tvsony-braviatclvibe-codingxiaomi
Safety: disable.sh and disable.ps1 warn when ro.adb.secure is empty, not only when it is 0 (#1720)
Gathin23ba52c1b87d27
diff --git a/PROMPT.md b/PROMPT.mdindex 49d4018cf..a13c49732 100644--- a/PROMPT.md+++ b/PROMPT.md@@ -65,8 +65,8 @@ Back up firstover a USB Ethernet adapter, unplug and replug the adapter once debugging is off - the port comesback with it - and have me confirm the TV is on Ethernet again, not Wi-Fi. Don't trust `nc` on macOS:Local Network privacy can block it too, and its "No route to host" proves nothing; only "Connection- refused" does. Port 5555 is plaintext, and on firmware with `ro.adb.secure=0` any device on the LAN- can get a shell.+ refused" does. Port 5555 is plaintext, and on firmware with `ro.adb.secure=0` (or with it unset, on a+ debug build or Android 4.4) any device on the LAN can get a shell.Debloat- List installed packages and sort them into: manufacturer telemetry/analytics, manufacturer contentdiff --git a/README.md b/README.mdindex f77fa0015..c82573280 100644--- a/README.md+++ b/README.md@@ -947,7 +947,8 @@ TV at their home routing to it. Never by opening the ADB port on their router. SIt is one explicit "allow this computer" tap on the TV, thesame as developing an Android app. Network ADB is plaintext on port 5555, and on firmware with-`ro.adb.secure=0` any device on the LAN gets a shell, so close it when you are done:+`ro.adb.secure=0` (or with it unset, on a debug build or Android 4.4) any device on the LAN gets a+shell, so close it when you are done:1. On the TV: Developer options → turn **USB debugging**, **Network/ADB debugging** and**Wireless debugging** off, whichever the TV has, and revoke the USB debugging authorisations.diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.mdindex 1f73902a3..4e226de52 100644--- a/docs/TROUBLESHOOTING.md+++ b/docs/TROUBLESHOOTING.md@@ -89,7 +89,7 @@ Things that bit us or contributors, so they don't bite you. Commands use `tools/`nmap -sT -Pn` (a plain TCP connect scan, no ping, no raw sockets) or the Python loop above stillworks there. Every script here honours `TV_PORT`, andthe closing check is `tools/adb-closed.py <tv ip> <port>`.-- **Close port 5555 when you're done**: plaintext, and a shell for anyone on the LAN when `ro.adb.secure=0`. Steps in the README FAQ.+- **Close port 5555 when you're done**: plaintext, and a shell for anyone on the LAN when `ro.adb.secure=0`, or when it is unset on a debug build or Android 4.4 (`tools/disable.sh` warns about both). Steps in the README FAQ.- **The TV sits on a separate VLAN or IoT network** and `tools/tv.py` times out although debuggingis on: the router keeps the computer from reaching it. Add a temporary firewall rule from yourcomputer to the TV on tcp/5555 only, and remove it again when you close ADB. Opening the wholediff --git a/tests/test_adb_exposure.py b/tests/test_adb_exposure.pyindex d0cb5f2c2..38563e438 100644--- a/tests/test_adb_exposure.py+++ b/tests/test_adb_exposure.py@@ -1,9 +1,16 @@-"""tools/disable.sh says so when the TV's network ADB accepts any device (ro.adb.secure=0).+"""tools/disable.sh and disable.ps1 say so when the TV's network ADB accepts any device.++That is `ro.adb.secure=0`, and also an empty value (#1720): on a debug (userdebug or eng) build, and on+Android 4.4, adbd reads a missing ro.adb.secure as 0 and gives a shell with no "Allow" prompt. Sets in+examples/ do exactly that (rca-rc50a21s-4ksm, a userdebug build; caxium-58-realtek-rtk2851p,+"ro.adb.secure unset"). A read that fails is not an empty value: it only skips the note.Run from the repository root: python3 -m unittest discover -s tests -v+The PowerShell cases need `pwsh` on PATH and skip without it; CI's Ubuntu runner has it."""import osfrom pathlib import Path+import reimport shutilimport subprocessimport tempfile@@ -12,61 +19,93 @@ import unittestROOT = Path(__file__).resolve().parents[1]LIST = "# My TV\ncom.example.one\ncom.example.two\n"+PWSH = shutil.which("pwsh")class AdbSecureWarningTest(unittest.TestCase):- def run_disable(self, getprop):+ def entries(self):+ return ("sh", "ps1") if PWSH else ("sh",)++ def run_disable(self, getprop, entry="sh"):with tempfile.TemporaryDirectory() as tmp:repo = Path(tmp) / "repo"tools = repo / "tools"tools.mkdir(parents=True)shutil.copy2(ROOT / "tools" / "disable.sh", tools / "disable.sh")- client = tools / "venv" / "bin" / "python"- client.parent.mkdir(parents=True)+ shutil.copy2(ROOT / "tools" / "disable.ps1", tools / "disable.ps1")# Fake tv.py client: answers getprop as given, takes the record, records --batch input.- client.write_text(- '#!/bin/sh\n'- 'case "$*" in\n'- f' *getprop*) {getprop} ;;\n'- ' *recovery.py*) exit 0 ;;\n'- ' *--batch*) cat > "$CAPTURE" ;;\n'- 'esac\n')- client.chmod(0o755)+ # disable.ps1 asks for the Windows path, which PowerShell resolves here too.+ for client in (tools / "venv" / "bin" / "python",+ tools / "venv" / "Scripts" / "python.exe"):+ client.parent.mkdir(parents=True)+ client.write_text(+ '#!/bin/sh\n'+ 'case "$*" in\n'+ f' *getprop*) {getprop} ;;\n'+ ' *recovery.py*) exit 0 ;;\n'+ ' *--batch*) cat > "$CAPTURE" ;;\n'+ 'esac\n')+ client.chmod(0o755)(repo / "debloat.txt").write_text(LIST)capture = Path(tmp) / "commands.txt"env = dict(os.environ, CAPTURE=str(capture))- if os.name != "posix" or not shutil.which("bash"):- raise unittest.SkipTest("disable.sh is a POSIX script; it needs bash")- result = subprocess.run(["bash", str(tools / "disable.sh"), "debloat.txt"], cwd=repo,- env=env, capture_output=True, text=True,- stdin=subprocess.DEVNULL, timeout=60)- self.assertEqual(result.returncode, 0, result.stderr)+ if entry == "sh":+ if os.name != "posix" or not shutil.which("bash"):+ raise unittest.SkipTest("disable.sh is a POSIX script; it needs bash")+ argv = ["bash", str(tools / "disable.sh"), "debloat.txt"]+ else:+ argv = [PWSH, "-NoProfile", "-File", str(tools / "disable.ps1"),+ "-TvHost", "192.168.1.50", "debloat.txt"]+ result = subprocess.run(argv, cwd=repo, env=env, capture_output=True, text=True,+ stdin=subprocess.DEVNULL, timeout=120)+ output = result.stdout + result.stderr+ self.assertEqual(result.returncode, 0, output)# The note never changes what is sent.self.assertEqual(capture.read_text().splitlines(), ["pm disable-user --user 0 com.example.one","pm disable-user --user 0 com.example.two","pm list packages -d",])- return result.stderr+ # Write-Warning can wrap a long line, so the PowerShell output is read with the line+ # breaks joined; the shell script's lines are joined the same way.+ return re.sub(r"\s+", " ", output)def test_warns_when_adb_is_not_secure(self):- stderr = self.run_disable('echo 0')- self.assertIn("ro.adb.secure=0", stderr)- self.assertIn("Isn't ADB access a security hole?", stderr)+ for entry in self.entries():+ with self.subTest(entry=entry):+ output = self.run_disable('echo 0', entry)+ self.assertIn("reports ro.adb.secure=0", output)+ self.assertIn("Isn't ADB access a security hole?", output)+ self.assertIn("Wireless debugging", output)def test_warns_with_crlf_output(self):- stderr = self.run_disable(r"printf '0\r\n'")- self.assertIn("ro.adb.secure=0", stderr)+ for entry in self.entries():+ with self.subTest(entry=entry):+ self.assertIn("reports ro.adb.secure=0", self.run_disable(r"printf '0\r\n'", entry))def test_quiet_when_adb_is_secure(self):- self.assertNotIn("ro.adb.secure", self.run_disable('echo 1'))+ for entry in self.entries():+ with self.subTest(entry=entry):+ self.assertNotIn("ro.adb.secure", self.run_disable('echo 1', entry))- def test_quiet_when_the_property_is_missing(self):- self.assertNotIn("ro.adb.secure", self.run_disable('echo'))+ def test_warns_when_the_property_is_empty(self):+ # #1720: an unset ro.adb.secure is no authorisation on a debug build or Android 4.4.+ for entry in self.entries():+ for getprop in ('echo', r"printf '\r\n'", "printf ''"):+ with self.subTest(entry=entry, getprop=getprop):+ output = self.run_disable(getprop, entry)+ self.assertIn("does not set ro.adb.secure", output)+ self.assertIn("the same as ro.adb.secure=0", output)+ self.assertIn("Isn't ADB access a security hole?", output)+ self.assertNotIn("reports ro.adb.secure=0", output)def test_quiet_when_the_read_fails(self):- # A failed read skips the note; it does not stop the list or guess.- self.assertNotIn("ro.adb.secure", self.run_disable('echo 0; exit 1'))+ # A failed read skips the note; it does not stop the list or guess, and it is not read as an+ # empty value either.+ for entry in self.entries():+ for getprop in ('echo 0; exit 1', 'exit 1'):+ with self.subTest(entry=entry, getprop=getprop):+ self.assertNotIn("ro.adb.secure", self.run_disable(getprop, entry))if __name__ == "__main__":diff --git a/tests/test_disable_verify.py b/tests/test_disable_verify.pyindex 1773aa051..44772e65b 100644--- a/tests/test_disable_verify.py+++ b/tests/test_disable_verify.py@@ -214,9 +214,11 @@ class DisableVerifyTest(unittest.TestCase):# What each script runs: the read, the line that fails the run, and the ADB warning before it.lines = {"disable.sh": ("\"$PY\" -I tools/tv.py 'pm list packages -e --user 0' 2>/dev/null",- '\n[ -z "$STILL_ON" ] || exit 1\n', '\nif [ "$ADB_SECURE" = "0" ]; then\n'),+ '\n[ -z "$STILL_ON" ] || exit 1\n',+ '\nif [ "$ADB_SECURE" = "0" ] || [ -z "$ADB_SECURE" ]; then\n'),"disable.ps1": ("& $py -I $tvPy 'pm list packages -e --user 0' 2>$null",- "\nif ($stillOn.Count -gt 0) { exit 1 }\n", "\nif ($adbSecure -eq '0') {\n"),+ "\nif ($stillOn.Count -gt 0) { exit 1 }\n",+ "\nif ($null -ne $adbSecure -and ($adbSecure -eq '0' -or $adbSecure -eq '')) {\n"),}for name, text in (("disable.sh", sh), ("disable.ps1", ps1)):read, fail, adb = lines[name]diff --git a/tools/disable.ps1 b/tools/disable.ps1index 1f3fa91e6..72c09302d 100644--- a/tools/disable.ps1+++ b/tools/disable.ps1@@ -80,11 +80,13 @@ if ($pkgs -contains $setup -and -not $forceSetupwraith) {}}# Same as disable.sh: on firmware with ro.adb.secure=0, port 5555 gives a shell to any device on the-# network without the "Allow" prompt. Read it now and say so once the list is done; a failed read only-# skips the note. Windows PowerShell turns redirected stderr into an error that 'Stop' would throw.+# network without the "Allow" prompt, and an empty value counts too (a debug build or Android 4.4 reads+# a missing ro.adb.secure as 0, #1720). Read it now and say so once the list is done; a failed read+# only skips the note, so it is $null, apart from an empty answer. Windows PowerShell turns redirected+# stderr into an error that 'Stop' would throw.$ErrorActionPreference = 'Continue'$adbSecure = ((& $py -I (Join-Path $repo 'tools\tv.py') 'getprop ro.adb.secure' 2>$null) -join '').Trim()-if ($LASTEXITCODE -ne 0) { $adbSecure = '' }+if ($LASTEXITCODE -ne 0) { $adbSecure = $null }$ErrorActionPreference = 'Stop'# Home guard (#239), the same check as disable.sh: a list that disables the app owning Home leaves the@@ -284,8 +286,9 @@ if ($pkgs.Count -gt 0) {}}-if ($adbSecure -eq '0') {- Write-Warning "This TV reports ro.adb.secure=0, so network ADB on port 5555 accepts any device on your network without the `"Allow`" prompt. When you are done, turn USB debugging and Network/ADB debugging off on the TV and reboot it (README: `"Isn't ADB access a security hole?`")."+if ($null -ne $adbSecure -and ($adbSecure -eq '0' -or $adbSecure -eq '')) {+ $why = if ($adbSecure -eq '') { 'This TV does not set ro.adb.secure. On a debug (userdebug) build or Android 4.4 that is the same as ro.adb.secure=0: network ADB on port 5555 accepts any device on your network without the "Allow" prompt.' } else { 'This TV reports ro.adb.secure=0, so network ADB on port 5555 accepts any device on your network without the "Allow" prompt.' }+ Write-Warning "$why When you are done, turn USB debugging, Network/ADB debugging and Wireless debugging off on the TV and reboot it (README: `"Isn't ADB access a security hole?`")."}if ($batchExit -ne 0) { throw "tv.py --batch failed (exit code $batchExit): the list may be only partly applied; run it again once the TV answers" }# Last, so the warnings above are printed either way: a listed package the TV left enabled fails thediff --git a/tools/disable.sh b/tools/disable.shindex 8e7fb9cd2..c6505bce7 100755--- a/tools/disable.sh+++ b/tools/disable.sh@@ -93,8 +93,11 @@ if printf '%s\n' "$PKGS" | grep -x "$SETUP" >/dev/null && [ -z "${FORCE_SETUPWRAfi# On firmware with ro.adb.secure=0, port 5555 gives a shell to any device on the network without the-# "Allow" prompt. Read it now and say so once the list is done; a failed read only skips the note.-ADB_SECURE="$("$PY" -I tools/tv.py "getprop ro.adb.secure" 2>/dev/null | tr -d '[:space:]')" || ADB_SECURE=""+# "Allow" prompt. An empty value counts too: on a debug (userdebug or eng) build, and on Android 4.4,+# adbd reads a missing ro.adb.secure as 0, and sets in examples/ give a shell that way (#1720). Read it+# now and say so once the list is done; a failed read only skips the note, so it is kept apart from an+# empty answer.+ADB_SECURE="$("$PY" -I tools/tv.py "getprop ro.adb.secure" 2>/dev/null | tr -d '[:space:]')" || ADB_SECURE="unread"# Home guard (#239): a list that disables the app owning Home leaves the TV with nothing to start the# next time Home is pressed, and on Google TV the setup wizard can take the category instead and@@ -356,10 +359,17 @@ if [ -n "$PKGS" ]; thenfifi-if [ "$ADB_SECURE" = "0" ]; then- echo "Warning: this TV reports ro.adb.secure=0, so network ADB on port 5555 accepts any device on" >&2- echo "your network without the \"Allow\" prompt. When you are done, turn USB debugging and" >&2- echo "Network/ADB debugging off on the TV and reboot it (README: \"Isn't ADB access a security hole?\")." >&2+if [ "$ADB_SECURE" = "0" ] || [ -z "$ADB_SECURE" ]; then+ if [ -z "$ADB_SECURE" ]; then+ echo "Warning: this TV does not set ro.adb.secure. On a debug (userdebug) build or Android 4.4 that" >&2+ echo "is the same as ro.adb.secure=0: network ADB on port 5555 accepts any device on your network" >&2+ echo "without the \"Allow\" prompt." >&2+ else+ echo "Warning: this TV reports ro.adb.secure=0, so network ADB on port 5555 accepts any device on" >&2+ echo "your network without the \"Allow\" prompt." >&2+ fi+ echo "When you are done, turn USB debugging, Network/ADB debugging and Wireless debugging off on the" >&2+ echo "TV and reboot it (README: \"Isn't ADB access a security hole?\")." >&2fi# Last, so the warning above is printed either way: a listed package the TV left enabled fails the