Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions kali_on_x3_installer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
#!/usr/bin/env bash
set -euo pipefail

# Kali NetHunter Rootless automated setup for Termux on Huawei Mate X3
# Target directory: /data/data/com.termux/files/home/KALI_ON_X3

TARGET_DIR="/data/data/com.termux/files/home/KALI_ON_X3"
NH_INSTALLER_URL="https://offs.ec/2MceZWr" # OffSec shortlink to NetHunter Rootless installer

command_exists() {
command -v "$1" >/dev/null 2>&1
}

log() {
echo "[KALI_ON_X3] $*"
}

abort() {
echo "[KALI_ON_X3][ERROR] $*" >&2
exit 1
}

ensure_termux() {
if [[ ! -d "/data/data/com.termux/files/home" ]]; then
abort "This installer must be run inside Termux."
fi
}

ensure_prereqs() {
log "Updating Termux packages and installing prerequisites..."
# Use pkg if available, otherwise apt.
if command_exists pkg; then
pkg update -y || true
pkg upgrade -y || true
pkg install -y wget proot tar curl grep coreutils || true
else
apt update -y || true
apt upgrade -y || true
apt install -y wget proot tar curl grep coreutils || true
fi

# Some Termux builds require storage permission for large downloads (optional)
if command_exists termux-setup-storage; then
termux-setup-storage || true
fi
}

create_layout() {
log "Creating target directory at ${TARGET_DIR}..."
mkdir -p "${TARGET_DIR}/bin" "${TARGET_DIR}/cache" "${TARGET_DIR}/logs"
}

download_installer() {
local out="${TARGET_DIR}/cache/install-nethunter-termux"
log "Fetching NetHunter Rootless installer..."
curl -fsSL -o "$out" "$NH_INSTALLER_URL" || wget -qO "$out" "$NH_INSTALLER_URL"
chmod +x "$out"
echo "$out"
}

run_nethunter_install() {
local installer_path="$1"
log "Running NetHunter installer (this will take a while)..."
# The official script installs under $HOME by default. We isolate by pushing a temp HOME.
# Using env HOME to direct installation into TARGET_DIR by temporarily setting HOME.
HOME_ORIG="$HOME"
export HOME="$TARGET_DIR"
# Non-interactive: the script is interactive by default; however, it usually proceeds without prompts.
# If prompts occur, they are typically for selecting arch; we detect device arch and export appropriately.
ARCH=$(uname -m)
case "$ARCH" in
armv7l|armv8l|arm)
export NHARCH="arm"
;;
aarch64|arm64)
export NHARCH="arm64"
;;
x86_64)
export NHARCH="amd64"
;;
*)
export NHARCH="arm64" # Mate X3 is ARM64
;;
endcase
# Run inside a subshell to avoid leaking env
( bash "$installer_path" )
# Restore HOME
export HOME="$HOME_ORIG"
}

create_wrappers() {
log "Creating startup wrappers..."
local start_wrapper="${TARGET_DIR}/bin/kali"
local su_wrapper="${TARGET_DIR}/bin/kali-su"
local profile_d="/data/data/com.termux/files/usr/etc/profile.d"
mkdir -p "${TARGET_DIR}/bin"
cat > "$start_wrapper" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
if command -v nethunter >/dev/null 2>&1; then
# Prefer official nethunter entrypoint if available in PATH
exec nethunter "$@"
fi
# Fallback to startkali if provided by scripts
if command -v startkali >/dev/null 2>&1; then
exec startkali "$@"
fi
echo "Kali entrypoint not found. Ensure NetHunter Rootless is installed." >&2
exit 1
EOF
chmod +x "$start_wrapper"

cat > "$su_wrapper" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
if command -v nethunter >/dev/null 2>&1; then
exec nethunter -r su -c bash "$@"
fi
echo "nethunter not available; root shell wrapper cannot proceed." >&2
exit 1
EOF
chmod +x "$su_wrapper"

# Add to PATH via profile.d if not already
mkdir -p "$profile_d"
local export_file="$profile_d/kali_on_x3_path.sh"
if [[ ! -f "$export_file" ]]; then
echo "export PATH=\"${TARGET_DIR}/bin:\$PATH\"" > "$export_file"
fi
}

post_install_notes() {
log "Post-install: updating Kali if available..."
# Try a quick non-interactive update if nethunter is present
if command -v nethunter >/dev/null 2>&1; then
nethunter -r apt-get update || true
fi
log "Installation complete. Open a new Termux session or run: source /data/data/com.termux/files/usr/etc/profile"
log "Then start Kali with: kali"
}

already_installed() {
# Heuristic: if NetHunter created its directory or entrypoint
if command -v nethunter >/dev/null 2>&1; then
return 0
fi
if [[ -d "${TARGET_DIR}/kali-arm64" || -d "${TARGET_DIR}/kali-arm" || -f "${TARGET_DIR}/.nh-installed" ]]; then
return 0
fi
return 1
}

mark_installed() {
touch "${TARGET_DIR}/.nh-installed" || true
}

uninstall() {
log "Uninstalling Kali NetHunter Rootless from ${TARGET_DIR}..."
# Remove wrappers from PATH
rm -f "${TARGET_DIR}/bin/kali" "${TARGET_DIR}/bin/kali-su" || true
# Remove PATH export file
rm -f "/data/data/com.termux/files/usr/etc/profile.d/kali_on_x3_path.sh" || true
# Attempt to remove NetHunter directories commonly used
rm -rf "${TARGET_DIR}/kali-arm64" "${TARGET_DIR}/kali-arm" "${TARGET_DIR}/cache" "${TARGET_DIR}/logs" || true
rm -f "${TARGET_DIR}/.nh-installed" || true
# Remove target dir if empty
rmdir "${TARGET_DIR}/bin" 2>/dev/null || true
rmdir "${TARGET_DIR}" 2>/dev/null || true
log "Uninstall complete. You may need to start a new Termux session to refresh PATH."
}

main() {
ensure_termux
if [[ "${1:-}" == "--uninstall" ]]; then
uninstall
return 0
fi
ensure_prereqs
create_layout
if already_installed; then
log "NetHunter appears to be already installed. Skipping install step."
create_wrappers
post_install_notes
return 0
fi
local installer_path
installer_path=$(download_installer)
run_nethunter_install "$installer_path"
create_wrappers
mark_installed
post_install_notes
}

main "$@"