Created
November 23, 2015 14:03
-
-
Save shivamMg/104c0b4574e63c9d6d46 to your computer and use it in GitHub Desktop.
Connect to captive-portal with a list of username-passwords
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
| #!/usr/bin/python3 | |
| import requests | |
| import time | |
| import re | |
| # Connection Delay in seconds | |
| conn_delay = 30 | |
| # File with space separated username-passwords on each line | |
| filename = 'user-list.txt' | |
| # eg: | |
| # ct41111u 1234 | |
| # ct41112u 1234 | |
| # ct41113u 1234 | |
| # Portal URL | |
| portal_url = 'http://10.1.1.9:8090/login.xml' | |
| # Extract username-passwords | |
| user_list = [] | |
| def get_user_list(): | |
| try: | |
| with open(filename, 'r') as fptr: | |
| for line in fptr: | |
| username, password = line.rstrip().split() | |
| user_list.append([ | |
| username, | |
| password | |
| ]) | |
| except FileNotFoundError: | |
| print('Error') | |
| print('File not found') | |
| return False | |
| else: | |
| if user_list == []: | |
| print('Error') | |
| print('File either empty or not in required format') | |
| return False | |
| else: | |
| print('Done') | |
| return True | |
| # Response Messages from Captive Portal | |
| msg_success = '<![CDATA[You have successfully logged in]]>' | |
| msg_invalid_passwd = '<![CDATA[The system could not log you on. Make sure your password is correct]]>' | |
| msg_max_login = '<![CDATA[You have reached Maximum Login Limit.]]>' | |
| msg_max_data = '<![CDATA[Your data transfer has been exceeded, Please contact the administrator]]>' | |
| msg_conn_err = 'ConnectionError' | |
| msg_timeout = 'Timeout' | |
| message_filter = { | |
| msg_success: '', | |
| msg_invalid_passwd: 'Invalid Password', | |
| msg_max_login: 'Maximum Login Limit Reached', | |
| msg_max_data: 'No Data Left', | |
| msg_conn_err: '', | |
| msg_timeout: '' | |
| } | |
| message_flag = { | |
| msg_success: True, | |
| msg_invalid_passwd: False, | |
| msg_max_login: False, | |
| msg_max_data: False, | |
| msg_conn_err: True, | |
| msg_timeout: True | |
| } | |
| # Login Request at Captive Portal | |
| def create_portal(): | |
| # Response Message Regex | |
| regex = re.compile(r'.*<message>(.*)</message>.*', re.UNICODE) | |
| try: | |
| emily = requests.Session() | |
| corvo = emily.post(portal_url, data=payload, timeout=4) | |
| except requests.ConnectionError: | |
| return msg_conn_err | |
| except requests.Timeout: | |
| return msg_timeout | |
| else: | |
| return regex.match(corvo.text).group(1) | |
| payload = { | |
| 'mode': '191', | |
| 'a': '1441368345752', | |
| 'producttype': '0', | |
| } | |
| if __name__ == '__main__': | |
| print("Fetching data from '" + filename + "'... ", end='') | |
| if get_user_list(): | |
| # Extract IP and Port from portal_url | |
| regex = re.compile(r'^https?://([^/]*)/?.*$') | |
| print('Connecting at', regex.match(portal_url).group(1), '[Ctrl-c to quit]') | |
| try: | |
| for user in user_list: | |
| payload['username'] = user[0] | |
| payload['password'] = user[1] | |
| print('Login:', user[0]) | |
| while True: | |
| portal_message = create_portal() | |
| if message_flag[portal_message]: | |
| if portal_message == msg_success: | |
| print('.', end='', flush=True) | |
| time.sleep(conn_delay) | |
| else: | |
| print('E', end='', flush=True) | |
| time.sleep(3) | |
| else: | |
| print('[E]:', message_filter[portal_message]) | |
| break | |
| except KeyboardInterrupt: | |
| print() | |
| print('Keyboard Interrupt') | |
| print('Program Ended') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
znarkashni