Improved process detections by monitoring new processes execution. It allow us to know the path of a process before a socket is opened. Closes #617 Other improvements: - If we fail to retrieve the path of a process, then we'll use the comm name of the connection/process. - Better kernel connections detection. - If debugfs is not loaded, we'll try to mount it, to allow to use eBPF monitor method. Future work (help wanted): - Extract command line arguments from the kernel (sys_execve, or mm struct). - Monitor other functions (execveat, clone*, fork, etc). - Send these events to the server (GUI), and display all the commands an application has executed.
104 lines
3 KiB
Go
104 lines
3 KiB
Go
package procmon
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/evilsocket/opensnitch/daemon/log"
|
|
"github.com/evilsocket/opensnitch/daemon/procmon/audit"
|
|
)
|
|
|
|
func getPIDFromAuditEvents(inode int, inodeKey string, expect string) (int, int) {
|
|
audit.Lock.RLock()
|
|
defer audit.Lock.RUnlock()
|
|
|
|
auditEvents := audit.GetEvents()
|
|
for n := 0; n < len(auditEvents); n++ {
|
|
pid := auditEvents[n].Pid
|
|
if inodeFound("/proc/", expect, inodeKey, inode, pid) {
|
|
return pid, n
|
|
}
|
|
}
|
|
for n := 0; n < len(auditEvents); n++ {
|
|
ppid := auditEvents[n].PPid
|
|
if inodeFound("/proc/", expect, inodeKey, inode, ppid) {
|
|
return ppid, n
|
|
}
|
|
}
|
|
return -1, -1
|
|
}
|
|
|
|
// GetPIDFromINode tries to get the PID from a socket inode following these steps:
|
|
// 1. Get the PID from the cache of Inodes.
|
|
// 2. Get the PID from the cache of PIDs.
|
|
// 3. Look for the PID using one of these methods:
|
|
// - ftrace: listening processes execs/exits from /sys/kernel/debug/tracing/
|
|
// - audit: listening for socket creation from auditd.
|
|
// - proc: search /proc
|
|
//
|
|
// If the PID is not found by one of the 2 first methods, it'll try it using /proc.
|
|
func GetPIDFromINode(inode int, inodeKey string) int {
|
|
found := -1
|
|
if inode <= 0 {
|
|
return found
|
|
}
|
|
start := time.Now()
|
|
|
|
expect := fmt.Sprintf("socket:[%d]", inode)
|
|
if cachedPidInode := inodesCache.getPid(inodeKey); cachedPidInode != -1 {
|
|
log.Debug("Inode found in cache: %v %v %v %v", time.Since(start), inodesCache.getPid(inodeKey), inode, inodeKey)
|
|
return cachedPidInode
|
|
}
|
|
|
|
cachedPid, pos := pidsCache.getPid(inode, inodeKey, expect)
|
|
if cachedPid != -1 {
|
|
log.Debug("Socket found in known pids %v, pid: %d, inode: %d, pos: %d, pids in cache: %d", time.Since(start), cachedPid, inode, pos, pidsCache.countItems())
|
|
pidsCache.sort(cachedPid)
|
|
inodesCache.add(inodeKey, "", cachedPid)
|
|
return cachedPid
|
|
}
|
|
|
|
if MethodIsAudit() {
|
|
if aPid, pos := getPIDFromAuditEvents(inode, inodeKey, expect); aPid != -1 {
|
|
log.Debug("PID found via audit events: %v, position: %d", time.Since(start), pos)
|
|
return aPid
|
|
}
|
|
} else if MethodIsFtrace() && IsWatcherAvailable() {
|
|
forEachProcess(func(pid int, path string, args []string) bool {
|
|
if inodeFound("/proc/", expect, inodeKey, inode, pid) {
|
|
found = pid
|
|
return true
|
|
}
|
|
// keep looping
|
|
return false
|
|
})
|
|
}
|
|
if found == -1 || methodIsProc() {
|
|
found = lookupPidInProc("/proc/", expect, inodeKey, inode)
|
|
}
|
|
log.Debug("new pid lookup took (%d): %v", found, time.Since(start))
|
|
|
|
return found
|
|
}
|
|
|
|
// FindProcess checks if a process exists given a PID.
|
|
// If it exists in /proc, a new Process{} object is returned with the details
|
|
// to identify a process (cmdline, name, environment variables, etc).
|
|
func FindProcess(pid int, interceptUnknown bool) *Process {
|
|
if interceptUnknown && pid < 0 {
|
|
return NewProcess(0, "")
|
|
}
|
|
|
|
if proc := findProcessInActivePidsCache(uint64(pid)); proc != nil {
|
|
return proc
|
|
}
|
|
|
|
proc := NewProcess(pid, "")
|
|
if err := proc.GetInfo(); err != nil {
|
|
log.Error("[%d] FindProcess() error: %s", pid, err)
|
|
return nil
|
|
}
|
|
|
|
AddToActivePidsCache(uint64(pid), proc)
|
|
return proc
|
|
}
|