Skip to content

Instantly share code, notes, and snippets.

@leonjza
Last active April 20, 2024 09:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leonjza/3751e551437c108edd688501deafa2a1 to your computer and use it in GitHub Desktop.
Save leonjza/3751e551437c108edd688501deafa2a1 to your computer and use it in GitHub Desktop.
Forward a remote iOS device, locally.

fwd_ios.sh

Forwards a remote iOS device, exposed via usbmuxd on Linux to a local macOS client.
The inverse (aka ssh -R) is left as an excercise to the reader, but shouldn't be too hard :)

This is basically a simple usbfluxd that only depends on socat.

install

Copy the bash script to a local file, say fwd_ios.sh and make it executable with chmod +x fwd_ios.sh.

Then, run it and specify the remote host that has the physical iOS device you want connected. Example:

leons-Mac:~ root# ./fwd_ios.sh leon@172.16.182.172
[+] moving real usbmuxd socket out of the way
[+] configuring ssh tunnel to leon@172.16.182.172
leon@172.16.182.172's password:
[+] ssh tunnel PID is 914
[+] connecting remote socket to local socket. ^C to quit and revert

To end the session, hit ^C.

^C[+] killing ssh tunnel with PID 914
[+] restoring real usbmuxd socket
[+] removing dangling remote socket
#!/bin/bash
# forward a remote usbmuxd socket locally.
# useful to make remote iOS devices available to cloud macOS instances
#
# 2022 @leonjza
set -e
# arg 1 being the target. eg: leon@remote
SSH_TARGET=$1
if [ -z $SSH_TARGET ]
then
echo "[+] usage: $0 ssh_target (eg: $0 user@host)"
exit 1
fi
REAL_SOCKET=/var/run/usbmuxd
BACKUP_SOCKET=/var/run/usbmuxd.orig
REMOTE_SOCKET=/var/run/usbmuxd.remote
SSH_TUNNEL_PID=""
# handle ^C && errors
trap cleanup INT
trap cleanup ERR
function cleanup() {
if [ ! -z $SSH_TUNNEL_PID ]
then
echo "[+] killing ssh tunnel with PID $SSH_TUNNEL_PID"
kill -15 $SSH_TUNNEL_PID
fi
echo "[+] restoring real usbmuxd socket"
mv $BACKUP_SOCKET $REAL_SOCKET
echo "[+] removing dangling remote socket"
rm -f $REMOTE_SOCKET
exit
}
echo "[+] moving real usbmuxd socket out of the way"
mv $REAL_SOCKET $BACKUP_SOCKET
echo "[+] configuring ssh tunnel to $SSH_TARGET"
ssh -C -L $REMOTE_SOCKET:$REAL_SOCKET $SSH_TARGET -N -f
SSH_TUNNEL_PID=$(pgrep ssh | tail -n 1)
echo "[+] ssh tunnel PID is $SSH_TUNNEL_PID"
echo "[+] connecting remote socket to local socket. ^C to quit and revert"
socat UNIX-LISTEN:$REAL_SOCKET,mode=777,reuseaddr,fork UNIX-CONNECT:$REMOTE_SOCKET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment