-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellapi.go
More file actions
57 lines (50 loc) · 1.45 KB
/
Copy pathshellapi.go
File metadata and controls
57 lines (50 loc) · 1.45 KB
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
// This tool exposes any binary (shell command/script) as an HTTP service.
// A remote client can trigger the execution of the command by sending
// a simple HTTP request. The output of the command execution is sent
// back to the client in plain text format.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
)
func main() {
binary := flag.String("b", "", "Path to the executable binary")
port := flag.Int("p", 8080, "HTTP port to listen on")
flag.Parse()
if *binary == "" {
fmt.Println("Path to binary not specified.")
return
}
l := log.New(os.Stdout, "", log.Ldate|log.Ltime)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
var argString string
if r.Body != nil {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
l.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
argString = string(data)
}
fields := strings.Fields(*binary)
args := append(fields[1:], strings.Fields(argString)...)
l.Printf("Command: [%s %s]", fields[0], strings.Join(args, " "))
output, err := exec.Command(fields[0], args...).Output()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/plain")
w.Write(output)
})
l.Printf("Listening on port %d...", *port)
l.Printf("Exposed binary: %s", *binary)
http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", *port), nil)
}