Phone Home Anti-Robber Mac Security Script 24

Terminal

Phone Home Anti-Robber Mac Security Script

Hey

If you have a laptop or a normal Mac and you are interested in a way to find out the culprit of the person who has stolen it you Mac there are a couple of applications out there which can help you. Although you can use these, they are not as fun as making your own. This post will go through the steps needed in creating your very own thief catcher script that will take pictures of the culprit and email them back to you. It has taken me a couple of days to get this script working and I think it does the job well. Like many programs there is always more than one (better) way of doing it. I have previously written a shell script previously to log cpu load. It is located here. This script builds upon that previous post.

The idea behind this script is simple. There is a file stored on your web server. Every 5 or so minutes this script will check this file. If you have found that you laptop has been stolen you change this file. Your computer will then take pictures of your thief, along with your computer IP address and other information. It will then email this back to you so you can contact the authorities and get your Mac back. This script is by no means perfect. But it is the method I have developed. It works which is the most important thing.

Prerequisites

There are a couple of things you need before you can use this script.

  • iSightCapture. A command line tool to take pictures through your iSight.
  • Wget. I’ve used wget to download various files in the script. I think it is better than the built in version curl. If you don’t have it ready read the tutorial or download the file below (its not here because the download plug-ins screws with the css formatting).
  • Email which is not frequently used.
  • Web server. If you don’t have a web server there is another method to using this script. Although you can get some form of free hosting on the net.

If you are ready to begin, the best method is to download the script. So I can walk you through the script.

Wget For Mac (94.6 KiB, 615 hits)

Anti-Robber Script (1.9 KiB, 870 hits)

Extract the file into a location that a robber will not likely find. Put it in lots of sub folders. Then open the file in your favourite text editor. I prefer TextMate due to syntax highlighting. It makes reading the script a lot easier.

The Code

In this section I will move down the script line by line (bypassing comments). I will tell you what they mean and why they are there. As well as if you have to change anything. You do have to do a couple of extra steps to make it work. It doesn’t work right out of the box.

#!/bin/sh

This first line tells Terminal that we are running an shell script. This makes sure we are running the right interpreter.

robber=`wget http://www.example.com/file.html -O - -o /dev/null`

This is file you will change if you have found that your Mac has been stolen. The file should be stored on your server. It should contain anything but the number one (1). Type in anything you want so it isn’t a blank file. This is a variable that will be used to determine a function within the if statement.

thedate=`date`

A lot of the file names will include dates within them so you know what time the image was taken. This variable sets the date. This is so the same date number is present over all of the functions within the script. The script takes a while to execute, we don’t want it changing.

path="/A/Path/To/Hidden/Location/"

This is the file path to where you want to store your files. Make it well hidden. Make sure to include a backslash at the end and the beginning.

if [ "$robber" != "1" ]; then
echo Test Done At $thedate, Pass >> $path"Checks".txt

This is the main if statement. The first line basically means if you find anything but the number one then do the second line. The second line writes to a log file that the check has been completed. This includes the data and the word “Pass”. It will then write it to the end of the file stored in your secret location. If you have read the previous post, I developed a method in which it would right to a different file and then combine them. I did this because I couldn’t work out how to write to the ends of files. The double arrows (>>) do this for you.

exit 1
else

This exits with a 1. I don’t know why I did this, all the examples of if statements scripts I looked at included it. The else section sends us onto the bulk of our code.

The Business End

This section is where the magic happens. It will format a variety of information and send it in a file. The system information includes the external IP address, Internal IP address, Mac Address as well as your Serial Number. Please note that you need to put the full path to wget (usually /usr/local/bin/wget) for it to work. I didn’t realise this, as a result it was not included in the scipt.

extip=`wget www.whatismyip.com/automation/n09230945.asp -O - -o /dev/null`

