Credential leak in shell history
The first thing you do in somebody else's account is look at what the previous user did. The shell writes everything down, and it writes it down unfiltered: a password passed on the command line, the name of a file that appears nowhere public, a backup path, the real name of an internal service.
cat ~/.bash_history
ls -la ~ | grep history
cat ~/.zsh_history ~/.sh_history ~/.local/share/fish/fish_history 2>/dev/nullAnd not just the shell's. Every interactive client keeps its own, and those are the ones that tend to carry credentials, because the client asks for them as a parameter:
cat ~/.mysql_history ~/.psql_history ~/.rediscli_history ~/.dbshell 2>/dev/null
cat ~/.viminfo ~/.lesshst ~/.wget-hsts 2>/dev/null
grep -rE "(-p|--password|PASS|token|Authorization)" ~/.*history* 2>/dev/nullWhat pays is not only what they typed: it is where they looked. A repetitive history is a map of somebody's frustration, and that frustration points somewhere specific. A history with fifteen su -, half a dozen sudo /root/troll and, in between, three lone lines (ls /backup, ls -l /backup, cat /backup/shadow.backup) is not telling you a password: it is telling you there is something to look at in /backup. And what was there was a copy of /etc/shadow readable by a normal user, which is cracking material straight away.
Read it whole rather than grepping for password inside it: the commands that carry no secret are the ones that draw the path. What you are after, in order of value:
- Literal credentials (
mysql -u root -pXXX,curl -u user:pass,sshpass -p). - Paths and file names that show up in no enumeration (backups, dumps, keys).
- Internal host and user names, which feed spraying and reuse.
- Installed tools and versions, which change the inventory of what can be used without uploading anything.
The Windows equivalent is the PowerShell history, and the principle is the same; the difference is that PowerShell's lives in a file whose path almost nobody remembers, while bash's sits in the home directory in plain sight.
Requirements#
A session as any user and read access to their home. The file may be empty, symlinked to /dev/null or truncated by HISTSIZE: that is itself a finding, because somebody took the trouble. And the current session's history is not in the file yet: it is written on exit, so for your own session use history.
How to detect it. From the defender's side this is not a technique you detect on the network: you detect it on the file. An auditd rule watching reads of ~/.bash_history, ~/.mysql_history and ~/.psql_history reports who opens them, and what gives it away is who does: a cat of another user's history, or of your own right after a logon from a new address. On Linux, an execve of grep -r password over a home directory is a reasonable signature.
Defence. Do not pass secrets on the command line, which is the root cause: mysql reads ~/.my.cnf, curl accepts --netrc, and almost everything accepts the password on stdin or via an environment variable. For whatever cannot be fixed, HISTCONTROL=ignorespace plus a leading space, or unset HISTFILE in administrative sessions. And audit the history like any other file holding credentials, not as an innocuous user file.
Machines where it's used
- SundayHackTheBox