From fce491767ce106c7c3843cae67ebf0b6605481ed Mon Sep 17 00:00:00 2001 From: Andris Raugulis Date: Sat, 17 Sep 2016 19:23:24 +0300 Subject: [PATCH] Signed mpint. --- ssh-audit.py | 8 +++++--- test/test_protocol.py | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/ssh-audit.py b/ssh-audit.py index cfc5d48..72603ad 100755 --- a/ssh-audit.py +++ b/ssh-audit.py @@ -390,7 +390,7 @@ class WriteBuf(object): return len(bin(n)) - (2 if n > 0 else 3) @classmethod - def _create_mpint(cls, n, bits=None): + def _create_mpint(cls, n, signed=True, bits=None): if bits is None: bits = cls._bitlength(n) length = bits // 8 + (1 if n != 0 else 0) @@ -400,14 +400,16 @@ class WriteBuf(object): v2[ql - i - 1] = (n & 0xffffffffffffffff) n >>= 64 data = bytes(struct.pack(fmt, *v2)[-length:]) - if data.startswith(b'\xff\x80'): + if not signed: + data = data.lstrip(b'\x00') + elif data.startswith(b'\xff\x80'): data = data[1:] return data def write_mpint1(self, n): # NOTE: Data Type Enc @ http://www.snailbook.com/docs/protocol-1.5.txt bits = self._bitlength(n) - data = self._create_mpint(n, bits) + data = self._create_mpint(n, False, bits) self.write(struct.pack('>H', bits)) return self.write(data) diff --git a/test/test_protocol.py b/test/test_protocol.py index 8519930..78c4291 100644 --- a/test/test_protocol.py +++ b/test/test_protocol.py @@ -20,7 +20,8 @@ class TestProtocol(object): mpint1r = lambda x: self.rbuf(x).read_mpint1() tc = [(0x0, '00 00'), (0x1234, '00 0d 12 34'), - (0x12345, '00 11 01 23 45')] + (0x12345, '00 11 01 23 45'), + (0xdeadbeef, '00 20 de ad be ef')] for p in tc: assert mpint1w(p[0]) == self._b(p[1]) assert mpint1r(self._b(p[1])) == p[0]