How to login cookies enabled websites using curl command line tool How to login cookies enabled websites using curl command line tool curl curl

How to login cookies enabled websites using curl command line tool


The problem identified!

The HTML form must not be simplified. Login will be succeeded when the full HTML form is used.

The form bellow:

<form action="https://uva.onlinejudge.org/index.php?option=com_comprofiler&task=login" method="post" id="mod_loginform" style="margin:0px;"><input type="text" name="username" id="mod_login_username" class="inputbox" size="10" /><input type="password" name="passwd" id="mod_login_password" class="inputbox" size="10" /></span><input type="hidden" name="op2" value="login" /><input type="hidden" name="lang" value="english" /><input type="hidden" name="force_session" value="1" /><input type="hidden" name="return" value="B:aHR0cHM6Ly91dmEub25saW5lanVkZ2Uub3JnLw==" /><input type="hidden" name="message" value="0" /><input type="hidden" name="loginfrom" value="loginform" /><input type="hidden" name="cbsecuritym3" value="cbm_6d014498_5fb7509f_73f7153baaa7d545a091ca80411ca167" /><input type="hidden" name="j9bec7c5b8b989a1feec6f95855aab890" value="1" /><input type="checkbox" name="remember" id="mod_login_remember" value="yes" /> <input type="submit" name="Submit" class="button" value="Login" /></form>

formfind.pl script can be used to find the post data field. As the login page use static post data, so the form have to update regularly.

This BASH script automatically update POST data and make a string to POST using curl.

#!/bin/bash#this is rough and made quicklycookieFile="uva.onlinejudge.org_cookie.txt"curlFunc() { curl -f -L -s --compressed $@ ;}formData="$(curlFunc "https://uva.onlinejudge.org/" | grep -B10 'value="Login"' | awk ' \    BEGIN{        printf "username=uname&passwd=pass&Submit=Login"    }     /type="hidden"/ { \        gsub(/\r/,""); \        gsub("^.*name=\"",""); \        gsub("\" value=\"","="); \        gsub("\" \/>",""); \        gsub(":","\"%\"3A"); \        {printf "\&%s",$0} \   }' \)"#POST to login formcurlFunc --cookie-jar "$cookieFile" --data "$formData" "https://uva.onlinejudge.org/index.php?option=com_comprofiler&task=login" >/dev/nullcurlFunc --cookie "$cookieFile" "https://uva.onlinejudge.org" | grep -i "logout" >/dev/null && echo "Logged in successfully" || echo "Failed to login"#------------------------------------------------------------------------

The final curl command:

curl --compressed --cookie-jar ".uva_cookie" --data \"username=uname&passwd=pass&Submit=Login&op2=login&lang=english&force_session=1&return=B"%"3AaHR0cDovL3V2YS5vbmxpbmVqdWRnZS5vcmcv&message=0&loginfrom=loginmodule&cbsecuritym3=cbm_4f91ddd4_48433724_f4574ec18b7b5d249835064c788445f4&j9bec7c5b8b989a1feec6f95855aab890=1" \"https://uva.onlinejudge.org/index.php?option=com_comprofiler&task=login"