Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use libcurl to login to a secure website and get at the html behind the login

I was wondering if you could help me work through accessing the html behind a login page using C and libcurl.

Specific Example:

The website I'm trying to access is https://onlineservices.ubs.com/olsauth/ex/pbl/ubso/dl

Is it possible to do something like this?

The problem is that we have a lot of clients each of which has a separate login. We need to get data from each of their accounts every day. It would be really slick if we could write something in C to do this and save all the pertinent data into a file. (like the values of the accounts and positions which I can parse from the html)

What do you guys think? Is this possible and could you help point me in the right direction with some examples, etc...?

like image 253
Tyler Brock Avatar asked Aug 23 '10 17:08

Tyler Brock


People also ask

Can I use curl to login to a website?

For example, if a website has protected content curl allows you to pass authentication credentials. To do so use the following syntax: curl --user "USERNAME:PASSWORD" https://www.domain.com . “USERNAME” must be replaced with your actual username in quotes.

What can you do with Libcurl?

libcurl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, HTTP/3, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, Kerberos), file transfer resume, http proxy tunneling and more!


1 Answers

After a cursory glance at the login page, it is possible to do this with libcurl, by posting the username/password combo to their authenticating page, and assuming they use cookies to represent a login session. The first step is to make sure that you've got the following options set:

  • CURLOPT_FOLLOWLOCATION - The server may redirect after authenticating, this is quite common.
  • CURLOPT_POST - This tells libcurl to switch into post mode.
  • CURLOPT_POSTFIELDS - This tells libcurl the values to set for the post fields. Set this option to "userId=<insert username>&password=<insert password>". That value is derived from the source code for that page.
  • CURLOPT_USERAGENT - Set a simple user-agent, so that the web server won't throw it out (some strict ones will do this).

Then, once the post is complete, the libcurl instance should contain some sort of authorisation cookie used by the site to identify a logged-in user. Curl should keep track of cookies within a given instance. There are plenty of options for Curl if you want to tweak how cookies behave.

Make sure that once you are 'logged-in' that the same libcurl instance is used for each request under that account, otherwise it will see you as logged out.

As for parsing the resulting pages go, there are tonnes of HTML parsers for c - just google. The only thing I will say is do not try to write an HTML parser yourself. It is notoriously tricky, because a lot of sites don't produce good (or even working) HTML.

like image 69
Alistair Evans Avatar answered Oct 21 '22 21:10

Alistair Evans