Locate IP addresses captured with tcpdump (GeoIP)
Extract IP addresses from a tcpdump capture and locate them with the MaxMind GeoIP databases.
This page shows how to extract the remote IP addresses from a tcpdump capture, then locate them with the MaxMind GeoIP databases, using a small shell script.
Prerequisites
- A MaxMind account with a license key (created on the MaxMind website).
- The
GeoIPandgeoipupdatepackages.
Installation and configuration
sudo yum makecache
sudo yum install GeoIP geoipupdate
Edit /etc/GeoIP.conf (as root):
# `AccountID` is from your MaxMind account.
AccountID <your_account_id>
# `LicenseKey` is from your MaxMind account
LicenseKey <your_license_key>
# `EditionIDs` is from your MaxMind account.
EditionIDs GeoLite2-ASN GeoLite2-City GeoLite2-Country
Update the databases:
/bin/geoipupdate
Extract IP addresses with tcpdump
List the unique IP addresses seen on the interface:
# unique
sudo tcpdump -i ens160 -nn -q ip -l | awk '{ ip = gensub(/([0-9]+.[0-9]+.[0-9]+.[0-9]+)(.*)/,"\\1","g",$3); if(!d[ip]) { print ip; d[ip]=1; fflush(stdout) } }' > ip.test
List every occurrence (with duplicates):
# all
sudo tcpdump -i ens160 -nn -q ip -l | awk '{ ip = gensub(/([0-9]+.[0-9]+.[0-9]+.[0-9]+)(.*)/,"\\1","g",$3); { print ip; d[ip]=1; fflush(stdout) } }' > ip.test
Location script
The getLocation.sh script skips private addresses and prints the location of public ones:
#!/bin/bash
# Fonction pour vérifier si l'adresse IP est privée
is_private_ip() {
local ip=$1
if [[ $ip =~ ^10\. ]] || [[ $ip =~ ^172\.(1[6-9]|2[0-9]|3[0-1])\. ]] || [[ $ip =~ ^192\.168\. ]]; then
return 0 # IP privée
fi
return 1 # IP publique
}
# Assurez-vous que le fichier est passé en argument
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <fichier_ip.txt>"
exit 1
fi
FILE=$1
# Lire chaque ligne du fichier
while IFS= read -r ip
do
if is_private_ip $ip; then
# Ignorer les IP privées
continue
else
# Concaténer l'IP et sa localisation
location=$(geoiplookup $ip | awk -F ': ' '{print $2}')
echo "$ip - $location"
fi
done < "$FILE"
Usage:
./getLocation.sh ~/ip.test > ipWithLocation.txt
./getLocation.sh ip.test | sort | uniq -c | sort -n
sort ipWithLocation.txt | uniq -c
Notes
geoiplookupreads GeoIP databases in the legacy.datformat. Check that the editions downloaded bygeoipupdateare compatible with the installed version of the tool.- The private address filter only covers
10.0.0.0/8,172.16.0.0/12and192.168.0.0/16.