SecureCRT 服务器链接信息密码忘记找回

当忘记SecureCRT中Linux服务器的密码时,可以通过查找配置文件并使用Python脚本来解密找回。首先找到SecureCRT的配置文件,然后新建一个Python脚本,利用pycryptodome库解密密码。根据SecureCRT的密码加密格式执行相应的解密命令,即可得到明文密码。
摘要由CSDN通过智能技术生成

在日常工作和学习中,服务器常为 Linux 服务器,而主机电脑一版为 Windows 系统,在主机上如何连接 Linux 服务器,大家用到的工具也是五花八门,很多朋友都喜欢用 XShell,而我习惯用 SecureCRT。

可当有时候,工具中链接服务器的链接信息,忘记了用户对应的密码,而且手头还没有保存明文密码,这就比较尴尬了。那如何找回密码呢?这篇简单说说找回步骤。

第一步:找到 SecureCRT 保存链接信息的配置文件。

在工具菜单栏,依次点击 Options -> Global Options...,打开选项面板,找到如图所示位置。

在对应路径找到机器对应的配置文件,比如这里要找回 192.168.220.10 机器的链接密码,如图:

打开配置文件后,可以看到用户名和密码信息等,如图:

第二步:新建一个 Python 脚本,代码如下:

#!/usr/bin/env python3
import os
from Crypto.Hash import SHA256
from Crypto.Cipher import AES, Blowfish
 
class SecureCRTCrypto:
 
    def __init__(self):
        '''
        Initialize SecureCRTCrypto object.
        '''
        self.IV = b'\x00' * Blowfish.block_size
        self.Key1 = b'\x24\xA6\x3D\xDE\x5B\xD3\xB3\x82\x9C\x7E\x06\xF4\x08\x16\xAA\x07'
        self.Key2 = b'\x5F\xB0\x45\xA2\x94\x17\xD9\x16\xC6\xC6\xA2\xFF\x06\x41\x82\xB7'
 
    def Encrypt(self, Plaintext : str):
        '''
        Encrypt plaintext and return corresponding ciphertext.
        Args:
            Plaintext: A string that will be encrypted.
        Returns:
            Hexlified ciphertext string.
        '''
        plain_bytes = Plaintext.encode('utf-16-le')
        plain_bytes += b'\x00\x00'
        padded_plain_bytes = plain_bytes + os.urandom(Blowfish.block_size - len(plain_bytes) % Blowfish.block_size)
 
        cipher1 = Blowfish.new(self.Key1, Blowfish.MODE_CBC, iv = self.IV)
        cipher2 = Blowfish.new(self.Key2, Blowfish.MODE_CBC, iv = self.IV)
        return cipher1.encrypt(os.urandom(4) + cipher2.encrypt(padded_plain_bytes) + os.urandom(4)).hex()
 
    def Decrypt(self, Ciphertext : str):
        '''
        Decrypt ciphertext and return corresponding plaintext.
        Args:
            Ciphertext: A hex string that will be decrypted.
        Returns:
            Plaintext string.
        '''
 
        cipher1 = Blowfish.new(self.Key1, Blowfish.MODE_CBC, iv = self.IV)
        cipher2 = Blowfish.new(self.Key2, Blowfish.MODE_CBC, iv = self.IV)
        ciphered_bytes = bytes.fromhex(Ciphertext)
        if len(ciphered_bytes) <= 8:
            raise ValueError('Invalid Ciphertext.')
        
        padded_plain_bytes = cipher2.decrypt(cipher1.decrypt(ciphered_bytes)[4:-4])
        
        i = 0
        for i in range(0, len(padded_plain_bytes), 2):
            if padded_plain_bytes[i] == 0 and padded_plain_bytes[i + 1] == 0:
                break
        plain_bytes = padded_plain_bytes[0:i]
 
        try:
            return plain_bytes.decode('utf-16-le')
        except UnicodeDecodeError:
            raise(ValueError('Invalid Ciphertext.'))
 
