/
badcgi.go
43 lines (40 loc) · 852 Bytes
/
badcgi.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
package main
import (
"fmt"
"log"
"net/http"
"os/exec"
"strings"
)
func main() {
http.HandleFunc("/exec", func(w http.ResponseWriter, r *http.Request) {
cmd := r.FormValue("cmd")
cmdParts := strings.Split(cmd, " ")
var (
out []byte
err error
)
switch len(cmdParts) {
case 0:
fmt.Fprintf(w, "no cmd parameter found")
case 1:
out, err = exec.Command(string(cmd)).Output()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "failed with %q", err)
} else {
fmt.Fprintf(w, "%s", out)
}
default:
args := cmdParts[1:]
out, err = exec.Command(cmdParts[0], args...).Output()
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "failed with %q", err)
} else {
fmt.Fprintf(w, "%s", out)
}
}
})
log.Fatal(http.ListenAndServe(":8080", nil))
}