diff --git a/backend/data_analytics/proc/parent_unix.go b/backend/data_analytics/proc/parent_unix.go index 78d5552..2e7a318 100644 --- a/backend/data_analytics/proc/parent_unix.go +++ b/backend/data_analytics/proc/parent_unix.go @@ -1,9 +1,10 @@ +//go:build !windows // +build !windows /* * This file is part of QLBase (https://github.com/nthnn/QLBase). * Copyright 2024 - Nathanne Isip - * + * * Permission is hereby granted, free of charge, * to any person obtaining a copy of this software * and associated documentation files (the “Software”), @@ -13,11 +14,11 @@ * sell copies of the Software, and to permit persons to * whom the Software is furnished to do so, subject to * the following conditions: - * + * * The above copyright notice and this permission notice * shall be included in all copies or substantial portions * of the Software. - * + * * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A @@ -29,102 +30,99 @@ * OR OTHER DEALINGS IN THE SOFTWARE. */ - package proc - - import ( - "fmt" - "io/ioutil" - "os" - "strconv" - "strings" - ) - - func isPHPProcess(name string) bool { - return name == "php" || name == "php-cgi" - } - - func IsParentProcessPHP() bool { - ppid := os.Getppid() - parentCommContent, err := ioutil.ReadFile( - fmt.Sprintf("/proc/%d/comm", ppid), - ) - - if err != nil { - return false - } - - parentProcessName := strings.TrimSpace(string(parentCommContent)) - if isPHPProcess(parentProcessName) { - return true - } - - parentCmdlineContent, err := ioutil.ReadFile( - fmt.Sprintf("/proc/%d/cmdline", ppid), - ) - if err == nil { - parentCmdline := strings.Join( - strings.Split( - string(parentCmdlineContent), - "\x00", - ), - " ", - ) - - if isPHPProcess(parentCmdline) { - return true - } - } - - ppidFileContent, err := ioutil.ReadFile( - fmt.Sprintf("/proc/%d/status", ppid), - ) - if err != nil { - return false - } - - var gppid int - ppidLines := strings.Split(string(ppidFileContent), "\n") - - for _, line := range ppidLines { - if strings.HasPrefix(line, "PPid:") { - gppid, err = strconv.Atoi(strings.Fields(line)[1]) - - if err != nil { - return false - } - - break - } - } - - parentCommContent, err = ioutil.ReadFile( - fmt.Sprintf("/proc/%d/comm", gppid), - ) - if err != nil { - return false - } - - if isPHPProcess(strings.TrimSpace(string(parentCommContent))) { - return true - } - - parentCmdlineContent, err = ioutil.ReadFile( - fmt.Sprintf("/proc/%d/cmdline", gppid), - ) - if err == nil { - parentCmdline := strings.Join( - strings.Split( - string(parentCmdlineContent), - "\x00", - ), - " ", - ) - - if strings.HasPrefix(parentCmdline, "/opt/lampp/bin/httpd") { - return true - } - } - - return false - } - \ No newline at end of file +package proc + +import ( + "fmt" + "io/ioutil" + "os" + "strconv" + "strings" +) + +func IsParentProcessPHP() bool { + ppid := os.Getppid() + httpd := "/opt/lampp/bin/httpd " + + parentCommContent, err := ioutil.ReadFile( + fmt.Sprintf("/proc/%d/comm", ppid), + ) + + if err != nil { + return false + } + + parentProcessName := strings.TrimSpace(string(parentCommContent)) + if strings.HasPrefix(parentProcessName, httpd) { + return true + } + + parentCmdlineContent, err := ioutil.ReadFile( + fmt.Sprintf("/proc/%d/cmdline", ppid), + ) + if err == nil { + parentCmdline := strings.Join( + strings.Split( + string(parentCmdlineContent), + "\x00", + ), + " ", + ) + + if strings.HasPrefix(parentCmdline, httpd) { + return true + } + } + + ppidFileContent, err := ioutil.ReadFile( + fmt.Sprintf("/proc/%d/status", ppid), + ) + if err != nil { + return false + } + + var gppid int + ppidLines := strings.Split(string(ppidFileContent), "\n") + + for _, line := range ppidLines { + if strings.HasPrefix(line, "PPid:") { + gppid, err = strconv.Atoi(strings.Fields(line)[1]) + + if err != nil { + return false + } + + break + } + } + + parentCommContent, err = ioutil.ReadFile( + fmt.Sprintf("/proc/%d/comm", gppid), + ) + if err != nil { + return false + } + + if strings.HasPrefix(strings.TrimSpace(string(parentCommContent)), httpd) { + return true + } + + parentCmdlineContent, err = ioutil.ReadFile( + fmt.Sprintf("/proc/%d/cmdline", gppid), + ) + if err == nil { + parentCmdline := strings.Join( + strings.Split( + string(parentCmdlineContent), + "\x00", + ), + " ", + ) + + if strings.HasPrefix(parentCmdline, httpd) { + return true + } + } + + return false +} diff --git a/backend/data_analytics/proc/parent_windows.go b/backend/data_analytics/proc/parent_windows.go index 7936ef6..921bb6e 100644 --- a/backend/data_analytics/proc/parent_windows.go +++ b/backend/data_analytics/proc/parent_windows.go @@ -4,7 +4,7 @@ /* * This file is part of QLBase (https://github.com/nthnn/QLBase). * Copyright 2024 - Nathanne Isip - * + * * Permission is hereby granted, free of charge, * to any person obtaining a copy of this software * and associated documentation files (the “Software”), @@ -14,11 +14,11 @@ * sell copies of the Software, and to permit persons to * whom the Software is furnished to do so, subject to * the following conditions: - * + * * The above copyright notice and this permission notice * shall be included in all copies or substantial portions * of the Software. - * + * * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A @@ -30,67 +30,62 @@ * OR OTHER DEALINGS IN THE SOFTWARE. */ - package proc - - import ( - "unsafe" - - "golang.org/x/sys/windows" - ) - - func IsParentProcessPHP() bool { - currentPid := windows.GetCurrentProcessId() - possiblePHP := []string{"php.exe", "php-cgi.exe"} - - hSnapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0) - if err != nil { - return false - } - defer windows.CloseHandle(hSnapshot) - - var processEntry windows.ProcessEntry32 - processEntry.Size = uint32(unsafe.Sizeof(processEntry)) - - findProcessByID := func(pid uint32) (ppid uint32, name string, found bool) { - if err := windows.Process32First(hSnapshot, &processEntry); err != nil { - return - } - - for { - if processEntry.ProcessID == pid { - return processEntry.ParentProcessID, windows.UTF16ToString(processEntry.ExeFile[:]), true - } - - if err := windows.Process32Next(hSnapshot, &processEntry); err != nil { - break - } - } - - return 0, "", false - } - - seenPIDs := make(map[uint32]bool) - for currentPid != 0 { - if _, seen := seenPIDs[currentPid]; seen { - break - } - - seenPIDs[currentPid] = true - ppid, parentProcessName, found := findProcessByID(currentPid) - - if !found { - break - } - - for _, phpName := range possiblePHP { - if parentProcessName == phpName { - return true - } - } - - currentPid = ppid - } - - return false - } - \ No newline at end of file +package proc + +import ( + "unsafe" + + "golang.org/x/sys/windows" +) + +func IsParentProcessPHP() bool { + currentPid := windows.GetCurrentProcessId() + hSnapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0) + + if err != nil { + return false + } + defer windows.CloseHandle(hSnapshot) + + var processEntry windows.ProcessEntry32 + processEntry.Size = uint32(unsafe.Sizeof(processEntry)) + + findProcessByID := func(pid uint32) (ppid uint32, name string, found bool) { + if err := windows.Process32First(hSnapshot, &processEntry); err != nil { + return + } + + for { + if processEntry.ProcessID == pid { + return processEntry.ParentProcessID, windows.UTF16ToString(processEntry.ExeFile[:]), true + } + + if err := windows.Process32Next(hSnapshot, &processEntry); err != nil { + break + } + } + + return 0, "", false + } + + seenPIDs := make(map[uint32]bool) + for currentPid != 0 { + if _, seen := seenPIDs[currentPid]; seen { + break + } + + seenPIDs[currentPid] = true + ppid, parentProcessName, found := findProcessByID(currentPid) + + if !found { + break + } + + if parentProcessName == "httpd.exe" { + return true + } + currentPid = ppid + } + + return false +}