Skip to content

Instantly share code, notes, and snippets.

@Apihplays
Created June 1, 2026 16:08
Show Gist options
  • Select an option

  • Save Apihplays/f19c8e98f69d10a4d633f9f8c0740af9 to your computer and use it in GitHub Desktop.

Select an option

Save Apihplays/f19c8e98f69d10a4d633f9f8c0740af9 to your computer and use it in GitHub Desktop.
Setting TTL to 65 Android 16 using KernelSU
#!/system/bin/sh
# ==============================================================================
# SECURED EVENT-DRIVEN TTL DAEMON FOR ANDROID 16 (WITH FAILSAFES)
# ==============================================================================
TARGET_TTL=65
# FAILSAFE 1: Wait until the system is fully booted and decrypted
# This prevents early boot conflicts that cause bootloops.
while [ "$(getprop sys.boot_completed)" != "1" ]; do
sleep 3
done
# Define absolute paths to system binaries to prevent PATH-hijack vulnerabilities
IPTABLES="/system/bin/iptables"
IP6TABLES="/system/bin/ip6tables"
SYSCTL="/system/bin/sysctl"
IP_TOOL="/system/bin/ip"
apply_ttl_rules() {
# Check if binaries exist before calling them
if [ ! -f "$SYSCTL" ] || [ ! -f "$IPTABLES" ]; then
return 1
fi
# Update Global System Configuration safely
CURRENT_SYS=$($SYSCTL -n net.ipv4.ip_default_ttl 2>/dev/null)
if [ "$CURRENT_SYS" != "$TARGET_TTL" ]; then
$SYSCTL -w net.ipv4.ip_default_ttl=$TARGET_TTL >/dev/null 2>&1
fi
# Secure deletion and insertion using safe absolute paths
# FIX: Run a drain loop to remove EVERY duplicate rule instance before inserting
while $IPTABLES -t mangle -D POSTROUTING -j TTL --ttl-set $TARGET_TTL >/dev/null 2>&1; do
: # Loop until all matching rules are completely drained
done
$IPTABLES -t mangle -I POSTROUTING 1 -j TTL --ttl-set $TARGET_TTL >/dev/null 2>&1
if [ -f "$IP6TABLES" ]; then
while $IP6TABLES -t mangle -D POSTROUTING -j HL --hl-set $TARGET_TTL >/dev/null 2>&1; do
: # Loop until all matching IPv6 rules are completely drained
done
$IP6TABLES -t mangle -I POSTROUTING 1 -j HL --hl-set $TARGET_TTL >/dev/null 2>&1
fi
}
# Run first application safely
apply_ttl_rules
# FAILSAFE 2: Gracefully handle loop if 'ip monitor' tool fails
if [ -f "$IP_TOOL" ]; then
$IP_TOOL monitor route 2>/dev/null | while read -r change_event; do
apply_ttl_rules
done
else
# Fallback to extremely low frequency polling if ip tool is missing
while true; do
apply_ttl_rules
sleep 10
done
fi
```
eof
### How to Apply the Fix Right Now:
1. Copy the updated code above.
2. Replace your existing `ttl_optimized_daemon.sh` script with this new version.
3. Run it once manually, or trigger a network event (like toggling airplane mode) to execute the new loop logic.
4. Verify your tables again with:
```bash
iptables -t mangle -L POSTROUTING -n -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment