Last active
November 5, 2020 16:08
-
-
Save pallavagarwal07/5f5beef5d91fadfcca9a6d02098318f2 to your computer and use it in GitHub Desktop.
IITK firewall authentication script that uses nothing except for curl. It is possible to specify an interface manually (for compatibility with some unix systems). In case the user is already logged in, it can even force a logout to get the authentication page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -eu | |
# Copyright 2017 Pallav Agarwal | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
firewall_url="https://172.31.1.251:1003" | |
ironport1_ip="172.22.1.1:3128" | |
ironport2_ip="172.22.1.2:3128" | |
example_url="http://93.184.216.34" | |
# To reduce dependencies, replace this line below with the interface | |
# name like 'eth0' or 'enp9s0' | |
interface="$(route | grep '^default' | grep -o '[^ ]*$')" | |
printf "Username: "; read username | |
printf "Password: "; read password | |
url_encode() { | |
local word="${1}"; shift | |
local dataLength="${#word}" | |
local index | |
for ((index = 0; index < dataLength; index++)); do | |
local char="${word:index:1}" | |
case "${char}" in | |
[a-zA-Z0-9.~_-]) | |
printf "${char}" | |
;; | |
*) | |
printf "%%%02X" "'${char}" | |
;; | |
esac | |
done | |
} | |
ironport() { | |
for i in 1 2; do | |
local name="ironport${i}_ip" | |
eval local url='${'$name'}' | |
local output="$(curl --silent --include \ | |
--proxy "http://${username}:${password}@${url}/" \ | |
"${example_url}" | head -1 | grep -o "407" )" | |
if [ "${output}" == "407" ]; then | |
echo "Unable to login to ironport${num}" | |
fi | |
done | |
} | |
get_login_page() { | |
local output="$(curl --silent \ | |
--interface "${interface}" -k "${firewall_url}/logout?")" | |
echo "${output}" | grep -oE '"htt[^"]*"' | grep -oE 'http[^"]*' | |
} | |
get_magic() { | |
local login_page="${1}"; shift | |
local output="$(curl --silent \ | |
--interface "${interface}" -k "${login_page}")" | |
local quoted="$(echo "${output}" | grep -oE \ | |
'"magic" value="[^"]*"' | grep -oE '"[^"]*"$')" | |
echo "${quoted}" | grep -oE '[^"]*' | |
} | |
do_login() { | |
local magic="${1}"; shift | |
local output="$(curl -k --interface "${interface}" -X POST --silent \ | |
-d "username=${username}" \ | |
-d "password=${password}" \ | |
-d "magic=${magic}" \ | |
-d "submit=Continue" \ | |
"${firewall_url}/" )" | |
echo "${output}" | grep -oE 'http[^"]*logout[^"]*' | |
echo "${output}" | grep -oE 'http[^"]*keepalive[^"]*' | |
} | |
log_out() { | |
local url="${1}"; shift | |
curl -k --interface "${interface}" --silent "${url}" | |
} | |
keep_alive() { | |
local url="${1}"; shift | |
echo "Keepalive url is" "${url}" | |
while :; do | |
local output="$(curl -k -silent --interface "${interface}" "${url}")" | |
ironport # Run ironport login too | |
if echo "${output}" | grep -q "leave it open"; then | |
echo "Keepalive successful" | |
sleep 10 | |
else | |
echo "Keepalive unsuccessful; Trying to get login screen" | |
break | |
fi | |
done | |
} | |
main() { | |
local output="${1}"; shift | |
logout_url=$(echo "$output" | head -1) | |
keepalive_url=$(echo "$output" | tail -1) | |
echo "${logout_url}" | |
echo "${keepalive_url}" | |
ironport # Run ironport login too | |
keep_alive "${keepalive_url}" | |
} | |
while :; do | |
lpage="$(get_login_page)" | |
magic="$(get_magic "${lpage}")" | |
lurls="$(do_login "${magic}")" | |
main "${lurls}" | |
sleep 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment