2017-09-06 45 views
1

編寫Python腳本,我想知道是否可以綁定到一個LDAP服務器,而不在這個例子中明文寫入密碼,如:Python-ldap:是否可以在不明確寫入密碼的情況下進行綁定?

import ldap 

l = ldap.open("myserver") 
username = "cn=Manager, o=mydomain.com" 

## I don't want to write the password here in plaintext 
password = "secret" 

l.simple_bind(username, password) 
+0

是的,它是可能的,我通常使用PyCrypto憑據加密一個文件。然後我會解密該文件並傳入值。 – iNoob

回答

0

示例功能用於解密文件名爲」。證書'。在嘗試使用它之前,這當然會有一個seporate腳本來首先將憑證加密到文件中。

所以,你會調用該函數:

username, password = decrypt() 

l.simple_bind(username, password) 

from Crypto.Cipher import AES 
import base64 
from local_logging import info 

def decrypt(dir_path): 
    #Read '.credentials' file and return unencrypted credentials (user_decoded, pass_decoded) 

    lines = [line.rstrip('\n') for line in open(dir_path + '/.credentials')] 

    user_encoded = lines[0] 
    user_secret = lines[1] 
    pass_encoded = lines[2] 
    pass_secret = lines[3] 

    # the character used for padding--with a block cipher such as AES, the value 
    # you encrypt must be a multiple of BLOCK_SIZE in length. This character is 
    # used to ensure that your value is always a multiple of BLOCK_SIZE 
    PADDING = '{' 

    DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING) 

    # create a cipher object using the random secret 
    user_cipher = AES.new(user_secret) 
    pass_cipher = AES.new(pass_secret) 

    # decode the encoded string 
    user_decoded = DecodeAES(user_cipher, user_encoded) 
    pass_decoded = DecodeAES(pass_cipher, pass_encoded) 

    return (user_decoded, pass_decoded) 
相關問題