3

I log all my shell activity using script, but am worried that there are quite a few passwords and other secret stuff in these log files.

Does anybody know of any way of logging the shell sessions in an encrypted manner? Are there maybe some similar utilities to script that integrates with GPG?

2 Answers 2

0

You could put the logfiles in an encrypted vault instead of encrypting individual files.

Or maybe let script write to a fifo, read that fifo with gpg and let gpg do the encryption and write it to a file.

0

If you are using bash, you could use your ~/.bash_logout script to encrypt the script logfile with gpg or something.

.bash_logout will need to know what the log's filename is. For example, instead of just running script filename, run a myscript function defined in your .bashrc.

function myscript() {

  # Very primitive option handling. This really needs to pass any script
  # command-line options on to script.
  # Left as an exercise for the reader and the getopts documentation :)
  if [ -n "$1" ] ; then
    SCRIPT_FILENAME="$1"
  else
    SCRIPT_FILENAME="$USER-$(tty)-$(date +%s)"
  fi
  readonly SCRIPT_FILENAME
  export SCRIPT_FILENAME

  script "$SCRIPT_FILENAME"
}

And then in the .bash_logout file:

  if [ -n "$SCRIPT_FILENAME" ] && [ -e "$SCRIPT_FILENAME" ] ; then
      gpg --batch --yes  -r 'YOUR-EMAIL-OR-ID' -e "$SCRIPT_FILENAME"
      shred "$SCRIPT_FILENAME" # or any other file secure-erase tool.
      rm -f ""$SCRIPT_FILENAME"
  fi

SECURITY WARNING: This makes your system vulnerable to anything (e.g. a sourced script) that can set the SCRIPT_FILENAME variable in your shell's environment. An attacker could set it to any file your userid has write access to. (Of course, an attacker could just overwrite that file themselves if they can run anything as you)

You could avoid this by setting $SCRIPT_FILENAME and making it readonly in your ~/.bashrc rather than in the myscript function. This would make it impossible to use a different filename with the myscript function.

Or you could store all your typescripts in one directory (e.g. /home/username/typescripts/). Then the .bash_logout script could check that:

  • the filename begins with an absolute path to that dir,
  • the filename does not end with .gpg
  • there are no ../s in the filename
  • the filename is not a symlink
  • the filename's inode count = 1
  • etc

before encrypting, shredding, and deleting it.

ANOTHER SECURITY WARNING:

BEWARE. DO NOT USE CODE EXAMPLES EVEN AT OWN RISK. MINE EXAMPLES FOR IDEAS ONLY.

While small parts of this have been tested individually, it has not been tested as a complete, functioning system. This answer is NOT intended to provide a complete solution. It is intended to provide ideas for you to explore and implement yourself.

2
  • 1
    Also worth noting: until encrypted by the .bash_logout code, the typescript file is NOT secured in any way and can be read by root (or by anyone else on the system if the file's permissions allow it). The same will be true if the system crashes, preventing the .bash_logout from being run.
    – cas
    Sep 5, 2019 at 11:15
  • I would use stream encryption (not batch encryption). I would also learn to not add passwords to command line arguments. Note these can be accessible to anyone that can do a process list (by default anyone). Jul 17, 2020 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .