linux中隐藏进程信息,不让ps, top, htop 等命令看到其他账号执行的命令

为什么会想到这个, 为什么要隐藏进程信息呢?
在工作中,jenkins 挂载了一个slave的服务器, 这台服务器上有多个员工用来编译android使用.
假设jenkins挂载的slave使用的是buildfarm这样命名的一个账号, 在jenkins上执行了比较敏感
的一些命令,例如命令中带有帐号密码等信息, 这样你执行的时候就会被其他 员工看到,这样就有
安全方面的风险了. 如果你隐藏掉进程信息, 其他员工就看不到你命令是什么,看不到你命令带的
什么参数等等了.

Linux hide processes from other users and ps command

原始参考
https://www.cyberciti.biz/faq/linux-hide-processes-from-other-users/

run a multi-user system. Most users access resources using SSH client.
How can I stop leaking process information to all users on Linux operating systems?
How do I prevent users from seeing processes that do not belong to them on a
Debian/Ubuntu/RHEL/CentOS Linux server? Is there any way to hide other users
process when running ps command?

If you are using Linux kernel version 3.2+ (or RHEL/CentOS v6.5+ above) you
can hide process from other users. Only root can see all process and user only
see their own process. All you have to do is remount the /proc filesystem with
the Linux kernel hardening hidepid option. This hides process from all other
commands such as ps, top, htop, pgrep and more.

Linux hide processes from other users using hidepid option
This option defines how much info about processes we want to be available for non-owners. The values are as follows:

1
2
3
4
5
6
7
8
9
10
hidepid=0 – The old behavior – anybody may read all world-readable /proc/PID/* files (default).

hidepid=1 – It means users may not access any /proc/<pid>/ directories, but their own.
Sensitive files like cmdline, sched*, status are now protected against other users.

hidepid=2 It means hidepid=1 plus all /proc/PID/ will be invisible to other users.
It compicates intruder’s task of gathering info about running processes, whether some
daemon runs with elevated privileges, whether another user runs some sensitive program,
whether other users run any program at all, etc.

Linux kernel protection: Hiding processes from other users
Type the following mount command:

1
root# mount -o remount,rw,nosuid,nodev,noexec,relatime,hidepid=2 /proc

Edit /etc/fstab using a text editor such as nano command/vim command, enter:

1
2
3
4
root# vi /etc/fstab
Update/append/modify proc entry as follows so that protection get enabled automatically at server boot-time:

proc /proc proc defaults,nosuid,nodev,noexec,relatime,hidepid=2 0 0

Save and close the file. Where security mount options are as follows:

mount 选项解释

1
2
3
4
nosuid : Do not allow set-user-ID or set-group-ID bits to take effect.
nodev : Do not interpret character or block special devices on the file system.
noexec : Do not permit direct execution of any binaries on the mounted filesystem.
hidepid: Option defines how much info about processes hidden.

Tip: Dealing with apps that breaks when you implement this technique
You need to use gid=VALUE_HERE option:

gid=XXX defines a group that will be able to gather all processes’ info (as in hidepid=0 mode).
This group should be used instead of putting nonroot user in sudoers file or something.
However, untrusted users (like daemons, etc.) which are not supposed to monitor the tasks
in the whole system should not be added to the group.

So add the user called monapp to group (say admin) that want to see process information and
mount /proc as follows in /etc/fstab:
https://www.cyberciti.biz/faq/howto-linux-add-user-to-group/

1
2
3
4
5
6
proc /proc proc defaults,hidepid=2,gid=admin 0 0 

or:

proc /proc proc defaults,hidepid=2,gid=sudo 0 0

Conclusion

Now you know how to hide Linux processes from other users and commands like ps, top, htop and others.
For more information see the following URLs:
procfs: add hidepid= and gid= mount options https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0499680a42141d86417a8fbaa8c8db806bea1201
40 Linux Server Hardening Security Tips https://www.cyberciti.biz/tips/linux-security.html

下面是关于kernel增加hidepid功能的一个提交补丁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0499680a42141d86417a8fbaa8c8db806bea1201
procfs: add hidepid= and gid= mount options
Add support for mount options to restrict access to /proc/PID/
directories. The default backward-compatible "relaxed" behaviour is left
untouched.

The first mount option is called "hidepid" and its value defines how much
info about processes we want to be available for non-owners:

hidepid=0 (default) means the old behavior - anybody may read all
world-readable /proc/PID/* files.

hidepid=1 means users may not access any /proc/<pid>/ directories, but
their own. Sensitive files like cmdline, sched*, status are now protected
against other users. As permission checking done in proc_pid_permission()
and files' permissions are left untouched, programs expecting specific
files' modes are not confused.

hidepid=2 means hidepid=1 plus all /proc/PID/ will be invisible to other
users. It doesn't mean that it hides whether a process exists (it can be
learned by other means, e.g. by kill -0 $PID), but it hides process' euid
and egid. It compicates intruder's task of gathering info about running
processes, whether some daemon runs with elevated privileges, whether
another user runs some sensitive program, whether other users run any
program at all, etc.

gid=XXX defines a group that will be able to gather all processes' info
(as in hidepid=0 mode). This group should be used instead of putting
nonroot user in sudoers file or something. However, untrusted users (like
daemons, etc.) which are not supposed to monitor the tasks in the whole
system should not be added to the group.

hidepid=1 or higher is designed to restrict access to procfs files, which
might reveal some sensitive private information like precise keystrokes
timings:

http://www.openwall.com/lists/oss-security/2011/11/05/3

hidepid=1/2 doesn't break monitoring userspace tools. ps, top, pgrep, and
conky gracefully handle EPERM/ENOENT and behave as if the current user is
the only user running processes. pstree shows the process subtree which
contains "pstree" process.

Note: the patch doesn't deal with setuid/setgid issues of keeping
preopened descriptors of procfs files (like
https://lkml.org/lkml/2011/2/7/368). We rely on that the leaked
information like the scheduling counters of setuid apps doesn't threaten
anybody's privacy - only the user started the setuid program may read the
counters.