Automated Login For Captive Portals in Linux

Every time you connect to a private network, may it be your college, office, school, etc. the captive portal screen appears where you have to enter your credentials provided by the organization. The idea is to automate that process so that whenever we are connected to any router in the same network, it automatically gets logged in.

Note: In the following article, the steps are provided which may not work for every network, but the idea is to understand the process, and it can be tried as most of the captive portals are loosely built, so it is worth a try.

System Requirement: Any Unix based system, preferably ubuntu Linux.

Packages that need to be Installed: wget, w3m. (can be installed using apt-get)

Step 1: Study of the captive portal page. This is one of the important steps and has to be carried out keenly. Right-Click on the text area provided and select inspect (Considering the browser being used is Chrome/Firefox). Notice the “name” field in the input text area. It must be something like ‘username’ or ‘password'(as shown in the image).



 Study of the captive portal page

The HTML will have JavaScript embedded into it(Obviously!). Notice the functions getting called by it.

Step 2: Study the JavaScript Code. The functions that we saw earlier must be in the javascript code that the browser is using. That can be accessed in the same way the HTML was accessed. Go to the inspect and this time, click on the sources and you will see the JS codes for the website.

Note: Generally these codes will be in a single straight line, so hard to interpret. Hence any online beautifier can be used to format the code to make it more readable.

Step 3: Finding the link! As we very well know that every HTTP POST request is used to send data to a server. So, now in the javascript code, there must be a link or say query that is being made up and sending the POST request. So we have to find the query. The important part here is not finding the query but finding the signature that is being used along with the link.

Signature means here the number of variables required, their sequence, etc. Sometimes some variables require timestamp, so a dummy value can be supplied to it(if required, the actual timestamp can also be used). Notice in the given image, the “mode” variable is given value as 191, which means login state and the value 193 which means logout state. This can be derived by reading the code keenly.

Query that was found for login:

queryString = “mode=191&username=” + encodeURIComponent(UserValue) + “&password=” + encodeURIComponent(document.frmHTTPClientLogin.password.value) + “&a=” + (new Date).getTime() + producttype;



Query that was found for logout:

queryString = “mode=193&username=” + encodeURIComponent(document.frmHTTPClientLogin.username.value) + “&a=” + (new Date).getTime() + producttype;

Step 4: Creation of own query string with the variables. Once the query is observed, it is easy to make the own query with the same variables and filling in the appropriate data accordingly. The link that was made in this case was as follows :

'mode=191&username=admin&password=root&a=1551345153461'

An important thing to be noted here is that one might think that this string can be appended to the IP of the captive portal and then used in browser. For example,

http://192.168.1.8/loginpage.html?mode=191&username=admin&password=root&a=1551345153461

But it is important to note that this won’t work in all cases this can only work in the case where the backend technology like PHP was used where variable parsing using link is possible.

Step 5: Sending the POST request using the terminal. Once the POST request can be sent by the terminal in Linux, then it can be inserted in a bash script for automation. To send POST request here, a non-graphical browser was used made for Linux. There are many non-graphical browsers out there like lynx, w3m, etc. In this case, the w3m browser was used. It can be studied easily from the manual pages of Linux. The use of POST by w3m is well explained in its manual page (shown in the image).

w3m-post

In this case, Ajax was used to communicate with the backend and the request-response was in XML, hence the POST request was to be sent to an XML link and not a normal HTML page. This may vary network to network. The page to which the request is to be sent can be identified in the same JavaScript code in which the variables were found. Finally, the command would look something like this:

w3m -post – http://192.168.1.8:8294/login.xml<<<'mode=191&username=admin&password=root&a=1551345153461'



Step 6: Inserting it into a bash script. To make the process automated, it is necessary to make a bash script file that will run every time you connect to the network of your organization. To make a simple bash script go to terminal and type

nano autologin

Nano is a command-line editor, which will now create a file named autologin. Now paste the following script in the editor, by pressing ctrl+shift+V.

Note: To create a bash file do not enter an extension to the filename as Linux never works on extension but identifies the files internally. Linux knows itself, whether the file is an ASCII text file, doc file or a bash file.

#!/bin/bash

wget -q --tries=10 --timeout=10  http://192.168.1.8:8294
if [[ $? -eq 0 ]]; then
    w3m -post - http://192.168.1.8:8294/login.xml <<<'mode=191&username=admin&password=root&a=1551345153461'
        echo "Online!"
else
        echo "Unreachable!"
fi

Explanation:

  • The first line of the code indicates that the file is a bash script, which is to be executed using bash only and the location of it is /bin/bash.
  • wget is a Linux command, it is basically a program that is used to get content from the internet.
  • Here, in this case, wget is used to check the connectivity of the PC to the network, the IP is checked that if it is active or not. If that IP is not active then that means that the device is not connected to the private network of the organization and the script won’t be executed further.
  • The flags to wget are the number of tries to do before aborting, similarly for timeout. -q is used to turn off verbose.
  • The $? is an environment variable that is used to store return values in Linux environment. If wget returns false, $? turns 0 and the control will go to else and nothing will happen.
  • But when the IP is active, i.e. when wget will return a positive return value, it will be reflected in $? and the command w3m will be executed and the login will be done successfully.

Step 7: Making the script executable, every time it is connected to a network. This is the final part, where the script is to be made executable, every time it is connected to any network. It will internally find if the network is the private network of the organization or not and will log in accordingly. For this step, go to your /etc/network folder.

In this folder, there are 4 folders(as of the latest version of ubuntu-19.10) named: if-down.d, if-post-down.d, if-pre-up.d, if-up.d. The script just was written has to be pasted in the “if-up.d” folder. Linux internally calls all the scripts present in this folder every time it is connected to a new network.

Note: If you don’t find the 4 folders by default, create the one we need and add the entry to the /etc/network/interfaces file. To paste the script, go to the directory where the script is present, and type:

sudo cp autologin /etc/network/if-up.d

Step 8: Making the file executable and rebooting network. The ownership of the file must be changed to root. Hence type the command:

sudo chown root:root autologin

Then give the permissions to make the file executable:

sudo chmod +x autologin

Finally, restart the network manager for the new settings to take place!

/etc/init.d/network-manager restart

This should make your PC able to login to the private network automatically, every time you connect it to the network! Although now you are logged in, the pop-up window of the captive portal may always occur for logging into the network, every time you connect to a network. To get rid of it, go to Settings->Privacy and turn the Connectivity Checking off.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.