class SecureCRTCryptoV2:
 
    def __init__(self, ConfigPassphrase : str = ''):
        '''
        Initialize SecureCRTCryptoV2 object.
        Args:
            ConfigPassphrase: The config passphrase that SecureCRT uses. Leave it empty if config passphrase is not set.
        '''
        self.IV = b'\x00' * AES.block_size
        self.Key = SHA256.new(ConfigPassphrase.encode('utf-8')).digest()
 
    def Encrypt(self, Plaintext : str):
        '''
        Encrypt plaintext and return corresponding ciphertext.
        Args:
            Plaintext: A string that will be encrypted.
        Returns:
            Hexlified ciphertext string.
        '''
        plain_bytes = Plaintext.encode('utf-8')
        if len(plain_bytes) > 0xffffffff:
            raise OverflowError('Plaintext is too long.')
        
        plain_bytes = \
            len(plain_bytes).to_bytes(4, 'little') + \
            plain_bytes + \
            SHA256.new(plain_bytes).digest()
        padded_plain_bytes = \
            plain_bytes + \
            os.urandom(AES.block_size - len(plain_bytes) % AES.block_size)
        cipher = AES.new(self.Key, AES.MODE_CBC, iv = self.IV)
        return cipher.encrypt(padded_plain_bytes).hex()
 
    def Decrypt(self, Ciphertext : str):
        '''
        Decrypt ciphertext and return corresponding plaintext.
        Args:
            Ciphertext: A hex string that will be decrypted.
        Returns:
            Plaintext string.
        '''
        cipher = AES.new(self.Key, AES.MODE_CBC, iv = self.IV)
        padded_plain_bytes = cipher.decrypt(bytes.fromhex(Ciphertext))
        
        plain_bytes_length = int.from_bytes(padded_plain_bytes[0:4], 'little')
        plain_bytes = padded_plain_bytes[4:4 + plain_bytes_length]
        if len(plain_bytes) != plain_bytes_length:
            raise ValueError('Invalid Ciphertext.')
 
        plain_bytes_digest = padded_plain_bytes[4 + plain_bytes_length:4 + plain_bytes_length + SHA256.digest_size]
        if len(plain_bytes_digest) != SHA256.digest_size:
            raise ValueError('Invalid Ciphertext.')
 
        if SHA256.new(plain_bytes).digest() != plain_bytes_digest:
            raise ValueError('Invalid Ciphertext.')
 
        return plain_bytes.decode('utf-8')
 
if __name__ == '__main__':
    import sys
 
    def Help():
        print('Usage:')
        print('    SecureCRTCipher.py <enc|dec> [-v2] [-p ConfigPassphrase] <plaintext|ciphertext>')
        print('')
        print('    <enc|dec>              "enc" for encryption, "dec" for decryption.')
        print('                           This parameter must be specified.')
        print('')
        print('    [-v2]                  Encrypt/Decrypt with "Password V2" algorithm.')
        print('                           This parameter is optional.')
        print('')
        print('    [-p ConfigPassphrase]  The config passphrase that SecureCRT uses.')
        print('                           This parameter is optional.')
        print('')
        print('    <plaintext|ciphertext> Plaintext string or ciphertext string.')
        print('                           NOTICE: Ciphertext string must be a hex string.')
        print('                           This parameter must be specified.')
        print('')
    
    def EncryptionRoutine(UseV2 : bool, ConfigPassphrase : str, Plaintext : str):
        try:
            if UseV2:
                print(SecureCRTCryptoV2(ConfigPassphrase).Encrypt(Plaintext))
            else:
                print(SecureCRTCrypto().Encrypt(Plaintext))
            return True
        except:
            print('Error: Failed to encrypt.')
            return False
 
    def DecryptionRoutine(UseV2 : bool, ConfigPassphrase : str, Ciphertext : str):
        try:
            if UseV2:
                print(SecureCRTCryptoV2(ConfigPassphrase).Decrypt(Ciphertext))
            else:
                print(SecureCRTCrypto().Decrypt(Ciphertext))
            return True
        except:
            print('Error: Failed to decrypt.')
            return False
 
    def Main(argc : int, argv : list):
        if 3 <= argc and argc <= 6:
            bUseV2 = False
            ConfigPassphrase = ''
 
            if argv[1].lower() == 'enc':
                bEncrypt = True
            elif argv[1].lower() == 'dec':
                bEncrypt = False
            else:
                Help()
                return -1
            
            i = 2
            while i < argc - 1:
                if argv[i].lower() == '-v2':
                    bUseV2 = True
                    i += 1
                elif argv[i].lower() == '-p' and i + 1 < argc - 1:
                    ConfigPassphrase = argv[i + 1]
                    i += 2
                else:
                    Help()
                    return -1
 
            if bUseV2 == False and len(ConfigPassphrase) != 0:
                print('Error: ConfigPassphrase is not supported if "-v2" is not specified')
                return -1
 
            if bEncrypt:
                return 0 if EncryptionRoutine(bUseV2, ConfigPassphrase, argv[-1]) else -1
            else:
                return 0 if DecryptionRoutine(bUseV2, ConfigPassphrase, argv[-1]) else -1
        else:
            Help()
 
    exit(Main(len(sys.argv), sys.argv))

