-
Notifications
You must be signed in to change notification settings - Fork 4
/
rootsh.go
57 lines (45 loc) · 991 Bytes
/
rootsh.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
package main
import (
"bytes"
"flag"
"io"
"log/syslog"
"os"
"os/exec"
"github.com/kr/pty"
"golang.org/x/crypto/ssh/terminal"
)
var (
logfile string
rsyslog string
)
func main() {
flag.StringVar(&logfile, "logfile", "./log.txt", "Path to the log file.")
flag.StringVar(&rsyslog, "syslog", "127.0.0.1:512", "Remote syslog address.")
flag.Parse()
cmd := exec.Command("/bin/bash")
tty, err := pty.Start(cmd)
if err != nil {
panic(err)
}
defer tty.Close()
oldState, err := terminal.MakeRaw(0)
if err != nil {
panic(err)
}
defer terminal.Restore(0, oldState)
file, err := os.OpenFile(logfile, os.O_RDWR|os.O_APPEND, 0775)
if err != nil {
panic(err)
}
defer file.Close()
logwriter, err := syslog.Dial("udp", rsyslog, syslog.LOG_DEBUG, "rootsh")
if err != nil {
panic(err)
}
defer logwriter.Close()
l := &Logger{logwriter: logwriter, buffer: &bytes.Buffer{}}
mw := io.MultiWriter(os.Stdout, file, l)
go io.Copy(tty, os.Stdin)
io.Copy(mw, tty)
}