Wireless Hacks Free Open Book

Wireless Hacks

Previous Section Next Section

Hack 41 Network Monitoring with ngrep

figs/expert.giffigs/hack41.gif

See who's doing what, with a grep for your network interface.

The ngrep (http://www.packetfactory.net/Projects/ngrep) utility is an interesting packet capture tool, similar to [Hack #37] and [Hack #38]. It is unique in that it attempts to make it as easy as possible to match which captured packets to print, by using a grep-compatible format (complete with regular expressions and a bunch of GNU grep's switches). It also converts the packets to ASCII (or hex) before printing.

For example, to see the contents of all HTTP GET requests that pass through your router, try this:

# ngrep -q GET

If you're interested only in a particular host, protocol, or port (or other packet matching criteria), you can specify a bpf filter as well as a data pattern. It uses a syntax similar to tcpdump:

# ngrep -qi rob@nocat.net port 25
T 10.42.4.7:65174 -> 209.204.146.26:25 [AP]
 RCPT TO:..                         

T 209.204.146.26:25 -> 10.42.4.7:65174 [AP]
 250 2.1.5 ... Recipient ok..                

T 10.42.4.7:65174 -> 209.204.146.26:25 [AP]
 Date: Sun, 8 Sep 2002 23:55:18 -0700..Mime-Version: 1.0 (Apple Message fram
 ework v543)..Content-Type: text/plain; charset=US-ASCII; format=flowed..Sub
 ject: Greetings.....From: John Doe ..To: rob@nocat.net..Content-Transfer-En
 coding: 7bit..Message-Id: ..X-Mailer: Apple Mail v2)....What does t
 hat pgp command you mentioned do again?....Thanks,....--A Friend....

Since ngrep prints to STDOUT, you can do post-processing on the output to make a nice printing filter. If you process the output yourself, add the -l switch to make the output line buffered.

The Code

If you're interested in what people on the local wireless network are searching for online, try something like this bit of Perl:

#!/usr/bin/perl
use Socket;
$|++;

open(NG,"ngrep -d en1 -lqi '(GET|POST).*/(search|find)' |");
print "Go ogle online.\n";
my ($go,$i) = 0;
my %host = ( );

while( ) {

 if(/^T (\d+\.\d+.\d+\.\d+):\d+ -> (\d+\.\d+\.\d+\.\d+):80/) {
  $i = inet_aton($1);
  $host{$1} ||= gethostbyaddr($i, AF_INET) || $1;
  $i = inet_aton($2);
  $host{$2} ||= gethostbyaddr($i, AF_INET) || $2;
  print "$host{$1} -> $host{$2} : ";
  $go = 1;
  next;  
 }
 if(/(q|p|query|for)=(.*)?(&|HTTP)/) {
  next unless $go;
  my $q = $2;
  $q =~ s/(\+|&.*)/ /g;
  $q =~ s/%(\w+)/chr(hex($1))/ge;
  print "$q\n";
  $go = 0;
 }
 else {
  next unless $go;
  $go = 0;
  print "\n";
 }
}

Running the Hack

I call the script go-ogle. This runs an ngrep looking for any GET or POST request that includes search or find somewhere in the URL. Save the code to a file called go-ogle.pl and invoke it on the command line. The results look something like this:

# perl go-ogle.pl
Go ogle online.
caligula.nocat.net -> www.google.com : o'reilly mac os x conference
caligula.nocat.net -> s1.search.vip.scd.yahoo.com : junk mail $$$
tiberius.nocat.net -> altavista.com : babel fish 
caligula.nocat.net -> 166-140.amazon.com : Brazil 
livia.nocat.net -> 66.161.12.119 : lart

It will very lazily unescape encoded strings in the query (note the ' in the Google query, and the $$$ from Yahoo!). It will also convert IP addresses to hostnames for you (since ngrep doesn't seem to have this feature, probably so it can optimize capturing for speed). The last two results are interesting: the "Brazil" query was actually run on http://www.imdb.com/, and the last one was to http://www.dictionary.com/. Evidently IMDB is now in a partnership with Amazon, and Dictionary.com's search machine doesn't have a PTR record. It's amazing how much you can learn about the world by watching other people's packets.

Note that you must be root to run ngrep; for best results it should be run from the router at the edge of your network or from any wireless client associated with a busy AP.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Credits
    Foreword
    Preface
    Chapter 1. The Standards
    Chapter 2. Bluetooth and Mobile Data
    Chapter 3. Network Monitoring
    3.1 Hacks #20-42
    Hack 20 Find All Available Wireless Networks
    Hack 21 Network Discovery Using NetStumbler
    Hack 22 Network Detection on Mac OS X
    Hack 23 Detecting Networks Using Handheld PCs
    Hack 24 Passive Scanning with KisMAC
    Hack 25 Establishing Connectivity
    Hack 26 Quickly Poll Wireless Clients with ping
    Hack 27 Finding Radio Manufacturers by MAC Address
    Hack 28 Rendezvous Service Advertisements in Linux
    Hack 29 Advertising Arbitrary Rendezvous Services in OS X
    Hack 30 'Brought to you by' Rendezvous Ad Redirector
    Hack 31 Detecting Networks with Kismet
    Hack 32 Running Kismet on Mac OS X
    Hack 33 Link Monitoring in Linux with Wavemon
    Hack 34 Historical Link State Monitoring
    Hack 35 EtherPEG and DriftNet
    Hack 36 Estimating Network Performance
    Hack 37 Watching Traffic with tcpdump
    Hack 38 Visual Traffic Analysis with Ethereal
    Hack 39 Tracking 802.11 Frames in Ethereal
    Hack 40 Interrogating the Network with nmap
    Hack 41 Network Monitoring with ngrep
    Hack 42 Running ntop for Real-Time Network Stats
    Chapter 4. Hardware Hacks
    Chapter 5. Do-It-Yourself Antennas
    Chapter 6. Long Distance Links
    Chapter 7. Wireless Security
    Appendix A. Deep Dish Parabolic Reflector Template
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele