【问题标题】:Automating Passphrase in a Bash Script (steghide, gpg, etc.)自动化 Bash 脚本中的密码短语(steghide、gpg 等)
【发布时间】:2014-10-09 11:20:20
【问题描述】:

我一直在编写一系列 bash 脚本,我需要自动输入密码以对文件和操作进行批处理。

这不仅适用于一个程序,例如,有时需要为 GPG 完成,有时需要为 steghide 完成。

这样做是有特定原因的,我了解其背后的安全因素。脚本的存储和工作方式考虑并否定了这一点。

密码或密码短语通过命令行参数传递给脚本,并且密码/短语必须以编程方式重复多次。

这是我在脚本中使用的示例:

for f in $dir
do
    steghide embed -cf $f -ef /path/to/secret.txt
done

这只是对每张图片进行交互询问: 输入密码: 重新输入密码:

对于目录中的每个图像,都将请求此密码,因此该密码应该能够存储在变量中并可以重复使用。

我最近一直在使用 steghide,但以后还需要使用 GPG 自动化密码短语,尽管方法不需要相同。

【问题讨论】:

    标签: bash security scripting automation passwords


    【解决方案1】:

    man steghide:

       -p, --passphrase
              Use  the  string  following  this  argument  as the
              passphrase. If your passphrase contains whitespace,
              you  have  to enclose it in quotes, for example: -p
              "a very long passphrase".
    

    man gpg:

       --passphrase string
              Use  string  as  the  passphrase. This can only be used if only one
              passphrase is supplied. Obviously, this  is  of  very  questionable
              security  on  a multi-user system. Don't use this option if you can
              avoid it.
    

    【讨论】:

      【解决方案2】:

      它未经公开测试,边缘粗糙,可以改进...但这里是我的一些研究脚本的预览,这些脚本尚未合并到我正在编写的 GitHub 项目之一中...绝对可以运行shellcheck 对照下面的脚本来捕捉任何错别字。

      #/usr/bin/env bash
      Var_stego_out_dir="${1}"
      Var_stego_in_dir="${2}"
      Var_stego_cover_dir="${3}"
      Var_passphrase_file="${4}"
      Var_passphrase="${5}"
      Var_auto_pass_length="${6:-64}"
      Func_build_array_of_paths(){
          _dir="${1}"
          _arr="${2}"
          _file_extension_list="${3}"
          if [ -d "${_dir}" ] && [ "${#${_arr}[@]}" = "0" ]; then
              find "${_dir}" -xtype f | while read _path; do
                  case "${_path##*.}" in
                      ${_file_extension_list//,/|})
                          declare -ag "${_arr}+=( ${_path} )"
                      ;;
                  esac
              done
          fi
      }
      Func_enc_stego(){
           _cover_file="${1}"
           _enc_file="${2}"
           _pass_file="${3}"
           _output_file="${Var_stego_out_dir}/${_cover_file##*/}"
          if [ -f "${_cover_file}" ] && [ -f "${_enc_file}" ]; then
              _auto_passphrase="${Var_passphrase:-$(base64 /dev/random | tr -cd '[:print:]' head -c${Var_auto_pass_length})}"
               if ! [ -f "${_output_file}" ]; then
                   steghide -p ${_auto_passphrase} -ef ${_enc_file} -cf ${_cover_file} -sf ${_output_file}
                   cat <<<"### ${_output_file} ### ${_auto_passphrase}" >> "${_pass_file}"
               else
                  steghide -p ${_auto_passphrase} -ef ${_enc_file} -cf ${_cover_file} -sf ${_output_file}_$(date -u +%s)
                   cat <<<"### ${_output_file}_$(date -u +%s) ### ${_auto_passphrase}" >> "${_pass_file}"
              fi
          fi
      }
      Func_main(){
          ## Build array of file paths for cover file use
          Func_build_array_of_paths "${Var_stego_cover_dir}" "Arr_cover_list" "au,AU,bmp,BMP,jpeg,JPEG,wav,WAV"
          ## Build array of file paths for embed file use
          Func_build_array_of_paths "${Var_stego_in_dir}" "Arr_input_list" "gpg,GPG,txt,TXT"
          let _arr_input_count=0
          let _arr_cover_count=0
          until [ "${_arr_input_count}" = "${#Arr_input_list}" ]; do
              if [ -f "${Arr_cover_list[${_arr_cover_count}]}" ]; then
                  Func_enc_stego "${Arr_cover_list[${_arr_cover_count}]}" "${Arr_input_list[${_arr_input_count}]}" "${Var_passphrase_file}"
                  let _arr_cover_count++
                  let _arr_input_count++
              elif  [ -f "${Arr_cover_list[$((${_arr_cover_count}-1))]}" ]; then
                  Func_enc_stego "${Arr_cover_list[$((${_arr_cover_count}-1))]}" "${Arr_input_list[${_arr_input_count}]}" "${Var_passphrase_file}"
                  let _arr_input_count++
                  _arr_cover_count="$((${_arr_cover_count}-1))"
              if
      
          done
      }
      Func_main
      

      在上面填写以下部分运行

      script.sh "/path/to/stego_out_dir" "/path/to/stego_in_dir" "/path/to/stego_cover_dir" "/path/to/passphrase_file"
      ## or define static passphrase
      #script.sh "/path/to/stego_out_dir" "/path/to/stego_in_dir" "/path/to/stego_cover_dir" "/path/to/passphrase_file" "passphrase"
      

      注意,像上面那样以纯文本格式保存密码和文件是非常糟糕的做法,因为 OP 表示他们也在查看 GnuPG 自动化,所以读者和 OP 的作者应该查找 @987654321 @; 尤其是 GnuPG_Gen_Key.sh 脚本和 Paranoid_Pipes.sh 中以 Func_dec_* 开头的函数,用于涉及 GnuPG 密码短语的工作/测试自动化示例;以及用于保护由上述以 @ 开头的脚本查找函数编写的密码短语987654328@ 在Paranoid_Pipes.sh 脚本中,了解mkfifo 命令和生成的命名管道如何用于自动加密大多数数据类型。提示第四个示例参数"/path/to/passphrase_file" 将指向由链接脚本生成的加密命名管道让事情更安全;-)

      【讨论】:

        猜你喜欢
        • 2011-05-30
        • 2017-10-08
        • 1970-01-01
        • 1970-01-01
        • 2019-01-11
        • 2021-07-12
        • 1970-01-01
        • 2015-04-13
        • 1970-01-01
        相关资源
        最近更新 更多