Skip to content

Commit

Permalink
Fixed bugs on Data Analytics back-end parent process checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Jul 26, 2024
1 parent 148d6f4 commit 1dbcc6b
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 169 deletions.
202 changes: 100 additions & 102 deletions backend/data_analytics/proc/parent_unix.go
Original file line number Diff line number Diff line change
@@ -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”),
Expand All @@ -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
Expand All @@ -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
}

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
}
129 changes: 62 additions & 67 deletions backend/data_analytics/proc/parent_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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”),
Expand All @@ -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
Expand All @@ -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
}

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
}

0 comments on commit 1dbcc6b

Please sign in to comment.