文件名随便自定义,比如 SecureCRTCipher.py。

第三步:主机搭建 Python3 环境。

由于脚本为 Python 脚本,需要 Python 环境才能执行,需要在 Windows 主机上安装 Python3 环境。该步骤此处省略,请自行安装并配置环境变量等。

另外注意,需要安装模块 pycryptodome,否则在第四步执行命令时,会报如下错误:

Traceback (most recent call last):
  File "SecureCRTCipher.py", line 3, in <module>
    from Crypto.Hash import SHA256
ModuleNotFoundError: No module named 'Crypto'

安装模块命令:pip install pycryptodome

第四步:执行脚本,找回密码。

打开 CMD 窗口,进入脚本存放目录位置,如图:

SecureCRT 的版本不同,加密格式有两种。

格式一:

S:"Password V2"=02:30d93758e1ea303c18235301266dc93a5d69070b40668251a2c75de132a5a034b018dab1a172bd73da69151d6fd4099824f2a4fb5f57c5f854e9a9012ff0e6c1

执行命令:python SecureCRTCipher.py dec -v2 30d93758e1ea303c18235301266dc93a5d69070b40668251a2c75de132a5a034b018dab1a172bd73da69151d6fd4099824f2a4fb5f57c5f854e9a9012ff0e6c1

注意:-V2 参数后的值,是格式一中【=】后面的值,并且去掉【02:】。

执行后,可以看到明文密码已经输出到终端中了,如图: 

格式二:

S:"Password"=uc71bd1c86f3b804e42432f53247c50d9287f410c7e59166969acab69daa6eaadbe15c0c54c0e076e945a6d82f9e13df2

执行命令:python SecureCRTCipher.py dec c71bd1c86f3b804e42432f53247c50d9287f410c7e59166969acab69daa6eaadbe15c0c54c0e076e945a6d82f9e13df2

注意:dec 后的值,是格式二中【=】后面的值,并且去掉【u】。

执行后,可以看到明文密码已经输出到终端中了,如图:

Good Luck!

如果SecureCRT无法连接服务器,可能是由于以下原因: 1. 没有正确安装openssh-server: SecureCRT是通过SSH协议来连接服务器的,因此在服务器上需要安装openssh-server。你可以通过检查是否安装了openssh-server来确认。如果没有安装,可以使用以下命令安装openssh-server: ``` yum -y install openssh-server ``` 2. 防火墙或网络问题: SecureCRT连接服务器时,可能会受到防火墙或网络设置的限制。请确保服务器的防火墙设置允许SSH连接,并且网络连接正常。 3. SecureCRT配置问题: 请确保在SecureCRT中正确配置了服务器的连接参数,包括服务器的IP地址、端口号、用户名和密码信息。你可以参考SecureCRT的使用文档或官方网站来了解如何正确配置连接参数。 4. 服务器端SSH配置问题: 如果以上步骤都没有解决问题,可能是服务器端的SSH配置有问题。你可以检查服务器的SSH配置文件(通常是`/etc/ssh/sshd_config`)是否正确,并尝试重新启动SSH服务。 如果问题仍然存在,请提供更多详细的错误信息或尝试其他调试方法,以便进一步排查问题的原因。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [SecureCRT无法远程连接服务器的问题](https://blog.csdn.net/weily11/article/details/81162113)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [SecureCRT远程连接服务器](https://download.csdn.net/download/weixin_44517536/11074072)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值