我知道在 Linux 机器上,如果一个进程能够获得 root 权限,它就可以访问它通常无法访问的部分文件系统。
我想问一个 root 的 Linux 进程是否可以检查系统上任何其他进程的内存?因此,如果我有一个进程在其堆或堆栈中包含秘密,那么根进程是否能够访问它,如果是,它将如何做到这一点?
(无论是谁回答,也可以考虑 root shell 进程;我只关心具有 root 访问权限的进程)
答案1
是的,你可以,例如,请参阅这个答案 https://stackoverflow.com/questions/12977179/reading-living-process-memory-without-interrupting-it
或玩/dev/<pid>/mem
或/dev/kmem
像这样
例如使用这段代码,根用户可以读取主机上任何进程的内存。
#! /usr/bin/env python
import re
import sys
print(sys.argv[1] + ".dump")
maps_file = open("/proc/"+ sys.argv[1] + "/maps", 'r')
mem_file = open("/proc/" + sys.argv[1] + "/mem", 'rb', 0)
output_file = open(sys.argv[1] + ".dump", 'wb')
for line in maps_file.readlines(): # for each mapped region
m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r])', line)
if m.group(3) == 'r': # if this is a readable region
start = int(m.group(1), 16)
end = int(m.group(2), 16)
mem_file.seek(start) # seek to region start
chunk = mem_file.read(end - start) # read region contents
output_file.write(chunk) # dump contents to standard output
maps_file.close()
mem_file.close()
output_file.close()
ptrace
另请参阅和的使用gdb
,这些工具旨在实时执行此操作。
答案2
是的。
根功能已被破坏。现在,一个进程可以拥有它们的子集(包括根没有)。
通过查看功能手册页,我们可以看到 root 可以(通常)做什么。
我包括一个很少这里:
CAP_DAC_OVERRIDE
Bypass file read, write, and execute permission checks. (DAC
is an abbreviation of "discretionary access control".)
CAP_KILL
Bypass permission checks for sending signals (see kill(2)).
This includes use of the ioctl(2) KDSIGACCEPT operation.
CAP_NET_BIND_SERVICE
Bind a socket to Internet domain privileged ports (port
numbers less than 1024).
CAP_SYS_MODULE
* Load and unload kernel modules (see init_module(2) and
delete_module(2));
* in kernels before 2.6.25: drop capabilities from the system-
wide capability bounding set.
CAP_SYS_TIME
Set system clock (settimeofday(2), stime(2), adjtimex(2)); set
real-time (hardware) clock.
CAP_SYS_RAWIO
* Perform I/O port operations (iopl(2) and ioperm(2));
* access /proc/kcore;
* employ the FIBMAP ioctl(2) operation;
* open devices for accessing x86 model-specific registers
(MSRs, see msr(4));
* update /proc/sys/vm/mmap_min_addr;
* create memory mappings at addresses below the value
specified by /proc/sys/vm/mmap_min_addr;
* map files in /proc/bus/pci;
* open /dev/mem and /dev/kmem;
* perform various SCSI device commands;
* perform certain operations on hpsa(4) and cciss(4) devices;
* perform a range of device-specific operations on other
devices.
此处显示的模块CAP_SYS_MODULE
可用于加载可以执行此操作的内核模块;CAP_SYS_RAWIO
可以用来打开/dev/mem
;还有其他具有其他功能的方法,包括ptrace
.