diff --git a/monkey/infection_monkey/network/smbfinger.py b/monkey/infection_monkey/network/smbfinger.py index 1e765114c..8a267e9d1 100644 --- a/monkey/infection_monkey/network/smbfinger.py +++ b/monkey/infection_monkey/network/smbfinger.py @@ -12,7 +12,7 @@ SMB_SERVICE = 'tcp-445' LOG = logging.getLogger(__name__) -class Packet(object): +class Packet: fields = odict([ ("data", ""), ]) @@ -25,78 +25,79 @@ class Packet(object): else: self.fields[k] = v - def __str__(self): - return "".join(map(str, list(self.fields.values()))) + def to_byte_string(self): + content_list = [(x.to_byte_string() if hasattr(x, "to_byte_string") else x) for x in self.fields.values()] + return b"".join(content_list) ##### SMB Packets ##### class SMBHeader(Packet): fields = odict([ - ("proto", "\xff\x53\x4d\x42"), - ("cmd", "\x72"), - ("errorcode", "\x00\x00\x00\x00"), - ("flag1", "\x00"), - ("flag2", "\x00\x00"), - ("pidhigh", "\x00\x00"), - ("signature", "\x00\x00\x00\x00\x00\x00\x00\x00"), - ("reserved", "\x00\x00"), - ("tid", "\x00\x00"), - ("pid", "\x00\x00"), - ("uid", "\x00\x00"), - ("mid", "\x00\x00"), + ("proto", b"\xff\x53\x4d\x42"), + ("cmd", b"\x72"), + ("errorcode", b"\x00\x00\x00\x00"), + ("flag1", b"\x00"), + ("flag2", b"\x00\x00"), + ("pidhigh", b"\x00\x00"), + ("signature", b"\x00\x00\x00\x00\x00\x00\x00\x00"), + ("reserved", b"\x00\x00"), + ("tid", b"\x00\x00"), + ("pid", b"\x00\x00"), + ("uid", b"\x00\x00"), + ("mid", b"\x00\x00"), ]) class SMBNego(Packet): fields = odict([ - ("wordcount", "\x00"), - ("bcc", "\x62\x00"), + ("wordcount", b"\x00"), + ("bcc", b"\x62\x00"), ("data", "") ]) def calculate(self): - self.fields["bcc"] = struct.pack("i", len(packet_)) + packet_.encode() + packet_ = h.to_byte_string() + n.to_byte_string() + buffer = struct.pack(">i", len(packet_)) + packet_ s.send(buffer) data = s.recv(2048) - if data[8:10] == "\x72\x00": - header = SMBHeader(cmd="\x73", flag1="\x18", flag2="\x17\xc8", uid="\x00\x00") + if data[8:10] == b"\x72\x00": + header = SMBHeader(cmd=b"\x73", flag1=b"\x18", flag2=b"\x17\xc8", uid=b"\x00\x00") body = SMBSessionFingerData() body.calculate() - packet_ = str(header) + str(body) - buffer = struct.pack(">i", len(packet_)) + packet_.encode() + packet_ = header.to_byte_string() + body.to_byte_string() + buffer = struct.pack(">i", len(packet_)) + packet_ s.send(buffer) data = s.recv(2048) - if data[8:10] == "\x73\x16": + if data[8:10] == b"\x73\x16": length = struct.unpack('