def handleRegister(self, data): (dUsername, dPassword) = netstruct.unpack("b$b$", data) username = str(dUsername) password = str(dPassword) print "Login:"******"Passwort:", password userExists = False for userrow in DatabaseConnector.Instance().getConnector().execute( "SELECT * FROM chat_users WHERE username=?", (username.lower(), )): userExists = True if userExists: responseData = netstruct.pack("ib$", 3, "Username already Exists") self.sendPacket(1, responseData) return pwhash = bcrypt.hashpw(password, bcrypt.gensalt()) DatabaseConnector.Instance().getConnector().execute( "INSERT INTO chat_users (id, username, password, ismod, isadmin, lastlogin, lastip, banned, banreason, banliftdate) VALUES(NULL, ?, ?, 0, 0, NULL, '0.0.0.0', 0, '', 0) ", ( username.lower(), pwhash, )) DatabaseConnector.Instance().writeDB() self.m_clientNick = username responseData = netstruct.pack("ib$", 0, "") self.sendPacket(1, responseData)
def handleJoinRoomRequest(self, data): (roomid, password) = netstruct.unpack("b$b$", data) print "User Joining Room " + roomid room = RoomManager.Instance().getRoomByID(roomid) if room: room.joinRoom(self) print "User Joined Room " + room.getRoomName()
def handleRoomAdd(self, data): (roomid, roomtype, usercount, roomname) = netstruct.unpack("b$iib$", data) self.m_roomAddCallback(roomid, roomtype, usercount, roomname) print "ID: " + roomid print "Type: " + str(roomtype) print "Users: " + str(usercount) print "Name: " + roomname
def get_credential(self, service, username): if username is None: username = service cred = super().get_credential(service, username) if cred is not None: u, p = netstruct.unpack(self.pattern, cred.password.encode('UTF-8')) cred = keyring.credentials.SimpleCredential(u.decode('UTF-8'), p.decode('UTF-8')) return cred
def loginCallback(self, status, data): if status: self.switch() else: (errorCode, errorReason) = netstruct.unpack("ib$", data) errorReasons = { 1: "Login: "******"User: "******"Register: " } self.dataErrorText = errorReasons.get(errorCode) + errorReason print "Error"
def handleLogin(self, data): (dUsername, dPassword) = netstruct.unpack("b$b$", data) username = str(dUsername) password = str(dPassword) print "Login:"******"Passwort:", password loginValid = False errorCode = 0 errorReason = "" userIsBanned = 0 banReason = "" banLift = -1 for userrow in DatabaseConnector.Instance().getConnector().execute( "SELECT * FROM chat_users WHERE username=?", (username.lower(), )): if bcrypt.checkpw(password.encode('utf-8'), userrow[2].encode('utf-8')): loginValid = True userIsBanned = userrow[7] banReason = userrow[8] banLift = userrow[9] if loginValid: print "Login is Valid" if userIsBanned != 0: errorCode = 2 errorReason = "Banned: " + str(banReason) print "User is Banned!" else: print "Login is Invalid" errorCode = 1 errorReason = "Username/Password Invalid" self.m_clientNick = username responseData = netstruct.pack("ib$", errorCode, errorReason) self.sendPacket(1, responseData)
def test_empty(self): self.assertEqual(netstruct.unpack(b"", b""), [])
def test_not_enough(self): with self.assertRaises(netstruct.error): netstruct.unpack(b"4i", b"\x00\x01\x02\x03")
def unpack(payload, key_finder): ct, nonce, user_id = netstruct.unpack(b'!b$b$i', payload) symm_key = key_finder(user_id) ticket, ip, new_ticket, new_n = Packet._decrypt( ct, nonce, symm_key, user_id) return Packet(ip, user_id, ticket, new_ticket, new_n)
def _decrypt(ct, nonce, symm_key, user_id): aesgcm = AESGCM(symm_key) buffer = aesgcm.decrypt(nonce, ct, str(user_id).encode()) ticket, new_ticket, ip, new_n = netstruct.unpack(b'!b$b$b$i', buffer) return ticket, ip.decode(), new_ticket, new_n
#| | python PackedStringsTest.py #| | #| # #| #| @license Please see LICENSE. #| import serial # To talk to the serial ports from cobs import cobs # Decode packets in COBS format import time # To sleep a bit import netstruct # Parse the data dataBuffer = b'' serialPort = serial.Serial() serialPort.port = 'COM4' # serialPort.port = '/dev/ttyACM0' serialPort.baudrate = 115200 serialPort.open() while True: if serialPort.in_waiting > 0: # if we have some data dataBuffer += serialPort.read(serialPort.in_waiting) # add it if b'\x00' in dataBuffer: # if we have a packet marker... # We have a packet! (packet, dataBuffer) = dataBuffer.split(b'\x00', 1) # split it print(netstruct.unpack(b'b$b$', cobs.decode(packet))) # decode and unpack time.sleep(0.5) # have a snooze
def test_too_much(self): self.assertEqual(netstruct.unpack(b"i", b"\x00\x01\x02\x03\x04"), [66051])
def handleHello(self, data): (bClientNick) = netstruct.unpack("b$", data) self.m_clientNick = bClientNick[0] print "Hello from '" + self.m_clientNick + "'" return
def deserialize(cls, data, *args, **kwargs): endpoint = Endpoint.deserialize(data[:6]) initiator_name = netstruct.unpack('H$', data[6:]) return ConnectionRequest(initiator_name, endpoint)
# TODO Can estruct return byte strings instead of a list of chars? FIDOAttestation = estruct.EStruct( 'FIDOAttestation', '''tag flags sign_count pubkey_encoding pubkey_len pubkey key_handle_len key_handle client_data_hash_len client_data_hash ''', '>HBLHH{pubkey_len}[s]H{key_handle_len}[c]H{client_data_hash_len}[c]', ) result = FIDOAttestation.unpack(a) print '\t', type(result), hex(result[0]), result.pubkey import netstruct # Apache, big-endian by default result = netstruct.unpack('H B L H H$ H$ H$', a) print 'netstruct' print '\t', result from infi.instruct.buffer import ( Buffer, be_uint_field, bytearray_field, after_ref, bytes_ref, len_ref, num_ref, self_ref, ) class FIDOAttestation(Buffer): tag = be_uint_field(where=bytes_ref[0:2]) flags = be_uint_field(where=bytes_ref[2:3]) sign_count = be_uint_field(where=bytes_ref[3:7]) pubkey_encoding = be_uint_field(where=bytes_ref[7:9]) pubkey_len = be_uint_field(where=bytes_ref[9:11],
def test_too_much(self): self.assertEqual( netstruct.unpack(b"i", b"\x00\x01\x02\x03\x04"), [66051] )
def deserialize(cls, data): port = netstruct.unpack('H', data[:2])[0] ipv4_address = socket.inet_ntoa(data[2:6]) return Endpoint(port, ipv4_address)