VNC connection to iPhone
After a short hiatus I thought I’d post something short and sweet to ease back into it.
There’s been quite a bit of news of people getting the location of their stolen iPhones via MobileMe. It’s also nice for your own peace of mind to be able to tell the phone to wipe itself so all your precious data doesn’t fall into thieving hands.
MobileMe is $99 a year though. Count me out.
Now I’ve previously talked about how to setup a tunnel to connect to a remote PC securely via Remote Desktop. In the same way it’s possible to connect to your iPhone securely, and, more importantly, wherever it has data access!
Here’s how to setup a persistent SSH tunnel from the iPhone to your home server that gives you SSH and VNC access to the phone.
- A home SSH server visible from the outside. If you want your phone to phone home, you need something to pick up.
- A jailbroken iPhone. If you don’t want to jailbreak it, there’s always MobileMe.
- OpenSSH for iPhone. Install it via Cydia. Make sure you change the default passwd for both ‘root’ and ‘mobile’ users.
- VNC server for iPhone. It’s called Veency, install it via Cydia. Go to the Settings and set a connection password.
- Autossh. Install it via Cydia.
That’s it, now you just need a script to start the tunnel, another script to start the first script at load time, to register your phone ssh key on your home server and we’re done here.
The tunnel script, via the Terminal or a SFTP application (I recommend the free FileZilla), write it as /bin/autohome.sh:
autossh -M 20000 -f -2 -N -C -R *:50022:localhost:22 -R *:5901:localhost:5900 USERNAME@HOME
Set the permissions to 755 (chmod 755 /bin/autohome.sh, that’s read/write/execute as root, read/execute for others).
Easy enough. Set your USERNAME and HOME address to your public home SSH server. The only gotcha is the gatetime setting, it must be set to 0 to allow the connection to start at boot time: it will very likely fail a few times until the iPhone finds a data connection (3G or Wifi), but you want autossh to retry until it connects, which is not the default behaviour. Make sure that the file has the Linux end of lines.
The boot script, write it as /System/Library/LaunchDaemons/com.autohome.startup.plist:
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple Computer//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
Set the permissions to 644 (read/write as root, read only for others).
Now you need to be able to login to your home server from the phone without typing your password. You also don’t want to compromise the security of your home machine, if it can be avoided. For that, you can add a new user specifically for the phone (for instance ‘mobilemoi’), set its login shell to /bin/false (using the chsh command) and remove its password from the /etc/passwd file. Give this user access to nothing. Don’t forget to use this user in the tunnel script.
Login onto the iPhone via SSH as root (using putty.exe for example), run ssh-keygen using the default settings. We’ll need the public key that was just created, so cat ~/.ssh/id_rsa.pub and copy the string in the clipboard.
Login onto the home server as root, go in the ‘mobilemoi’ directory, create a .ssh directory, in there create a file called authorized_keys and paste the public key in there. From the mobilemoi directory, run chown -R mobilemoi.mobilemoi .ssh to set the right permissions.
Test it. On the phone, as root, run autohome.sh. On the home server, run netstat -a. After a short while, you should see the phone’s connections, lines like these:
tcp 0 0 Unknown-xxxx:ssh xxxx:62595 ESTABLISHED
tcp6 0 0 [::]:20000 [::]:* LISTEN
tcp6 0 0 [::]:50022 [::]:* LISTEN
tcp6 0 0 [::]:5901 [::]:* LISTEN
If the connections look like “localhost:5901″ instead of “*:5901″ or “[::]:5901″ then that’s a problem in the sshd config file. Edit /etc/ssh/sshd_config and add “GatewayPorts yes”. Restart the ssh server.
Now to connect to the iPhone and control it from any PC, install VNC, start the viewer, connect to the VNC server using your home SSH server address (likely on the internal IP) on port 1.
VNC Viewer settings
You can also use an SSH client to connect to the iPhone via the tunnel. Point the client to your home server (again the internal IP), on port 50022.
Right. Reboot your iPhone, make sure that the tunnels are created. You’re done.
Now every time you boot your iPhone, a tunnel is created to your home server. This gives you the IP of the phone (via the server logs), SSH access to the phone and VNC control of the phone. The possibilities are limitless!
As of the time of writing, I have not fully verified the drain on the battery that results from the permanent connection. I believe that it should be minimal, but it has yet to be observed. I will update the post with more information when I know for sure one way or the other. The connection parameters may need to be tweaked to minimise the impact on the battery life.
EDIT: After some experimentation, it appears that there is a toll on the battery life after all. I will continue to investigate.
Ideally the connection would start on demand. It would be nice to send a push message to the phone that would trigger the start of autossh. This would essentially be free (or no more costly than push notifications are) and offer the same functionality.
Thanks to Saurik for the most of the software needed for this feature.