Wednesday, May 20, 2015

Dude, Where's my PC?

If you've ever had a PC or laptop stolen, lost, or knew someone who had that happen to them and wished there was an easy way to track it down, this may help.

It will work on Windows systems if you install Linux as a dual-boot option. It will default boot to Linux. Password protect Windows so that the "user" is forced to only use the Linux OS. You can have Linux boot without requiring a password login. This will keep the new "user" busy while your computer is phoning home. Just don't write the super-user password down on your computer.

If you have the public IP address, you can get a close physical location, but probably not close enough to matter. However, if you also have a list of the nearby wifi networks and signal strengths, you can pinpoint the location pretty accurately if you know some trigonometry. See? This is one good reason to pay attention in math class.

It may take some roaming around from the IP address's stated location before you find one of the networks, but in theory it should be possible to locate the missing PC. 

Here are the steps.

1. Backup your PC. You know, just in case you accidentally have it install Linux by erasing your Windows installation.

2. Install a linux operating system if you don't have one already. Choose dual-boot if you wish to keep Windows.

Prevent the new "user" from easily re-installing the operating system:

3. In BIOS, set the boot order to boot to the hard drive only.

4. Password protect BIOS so step #1 can't be changed without the password. 

5. If you'd like to be able to remotely log-in to the PC if you knew its IP address, install openssh-server.
sudo apt-get install openssh-server

6. Enable your PC to email you by setting up mail:
   sudo apt-get install ssmtp
   sudo apt-get install mailutils
If you have gmail, you have to change your google account setting to allow access from less secure apps, otherwise it will block your PC's login attempt. If you forget to do this, you'll get an email that will tell you that google blocked a login attempt.

7. Edit the mail configuration file
sudo gedit /etc/ssmtp/ssmtp.conf

# Example file /etc/ssmtp/ssmtp.conf ---------------------------------------------------
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=your.email.address@gmail.com

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587
AuthUser=your.email.address@gmail.com
AuthPass=your.gmail.password
UseTLS=YES
UseSTARTTLS=YES
# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=gmail.com

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
# end of file --------------------------------------------------------------------------------------------

8. Change permissions on the email config file so that a non-superuser can't view its contents. We want to hide the ssmtp.conf file from non-superusers because it contains the password for your email.
sudo chmod 600 /etc/ssmtp/ssmtp.conf

9. Install curl so we can get the public IP address the computer is using.
sudo apt-get install curl

10. Create a script. We'll show you an example in a bit. In order to run every day that the PC is powered on, copy your script to the directory: /etc/cron.daily/
Make sure the name of your script does not have an extension, or else it won't run. If you name it checkin.sh, thinking it'll work because some shell scripts end in .sh and it even runs from the command line, it still won't run automatically.

Here's an example script that will email you the public IP address and the surrounding wifi network names and signal strengths.


#!/bin/bash
#
# Get public IP address and save it in ~/.locality at the end of the file.
# Save only the IP address to the file, and error output to the bit bucket.
curl ident.me > ~/.locality 2>>/dev/null

# Get the location of the ip address that's saved in the file .locality and dump the error in the bit bucket.
curl ipinfo.io/<~/.locality >> ~/.locality 2>>/dev/null

# Check if I'm superuser. It should be, if it runs automatically.
if [[ $(id -u) -eq 0 ]]; then
echo "" >> ~/.locality
echo "Superuser" >> ~/.locality
echo "" >> ~/.locality
else
echo "" >> ~/.locality
echo "Regular user" >> ~/.locality
echo "" >> ~/.locality
fi

# Scan for wireless networks and save to the same file. Send errors to the bit bucket.
# must be superuser to run this command.
iw dev wlan0 scan >> ~/.locality 2>>/dev/null

# Email
# Subject
SUBJECT="My PC location"
# To
TO_EMAIL="your.email.address@gmail.com"
# Message
# Sending email using /bin/mail
mail -s "$SUBJECT" "$TO_EMAIL" < ~/.locality
# Delete the .locality file.
rm ~/.locality 
# end of script checkin ---------------------------------------------------------


11. Hide the contents of your script from regular users, but allow them to run it, because, you know, another email confirmation is great.
sudo chmod 711 /etc/cron.daily/checkin

Additional Information

How to find the public IP address from the command line:
xmodulo.com/how-to-find-the-public-ip-address-from-command-line.html

How to find the geographic location of the ip address
Use curl ipinfo.io/[ip address] (from xmodulo.com/geographic-location-ip-address-command-line.html)

How to scan nearby wireless networks from the command line:
xmodulo.com/manage-wifi-connection-command-line.html

How to send email from a script:
http://linuxconfig.net/manual-howto/sending-mail-from-bash-script.html

I'll bet some experts out there can improve on this. If you have any great ideas, leave a comment!