Best way to hide password in bash script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Best way to hide password in bash script?
# 15  
Old 04-13-2010
Yes, I was referring to argv[] manipulation. Using memset() on argv[1]
* works with Linux 2.6.22 (OpenSuSE) using a non-suid ps, and with ps run as root
* does not work on HP-UX 11.31 using a non-suid ps
* does not work on FreeBSD 8.0 using a non-suid ps (as root)
* does not work on OpenSolaris 2009.06 using a non-suid ps

Seems like it's not dependent on the permissions of ps, but rather on the system.

Last edited by pludi; 04-13-2010 at 10:53 AM..
# 16  
Old 04-13-2010
Hey, pludi:

memset() on the string pointed to by argv[1] works on osx 10.4.11, where modifying the pointer in argv[1] directly (as my code was doing) failed.

Incidentally, as an unprivileged user, copying the suid ps binary so that it is no longer suid seems to work fine. I only tested a few options, however, so it's possible there's a privileged code path that I did not visit, just waiting to blow things up, but if collecting the process info did not require privilege -- I took a brief peek at Apple's ps.c, and it's using sysctl to gather the info; so, not surprising that privilige is not required for that step -- I don't see what would.

Regards,
Alister

Last edited by alister; 04-13-2010 at 07:07 PM..
# 17  
Old 04-13-2010
Quote:
Originally Posted by Corona688
expect is an ugly, last-resort solution for things that won't cooperate any other way, and tends to make insecure and unreliable solutions. If you think ssh can't be automated without it, you've learned the wrong lesson; there's a much better and more secure method built into ssh itself: authorized keys. It doesn't make much sense to say "my account policy says I can't automate this; therefore I'll automate it anyway, avoid the proper way since that's not allowed, and use the least secure and most contrived way imaginable instead!" Somehow I don't think that's what their security policy had in mind.
Hi Corona, I think you got it wrong. I did mention that I knew authorized keys already (and I have been using it quite a while), but in my opinion it is not automation as expect (yes, I thought it wrong you may think), and that I did not hear of expect before.

Anyway, let us try not to diverge the main question of my post: how to fool ps or hide password in my script.

Thanks,

D.

---------- Post updated at 06:08 PM ---------- Previous update was at 06:02 PM ----------

Quote:
Originally Posted by virgil
1. Put the script in a separate expect script file, and use your bash script as a wrapper script. From bash, call the expect file. This will prevent the expect commands from appearing in ps.
Yes, I did. I tried it before combining my scripts to the script I posted. And the password still showed with ps.
Quote:
Originally Posted by virgil
2. Instead of hardcoding the password in the file, store the password in a separate file and protect the file so that only the authorised users can access it.
I thought of this already, and it actually is a solution that a lot people recommend instead of passing password through arguments. But the permission of the file can be easily change by root, hence can be seen easily right?
Quote:
Originally Posted by virgil
3. Instead of using a password, encode the password algorithm instead. For example your password may be month-based, e.g. mypasswdJul2010, so put the algorithm in the code to generate it.
And finally the decoded pass will be still passed to the argument of ssh or expect, or we can pass the encoded one?

Thanks,

D.

---------- Post updated at 06:14 PM ---------- Previous update was at 06:08 PM ----------

Quote:
Originally Posted by pludi
Yes, I was referring to argv[] manipulation. Using memset() on argv[1]
* works with Linux 2.6.22 (OpenSuSE) using a non-suid ps, and with ps run as root
* does not work on HP-UX 11.31 using a non-suid ps
* does not work on FreeBSD 8.0 using a non-suid ps (as root)
* does not work on OpenSolaris 2009.06 using a non-suid ps

Seems like it's not dependent on the permissions of ps, but rather on the system.
I found a post describing similar method, but I have no idea how to apply this to change (or fake) the name of the built-in process (such as expect or ssh) given its pid. If somehow ssh (or its argument) can be changed (or faked) then it is the solution, I think.

D.

---------- Post updated at 06:18 PM ---------- Previous update was at 06:14 PM ----------

Quote:
Originally Posted by alister
Hey, pludi:

memset() on the string pointed to by argv[1] works on osx 10.4.11, where modifying the pointer in argv[1] directly (as my code was doing) failed.

Incidentally, as an unprivileged user, copying the suid ps binary so that it is no longer suid seems to work fine. I only tested a few options, however, so it's possible there's a privileged code path that I did not visit, just waiting to blow things up, but if collecting the process info did not require privilege -- I took a brief peek at Apple's ps.c, and it's using sysctl to gather the info; so, not surprising that privilige is not required for that step -- I don't see what would.

Regards,
Alister
Hi Alister,

I tried your code (modifying argv[1]) and it did not work on 10.6.3 either. Do you mean modifying memset() can change output of ps for built-in processes (xterm, expect, ssh, bash...) in my first post?

Thanks,

D.
# 18  
Old 04-13-2010
Quote:
Originally Posted by dukevn
Hi Corona, I think you got it wrong. I did mention that I knew authorized keys already (and I have been using it quite a while), but in my opinion it is not automation as expect
In what manner is it worse do you think? It may not be easy to explain to your customers or superiors but it is fully automatic and secure. Your method is not and cannot be made secure. The inventor of ssh knew people would try to butcher in stored plaintext passswords anyway and designed it to prevent that.
Quote:
Anyway, let us try not to diverge the main question of my post: how to fool ps or hide password in my script.
Not until you understand that the reason you have to use expect at all is because you're brute-forcing an insecure solution ssh was expressly designed to not just prevent but make obsolete.

