From 3e49a044d22635157916651f0acb5a062397b34b Mon Sep 17 00:00:00 2001 From: We-unite <3205135446@qq.com> Date: Fri, 9 Aug 2024 13:56:37 +0800 Subject: Add db structure, fix filePath, start filtering This commit I made several changes: - Use structure instead of simple bson.M(interface{}). bson.M has some shortcomings: 1) It makes the database in chaos and hard to read, but this's not important; 2) Some entrys may has more or less content than others, which makes it hard to decode and filt. So I design new data structure to encode and decode. Hopes that there's no bugs. - Fix the way to calculate file path. The original method is to add all the PATH entries together, that's totally wrong! PATH entry has several types, as it shows in "objtype". I can't find it in the kernel src code, so what i know is just "PARENT" means the dir the file is in, while the filename itself has the path, so we whould ignore all "PARENT"s. When the src code is found, we should check it again. - Fix bugs in updating. The update function of mongodb is set to required to has a '$' such as 'set'/'push', so when we update a whole doc, we should use replace but not update function. And, we should never ignore the error infomation it gives us. Hope that there's no more bugs for this Big Change. Now its' time to write filter as well as viewer. Best wishes with NO BUGS! --- listener/basefunc.go | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 listener/basefunc.go (limited to 'listener/basefunc.go') diff --git a/listener/basefunc.go b/listener/basefunc.go new file mode 100644 index 0000000..2f39507 --- /dev/null +++ b/listener/basefunc.go @@ -0,0 +1,129 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "time" +) + +func figureOutSyscalls() error { + cmd := exec.Command("ausyscall", "--dump") + stdout, err := cmd.StdoutPipe() + if err != nil { + return err + } + + if err := cmd.Start(); err != nil { + return err + } + + scanner := bufio.NewScanner(stdout) + for i := 0; scanner.Scan(); i++ { + if i == 0 { + continue + } + line := scanner.Text() + parts := strings.Split(line, "\t") + if len(parts) != 2 { + return fmt.Errorf("invalid ausyscall format") + } + num, err := strconv.Atoi(parts[0]) + if err != nil { + return err + } + syscallTable[num] = parts[1] + } + + if err := scanner.Err(); err != nil { + return err + } + if err := cmd.Wait(); err != nil { + return err + } + return nil +} + +func getPid() (int, error) { + // 指定要搜索的关键词 + keyword := "/usr/bin/containerd" + + // 获取/proc目录下的所有子目录 + procDir, err := filepath.Glob("/proc/*") + if err != nil { + return 0, err + } + + // 遍历子目录,查找包含关键词的进程 + for _, dir := range procDir { + pid, err := strconv.Atoi(filepath.Base(dir)) + if err != nil { + continue // 跳过非PID的目录 + } + + // 检查进程是否包含关键词 + if containsKeyword(pid, keyword) { + return pid, nil + } + } + err = fmt.Errorf("Error: no containerd process found.") + return 0, err +} + +func containsKeyword(pid int, keyword string) bool { + // 构造完整的进程命令路径 + cmdPath := fmt.Sprintf("/proc/%d/cmdline", pid) + + // 打开文件 + file, err := os.Open(cmdPath) + if err != nil { + return false + } + defer file.Close() + + // 读取文件内容 + scanner := bufio.NewScanner(file) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + line := scanner.Text() + if strings.Contains(line, keyword) { + return true + } + } + return false +} + +func getTimeFromStr(timeStr string) (time.Time, error) { + timestampFloat, err := strconv.ParseFloat(timeStr, 64) + if err != nil { + return time.Unix(0, 0), err + } + secs := int64(timestampFloat) + nsecs := int64((timestampFloat - float64(secs)) * 1e9) + + // 只精确到毫秒就够了 + t := time.Unix(secs, nsecs).Truncate(time.Millisecond) + return t, nil +} + +func hexToAscii(hexString string) string { + bytes := []byte{} + for i := 0; i < len(hexString); i += 2 { + hexPair := hexString[i : i+2] + // 将十六进制数转换为十进制数 + decimal, err := strconv.ParseInt(hexPair, 16, 8) + if err != nil { + return "Invalid hex string" + } + char := byte(decimal) + bytes = append(bytes, char) + } + + asciiString := strings.ReplaceAll(string(bytes), "\000", " ") + + return asciiString +} -- cgit v1.2.3-70-g09d2