-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
151 lines (136 loc) · 3.54 KB
/
main.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"os"
"strconv"
"github.com/ancat/hypercam/cmd/hypercam"
"github.com/ancat/hypercam/pkg/freezer"
"github.com/urfave/cli/v2"
)
func requirePid(cCtx *cli.Context) (int, error) {
if cCtx.NArg() > 0 {
pid, err := strconv.Atoi(cCtx.Args().Get(0))
if err != nil {
panic(err)
}
return pid, nil
}
return 0, nil
}
func main() {
app := &cli.App{
Flags: []cli.Flag{
},
Commands: []*cli.Command{
{
Name: "pause",
Usage: "pause an entire cgroup by its pid",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pid",
Usage: "process id",
},
},
Action: func(cCtx *cli.Context) error {
pid, _ := requirePid(cCtx)
if pid == 0 {
return cli.Exit("pid missing", 1)
}
cgroup_name, _ := freezer.GetFreezerInfo(pid)
if cgroup_name == "" || cgroup_name == "/" {
return cli.Exit("no cgroup", 1)
}
freezer.UpdateFreezerStateByName(cgroup_name, "FROZEN")
return nil
},
},
{
Name: "unpause",
Usage: "resume an entire cgroup by its pid",
Action: func(cCtx *cli.Context) error {
pid, _ := requirePid(cCtx)
if pid == 0 {
return cli.Exit("pid missing", 1)
}
cgroup_name, _ := freezer.GetFreezerInfo(pid)
if cgroup_name == "" || cgroup_name == "/" {
return cli.Exit("no cgroup", 1)
}
freezer.UpdateFreezerStateByName(cgroup_name, "THAWED")
return nil
},
},
{
Name: "info",
Usage: "view open files and sockets for a given target",
Action: func(cCtx *cli.Context) error {
pid, _ := requirePid(cCtx)
if pid == 0 {
return cli.Exit("pid missing", 1)
}
hypercam.Pr(pid)
return nil
},
},
{
Name: "splice",
Usage: "splice an interactive shell into the target",
Flags: []cli.Flag {
&cli.BoolFlag {
Name: "no-portal",
Usage: "don't create a portal into the container",
},
&cli.StringFlag {
Name: "exec",
Usage: "path to shell executable (default /bin/sh)",
},
&cli.StringFlag {
Name: "exec-from-host",
Usage: "path to shell executable, copied from the host; useful if the target doesn't contain a usable shell. may need to be statically compiled.",
},
},
Action: func(cCtx *cli.Context) error {
pid, _ := requirePid(cCtx)
if pid == 0 {
return cli.Exit("pid missing", 1)
}
use_portals := !cCtx.Bool("no-portal")
host_executable := cCtx.String("exec-from-host")
guest_executable := cCtx.String("exec")
if host_executable != "" && guest_executable != "" {
return cli.Exit("--exec and --exec-from-host are mutually exclusive", 1)
} else if host_executable == "" && guest_executable != "" {
hypercam.SpawnShellInside(pid, use_portals, "", guest_executable)
} else if host_executable != "" && guest_executable == "" {
hypercam.SpawnShellInside(pid, use_portals, host_executable, "")
} else {
// neither is set
hypercam.SpawnShellInside(pid, use_portals, "", "/bin/sh")
}
return nil
},
},
{
Name: "scan",
Usage: "scan a process' stack and heap",
Flags: []cli.Flag {
&cli.BoolFlag {
Name: "hex",
Usage: "print a hex dump instead",
},
},
Action: func(cCtx *cli.Context) error {
pid, _ := requirePid(cCtx)
if pid == 0 {
return cli.Exit("pid missing", 1)
}
hex_dump := cCtx.Bool("hex")
hypercam.DumpMaps(pid, hex_dump)
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
panic(err)
}
}