1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| import os import re import time import json import argparse import requests import netifaces from getpass import getpass from urllib.parse import urlparse, parse_qs
SCRIPT_VERSION = "1.0.3.2"
HEADERS = { 'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh-TW;q=0.8,zh;q=0.6,en;q=0.4,ja;q=0.2', 'cache-control': 'max-age=0' }
PARSER = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description='GiWiFi认证登录脚本', epilog='(c) 2018 journey.ad') PARSER.add_argument('-g', '--gateway', type=str, help='网关IP') PARSER.add_argument('-u', '--username', type=str, help='用户名') PARSER.add_argument('-p', '--password', type=str, help='密码') PARSER.add_argument('-q', '--quit', action='store_true', help='登出') PARSER.add_argument('-d', '--daemon', action='store_true', help='在后台守护运行') PARSER.add_argument('-v', '--verbose', action='store_true', help='额外输出一些技术性信息') PARSER.add_argument('-V', '--version', action='version', version='giwifi-auth-helper {}'.format(SCRIPT_VERSION))
CONFIG = PARSER.parse_args()
if not CONFIG.quit: if not CONFIG.gateway: CONFIG.gateway = netifaces.gateways()['default'][netifaces.AF_INET][0] if not CONFIG.password: CONFIG.gateway = input('请输入网关地址(%s):' % (CONFIG.gateway)) or CONFIG.gateway
if not CONFIG.username: CONFIG.username = input('请输入上网账号:')
if not CONFIG.password: CONFIG.password = getpass('请输入账号密码:') else: if not CONFIG.gateway: CONFIG.gateway = netifaces.gateways()['default'][netifaces.AF_INET][0] CONFIG.gateway = input('请输入网关地址(%s):' % (CONFIG.gateway)) or CONFIG.gateway
def main(): logcat('正在获取网关信息…')
try: authUrl = requests.get('http://%s:8062/redirect' % (CONFIG.gateway), timeout=5).url authParmas = {k: v[0] for k, v in parse_qs(urlparse(authUrl).query).items()}
loginPage = requests.get('http://login.gwifi.com.cn/cmps/admin.php/api/login/?' + urlparse(authUrl).query, headers=HEADERS, timeout=5).text
pagetime = re.search(r'name="page_time" value="(.*?)"', loginPage).group(1) sign = re.search(r'name="sign" value="(.*?)"', loginPage).group(1)
except requests.exceptions.ConnectionError: logcat('连接失败,请检查网关地址是否正确') return
except requests.exceptions.Timeout: logcat('连接超时,可能已超出上网区间') return
except AttributeError: logcat('解析失败,可能网关设备重启或系统已更新') return
authState = getAuthState(authParmas, sign)
if CONFIG.quit: logout(authParmas)
if not authState: return
else: if authState['auth_state'] == 2: printStatus(authParmas, authState) logcat('你已登录,无需再次登录') else: data = { 'access_type': authState['access_type'], 'acsign': authState['sign'], 'btype': 'pc', 'client_mac': authState['client_mac'], 'contact_phone': '400-038-5858', 'devicemode': '', 'gw_address': authParmas['gw_address'], 'gw_id': authParmas['gw_id'], 'gw_port': authParmas['gw_port'], 'lastaccessurl': '', 'logout_reason': authState['logout_reason'], 'mac': authParmas['mac'], 'name': CONFIG.username, 'online_time': authState['online_time'], 'page_time': pagetime, 'password': CONFIG.password, 'sign': sign, 'station_cloud': 'login.gwifi.com.cn', 'station_sn': authState['station_sn'], 'suggest_phone': '400-038-5858', 'url': 'http://www.baidu.com', 'user_agent': '', }
if CONFIG.verbose: logcat(data)
result = login(data) if result['status']: authState = getAuthState(authParmas, sign) printStatus(authParmas, authState)
if authState['auth_state'] == 2: logcat('认证成功') else: logcat('认证失败') else: logcat('认证失败,提示信息:%s' % (result['info']))
def login(data): logcat('正在尝试认证…')
try: resp = json.loads(requests.post('http://login.gwifi.com.cn/cmps/admin.php/api/loginaction', data=data, timeout=5).text) result = { 'status': False, 'info': None }
if CONFIG.verbose: logcat(resp)
if 'wifidog/auth' in resp['info']: requests.get(resp['info']) result['status'] = True else: result['info'] = resp['info']
except requests.exceptions.Timeout: logcat('连接超时,可能已超出上网区间') finally: return result
def logout(authParmas): try: resp = json.loads(requests.get('http://%s/getApp.htm?action=logout' % (authParmas['gw_address'])).text) except requests.exceptions.Timeout: logcat('连接超时,可能已超出上网区间') return
if resp['resultCode'] == 0: logcat('下线成功') else: logcat('下线失败')
def getAuthState(authParmas, sign): try: params = { 'ip': authParmas['ip'], 'mac': authParmas['mac'], 'sign': sign, 'callback': '' } resp = json.loads(requests.get('http://%s:%s/wifidog/get_auth_state' % (authParmas['gw_address'], authParmas['gw_port']), params=params, timeout=5).text[1:-1]) except KeyError: logcat('所需参数不存在') return False
except requests.exceptions.Timeout: logcat('连接超时,可能已超出上网区间') return False
if CONFIG.verbose: logcat(resp)
if resp['resultCode'] == 0: return json.loads(resp['data']) else: return False
def printStatus(authParmas, authState): if not CONFIG.verbose: clear()
print( '''-------------------------------------------- SSID: %s AP MAC: %s GateWay: %s IP: %s MAC: %s Station SN: %s Logged: %s --------------------------------------------''' % ( authParmas['gw_id'], authParmas['apmac'], authParmas['gw_address'], authParmas['ip'], authParmas['mac'], authState['station_sn'], 'yes' if(authState['auth_state'] == 2) else 'no' ) )
def clear(): os.system('cls' if os.name == 'nt' else 'clear')
def logcat(msg, level='I'): print('%s %s: %s' % (time.ctime().split(' ')[-2], level, msg))
if __name__ == '__main__': if CONFIG.daemon: while True: main() time.sleep(30) else: main() input()
|