forked from open-falcon/falcon-plus
/
run.go
41 lines (35 loc) · 816 Bytes
/
run.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
package http
import (
"github.com/open-falcon/falcon-plus/modules/agent/g"
"github.com/toolkits/sys"
"io/ioutil"
"net/http"
)
func configRunRoutes() {
http.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request) {
if !g.Config().Http.Backdoor {
w.Write([]byte("/run disabled"))
return
}
if g.IsTrustable(r.RemoteAddr) {
if r.ContentLength == 0 {
http.Error(w, "body is blank", http.StatusBadRequest)
return
}
bs, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
body := string(bs)
out, err := sys.CmdOutBytes("sh", "-c", body)
if err != nil {
w.Write([]byte("exec fail: " + err.Error()))
return
}
w.Write(out)
} else {
w.Write([]byte("no privilege"))
}
})
}