使用 bash 暴力破解使用 Openssl 加密的文件

Bruteforcing a file encrypted with Openssl using bash

我正在做一个学校项目,在该项目中,我们获得了一个使用 openssl 和 AES-128 密码和 CBC 模式加密的密码文件。表明该文件采用无盐和IV加密,密码少于16个字符,末尾附加0-9的数字。我们得到了一本字典 (words.txt) 和密码文件,我必须编写一个脚本来查找密钥并解密文件的内容。

出于测试目的,我使用以下 openssl 命令加密了一个纯文本文件:

openssl enc -e -aes-128-cbc -in plain.txt -out test.txt -nosalt -pass pass:Zoroastrian6

我正在使用以下命令对其进行解密:

openssl enc -d -aes-128-cbc -in test.txt -out test2.txt -nosalt -pass pass:Zoroastrian6

效果很好。我使用的密码是我的讲师提供的 words.txt 中的最后一个条目。这是我到目前为止写的:

#!/bin/bash

filename="words.txt"
let flag=0

while read line
do
    # Second loop to append 0-9 at the end of each line read from
    # the text file
    let count=0
    while [ $count -lt 10 ];
    do
        comm=$(openssl enc -d -aes-128-cbc -in test.txt -out test2.txt -nosalt -pass pass:$line$count 2>/dev/null)
        # If the program exits without an error, we print 
        # the password and exit the loop before setting the
        # exit flag to be checked in the first loop to break
        # out of it.
        if [ $? -eq 0 ]; then
            echo "$line$count"
            flag=1
            break
        fi
        ((count++))
    done
    # If flag is set here, it means the password was found
    # and we can exit the loop.
    if [ $flag -eq 1 ]; then
        break
    fi
    
done < "$filename"

问题是它没有到​​达单词“Zoroastrian”所在的文件末尾,而是以密码“abstinent8”退出。我不知道如何调试它,因为我是 bash 脚本的新手。有没有办法检查 openssl 是否退出而没有任何错误,或者有办法检查解密是否出错?

编辑:words.txt 可在此处找到。

Is there a way to check if openssl exited without any errors or a way to check if the decryption gave an error?

是的,这就是你正在做的。

不幸的是,在使用像这样的低级原语时,使用错误的密钥并不是错误。它只会给你错误的明文。

在您的情况下,您使用 PKCS#5 填充(比较 -nopad),因此一些明文 (~99.7%) 恰好未通过填充检查并导致错误,但这远远不足以确保密码正确。

专业系统使用 Authenticated Encryption,但对于玩具示例,您可以例如检查消息中的所有字符是否可打印,或添加并检查“秘密:”header 以使正确的明文可识别。