forked from dsaveliev/rootsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rootsh.go
82 lines (70 loc) · 1.69 KB
/
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
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
package main
import (
"flag"
"io"
"log"
"log/syslog"
"os"
"os/exec"
"bufio"
"fmt"
"strings"
"bytes"
"github.com/kr/pty"
"golang.org/x/crypto/ssh/terminal"
)
var (
logfile string
rsyslog string
)
func main() {
fmt.Println("WARNING: Only authorized user access is allowed. All activity in this session will be monitored and evaluated.")
conffile, err := os.Open("/etc/rootsh.conf")
if err != nil {
log.Fatal(err)
}
defer conffile.Close()
var nlogfile string
var rootshell string
nlogfile="/dev/null"
rootshell="/bin/bash"
scanner := bufio.NewScanner(conffile)
for scanner.Scan() {
if (strings.Contains(scanner.Text(), "logfile=")) {
nlogfile=strings.Split(scanner.Text(), "=")[1]
}
if (strings.Contains(scanner.Text(), "shell=")) {
rootshell=strings.Split(scanner.Text(), "=")[1]
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
flag.StringVar(&logfile, "logfile", nlogfile, "Path to the log file.")
flag.Parse()
cmd := exec.Command(rootshell)
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.New(syslog.LOG_INFO, "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)
}