Skip to content

Instantly share code, notes, and snippets.

@peteroid
Created January 26, 2017 20:23
Show Gist options
  • Save peteroid/a65a5229d031dc198c67545bd7f9cded to your computer and use it in GitHub Desktop.
Save peteroid/a65a5229d031dc198c67545bd7f9cded to your computer and use it in GitHub Desktop.
CLI for HKU auth
#!/bin/bash
# config and clean up the script
FILE_PREFIX="login.res"
# rm ./$FILE_PREFIX*
# abort the script if any error occur
set -e
echo "Welcome to HKU Auth CLI v0.1"
echo
echo "Enter your username"
read LOGIN_USERNAME
echo "Enter your password"
read -s LOGIN_PASSWORD
# LOGIN_USERNAME=$1
# LOGIN_PASSWORD=$2
# // from hku log in website
# function getTimeStamp() {
# time_object = new Date();
# time = ""+time_object.getFullYear()+time_object.getMonth()+
# time_object.getDate()+time_object.getHours()+time_object.getMinutes()+
# time_object.getSeconds();
# document.form.keyid.value=time;
# }
LOGIN_KEY_ID_YEAR=`date +%Y | bc`
LOGIN_KEY_ID_MONTH=`echo "\`date +%m\` - 1" | bc`
LOGIN_KEY_ID_DAY=`date +%d | bc`
LOGIN_KEY_ID_HOUR=`date +%H | bc`
LOGIN_KEY_ID_MINUTE=`date +%M | bc`
LOGIN_KEY_ID_SECOND=`date +%S | bc`
LOGIN_KEY_ID="$LOGIN_KEY_ID_YEAR$LOGIN_KEY_ID_MONTH$LOGIN_KEY_ID_DAY$LOGIN_KEY_ID_HOUR$LOGIN_KEY_ID_MINUTE$LOGIN_KEY_ID_SECOND"
LOGIN_X=`echo "$RANDOM % 50" | bc`
LOGIN_Y=`echo "$RANDOM % 50" | bc`
echo "
This login will use these generated params:
LOGIN_KEY_ID=$LOGIN_KEY_ID
LOGIN_X=$LOGIN_X
LOGIN_Y=$LOGIN_Y
"
curl 'https://hkuportal.hku.hk/cas/servlet/edu.yale.its.tp.cas.servlet.Login' \
-H 'Origin: https://hkuportal.hku.hk' \
-H 'Accept-Encoding: gzip, deflate, br' \
-H 'Accept-Language: en-US,en;q=0.8' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'Mozilla/5.0 (Windows NT 6.2; rv:20.0) Gecko/20121202 Firefox/20.0' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' \
-H 'Cache-Control: max-age=0' \
-H 'Referer: https://hkuportal.hku.hk/cas/servlet/edu.yale.its.tp.cas.servlet.Login?service=http://booking.its.hku.hk/lebook/book/Web/index.php?redirect|||%2Flebook%2Fbook%2FWeb%2Fschedule.php%3F' \
--data 'keyid='"$LOGIN_KEY_ID"'&service=http%3A%2F%2Fbooking.its.hku.hk%2Flebook%2Fbook%2FWeb%2Findex.php%3Fredirect%7C%7C%7C%252Flebook%252Fbook%252FWeb%252Fschedule.php%253F&username='"$LOGIN_USERNAME"'&password='"$LOGIN_PASSWORD"'&x='"$LOGIN_X"'&y='"$LOGIN_Y"'' \
-D "$FILE_PREFIX.header" --compressed -s > "$FILE_PREFIX.out"
LOGIN_SUCCESS_URL=`cat "$FILE_PREFIX.out" | grep "<a href" | sed 's/.*href="\(.*\)">.*/\1/' | sed 's/\|\|\|/=/'`
if grep -sq "Login successful." "$FILE_PREFIX.out" ; then
echo "
Logged in.
Got [$LOGIN_SUCCESS_URL]
"
else
echo "
Cannot login. Please check your credential.
"
exit
fi
COOKIE_SECRET=""
COOKIES_TO_SET=`cat "$FILE_PREFIX.header" | grep "Set-Cookie:" | sed 's/Set-Cookie: \([^;]*\); .*/\1/'`
IFS="
"
for cookie in $COOKIES_TO_SET; do
COOKIE_SECRET=`echo "$cookie; $COOKIE_SECRET"`
done
echo "
Computed the secret [/* \$COOKIE_SECRET */]
"
curl "$LOGIN_SUCCESS_URL" \
-H 'Accept-Encoding: gzip, deflate, br' \
-H 'Accept-Language: en-US,en;q=0.8' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'Mozilla/5.0 (Windows NT 6.2; rv:20.0) Gecko/20121202 Firefox/20.0' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' \
-H 'Cache-Control: max-age=0' \
-H 'Cookie: '"$COOKIE_SECRET"'' \
-D "$FILE_PREFIX.02.header" --compressed -s > "$FILE_PREFIX.02.out"
PHPSESSID=`cat "$FILE_PREFIX.02.header" | grep "Set-Cookie: PHPSESSID" | sed 's/.*PHPSESSID=\(.*\);.*/\1/'`
COOKIE_SECRET="PHPSESSID=$PHPSESSID; $COOKIE_SECRET"
echo "
Authenicating with PHPSESSID=$PHPSESSID
"
curl 'http://booking.its.hku.hk/lebook/book/Web/schedule.php' \
-H 'Pragma: no-cache' -H 'Accept-Encoding: gzip, deflate, sdch' \
-H 'Accept-Language: en-US,en;q=0.8' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'Mozilla/5.0 (Windows NT 6.2; rv:20.0) Gecko/20121202 Firefox/20.0' \
-H 'Cache-Control: no-cache' \
-H 'Cookie: PHPSESSID='"$PHPSESSID"'' \
-D "$FILE_PREFIX.03.header" --compressed -s > "$FILE_PREFIX.03.out"
echo "
The page is fetched with $LOGIN_USERNAME logged in.
Generating results from $FILE_PREFIX.03.out...
"
cat "$FILE_PREFIX.03.out" | \
grep "\"reservable" | \
sed 's/.*ref=\"\(.*\)\" .*/\1/' | \
tr '\n' ','
echo "
done.
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment