Bash,openssl,退出状态下的奇怪行为

时间:2018-06-09 13:55:19

标签: bash openssl exit-code

我正在编写一个脚本,该脚本应对作为使用openssl加密的参数传递的文本文件进行字典攻击。 这是我写的:

#!/bin/bash
# written by Cosimo Colaci

passwords=( $(cat italian.txt) ) # italian.txt is a list of words

for word in ${passwords[@]}
  do
    openssl enc -d -aes-128-cfb1 -in "$1" -k $word 2>/tmp/err
    pid=$$
    wait $pid
    if [ -s /tmp/err ]
    then
      continue
    else
      openssl enc -d -aes-128-cfb1 -in "$1" -k $word;
      break;
    fi
  done

我也试过

for word in ${passwords[@]}
  do
    openssl enc -d -aes-128-cfb1 -in "$1" -k $word &>/dev/null
    exitstatus=$?
    if [ $exitstatus -ne 0 ]
    then
      continue
    else
      openssl enc -d -aes-128-cfb1 -in "$1" -k $word;
      break;
    fi
  done

问题在于,即使解密失败,在一些cicles上退出状态为0,我可以通过启动来看到: bash -x ./crack_italian.sh filetodecript.txt 但是终端中的相同命令的行为与预期的一致并且失败。

1 个答案:

答案 0 :(得分:2)

while read -r word; do
    if openssl enc -d -aes-128-cfb1 -in "$1" -k "$word" >openssl.out 2>&1
    then
        cat openssl.out
        break
    fi
done <italian.txt

rm -f openssl.out
  • 您不需要将文件读入数组。
  • 您可以在if语句中直接使用退出状态 。请注意,在第二个示例中,$?exitstatus的分配发生了变化$?
  • 变量扩展应该加双引号。

略短:

while read -r word; do
    openssl enc -d -aes-128-cfb1 -in "$1" -k "$word" >openssl.out 2>&1 &&
    { cat openssl.out; break; }
done <italian.txt

rm -f openssl.out