Skip to content

Instantly share code, notes, and snippets.

@ajfriesen
Created May 15, 2020 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajfriesen/784ec148e26d2d20f73888d0fe054e80 to your computer and use it in GitHub Desktop.
Save ajfriesen/784ec148e26d2d20f73888d0fe054e80 to your computer and use it in GitHub Desktop.
[TCP dump all traffic from FritzBix to mount] Dumps all traffic every hour for an hour to a mountpoint #netcologne #fritzbox
# /etc/cron.d/fritzdump
5 * * * * "/home/pi/mount/fritzdump.sh "${username}" "${password}" 3600" >/dev/null 2>&1
# /home/pi/mount/fritzdump.sh
#!/bin/bash
# This is the address of the router
FRITZIP=http://fritz.box
# This is the WAN interface
#IFACE="2-0"
# Lan Interface
IFACE="1-lan"
# If you use password-only authentication use 'dslf-config' as username.
FRITZUSER=$1
FRITZPWD=$2
TIME=$3
SIDFILE="/tmp/fritz.sid"
if [ -z "$FRITZPWD" ] || [ -z "$FRITZUSER" ] ; then echo "Username/Password empty. Usage: $0 <username> <password>" ; exit 1; fi
echo "Trying to login into $FRITZIP as user $FRITZUSER"
if [ ! -f $SIDFILE ]; then
touch $SIDFILE
fi
SID=$(cat $SIDFILE)
# Request challenge token from Fritz!Box
CHALLENGE=$(curl -k -s $FRITZIP/login_sid.lua | grep -o "<Challenge>[a-z0-9]\{8\}" | cut -d'>' -f 2)
# Very proprieatry way of AVM: Create a authentication token by hashing challenge token with password
HASH=$(perl -MPOSIX -e '
use Digest::MD5 "md5_hex";
my $ch_Pw = "$ARGV[0]-$ARGV[1]";
$ch_Pw =~ s/(.)/$1 . chr(0)/eg;
my $md5 = lc(md5_hex($ch_Pw));
print $md5;
' -- "$CHALLENGE" "$FRITZPWD")
curl -k -s "$FRITZIP/login_sid.lua" -d "response=$CHALLENGE-$HASH" -d 'username='${FRITZUSER} | grep -o "<SID>[a-z0-9]\{16\}" | cut -d'>' -f 2 > $SIDFILE
SID=$(cat $SIDFILE)
# Check for successfull authentification
if [[ $SID =~ ^0+$ ]] ; then echo "Login failed. Did you create & use explicit Fritz!Box users?" ; exit 1 ; fi
echo "Capturing traffic on Fritz!Box interface $IFACE ..." 1>&2
function collect_data {
DATE_TIME=$(date +%Y_%m_%d-%H_%M_%S)
echo ${DATE_TIME}
wget --no-check-certificate -qO- $FRITZIP/cgi-bin/capture_notimeout?ifaceorminor=$IFACE\&snaplen=\&capture=Start\&sid=$SID >> /home/pi/mount/dumps/${DATE_TIME} &
WGETPID=$!
echo ${WGETPID}
sleep ${TIME}
kill ${WGETPID}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment