Ejemplo n.º 1
0
    def _challenge_response(self, challenge):
        """ generate a response to a mapi login challenge """
        challenges = challenge.split(":")
        salt, identity, protocol, hashes, endian = challenges[:5]
        password = self.password

        if protocol == "9":
            algo = challenges[5]
            try:
                h = hashlib.new(algo)
                h.update(encode(password))
                password = h.hexdigest()
            except ValueError as e:
                raise NotSupportedError(str(e))
        else:
            raise NotSupportedError("We only speak protocol v9")

        h = hashes.split(",")
        if "SHA1" in h:
            s = hashlib.sha1()
            s.update(password.encode())
            s.update(salt.encode())
            pwhash = "{SHA1}" + s.hexdigest()
        elif "MD5" in h:
            m = hashlib.md5()
            m.update(password.encode())
            m.update(salt.encode())
            pwhash = "{MD5}" + m.hexdigest()
        else:
            raise NotSupportedError("Unsupported hash algorithms required"
                                    " for login: %s" % hashes)

        return (":".join(
            ["BIG", self.username, pwhash, self.language, self.database]) +
                ":")
Ejemplo n.º 2
0
    def _challenge_response(self, challenge, blocksize):
        """ generate a response to a mapi login challenge """
        challenges = challenge.split(':')
        salt, identity, protocol, hashes, endian = challenges[:5]
        password = self.password

        if protocol == '9':
            algo = challenges[5]
            try:
                h = hashlib.new(algo)
                h.update(password.encode())
                password = h.hexdigest()
            except ValueError as e:
                raise NotSupportedError(e.message)
        else:
            raise NotSupportedError("We only speak protocol v9")

        h = hashes.split(",")
        if "SHA1" in h:
            s = hashlib.sha1()
            s.update(password.encode())
            s.update(salt.encode())
            pwhash = "{SHA1}" + s.hexdigest()
        elif "MD5" in h:
            m = hashlib.md5()
            m.update(password.encode())
            m.update(salt.encode())
            pwhash = "{MD5}" + m.hexdigest()
        else:
            raise NotSupportedError("Unsupported hash algorithms required"
                                    " for login: %s" % hashes)

        protocol = Protocol.prot9
        compression = Compression.none
        response = ["BIG", self.username, pwhash, self.language, self.database]
        if "PROT10" in h:
            # protocol 10 is supported
            protocol = Protocol.prot10
            _compression = "COMPRESSION_NONE"
            if self.hostname != "localhost" and "COMPRESSION_SNAPPY" in h and HAVE_SNAPPY:
                _compression = "COMPRESSION_SNAPPY"
                compression = Compression.snappy
            response = [
                "LIT" if self.endianness == Endianness.little else "BIG",
                self.username, pwhash, self.language, self.database, "PROT10",
                _compression,
                str(blocksize)
            ]
        return (":".join(response) + ":", protocol, compression)