Last edited by Corona688; 04-13-2010 at 07:47 PM..
# 19  
Old 04-13-2010
Quote:
Originally Posted by dukevn
Hi Alister,

I tried your code (modifying argv[1]) and it did not work on 10.6.3 either. Do you mean modifying memset() can change output of ps for built-in processes (xterm, expect, ssh, bash...) in my first post?

Thanks,

D.
I meant that the following works on 10.4.11, for clearing out all argument strings pointed to from argv (which usually show up in ps output):
Code:
#include <string.h>
#include <unistd.h>

int
main(int argc, char **argv) {
    while (--argc) {
        memset(argv[argc], 0, strlen(argv[argc]));
    }
    sleep(30u);
    return 0;
}

You cannot do this from a shell script, though. Also, even if you could, there is a race condition present. The args could be visible from ps if ps is run at just the right time and the kernel's process scheduler conspires against you.

As Corona has made clear, there is no secure way to accomplish what you're trying to do. The best you can manage is a few obstacles to keep the undetermined at bay.

Regards,
Alister
# 20  
Old 04-14-2010
Quote:
Originally Posted by alister
I meant that the following works on 10.4.11, for clearing out all argument strings pointed to from argv (which usually show up in ps output):
Code:
#include <string.h>
#include <unistd.h>

int
main(int argc, char **argv) {
    while (--argc) {
        memset(argv[argc], 0, strlen(argv[argc]));
    }
    sleep(30u);
    return 0;
}

You cannot do this from a shell script, though. Also, even if you could, there is a race condition present. The args could be visible from ps if ps is run at just the right time and the kernel's process scheduler conspires against you.

As Corona has made clear, there is no secure way to accomplish what you're trying to do. The best you can manage is a few obstacles to keep the undetermined at bay.

Regards,
Alister
Got it. Thanks Alister.

D.
# 21  
Old 04-14-2010
One other point - this kind of thing is such a common question that you may as well be directed to a FAQ about UNIX passwords:
Unix Programming Frequently Asked Questions - 4. System Information You want section 4.2 Passwords

... to make your life easier consider reading all of the FAQ pages.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to hide password in shell script?

I am writing a shell script for sql loader (just copy part of the code) : For security reason, I have to put the below loginName and password into another separate file instead of in the same file of this script. Anyone can give me a hand. Thanks. Shell Script :... (12 Replies)
Discussion started by: Jaewong
12 Replies

2. Shell Programming and Scripting

Hide password from processes in Linux

i have a shell script which calls a java program with username and password arguments. #!/bin/ksh #set some classpaths here #finally run the command java com.test -u $U -p $P Now when i run it, the password shows up in the list of processes. I am not the admin on the server so cant... (3 Replies)
Discussion started by: ariesb2b
3 Replies

3. Shell Programming and Scripting

Make a password protected bash script resist/refuse “bash -x” when the password is given

I want to give my long scripts to customer. The customer must not be able to read the scripts even if he has the password. The following command locks and unlocks the script but the set +x is simply ignored. The code: read -p 'Script: ' S && C=$S.crypt H='eval "$((dd if=$0 bs=1 skip=//|gpg... (7 Replies)
Discussion started by: frad
7 Replies

4. Shell Programming and Scripting

Expect Script - Hide password from process table

i have an expect script that runs like this: /usr/bin/expect -f /home/skysmart/commandstoexecute.sh host2.net b$4aff Skysmart when i run this command, and i do a ps -ef and egrep for expect, i see the exact line in the process table and it shows my password for the world to see. how can i... (2 Replies)
Discussion started by: SkySmart
2 Replies

5. Shell Programming and Scripting

How to hide/encrypt password in script?

Hi I have following problem Im writing a script (in bash ) , where need to be written login & passwd for databas client . Its need to in following form login passwd@dbhostname . The problem is so anybody can read it so the passwd & login are visible and thats not very safety . Can... (8 Replies)
Discussion started by: kvok
8 Replies

6. UNIX for Dummies Questions & Answers

How can i hide username/password

hi all, i run sqlplus command on unix(HP-UX) like "sqlplus username/password@serverA @deneme.sql" but when someone run "ps -ef | grep sqlplus", it can see my username and password :( How can i hide username and password. thanx. (1 Reply)
Discussion started by: temhem
1 Replies

7. Shell Programming and Scripting

How Do I Hide the Password in a Script

Hi, I am writing a UNIX .ksh script and need to send the login password of the login id that is executing the script to a command that I am executing in the script. I don't want that password to be seen by anyone except whoever is executing the script. Does anyone know how I can accomplish... (6 Replies)
Discussion started by: samd
6 Replies

8. Shell Programming and Scripting

Want to hide password

All, In my script I am calling another script.. in that script I need to enter a password. Problem is that everyone is able to see the password when I enter that. Is there any way that when i enter that password it should not display or may look like *******. Or if there any other way that I... (1 Reply)
Discussion started by: arpitk
1 Replies

9. Shell Programming and Scripting

How to hide password on Linux?

Hi falks, I have the following ksh code: echo "Enter VS Admin password:" oldstty=`stty -g` stty -echo intr '$-' read password stty $oldstty echo This code ask from a user to enter his password. The OS suppose to hide the entering of the... (2 Replies)
Discussion started by: nir_s
2 Replies

10. Programming

hide password typing

I am doing a project in C program which requires to type in password in Unix terminal. Does anybody know how to shade or not output any words typed by user in the terminal? I use the function scan() to read typing from user. Thanks in advance. (2 Replies)
Discussion started by: ivancheung
2 Replies
Login or Register to Ask a Question