Created
November 17, 2020 10:02
-
-
Save developerfromjokela/eee1aa93f3c2606c74aebe5f6960d4ef to your computer and use it in GitHub Desktop.
Ohittaa suomalaisten koulujen/kuntien julkisen Wifin Captive portalin automaattisesti tämän skriptin avulla
This file contains 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
import requests | |
import urllib.parse as urlparse | |
import validators | |
import bs4 | |
def meta_redirect(content): | |
soup = bs4.BeautifulSoup(content, 'html.parser') | |
result = soup.find("meta", attrs={"http-equiv": "Refresh"}) | |
if result: | |
wait, text = result["content"].split(";") | |
if text.strip().lower().startswith("url="): | |
urlRd = text[4:] | |
return urlRd | |
return None | |
rqSession = requests.Session() | |
response = rqSession.get("http://detectportal.firefox.com", timeout=10) | |
textResp = response.text | |
metaRedir = False | |
redirUrl = None | |
if meta_redirect(textResp) is not None: | |
metaRedir = True | |
redirUrl = meta_redirect(textResp) | |
if response.history or metaRedir: | |
if not metaRedir: | |
print("Request was redirected") | |
for resp in response.history: | |
print(resp.status_code, resp.url) | |
print("Final destination:") | |
print(response.status_code, response.url) | |
url = response.url | |
else: | |
print("Request needs to be redirected using meta tag") | |
response = rqSession.get(redirUrl) | |
print("Final destination:") | |
print(response.status_code, response.url) | |
url = response.url | |
page = bs4.BeautifulSoup(response.text, 'html.parser') | |
forms = page.find_all("form") | |
if len(forms) > 0: | |
form = forms[0] | |
postUrl = url | |
if form.has_attr("action"): | |
actionUrl = form.attrs.get("action") | |
if (actionUrl.startswith("/") and not actionUrl.startswith("//")) or validators.url(actionUrl) is not True: | |
postUrl = urlparse.urljoin(url, actionUrl) | |
print("Posting to " + postUrl) | |
formInputs = form.find_all("input") | |
body = [] | |
for formInput in formInputs: | |
if formInput.has_attr("name"): | |
name = formInput.attrs.get("name", "") | |
value = "" | |
if formInput.has_attr("value"): | |
value = formInput.attrs.get("value", "") | |
body.append((name, value)) | |
print("Body:") | |
print(body) | |
returned = rqSession.post(postUrl, body, headers={ | |
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:82.0) Gecko/20100101 Firefox/82.0'}) | |
if returned.status_code != 200: | |
print("Got non-200 status code, still continuing") | |
print(returned.text) | |
print("Validating portal success") | |
response = rqSession.get("http://detectportal.firefox.com") | |
if "success" in response.text.strip(): | |
print("Ok") | |
else: | |
print("Failed!") | |
print(response) | |
else: | |
print("Request was not redirected") | |
if "success" in textResp.strip(): | |
print("Ok") | |
else: | |
print("Failed!") | |
print(textResp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment