From fabd1ffccdbcd569ebf75d2ab6491839914c9384 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Tue, 12 Mar 2013 16:03:16 -0400 Subject: [PATCH 1/2] additional sanity checking in the addr processing function --- bitmessagemain.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bitmessagemain.py b/bitmessagemain.py index 952cbc6f..46d41ef2 100755 --- a/bitmessagemain.py +++ b/bitmessagemain.py @@ -1677,6 +1677,10 @@ class receiveDataThread(QThread): if numberOfAddressesIncluded > 1000 or numberOfAddressesIncluded == 0: return + if self.payloadLength < lengthOfNumberOfAddresses + (34 * numberOfAddressesIncluded): + print 'addr message does not contain enough data. Ignoring.' + return + needToWriteKnownNodesToDisk = False for i in range(0,numberOfAddressesIncluded): try: @@ -1701,7 +1705,8 @@ class receiveDataThread(QThread): sys.stderr.write('ERROR TRYING TO UNPACK recaddr (recaddrStream). Message: %s\n' % str(err)) printLock.release() break #giving up on unpacking any more. We should still be connected however. - + if recaddrStream == 0: + continue try: recaddrServices, = unpack('>Q',self.data[32+lengthOfNumberOfAddresses+(34*i):40+lengthOfNumberOfAddresses+(34*i)]) except Exception, err: @@ -1725,10 +1730,12 @@ class receiveDataThread(QThread): if hostFromAddrMessage == '127.0.0.1': continue timeSomeoneElseReceivedMessageFromThisNode, = unpack('>I',self.data[24+lengthOfNumberOfAddresses+(34*i):28+lengthOfNumberOfAddresses+(34*i)]) #This is the 'time' value in the received addr message. + if recaddrStream not in knownNodes: + knownNodes[recaddrStream] = {} if hostFromAddrMessage not in knownNodes[recaddrStream]: if len(knownNodes[recaddrStream]) < 20000 and timeSomeoneElseReceivedMessageFromThisNode > (int(time.time())-10800) and timeSomeoneElseReceivedMessageFromThisNode < (int(time.time()) + 10800): #If we have more than 20000 nodes in our list already then just forget about adding more. Also, make sure that the time that someone else received a message from this node is within three hours from now. knownNodes[recaddrStream][hostFromAddrMessage] = (recaddrPort, timeSomeoneElseReceivedMessageFromThisNode) - print 'added new node', hostFromAddrMessage, 'to knownNodes.' + print 'added new node', hostFromAddrMessage, 'to knownNodes in stream', recaddrStream needToWriteKnownNodesToDisk = True hostDetails = (timeSomeoneElseReceivedMessageFromThisNode, recaddrStream, recaddrServices, hostFromAddrMessage, recaddrPort) listOfAddressDetailsToBroadcastToPeers.append(hostDetails) @@ -1744,7 +1751,7 @@ class receiveDataThread(QThread): output.close() self.broadcastaddr(listOfAddressDetailsToBroadcastToPeers) printLock.acquire() - print 'knownNodes currently has', len(knownNodes[recaddrStream]), 'nodes for this stream.' + print 'knownNodes currently has', len(knownNodes[self.streamNumber]), 'nodes for this stream.' printLock.release() #Function runs when we want to broadcast an addr message to all of our peers. Runs when we learn of nodes that we didn't previously know about and want to share them with our peers. From 9d385a290daebecc10363caded29ef67cdcaceb3 Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Thu, 14 Mar 2013 11:58:52 -0400 Subject: [PATCH 2/2] Upon receiving bad data from peer (bad magic bytes) print the data to console for analysis --- bitmessagemain.py | 1 + messages.dat reader.py | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/bitmessagemain.py b/bitmessagemain.py index 952cbc6f..8ba1ca2d 100755 --- a/bitmessagemain.py +++ b/bitmessagemain.py @@ -312,6 +312,7 @@ class receiveDataThread(QThread): if verbose >= 2: printLock.acquire() sys.stderr.write('The magic bytes were not correct. First 40 bytes of data: %s\n' % repr(self.data[0:40])) + print 'self.data:', self.data.encode('hex') printLock.release() self.data = "" else: diff --git a/messages.dat reader.py b/messages.dat reader.py index 7ca966a6..ec116a1e 100644 --- a/messages.dat reader.py +++ b/messages.dat reader.py @@ -39,7 +39,8 @@ def readSent(): cur.execute(item, parameters) output = cur.fetchall() for row in output: - print row + msgid, toaddress, toripe, fromaddress, subject, message, ackdata, lastactiontime, status, pubkeyretrynumber, msgretrynumber, folder = row + print msgid.encode('hex'), toaddress, 'toripe:', toripe.encode('hex'), 'fromaddress:', fromaddress, 'SUBJECT:', repr(subject), 'MESSAGE:', repr(message), 'ACKDATA:', ackdata.encode('hex'), lastactiontime, status, pubkeyretrynumber, msgretrynumber, folder def readSubscriptions(): print 'Printing everything in subscriptions table:' @@ -90,9 +91,9 @@ def takeSentMessagesOutOfTrash(): #takeInboxMessagesOutOfTrash() #takeSentMessagesOutOfTrash() #readInbox() -#readSent() +readSent() #readPubkeys() -readSubscriptions() +#readSubscriptions() #readInventory()