forked from ycrash/yc-360-script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudo_others.go
More file actions
64 lines (58 loc) · 1.13 KB
/
sudo_others.go
File metadata and controls
64 lines (58 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//go:build !windows
// +build !windows
package shell
import (
"bufio"
"fmt"
"os"
"os/exec"
"shell/logger"
"strconv"
"strings"
)
type SudoHooker struct {
PID int
}
func (s SudoHooker) After(command *exec.Cmd) {
}
func (s SudoHooker) Before(command Command) (result Command) {
uid, err := GetUid(s.PID)
if err != nil || len(uid) < 1 {
return command
}
id, err := strconv.Atoi(uid)
if err != nil {
return command
}
if id == os.Getuid() {
return command
}
_, err = os.Stat("/usr/bin/sudo")
if err != nil {
return command
}
result = append(Command{"sudo", "-E", "-u", fmt.Sprintf("#%s", uid)}, command...)
logger.Info().Str("cmd", strings.Join(result, " ")).Msg("sudo hooker result")
return
}
func GetUid(pid int) (uid string, err error) {
status, err := os.Open(fmt.Sprintf("/proc/%d/status", pid))
if err != nil {
return
}
defer func() {
_ = status.Close()
}()
scanner := bufio.NewScanner(status)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "Uid:") {
cols := strings.Split(line, "\t")
if len(cols) > 1 {
uid = strings.TrimSpace(cols[1])
}
break
}
}
return
}