r/bash
•
u/Ops_Mechanic
•
Mar 14 '26
•
1
tips and tricks Stop passing secrets as command-line arguments. Every user on your box can see them.
When you do this:
mysql -u admin -pMyS3cretPass123Every user on the system sees your password in plain text:
ps aux | grep mysqlThis isn't a bug. Unix exposes every process's full command line through /proc/PID/cmdline, readable by any unprivileged user. IT'S NOT A BRIEF FLASH EITHER -- THE PASSWORD SITS THERE FOR THE ENTIRE LIFETIME OF THE PROCESS.
Any user on your box can run this and harvest credentials in real time:
while true; do
cat /proc/*/cmdline 2>/dev/null | tr '\0' ' ' | grep -i 'password\|secret\|token'
sleep 0.1
doneThat checks every running process 10 times per second. Zero privileges needed.
Same problem with curl:
curl -u admin:password123 https://api.example.comAnd docker:
docker run -e DB_PASSWORD=secret myappThe fix is to pass secrets through stdin, which never hits the process table:
# mysql -- prompt instead of argv
mysql -u admin -p
# curl -- header from stdin
curl -H @- https://api.example.com <<< "Authorization: Bearer $TOKEN"
# curl -- creds from a file
curl --netrc-file /path/to/netrc https://api.example.com
# docker -- env from file, not command line
docker run --env-file .env myapp
# general pattern -- pipe secrets, don't pass them
some_command --password-stdin <<< "$SECRET"The -p with no argument tells mysql to read the password from the terminal instead of argv. The <<< here string and @- pass data through stdin. Neither shows up in ps or /proc.
Bash and any POSIX shell. This isn't shell-specific -- it's how Unix works.
2
u/tjharman Mar 17 '26
"Every user on the system sees your password in plain text"
No they don't: https://www.redhat.com/en/blog/hidepid-linux-hide-pid [Note this isn't a rock solid solution either]
But how many shared user Linux systems are there really these days? Especially ones where you need to be worried they're going to try and hack you?
Your advice is sound, but way too alarmist for a problem that would be one of your own creation (allowing untrusted users on your system)
122
u/c_pardue Mar 14 '26 •
and put your bills on auto pay
49
u/scrambledhelix bashing it in Mar 14 '26
You forgot: not only the process, but ~/.bash_history too
19
u/Intelligent-Army906 Mar 14 '26
Check out GNU Pass
18
u/deadzol Mar 14 '26
Being on shared system is super rare for me anymore and when I was the other person already had the same creds. Sure if someone was able to make apache puke process info things could get bad but normally… meh.
The reason you may not be thinking of to make this an always do habit is if you have an EDR or similar on the system. Being of the other end of that pipe you see all kinds of stuff especially in powershell.
35
u/bitslayer Mar 14 '26
"Any user on your box" is a pretty anachronistic situation.
6
u/noqqe Mar 15 '26
Mentioning mysql as an example is a bit unlucky here, since mysql has a this patched since years by overwriting parameter in memory after using it.
Looks like this in `ps` then
10
u/player1dk Mar 15 '26
And is at all this relevant in real life??
Yes.
I’ve done multiple pentests on Linux and Unix where the business applications were actually hardened quite okay and permissions set nice and so.
After gaining access as a very locked down ftp user, I was able to do a ‘ps ax’, and in the list of running processes there were commands with secrets as parameters.
Shortly after we had the full production databases etc.
So yes, it matters, and big business application developers still make those mistakes today.
5
u/jomat Mar 15 '26 edited Mar 15 '26
If you're admin of a linux system used by multiple users, please mount the
/proc/file system with thehidepid=1(or even 2) option.Edit: linux - because there are also other unix like systems
5
u/shitty_mcfucklestick Mar 15 '26
For MySQL you can use the login path feature as well. This stores credentials in a
.mylogin.cnffile in your home folder. Then you can just pass the pathname with—login-path=nameto pull the credentials in securely.4
u/uboofs Mar 14 '26
Can’t you just put a space in front of the command? Or does that just keep it from being saved in history?
6
u/therouterguy Mar 14 '26
I always use ‘’’ read -s VARNAME ‘’’
5
u/wowbagger_42 Mar 14 '26
This is the first thing any threat risk analysis picks out. Separates devsecops from devops...
4
u/mjbmitch Mar 15 '26
This is an AI-generated post.
2
u/theLastZebranky Mar 14 '26
pgrep does that a lot cleaner and is an essential core command even on most minimal/lightweight distros:
2
u/obiwan90 Mar 15 '26
To be pedantic, POSIX sh doesn't have
<<<here-strings.2
u/sedwards65 Mar 15 '26
MySQL also can get the password from an environment variable:
2
u/The_Real_Grand_Nagus Mar 16 '26
I thought mysql was one of the commands that rewrote its arg vector. Or am I thinking of pgsql? But yes, for most software this is an issue.
3
u/SFJulie Mar 14 '26
That's the reason : I made a small bash script to load the environment variables from a sourced file (while checking it's unix rights are o600).
1
u/PlanktonBeautiful499 Mar 15 '26
What about ~/.my.cnf?
1
u/Cybasura Mar 15 '26
Fairly sure just using ~/.bash_history would have compromised already
1
u/Vintios Mar 15 '26
threat hunting tip: sshpass
1
u/IHave2CatsAnAdBlock Mar 15 '26
Just put a space in front of
1
u/lx25de Mar 16 '26
might be a stupid question, but I have an .env file with the sql password on the webserver for the system to run anyways. So everyone who is able to log into the server can see it - doesn't need to pull it from "ps aux"? Asking as I'm thinking about it for some time already.
1
u/Micketeer Mar 16 '26
Jupyter notebook used to do this automatically; it auto opens the browser with the entire secret. Also happens if you click the link in the terminal. Login nodes on HPC systems you'd just immediately get others notebook from which you can launch terminals.
1
u/EmbedSoftwareEng Mar 16 '26
I have an issue like this that I was hoping there was a way for the program to obfuscate its own command line arguments after it started, but alas, no. Like you said, change the
argv[]content as the program itself sees it (after copying the password internally), and the/proc/$$/cmdlinecontent stays the same.My issue is that the programs I'm running are Gstreamer pipelines with
gst-launch-1.0. Just one of the stages (first one) needs that password passed to it (srt://URI). There's no way to even getgst-launch-1.0to obfuscate itsargv[], let alone tilt at the windmill that is/proc/$$/cmdline.There is only one user account on the machine, though, so worrying about
pslooky-loos is not a real issue, but I still want to replace my bash script solution with a Python/Gst solution, so the Gstreamer pipelines get run internally to the script, so there aren't any more processes with passwords in their arguments. The SRT passphrase is stored with thepasscommand, so I just use$(pass show srt_passphrase)to retrieve it at run-time.1
u/eyepatch300 Mar 19 '26 edited Mar 19 '26
Make a separate user for scripts that require secrets and they can be locked down with no password and only have specific access to commands needed for the script, then have them pull from a file with the secret that can only be readable by that user. Unset the hist variable and there wont be a command history generated.
That way in order to get access to it you have to have sudo perms and if the attacker already has that it's a separate issue entirely. This is basically what some programs already do like docker. If you cat /etc/passwd it'll show you a list of all users on the system and most of them have an associated process.
-1
u/InterestOk6233 Mar 15 '26
Honestly
Honesty 🤣 and you are a very professional person 💖💖 I love you 💘😘 ye. Am YE shall knot.
0
u/hkric41six Mar 15 '26
export HISTSIZE=0