Created
April 10, 2019 15:31
-
-
Save Warm-rain/c908f276bdfb306f07b6de30126b5652 to your computer and use it in GitHub Desktop.
不知到哪里看到的两个python代码,自行尝试
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
```python | |
''' | |
第一个 | |
''' | |
import os | |
import re | |
import time | |
import json | |
import argparse | |
import requests | |
from getpass import getpass | |
from urllib.parse import urlparse, parse_qs | |
SCRIPT_VERSION = "1.0.3.2" | |
HEADERS = { | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.25 Safari/537.36', | |
'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() | |
CONFIG.gateway = "172.16.1.1" # 填入网关 | |
CONFIG.username = "1aa" # 填入账号 | |
CONFIG.password = "aaaa" # 填入密码 | |
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 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)) | |
main() | |
''' | |
第二个 | |
''' | |
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import requests | |
import re | |
import threading | |
import os | |
import random | |
import socket | |
import struct | |
import time | |
from lxml import etree | |
import json | |
import random | |
"""" | |
""" | |
############################### | |
phone = "aaaa" # 账号 | |
password = "aaa" # 密码 | |
############################### | |
class Giwifi(object): | |
"""docstring for Giwifi""" | |
def __init__(self, phone, password): | |
super(Giwifi, self).__init__() | |
self.phone = phone | |
self.password = password | |
HEADERS = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36', | |
'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' | |
} | |
def GetInfo(self, session): | |
reqUrl = "http://login.gwifi.com.cn/cmps/admin.php/api/login/" | |
req = session.get(reqUrl, headers=self.HEADERS) | |
s = req.url.split("?") | |
# if s[0] != "172.16.1.1": | |
# # print("未连接GIWIFI或已登陆GIWIFI") | |
# # exit(0) | |
reUrl = reqUrl + "?" + s[1] | |
reqReq = session.get(reUrl, headers=self.HEADERS) | |
return req.url, reqReq.text | |
def DitEn(self, url="", session=""): | |
url = url.split("?")[1] | |
urlList = url.split("&") | |
dit = {} | |
for x in urlList: | |
path = x.split("=") | |
dit[path[0]] = path[1] | |
return dit | |
def GetStatus(self, dit, html, session): | |
url = re.search(r"http://" + dit['gw_address'] + ":" + dit['gw_port'] + "/wifidog/get_auth_state.*", html) | |
url = url.group(0)[:-3] | |
url = url + "&callback=''" | |
res = session.get(url, headers=self.HEADERS) | |
resp = json.loads(res.text[3:-1]) | |
return json.loads(resp['data']) | |
def ReqLogin(self, dit, html, session): | |
s = etree.HTML(html) | |
sign = s.xpath('//*[@id="frmLogin"]/ul/input[20]') | |
pageTime = s.xpath('//*[@id="page_time"]') | |
authStatus = self.GetStatus(dit, html, session) | |
params = { | |
'name': self.phone, | |
'password': self.password, | |
'gw_id': dit['gw_id'], | |
'gw_address': dit['gw_address'], | |
'gw_port': dit['gw_port'], | |
'mac': dit['mac'], | |
'page_time': pageTime[0].get("value"), | |
'client_mac': authStatus['client_mac'], | |
'url': dit['url'], | |
'station_sn': authStatus['station_sn'], | |
'acsign': authStatus['sign'], | |
'sign': sign[0].get("value"), | |
'station_cloud': 'login.gwifi.com.cn', | |
'apmac': dit['apmac'], | |
'btype': 'pc', | |
'suggest_phone': '400-038-5858', | |
'contact_phone': '400-038-5858', | |
'logout_reason': '0', | |
'online_time': '0', | |
'access_type': authStatus['access_type'], | |
'devicemode': '', | |
'user_agent': '', | |
'lastaccessurl': '', | |
'service_type': '1' | |
} | |
return params | |
def PostLogin(self, data, session): | |
print(data) | |
ran = random.randint(100, 999) | |
url = 'http://login.gwifi.com.cn/cmps/admin.php/api/loginaction?round=' + str(ran) | |
status = session.post(url, data=data, timeout=5, headers=self.HEADERS) | |
s = json.loads(status.text) | |
print(s['info']) | |
if s['status'] == 1: | |
status = session.get(s['info'], headers=self.HEADERS, allow_redirects=False) | |
url = s['info'].split("?")[1].split("&")[0] | |
url = "http://login.gwifi.com.cn/cmps/admin.php/api/portal/?gw_id=" + data[ | |
'gw_id'] + "&" + url + "&apmac=" + data['apmac'] + "&ssid=" | |
portal = session.get(url, headers=self.HEADERS) | |
url = "http://login.gwifi.com.cn/cmps/admin.php/api/gw_message?message=denied&apmac=" + data[ | |
'apmac'] + "&ssid=" | |
message = session.get(url, headers=self.HEADERS) | |
print("登录成功") | |
elif s['status'] == 0: | |
print("登录失败") | |
else: | |
print("未知错误") | |
def login(self): | |
session = requests.Session() | |
url, html = self.GetInfo(session) | |
dit = self.DitEn(url=url, session=session) | |
params = self.ReqLogin(dit, html, session) | |
loginStatus = self.PostLogin(params, session) | |
if __name__ == '__main__': | |
G = Giwifi(phone, password) | |
G.login() | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment