2

I have been trying to make a bash script that will login to the web login form using curl and perform some actions later on using cookies saved. I tried previous methods of stackoverflow answers but nothing works in my case, help is much appreciated.

WebForm of Login:

<form id="session_new" novalidate="novalidate" class="formtastic user" action="/admin/login" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="authenticity_token" value="sometoken" />
  <fieldset class="inputs">
    <ol>
      <li class="email input required autofocus stringish" id="user_email_input"><label for="user_email" class="label">Email<abbr title="required">*</abbr></label><input id="user_email" autofocus="autofocus" type="email" value="" name="user[email]" />

      </li>
      <li class="password input required stringish" id="user_password_input"><label for="user_password" class="label">Password<abbr title="required">*</abbr></label><input maxlength="128" id="user_password" type="password" name="user[password]" />

      </li>
      <li class="boolean input optional" id="user_remember_me_input"><input type="hidden" name="user[remember_me]" value="0" /><label for="user_remember_me" class=""><input type="checkbox" name="user[remember_me]" id="soc_user_remember_me" value="1" />Remember me</label>

      </li>
    </ol>
  </fieldset>
  <fieldset class="actions">
    <ol>
      <li class="action input_action " id="user_submit_action"><input type="submit" name="commit" value="Login" data-disable-with="Login" />

Here is the curl request i tried using ,

curl -d "user[email]=emailhere&user[password]=passwordhere&user[remember_me]=0&commit=Login" http://urlhere

But it didn't work,so i tired wget method,

wget --save-cookies cookies.txt --keep-session-cookies --post-data="user[email]=emailhere&user[password]=passwordhere&user[remember_me]=0&commit=Login" "http://urlhere

It didn't work either.

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
Billiards
  • 21
  • 1

1 Answers1

1

You need to url encode the data you put in -d or use --data-urlencode which will url encode the value. For instance :

email=test
password=test

curl --data-urlencode "utf8=&#x2713;" \
     --data-urlencode "authenticity_token=sometoken" \
     --data-urlencode "user%5Bemail%5D=$email" \
     --data-urlencode "user%5Bpassword%5D=$password" \
     --data-urlencode "user%5Bremember_me%5D=0" \
     --data-urlencode "commit=Login" http://your.domain/admin/login

Also my guess is that authenticity_token is a csrf token so you will have to scrap that value first before sending your request. The following uses pup to get that authenticity_token value and use that value in our request :

page_url=http://your.domain/myPage
login_url=http://your.domain/admin/login

email=test
password=test

rm -f cookies.txt

csrf_token=$(curl -s "$page_url" -c cookies.txt | pup 'input[name="authenticity_token"] attr{value}')

curl --data-urlencode "utf8=&#x2713;" \
     --data-urlencode "authenticity_token=$csrf_token" \
     --data-urlencode "user%5Bemail%5D=$email" \
     --data-urlencode "user%5Bpassword%5D=$password" \
     --data-urlencode "user%5Bremember_me%5D=0" \
     --data-urlencode "commit=Login" "$login_url" -b cookies.txt -c cookies.txt

In the script above I'm also saving the cookies in a file called cookies.txt, in subsequent request, use -b cookies.txt to keep the session

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159