A Simple Solution to Dynamic IP Tracking

by Gruggni

After reading TRM's article: "Using Perl to Defeat Provider Restrictions," I started thinking of a simple method for tracking the ISP-assigned dynamic IP address with a few lines of script and without using email.

Like TRM, I use a home network with a personal web server.  My goal was to create a simple way of keeping track of a dynamic IP address while away from home.

I didn't want to reinvent the wheel if I didn't have to.  I wanted a way to send the IP address, catch it, and record it.  I like to keep methods simple so others can duplicate them.

The simple solution I use for getting the IP past ISP restrictions is Lynx and a few lines of PHP to catch and record it.

Lynx, PHP, and Apache come standard with most versions of Linux.  Some configuration may be required.  This method allows Linux users to use the tools that are already on the system with a little tweaking.

My home setup consists of a router, PC with Linux, and a laptop with Windows 98.  The Linux box runs Apache 1.3.28 web server and PHP 4.3.3.  The router uses NAT and forwards port 80 requests to the web server.  I also use iptables to control access to the web server.

Why, How?  Lynx?

After I got my DSL line I set up a web server on my Linux box.

I didn't install X Windows.  I wanted a remote personal web server that I could use while at work.

After configuring Apache and creating an index page, I wanted to view the index page without turning on my laptop.

Since I didn't install X, how do I browse the server?

I love ideas born through laziness.  Aha, Lynx will work, lynx localhost and all looked good.  I checked the Apache logs, created a short script to send email to a free email account, scheduled it as a cron job, and went to sleep.  Unfortunately any email I sent out wasn't being received in a timely fashion.  Some days it took hours for the email to get through.

A few months later the Spring 2004 issue of 2600 arrived.  I came across TRM's article and began pondering a different solution without using CGI/Perl.

I don't use a cgi-bin, so I always removed it.  That night my subconscious put it together.  The next day the idea of using Lynx and server logs popped into my head.  Later that day I had the IP catcher working.  The IP addressed was received on time and the log directory was secured using .htaccess authentication.

Why PHP?

The idea for IP catching was born a few weeks before the spring issue came out.

I was studying how Apache's access log recorded various hits because my web server was receiving all kinds of hits.  I received a Code Red hit and several unsuccessful buffer overflow attacks.  My access log became hard to read so I wanted to isolate actual page visits and create a log viewable via browser.  A few lines of PHP in the main index page made this easy to do.

Lynx Options

After I read the man pages on Lynx, I found two options that would allow me to automate Lynx.

This happened to be the first time I ever read the man pages on Lynx.  "You learn something new every day."

  • -cmd_log=logfile  Creates a keystroke log.
  • -cmd_script=logfile  Loads the keystroke log.

Usage: lynx remotesite.net -cmd_log=logfile

Now that you accessed the site, type q to quit and y to acknowledge.  The keystrokes are logged.  Edit the log to see how it works.

You can use Lynx to create more complex keystroke scripts, i.e., download the latest version of Nmap from the insecure website.

Now test the keystroke log: lynx remotesite.net -cmd_script=logfile

Now that your IP sender is working, time to check the server logs.

If you can view the server logs of the remote site, you don't need a catcher.  The web server logs do the catching.

Just search the logs based on your scheduling and you will have your IP address.  If you can't check the logs then you need to make a catcher.  The main benefit of the IP catcher is a clean log of IP addresses for your home server.  You can study it to learn how often your ISP changes your IP.

PHP Script

ipcatcher.php:

<?php
# grab the ip
$ip = $_SERVER['REMOTE_ADDR'];
# timestamp: the r options gives you more info with less typing.
$date = date("r");
# format string data: 0.0.0.0 # date
$outp = $ip." # ".$date."\n";
# open file for appending
$fp = fopen("catches.log", "a");
# write to file
fwrite($fp, $outp);
# close file
fclose($fp);
# visual confirmation for testing
echo $outp;
?>

The above script will log page hits and page refreshing.

I recommend using the IP catcher just for catching; keep it away from high traffic hits.  Create another page for displaying the log file.  Keep the catcher hidden from regular web traffic.

If your remote website allows you to use directory authentication (i.e., .htaccess) use it to protect the directory that contains your log file and display page.  The IP log file will continue to grow so keep tabs on it.

Linux Server Setup

Now we make a little shell script so we schedule cron to run it.

Example script: sendip.sh

#!/usr/bash
lynx remotesite.net/ipcatcher.php -cmd_script=keystroke.log

# Don't forget to make it executable!
# chmod +x sendip.sh

Put the file someplace where cron can find it.  For this example I will use: /usr/bin/sendip.sh

Sample Cron Job

Do the following under root:

#  Opens the crontab file for editing
$ crontab -e

Add the following lines:

# Run daily at 7 am
0 7 * * * /usr/bin/sendip.sh 1>/dev/null

# Run daily at 9 am
0 9 * * * /usr/bin/sendip.sh 1>/dev/null

or

# Run job hourly 30 minutes after the hour
30 * * * * /usr/bin/sendip.sh 1>/dev/null

Add lines for the times you want.

Experiment until you find a timing you like.  I schedule a cron job eight times a day.  I have cron send me the IP while I'm at work which is every hour for eight hours.

If you want to study how often your ISP changes the IP, schedule it hourly 30 minutes after the hour.

That one line is all you need.  Any error messages will go to /dev/null.

Recommended Reference

Luke Welling and Laura Thomson wrote this awesome book called PHP and MySQL Web Development.  This book is the reason why I converted from Perl to PHP.

O'Reilly's Linux Server Hacks by Rob Flickenger.  Great iptables example for firewalling your server.  Just a good book.

Code: ipcatcher.php

Return to $2600 Index