Another variable (the number is used many times in the script. It gets your IP address from a file provided by the site. It will then send this file (because it uses wget) and prints it to nowhere. The important factor is that it stores this IP value in memory for later use.

echo 'A robber has been logged at the computer here are the details:' $thedate >> $path$thedate.txt

The start of the text file containing the details. It does a simple message thats includes the date and will store it in a new file. You don’t have to create this file the double arrows will do that for you.

echo External IP Address: $extip >> $path$thedate.txt

This will print the external IP address of the computer to the next line on the file. This assumes that the computer is connected to the Internet (more on this little aspect later).

echo Hostname: `host $extip | awk '{print $5}'` >> $path$thedate.txt

This section will get the host name of your computer by using the host command. This simply lets you know the ISP of the user using your computer, making the IP address easier to track down. The command accesses the host command with the IP taken from whatismyip. It will then print the piece of information that we need by cutting it out of the output. It will then put this piece of information at the end of the file we are creating containing out thief’s information.

echo Mac Address: `/sbin/ifconfig en0 | awk '/ether/ { gsub(":", ""); print $2 }'` >> $path$thedate.txt

This takes the Mac address of your computer and prints it to the file. The awk section is a simple command that searches the ifconfig command for the piece of information we need. It will then change the syntax slightly and print it to our file.

echo Internal Address en0: `(/sbin/ifconfig en0 | awk '/inet / {print $2 }')` >> $path$thedate.txt

This will take the internal IP address of Ethernet card en0. It will print it to your file.

echo Interal Address en1: `(/sbin/ifconfig en1 | awk '/inet / {print $2 }')` >> $path$thedate.txt

This does exactly the same as the previous command but it will get the IP address of the wi-fi card. If you have more than one network card repeat that command changing en0 and en1 as you see fit.

echo Mac Serial Number: `ioreg -l | grep "IOPlatformSerialNumber"| awk '{print $4}'` >> $path$thedate.txt

This final command will get your Macs serial number from a long list of serial numbers that are stored on your computer and print it to your file. When you get round to running the command you will end up with something that looks like this.

A robber has been logged at the computer here are the details: Wed 3 Sep 2008 12:23:14 BST
External IP Address: 123.123.123.123
Hostname: hostname.example.com
Mac Address: 123456789098
Internal Address en0: 192.123.123.123
Internal Address en1: 192.123.123.124
Mac Serial Number: "12345678909"

The next section is rather tricky. It involves taking a picture with your iSight camera. As a result you have to do a couple of extra steps due to security steps taken by Apple.

First open up your Apple Script Editor in Applications > AppleScript > Script Editor and then type the following:

do shell script "/path/to/isightcapture /hidden/path/to/folder/isightimage.jpg"

Go to File > Save As. Set the File Format to Application Bundle. Save this File. Then, in Finder, right click on this file and Show Package Contents. Open Info.plist in Content. Open TextEdit or similar and add the following under <dict>. Then save the file out.

<key>NSUIElement</key>
<string>1</string>

You have to do this because Apple has disallowed a setting somewhere so you can’t run the iSightcaputre script from Terminal or cron. You can type the command in fine but you can’t do it through an automated script. A work around this is to run the command in AppleScript. This will save our image as normal. We can then use that image later. The added key in the plist file will stop the thief from seeing the bouncing Applescript icon. The only thing you will see is the small light from your iSight briefly flashing on.

Back to our shell script we should be at this line:

open $path"isightscripts.app"

This will open up the AppleScript file we have just created.

sleep 5

To stop the script from moving on before the image has been take we let it rest for 5 seconds. If you iSight camera take a lot longer to take a picture, change this value.

mv $path"isightimage.jpg" "$path$thedate.jpg"

This will rename the isight image take with Applescript into the syntax which we are using. This syntax includes the date. Note the “isightimage.jpg” portion should match up to the name in the Applescript.

screencapture -t jpg -x "$path"desktop"$thedate.jpg"

In the UK (and probably everywhere else) it is a criminal offense to modify files and folders without your permission. Since you don’t give the thief permission, they commit the offense. As a result this line will take a picture of your desktop and add it to the list. You can then show the jury that the thief was messing with your files and folders and as a result you can claim damages and compensation. The -x option in the file name makes sure the command doesn’t make a noise, it normally would.

zip -D "$path$thedate.zip" "$path$thedate.txt" "$path$thedate.jpg" "$path"desktop"$thedate.jpg"

This final section will zip up the three files. I spent ages trying to get more than one attachment to be sent with mailx (Terminal Mail), as well as a message body. It wouldn’t work. The simpliest method was to simply zip them up and email them as one .zip attachment. This also means that the files are also stored on your computer, compared to being in variables.

uuencode "$path$thedate.zip" "$path$thedate.zip" | mailx -s "Robber Logged on $thedate at $extip" email@example.com

This final command encodes the zip file into a binary file that can be sent along the Internet. I’m not 100% sure why you should do this, but all the posts on the Internet mention this method. In this line you need to change the email address to something that you would not normally use and the login passwords are not stored on your computer. The email that will actually be send will contain a subject which includes the time and IP address and then a blank message body with an attachment. For some reason mailx does not allow you to send an attachment and a message body. I have checked this and gmail accepts the email. The email will not come from a normal address, an amalgamation of your computers name and IP address. You will understand what I mean when you test the script out.

echo Test Done At $thedate, Fail >> $path"Checks".txt
exit 0
fi

The final lines simply log the information to a log file and exits. It also includes the end to the the if statement.

Hopefully you have understood everything in the script and modified the script to suit your computer and location. The final steps are to add it to cron and make it executable.

Final Steps

The final steps are simple. First open up Terminal and use the cd command to change the directory to where you script is located. In Terminal type the following:

chmod +x scriptname.sh

This will allow the script to be executed in Terminal. For some reason the execute command doesn’t transfer when I zip it up and put it on my server.

The next step is to check to make sure it works. Very important. Type the following:

/path/to/script/scriptname.sh

This script assumes that you have everything in place, such as a web file. If the file on your server is not 1 then your log file should say “Passed”. Check it again by re-running the script with the server file containing the number 1. It should zip up all of the information and send it to your email. If all works the log file should say “Fail” and you should receive an email. If it doesn’t somewhere along the line something has gotten mixed up. Check Terminal to see what the error is and to see if you can fix it.

The very final step is to add it to cron. In terminal once again type:

crontab -e

Press “a” to go into insert mode. Then type the following:

*/5 * * * * /path/to/script/scriptname.sh

Then press escape and type the following without quotes: “wq!”. This will set up your crontab. Every 5 minutes it will check your server to see if anything has changed and run the script. As asked by whatismyip, you should not run the script more frequently than that, so you don’t pound their server.  This is so you don’t put to much load on there server asking for your IP address.

If all goes to plan you should be informed by your email address that the thief has been located, it should also send the images along. This script does rely on a couple of things. It is by no means perfect. For example it needs the Internet to work. If you don’t have the Internet then the script will not work. As well as this it also needs a server to check. In tomorrow’s post I will talk about how you can run commands by receiving an email. This means you don’t need to have a webserver.

Update: You can read up on how to control this script by email by reading How To Control Your Mac By Email.

If you want you can improve the script further. For example you could include commands to change your background, letting the thief know that you have their information and the police are on the way. You could change you login password so they can’t login, delete browser and keychain passwords to increase your security. One final note is you could change the scripts file location to temporary system folders. Theses are usually more secure. If you have any questions, comments or improvements please leave a comment. I want to hear what you think.

If you want to take your skills with Terminal a bit further I recommend you check out the Terminal Category on this site. If you fancy reading a book there is a couple on Amazon that I regularly see mentioned and recommend, O’reilly Unix Geeks and Unix Under the Hood both are designed for Mac OS X and take Terminal further.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

24 Responses to “Phone Home Anti-Robber Mac Security Script”

  1. 1

    You forgot about iAlertU, which when activated sounds an alarm (can only be disabled by the remote), also using the webcam, it’ll send images to a email of your choice.
    A bonus is that it’s free! :)

    Comment By Dave on September 4th, at 9:25 am

  2. 2

    Yeah, but as soon as you hit restart it stops, anyway you may need more than one image of the culprit.

    Comment By admin on September 4th, at 10:33 am

  3. 3

    What happens if your mac is stolen, then sold, then the buyer buys Leopard OSX and formats and deletes the whole thing and reinstall the OS???

    Comment By Vulgarism on September 4th, at 7:33 pm

  4. 4

    Well then you are stuck. But most people will connect it up to the internet at least once, all you need is one email to be sent to you.

    Comment By admin on September 4th, at 7:34 pm

  5. 5

    Kudos on the site. It’s very impressive.

    I have one problem with this tip though. I’ve installed everything, configured the scripts to fit my needs and tested them manually (ran robbed.sh). All good. But when I add the script to the crontab it says it can’t find wget. I assume it’s because it’s run by another user (who runs crontab?) and wget is only installed on my user profile… How do I change this?

    Comment By Kresten H. Jacobsen on September 4th, at 11:34 pm

  6. 6

    I’ve just encountered this problem and I didn’t realize my mistake. You need to put the exact path to wget. For example:

    /usr/local/bin/wget http://example.com/file ..etc

    It should then work. You can always see if there is any problems by, in Terminal, typing mailx. Then a dot (.), enter, and cycling through the messages to see what the problem is.

    I’ll up date my post. Thanks for pointing that out.

    Comment By admin on September 4th, at 11:47 pm

  7. 7

    Yeah, I used mailx to identify the problem too. Didn’t get the answer right though.

    Thanks for the ultra fast response!

    Comment By Kresten H. Jacobsen on September 5th, at 12:21 am

  8. 8

    The problem persists. Just with different commands. ioreg, screencapture… Testing if there is anything else. If I post too many comments you can jus delete them afterwards.

    Comment By Kresten H. Jacobsen on September 5th, at 12:33 am

  9. 9

    Just to finish things off. ioreg and screencapture can be found at
    /usr/sbin/
    I don’t get any errors now, but I only get a (totally) black screenshot. Wierd.

    Comment By Kresten H. Jacobsen on September 5th, at 12:57 am

  10. 10

    That is weird. From the sounds of it, it is probably due to something wrong with your computers permissions. I hope you get it sorted out.

    Comment By admin on September 5th, at 11:29 am

  11. 11

    Great,
    something like this is provided by Undercover but it’s not free.
    I’ll check this scripts.
    It would be good, thath script could take pictures in 10minutes interval.

    Comment By darek on September 5th, at 1:02 pm

  12. 12

    You can. In the cron script simply change */5 to */10 and it will work every ten minutes.

    Comment By admin on September 5th, at 1:04 pm

  13. 13

    I’ve done a very similar script (mine’s perl) for my Macbook. I always thought about generalizing it for anyone to use and releasing it for free, but you beat me to it. ;)

    Like yours, my script checks in with my external web server to see if it’s stolen. If so, it takes a screenshot and isight grab, and uploads them to my server. In addition, mine will create an ssh connection to that same server (I have shell access there) with a tunnel back into my Macbook. That way I can login to the Macbook itself, regardless of where it is, or whether it’s behind a typical home router.

    A couple of minor notes:

    - OS X comes with “curl” installed out of the box. curl works very much like wget, so it may be easier to use that in the script rather than downloading wget separately.

    - As of the OS X 10.5.4 update, I could no longer run screencapture directly from the script, for the same reason that isightgrab stopped working in one of the Tiger updates: they made it so you can’t run it from ssh or cron. The same AppleScript trick works for both isightgrab and screencapture.

    Comment By Mike H on September 8th, at 1:19 am

  14. 14

    Thanks for your comment Mike.

    I like the SSH idea, I didn’t think of something like that. Would be quite useful.

    Curl. I though about using curl, but I prefer using wget. When I tried it with curl it didn’t have the results I wanted.

    Screencapture. That functions works perfectly well for me. I received no problems.

    Comment By admin on September 8th, at 8:22 am

  15. 15

    [...] using the bash Terminal, you may only want to select certain parts of a commands output. In my previous tutorial I used this command extensively. For example cutting certain pieces of information out of the [...]

    Comment By Print Selected Info From Terminal Commands | Mac Tricks And Tips on September 15th, at 3:06 pm

  16. 16

    Hey, AWESOME post. Thanks so much. I just have a question. The first time I try to run the script terminal comes back with :

    … line 37: … Nov 2 13:42:49 HKT 2008.txt: No such file or directory

    about 7 times, do you have an explanation or a fix for this?

    Thanks much.

    Comment By Cam on November 2nd, at 6:53 am

  17. 17

    You will need to make sure you have the correct path. Make sure you encompass any spaces with quote marks or don’t have a file path with any spaces.

    Comment By admin on November 2nd, at 12:45 pm

  18. 18

    Okay, I have removed all of the instances of the variable $thedate, but now it keeps telling me

    /Users/…/Test/robber.sh: line 64: unexpected EOF while looking for matching `”‘
    /Users/…/Test/robber.sh: line 68: syntax error: unexpected end of file

    this is what my line 64 looks like

    uuencode “$pathtest.zip” “$pathtest.zip” | mailx -s “Robber Logged on $thedate at $extip” cam022408@gmail.com

    Comment By Cam on November 2nd, at 1:40 pm

  19. 19

    Sorry! That should be line 62, not 64.

    Comment By Cam on November 2nd, at 2:10 pm

  20. 20

    @Cam, that is really strange. Email me so I can have a closer look at your script. Please send it as a .txt so it doesn’t get caught in any of my filters.

    Comment By admin on November 3rd, at 8:14 pm

  21. 21

    Hi, I tried to follow all the instructions, and everything went smoothly, screenshot and isght cap worked perfectly, the ip was logged as well as serial number. But when I receive the e-mail (using a gmail powered server) it does not come with an attachment. I think it uses the zip as the text and prints it. The result is not usable.

    Do you know a way I can correct that?

    And congratulations for a very good tip.

    Comment By moidsch on March 26th, at 10:54 pm

  22. 22

    I’ve adjusted the script to not check a webserver on a constant basis. Instead, I’ve created a dummy non-passworded user account that isn’t used normally. How would i set cron up to run the script regularly only when this other user is logged in?

    Comment By Alan Wong on August 21st, at 4:22 am

  23. 23

    One other thing – is it possible to modify the above to use sendmail instead of mailx? The attachment being sent out to the particular email service (yahoo) is garbled, starting with “Begin 644″

    Comment By Alan on August 21st, at 12:13 pm

  24. 24

    Looks like iSightCapture is broken on Snow Leopard. I’m not aware of any alternatives.

    Comment By uxp on November 19th, at 4:21 am

Leave a Reply

You can use: <a href> <h1 - h6> <acronym> <code> <em> <strike> <strong> <i> <b>