Skip to content

Implement basic replay protection #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pysyncobj/tcp_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, poller, onMessageReceived = None, onConnected = None, onDisco

self.sendRandKey = None
self.recvRandKey = None
self.recvLastTimestamp = 0
self.encryptor = None

self.__socket = socket
Expand Down Expand Up @@ -101,7 +102,7 @@ def send(self, message):
message = (self.sendRandKey, message)
data = zlib.compress(pickle.dumps(message), 3)
if self.encryptor:
data = self.encryptor.encrypt(data)
data = self.encryptor.encrypt_at_time(data, int(monotonicTime()))
data = struct.pack('i', len(data)) + data
self.__writeBuffer += data
self.__trySendBuffer()
Expand All @@ -115,6 +116,7 @@ def disconnect(self):
needCallDisconnect = True
self.sendRandKey = None
self.recvRandKey = None
self.recvLastTimestamp = 0
if self.__socket is not None:
self.__socket.close()
self.__socket = None
Expand Down Expand Up @@ -232,12 +234,17 @@ def __processParseMessage(self):
data = self.__readBuffer[4:4 + l]
try:
if self.encryptor:
dataTimestamp = self.encryptor.extract_timestamp(data)
assert dataTimestamp >= self.recvLastTimestamp
self.recvLastTimestamp = dataTimestamp
# Unfortunately we can't get a timestamp and data in one go
data = self.encryptor.decrypt(data)
message = pickle.loads(zlib.decompress(data))
if self.recvRandKey:
randKey, message = message
assert randKey == self.recvRandKey
except:
# Why no logging of security errors?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add logging

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, of course. I see PySyncObj already has logging imported so printing something like "invalid message received from $ip, closing connection" should be not hard. I'll do a separate PR after I figure exactly how.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, do you think it should be logger.warning or more like logger.info? On private network it feels like warning because it means I have broken app somewhere, but on internet it feels wrong because I receive lot of junk, so its more like info or debug. Er, is it good idea to write anything to log just because somebody from internet connected?

wow... you are fast! Thank you.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably that's the reason it was missing in log ) Probably we can leave it as is for now..

self.disconnect()
return None
self.__readBuffer = self.__readBuffer[4 + l:]
Expand Down