From e013c878909f707ebae952796fd9c157665df4df Mon Sep 17 00:00:00 2001 From: Carter Li Date: Fri, 19 Jan 2024 11:00:25 +0800 Subject: [PATCH] TerminalShell (Linux): detect real exe path --- src/detection/terminalshell/terminalshell_linux.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index ed40f3773..d25e959be 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -31,16 +31,25 @@ static void getProcessInformation(pid_t pid, FFstrbuf* processName, FFstrbuf* ex #ifdef __linux__ - char cmdlineFilePath[64]; - snprintf(cmdlineFilePath, sizeof(cmdlineFilePath), "/proc/%d/cmdline", (int)pid); + char filePath[64]; + snprintf(filePath, sizeof(filePath), "/proc/%d/cmdline", (int)pid); - if(ffAppendFileBuffer(cmdlineFilePath, exe)) + if(ffAppendFileBuffer(filePath, exe)) { ffStrbufTrimRightSpace(exe); ffStrbufRecalculateLength(exe); //Trim the arguments ffStrbufTrimLeft(exe, '-'); //Login shells start with a dash } + snprintf(filePath, sizeof(filePath), "/proc/%d/exe", (int)pid); + ffStrbufEnsureFixedLengthFree(exePath, PATH_MAX); + ssize_t length = readlink(filePath, exePath->chars, exePath->allocated - 1); + if (length > 0) // doesn't contain trailing NUL + { + exePath->chars[length + 1] = '\0'; + exePath->length = (uint32_t) length; + } + #elif defined(__APPLE__) size_t len = 0;