Created
February 10, 2017 09:43
-
-
Save Gummary/69f4e0e5b3ea1a7035d521cfd58298e1 to your computer and use it in GitHub Desktop.
世纪天成账号半自动注册机
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 gi, requests | |
gi.require_version('Gtk', '3.0') | |
from gi.repository import Gtk | |
from user import User | |
from utils import Utils | |
from proxies import Proxy | |
class RegisterWindow(Gtk.Window): | |
def __init__(self, proxy = None): | |
''' | |
构造函数 | |
Args: | |
proxy:添加的代理 | |
''' | |
Gtk.Window.__init__(self, title="RegisterWindow") | |
self.user = User() | |
self.check_uid() | |
self.accounts = {} | |
self.init_ui() | |
self.code = None | |
self.update_code_img() | |
self.proxy = proxy | |
def init_ui(self): | |
""" | |
初始化用户界面 | |
Args: | |
None | |
Returns: | |
None | |
""" | |
self.vbox = Gtk.VBox(spacing=3) | |
self.username = Gtk.HBox(spacing=3) | |
self.username_label = Gtk.Label("UserName:") | |
self.username_input = Gtk.Label(self.user.get_uid()) | |
self.username.add(self.username_label) | |
self.username.add(self.username_input) | |
self.passwd = Gtk.HBox(spacing=3) | |
self.passwd_label = Gtk.Label("Password:") | |
self.passwd_input = Gtk.Label(self.user.get_pwd()) | |
self.passwd.add(self.passwd_label) | |
self.passwd.add(self.passwd_input) | |
self.image_area = Gtk.Image() | |
self.entry = Gtk.Entry() | |
self.button = Gtk.Button(label="Register") | |
self.vbox.add(self.username) | |
self.vbox.add(self.passwd) | |
self.vbox.add(self.image_area) | |
self.vbox.add(self.entry) | |
self.vbox.add(self.button) | |
self.button.connect("clicked", self.register_clicked) | |
self.add(self.vbox) | |
def do_delete_event(self, event): | |
""" | |
关闭窗口时触发 | |
Args: | |
event:关闭事件 | |
Returns: | |
False:关闭窗口 | |
""" | |
if len(self.accounts) > 0: | |
Utils.write_to_file(self.accounts) | |
return False | |
def pop_window(self): | |
""" | |
当注册次数过多时弹出新窗口二次验证 | |
Args: | |
None | |
Returns: | |
None | |
""" | |
self.ban_window = Gtk.Window() | |
self.ban_window.set_position(Gtk.WindowPosition.CENTER) | |
self.ban_image = Gtk.Image() | |
self.ban_code = Utils.get_code__and_tid() | |
Utils.save_img(self.ban_code[0], "bancode.jpeg", "JPEG") | |
self.ban_image.set_from_file("bancode.jpeg") | |
self.ban_input = Gtk.Entry() | |
self.ban_btn = Gtk.Button(label = "Sure") | |
vbox = Gtk.VBox() | |
vbox.add(self.ban_image) | |
vbox.add(self.ban_input) | |
vbox.add(self.ban_btn) | |
self.ban_window.add(vbox) | |
self.ban_window.show_all() | |
self.ban_btn.connect("clicked", self.ban_window_clicked, self.ban_window) | |
def ban_window_clicked(self, widget, window): | |
""" | |
点击弹出的对话框中的Sure按钮 | |
Args: | |
widget: | |
window: | |
Returns: | |
None | |
""" | |
params = {"userid":self.user.get_uid(), "randcode":self.ban_input.get_text(), "captid":self.ban_code[1]} | |
status = int(self.user.get_pwd_encrypt(params)) | |
self.ban_window.destroy() | |
if status != 1000: | |
self.pop_window() | |
def check_uid(self): | |
""" | |
调用User类的check_uid,判断返回的是1000或1040 | |
Args: | |
None | |
Returns: | |
None | |
""" | |
rescode = int(self.user.check_uid()) | |
while rescode != 1000: | |
if rescode == 1040: | |
self.pop_window() | |
break | |
elif rescode == 1020: | |
self.user.update_user() | |
rescode = int(self.user.check_uid()) | |
def update_code_img(self): | |
""" | |
将验证码保存到本地并更新UI | |
Args: | |
None | |
Returns: | |
None | |
""" | |
self.code = Utils.get_code__and_tid() | |
Utils.save_img(self.code[0], "code.jpeg", "JPEG") | |
self.image_area.set_from_file("code.jpeg") | |
def register_clicked(self,widget): | |
""" | |
register按钮点击事件 | |
Args: | |
widget | |
Returns: | |
None | |
""" | |
result = self.register_request() | |
status, returnStr = Utils.get_reg_result(result) | |
if status != 1: | |
print(returnStr) | |
self.update_code_img() | |
self.entry.set_text("") | |
else: | |
self.register_success() | |
def register_success(self): | |
""" | |
注册成功的回调函数 | |
Args: | |
None | |
Returns: | |
None | |
""" | |
self.accounts[self.user.get_uid()] = self.user.get_pwd() | |
if len(self.accounts) >= 100: | |
Utils.write_to_file(self.accounts) | |
self.user.update_user() | |
self.check_uid() | |
self.username_input.set_text(self.user.get_uid()) | |
self.passwd_input.set_text(self.user.get_pwd()) | |
self.update_code_img() | |
self.entry.set_text("") | |
def register_request(self): | |
""" | |
发送注册请求函数 | |
Args: | |
None | |
Returns: | |
None | |
""" | |
modulus = self.user.get_modulus() | |
en_pwd = Utils.encryptStr(modulus, self.user.get_pwd()) | |
en_pwd2 = Utils.encryptStr(modulus, self.user.get_pwd()) | |
params = { | |
"op" : 1, | |
"ud" : self.user.get_uid(), | |
"pw": en_pwd, | |
"pw2": en_pwd2, | |
"sp":"", | |
"sp2":"", | |
"um": "张三", | |
"cn" :"451101197002216744", | |
"ex":"", | |
"et":"1", | |
"fd":"", | |
"fm":"", | |
"ia":"true", | |
"rc": self.entry.get_text(), | |
"td": self.code[1], | |
"fu":"", | |
"if":0 | |
} | |
url = "http://member.tiancity.com/Handler/RegisterHandler.aspx" | |
r = requests.post(url, params = params, proxies = self.proxy) | |
return r.text | |
if __name__ == '__main__': | |
win = RegisterWindow(proxy = None) | |
win.connect("delete-event", Gtk.main_quit) | |
win.show_all() | |
Gtk.main() |
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, random, math, string | |
from io import BytesIO | |
from PIL import Image | |
from utils import Utils | |
class User: | |
commonreg_url ="https://member.tiancity.com/Handler/Reg/NewCommonRegChkHandler.ashx" | |
def __init__(self, proxy = None): | |
""" | |
构造函数 | |
Args: | |
proxy:要设置的代理 | |
Returns: | |
None | |
""" | |
self.uid = "" | |
self.res = None | |
self.pwd = "" | |
self.update_user() | |
self.proxy = proxy | |
def get_uid(self): | |
""" | |
获取用户名 | |
Args: | |
None | |
Returns: | |
用户名 | |
""" | |
return self.uid | |
def get_pwd(self): | |
""" | |
获取密码 | |
Args: | |
None | |
Returns: | |
未加密的密码 | |
""" | |
return self.pwd | |
def update_user(self): | |
""" | |
重新生成用户名及密码 | |
Args: | |
None | |
Returns: | |
None | |
""" | |
self.uid = Utils.get_random_str(10) | |
self.pwd = Utils.get_random_str() | |
def check_uid(self): | |
""" | |
检查当前用户名是否合法 | |
Args: | |
None | |
Returns: | |
状态1000或1040 | |
""" | |
params = {'userid':self.get_uid(), 'rnd':random.random()} | |
return self.get_pwd_encrypt(params) | |
def get_pwd_encrypt(self, params): | |
""" | |
获取要rsa秘钥 | |
Args: | |
params:{'userid':,'rnd'=''}或 | |
{'userid':'','randcode':'','captid':''} | |
Returns: | |
状态,1000或1040 | |
""" | |
r = requests.get("https://member.tiancity.com/Handler/Reg/NewCommonRegChkHandler.ashx", params = params, proxies = self.proxy) | |
self.res = r.text.split(',') | |
return self.res[0] | |
def get_modulus(self): | |
""" | |
获取要rsa秘钥modulus | |
Args: | |
None | |
Returns: | |
获取的秘钥modulus | |
""" | |
return self.res[-1] | |
if __name__ == '__main__': | |
user = User() |
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, math, random, string, rsa,binascii, json | |
from PIL import Image | |
from io import BytesIO | |
class Utils: | |
@staticmethod | |
def get_reg_result(json_str): | |
""" | |
返回注册的结果 | |
Args: | |
json_str:注册完成后返回的json格式的结果 | |
Returns: | |
注册的结果 | |
""" | |
j = json.loads(json_str) | |
return j['IsSuccess'], j['ReturnString'] | |
@staticmethod | |
def encryptStr(n, message): | |
""" | |
根据modulus的值n加密message | |
Args: | |
n:modulus的值 | |
message:要加密的值 | |
Returns: | |
加密完成的结果 | |
""" | |
rsaPublickey = int(n, 16) | |
key = rsa.PublicKey(rsaPublickey, int("010001", 16)) | |
passwd = rsa.encrypt(bytes(message, 'ascii'), key) | |
passwd = binascii.b2a_hex(passwd) | |
return passwd | |
@staticmethod | |
def save_img(url,name,img_type): | |
""" | |
根据url保存图片 | |
Args: | |
url:图片的地址 | |
name:要保存的图片名称 | |
img_type:图片类型 | |
Returns: | |
None | |
""" | |
r = requests.get(url) | |
image = Image.open(BytesIO(r.content)) | |
image.save(name, img_type) | |
@staticmethod | |
def get_code__and_tid(): | |
""" | |
获取验证码的地址和tid | |
Args: | |
None | |
Returns: | |
url:验证码的地址 | |
tid:验证码的tid | |
""" | |
tid = Utils.__get_code_td__() | |
url = "https://captcha.tiancity.com/getimage.ashx?fid=400&tid="+tid | |
return url, tid | |
@staticmethod | |
def __get_code_td__(): | |
result = "" | |
for i in range(32): | |
c = math.floor(random.random() * 16.0) | |
c = hex(c)[-1] | |
result += c | |
return result | |
@staticmethod | |
def get_random_str(len = 8): | |
""" | |
获取长度为len的随机字符串 | |
Args: | |
len:字符串长度 | |
Returns: | |
随机字符串 | |
""" | |
s = "" | |
while s == "" or s[0].isdigit(): | |
s = random.sample(string.ascii_lowercase+string.digits, len) | |
return "".join(map(str, s)) | |
@staticmethod | |
def write_to_file(account_dict): | |
""" | |
将字典写入文件 | |
Args: | |
account_dict:要写入的字典 | |
Returns: | |
None | |
""" | |
with open('account.txt', 'a') as f: | |
for k in account_dict.keys(): | |
f.write("{0} {1}\n".format(k, account_dict[k])) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment