def login(self, verifyCode=None): '''登录''' #发送登录请求 packet = '' #包头 packet += Coder.trim('00 00 00 08 02 00 00 00 04 00') packet += Coder.num2hexstr(len(self.qqHexstr)/2+4, 4) packet += self.qqHexstr #TEA加密的包体 packet += self.packSendLoginMessage(verifyCode) #总包长 packet = Coder.num2hexstr(len(packet)/2+4, 4) + packet #发送请求 self.socket.sendall(Coder.hexstr2str(packet)) #接收请求 ret = self.socket.recv() pack = HexPacket(Coder.str2hexstr(ret)) #返回包头 pack.shr(4) pack.shr(8) pack.shr(2 + len(self.qqHexstr)/2) #返回包体 self.unpackRecvLoginMessage(pack.remain()) if self.alive: #登录成功 threading.Thread(target=self.startHeart).start() #心跳 return True elif self.verify: #需要验证码 pass else: return False
def login(self, verifyCode=None): '''登录''' #发送登录请求 packet = '' #包头 packet += Coder.trim('00 00 00 08 02 00 00 00 04 00') packet += Coder.num2hexstr(len(self.qqHexstr) / 2 + 4, 4) packet += self.qqHexstr #TEA加密的包体 packet += self.packSendLoginMessage(verifyCode) #总包长 packet = Coder.num2hexstr(len(packet) / 2 + 4, 4) + packet #发送请求 self.socket.sendall(Coder.hexstr2str(packet)) #接收请求 ret = self.socket.recv() pack = HexPacket(Coder.str2hexstr(ret)) #返回包头 pack.shr(4) pack.shr(8) pack.shr(2 + len(self.qqHexstr) / 2) #返回包体 self.unpackRecvLoginMessage(pack.remain()) if self.alive: #登录成功 threading.Thread(target=self.startHeart).start() #心跳 return True elif self.verify: #需要验证码 pass else: return False
def unpackRecvLoginSucceedMessage(self, data): data = TEA.detea_hexstr(data, self.shareKey) pack = HexPacket(data) pack.shr(2 + 1 + 4) data = pack.shr(Coder.hexstr2num(pack.shr(2))) #TLV解包 data = TEA.detea_hexstr(data, self.tgtKey) pack = HexPacket(data) tlv_num = Coder.hexstr2num(pack.shr(2)) for i in xrange(tlv_num): tlv_cmd = pack.shr(2) tlv_data = pack.shr(Coder.hexstr2num(pack.shr(2))) self.decodeTlv(tlv_cmd, tlv_data)
def unpackRecvLoginSucceedMessage(data): data = TEA.detea_hexstr(data,'9A1BDA11D3BEA2DEDC58C487B3D174BA') #shareKey pack = HexPacket(data) pack.shr(2 + 1 + 4) data = pack.shr(Coder.hexstr2num(pack.shr(2))) #TLV解包 data = TEA.detea_hexstr(data,'9d2a2efab0653d0aecdb8a3e97c4dd22')#tgtKey pack = HexPacket(data) tlv_num = Coder.hexstr2num(pack.shr(2)) for i in xrange(tlv_num): tlv_cmd = pack.shr(2) tlv_data = pack.shr(Coder.hexstr2num(pack.shr(2))) decodeTlv(tlv_cmd, tlv_data)
def unpackRecvLoginVerifyMessage(self, data): data = TEA.detea_hexstr(data, self.shareKey) pack = HexPacket(data) pack.shr(3) tlv_num = Coder.hexstr2num(pack.shr(2)) for i in xrange(tlv_num): tlv_cmd = pack.shr(2) tlv_data = pack.shr(Coder.hexstr2num(pack.shr(2))) self.decodeTlv(tlv_cmd, tlv_data)
def unpackRecvLoginVerifyMessage(self, data): data = TEA.detea_hexstr(data, self.shareKey) pack = HexPacket(data) pack.shr(3) tlv_num = Coder.hexstr2num(pack.shr(2)) for i in xrange(tlv_num): tlv_cmd = pack.shr(2) tlv_data = pack.shr(Coder.hexstr2num(pack.shr(2))) self.decodeTlv(tlv_cmd, tlv_data) pass
def decodeTlv(self, cmd, data): if cmd == Coder.trim('01 6A'): pass elif cmd == Coder.trim('01 06'): pass elif cmd == Coder.trim('01 0C'): pass elif cmd == Coder.trim('01 0A'): self.token004c = data elif cmd == Coder.trim('01 0D'): pass elif cmd == Coder.trim('01 14'): pack = HexPacket(data) pack.shr(6) self.token0058 = pack.shr(Coder.hexstr2num(pack.shr(2))) elif cmd == Coder.trim('01 0E'): self.mst1Key = data elif cmd == Coder.trim('01 03'): self.stweb = data elif cmd == Coder.trim('01 1F'): pass elif cmd == Coder.trim('01 38'): pass elif cmd == Coder.trim('01 1A'): pack = HexPacket(data) pack.shr(2 + 1 + 1) self.nickname = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(1)))) elif cmd == Coder.trim('01 20'): self.skey = data elif cmd == Coder.trim('01 36'): self.vkey = data elif cmd == Coder.trim('01 1A'): pass elif cmd == Coder.trim('01 20'): pass elif cmd == Coder.trim('01 36'): pass elif cmd == Coder.trim('03 05'): self.sessionKey = data elif cmd == Coder.trim('01 43'): self.token002c = data elif cmd == Coder.trim('01 64'): self.sid = data elif cmd == Coder.trim('01 18'): pass elif cmd == Coder.trim('01 63'): pass elif cmd == Coder.trim('01 30'): pack = HexPacket(data) pack.shr(2) self.server_time = pack.shr(4) self.ip = Coder.hexstr2ip(pack.shr(4)) elif cmd == Coder.trim('01 05'): pack = HexPacket(data) self.verifyToken1 = pack.shr(Coder.hexstr2num(pack.shr(2))) self.verifyPicHexstr = pack.shr(Coder.hexstr2num(pack.shr(2))) elif cmd == Coder.trim('01 04'): self.verifyToken2 = data elif cmd == Coder.trim('01 65'): pack = HexPacket(data) pack.shr(4) title = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(1)))) msg = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(4)))) self.verifyReason = title + ": " + msg elif cmd == Coder.trim('01 08'): self.ksid = data elif cmd == Coder.trim('01 6D'): self.superKey = data elif cmd == Coder.trim('01 6C'): self.psKey = data else: print 'unknown tlv: ' print cmd, ': ', data
def unpackRecvLoginMessage(self, data): data = TEA.detea_hexstr(data, self.defaultKey) pack = HexPacket(data) head = pack.shr(Coder.hexstr2num(pack.shr(4))-4) body = pack.remain(1) #head pack = HexPacket(head) Coder.hexstr2num(pack.shr(4)) #seq pack.shr(4) pack.shr(Coder.hexstr2num(pack.shr(4))-4) Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(4))-4)) #cmd pack.shr(Coder.hexstr2num(pack.shr(4))-4) #body pack = HexPacket(body) pack.shr(4 + 1 + 2 + 10 + 2) retCode = Coder.hexstr2num(pack.shr(1)) if retCode == 0: #登录成功 self.unpackRecvLoginSucceedMessage(pack.remain()) print u'登录成功: ', self.nickname self.alive = True self.verify = False elif retCode == 2: #需要验证码 self.unpackRecvLoginVerifyMessage(pack.remain()) print self.verifyReason self.alive = False self.verify = True threading.Thread(target=Img.showFromHexstr, args=(self.verifyPicHexstr, )).start() code = raw_input(u'请输入验证码:') self.login(Coder.str2hexstr(code)) else: #登录失败 pack = HexPacket(TEA.detea_hexstr(pack.remain(), self.shareKey)) pack.shr(2 + 1 + 4 + 2) pack.shr(4) #type title = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) msg = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) print title, ': ', msg self.alive = False self.verify = False
def decodeTlv(self, cmd, data): if cmd == Coder.trim('01 6A'): pass elif cmd == Coder.trim('01 06'): pass elif cmd == Coder.trim('01 0C'): pass elif cmd == Coder.trim('01 0A'): self.token004c = data elif cmd == Coder.trim('01 0D'): pass elif cmd == Coder.trim('01 14'): pack = HexPacket(data) pack.shr(6) self.token0058 = pack.shr(Coder.hexstr2num(pack.shr(2))) elif cmd == Coder.trim('01 0E'): self.mst1Key = data elif cmd == Coder.trim('01 03'): self.stweb = data elif cmd == Coder.trim('01 1F'): pass elif cmd == Coder.trim('01 38'): pass elif cmd == Coder.trim('01 1A'): pack = HexPacket(data) pack.shr(2 + 1 + 1) self.nickname = Coder.hexstr2str( pack.shr(Coder.hexstr2num(pack.shr(1)))) elif cmd == Coder.trim('01 20'): self.skey = data elif cmd == Coder.trim('01 36'): self.vkey = data elif cmd == Coder.trim('01 1A'): pass elif cmd == Coder.trim('01 20'): pass elif cmd == Coder.trim('01 36'): pass elif cmd == Coder.trim('03 05'): self.sessionKey = data elif cmd == Coder.trim('01 43'): self.token002c = data elif cmd == Coder.trim('01 64'): self.sid = data elif cmd == Coder.trim('01 18'): pass elif cmd == Coder.trim('01 63'): pass elif cmd == Coder.trim('01 30'): pack = HexPacket(data) pack.shr(2) self.server_time = pack.shr(4) self.ip = Coder.hexstr2ip(pack.shr(4)) elif cmd == Coder.trim('01 05'): pack = HexPacket(data) self.verifyToken1 = pack.shr(Coder.hexstr2num(pack.shr(2))) self.verifyPicHexstr = pack.shr(Coder.hexstr2num(pack.shr(2))) elif cmd == Coder.trim('01 04'): self.verifyToken2 = data elif cmd == Coder.trim('01 65'): pack = HexPacket(data) pack.shr(4) title = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(1)))) msg = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(4)))) self.verifyReason = title + ": " + msg elif cmd == Coder.trim('01 08'): self.ksid = data elif cmd == Coder.trim('01 6D'): self.superKey = data elif cmd == Coder.trim('01 6C'): self.psKey = data else: print 'unknown tlv: ' print cmd, ': ', data
def unpackRecvLoginMessage(self, data): data = TEA.detea_hexstr(data, self.defaultKey) pack = HexPacket(data) head = pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) body = pack.remain(1) #head pack = HexPacket(head) Coder.hexstr2num(pack.shr(4)) #seq pack.shr(4) pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(4)) - 4)) #cmd pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) #body pack = HexPacket(body) pack.shr(4 + 1 + 2 + 10 + 2) retCode = Coder.hexstr2num(pack.shr(1)) if retCode == 0: #登录成功 self.unpackRecvLoginSucceedMessage(pack.remain()) print u'登录成功: ', self.nickname self.alive = True self.verify = False elif retCode == 2: #需要验证码 self.unpackRecvLoginVerifyMessage(pack.remain()) print self.verifyReason self.alive = False self.verify = True threading.Thread(target=Img.showFromHexstr, args=(self.verifyPicHexstr, )).start() code = raw_input(u'请输入验证码:') self.login(Coder.str2hexstr(code)) else: #登录失败 pack = HexPacket(TEA.detea_hexstr(pack.remain(), self.shareKey)) pack.shr(2 + 1 + 4 + 2) pack.shr(4) #type title = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) msg = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) print title, ': ', msg self.alive = False self.verify = False
def Fun_recv(self): list = [] ret = self.socket.recv() if ret: if ret == '0': return try: retdata = Coder.str2hexstr(ret) while retdata: n = retdata[:8] long = Coder.hexstr2num(n) * 2 list.append(retdata[:long]) retdata = retdata[long:] for item in list: qq = self.qqHexstr + '(.*)' data1 = re.findall(qq, item)[0] data = TEA.detea_hexstr(data1, self.qqkey) pack = HexPacket(data) head = pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) body = pack.remain(1) #head pack = HexPacket(head) Coder.hexstr2num(pack.shr(4)) #seq pack.shr(4) pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) cmd = Coder.hexstr2str( pack.shr(Coder.hexstr2num(pack.shr(4)) - 4)) #cmd print '##################', cmd, '#####################' pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) self.unpack(cmd, body) except: traceback.print_exc() else: print '返回包为空'
def unpackRecvLoginMessage(self, data): data = TEA.detea_hexstr(data, self.defaultKey) print 'undata:', data pack = HexPacket(data) head = pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) body = pack.remain(1) #head pack = HexPacket(head) Coder.hexstr2num(pack.shr(4)) #seq pack.shr(4) pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(4)) - 4)) #cmd pack.shr(Coder.hexstr2num(pack.shr(4)) - 4) #body pack = HexPacket(body) pack.shr(4 + 1 + 2 + 10 + 2) retCode = Coder.hexstr2num(pack.shr(1)) if retCode == 0: #登录成功 self.unpackRecvLoginSucceedMessage(pack.remain()) print u'登录成功: ', self.nickname print 'qqkey', self.qqkey self.alive = True self.verify = False elif retCode == 2: #需要验证码 self.unpackRecvLoginVerifyMessage(pack.remain()) print self.verifyReason self.alive = False self.verify = True print self.verifyPicHexstr code = self.window.SetVerification(self.verifyPicHexstr) print code self.login(Coder.str2hexstr(code)) else: #登录失败 pack = HexPacket(TEA.detea_hexstr(pack.remain(), self.shareKey)) pack.shr(2 + 1 + 4 + 2) pack.shr(4) #type title = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) msg = Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(2)))) print title, ': ', msg self.alive = False self.verify = False
def unpackRecvLoginMessage(data): data = TEA.detea_hexstr(data, '00'*16) pack = HexPacket(data) head = pack.shr(Coder.hexstr2num(pack.shr(4))-4) body = pack.remain(1) #head pack = HexPacket(head) Coder.hexstr2num(pack.shr(4)) #seq pack.shr(4) pack.shr(Coder.hexstr2num(pack.shr(4))-4) Coder.hexstr2str(pack.shr(Coder.hexstr2num(pack.shr(4))-4)) #cmd pack.shr(Coder.hexstr2num(pack.shr(4))-4) #body pack = HexPacket(body) pack.shr(4 + 1 + 2 + 10 + 2) retCode = Coder.hexstr2num(pack.shr(1)) if retCode == 0: #登录成功 unpackRecvLoginSucceedMessage(pack.remain())