Guest User

Untitled

a guest
Apr 26th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "strings"
  8. )
  9.  
  10. // Map to hold a list of "currently running" processes.
  11. var processes = make(map[string]string)
  12.  
  13. // Build a map of userid to username
  14. var userMap = make(map[string]string)
  15. func buildUserIdMap() {
  16. passwdContents, err := ioutil.ReadFile("/etc/passwd")
  17. if err != nil {
  18. return
  19. }
  20. lines := strings.Split(string(passwdContents), "\n")
  21. for _, line := range lines {
  22. if len(line) < 5 {
  23. continue
  24. }
  25. elements := strings.Split(line, ":")
  26. name := elements[0]
  27. uid := elements[2]
  28. userMap[uid] = name
  29. }
  30. }
  31.  
  32. // Function to check for new processes.
  33. func checkForNewProcesses() (newProcesses, processOwners map[string]string) {
  34.  
  35. // Get a list of all files/dirs inside /proc
  36. files, err := ioutil.ReadDir("//proc")
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40.  
  41. // Create a map to hold new process info
  42. newProcesses = make(map[string]string)
  43. processOwners = make(map[string]string)
  44.  
  45. // Iterate through the list of files/dirs
  46. for _, file := range files {
  47. // If this is a directory try to process it
  48. if file.IsDir() {
  49. name := file.Name()
  50.  
  51. // Does it already exist in the list of processes we've seen?
  52. if _, ok := processes[name]; ok {
  53. continue
  54. }
  55. // If not add it to the list of new processes
  56. cmdline, err := ioutil.ReadFile(fmt.Sprintf("//proc//%s//cmdline", name))
  57. if err != nil {
  58. continue
  59. }
  60.  
  61. // Split into strings whenever we find a null byte
  62. parts := make([]string,0)
  63. start := 0
  64. for i := 0; i < len(cmdline); i++ {
  65. if cmdline[i] == 0x00 {
  66. parts = append(parts, string(cmdline[start:i]))
  67. start = i
  68. }
  69. }
  70.  
  71. processArgs := strings.Join(parts, " ")
  72. newProcesses[name] = processArgs
  73. processes[name] = processArgs //TODO: With the way this is currently used could just be a slice
  74.  
  75. // Try to get owner info
  76. statusline, err := ioutil.ReadFile(fmt.Sprintf("//proc//%s//status", name))
  77. if err != nil {
  78. continue
  79. }
  80.  
  81. lines := strings.Split(string(statusline), "\n")
  82. uid := strings.Split(lines[8], "\t")[1]
  83. processOwners[name] = userMap[uid]
  84. }
  85. }
  86. return
  87. }
  88.  
  89. func main() {
  90.  
  91. // Build the map to translate uid to a friendly name
  92. buildUserIdMap()
  93.  
  94. for {
  95. newProcesses, processOwners := checkForNewProcesses()
  96. for key, value := range newProcesses {
  97. log.Printf("[%5s]\t%s [owner=%s]\n", key, value, processOwners[key])
  98. }
  99. }
  100. }
Add Comment
Please, Sign In to add comment