-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (117 loc) · 3.14 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
// ProcResult holds information and metadata about a /proc path
type ProcResult struct {
Path string `json:"path"`
Files []string `json:"files,omitempty"`
Dirs []string `json:"dirs,omitempty"`
Contents *string `json:"contents,omitempty"`
Err string `json:"err,omitempty"`
Mode string `json:"mode,omitempty"`
}
var bufmax = 1024 * 1024 * 4
var dirmax = 1024
func readFile(path string) (contents *string, err error) {
fd, err := os.Open(path)
if err != nil {
return
}
buf := make([]byte, bufmax)
length, err := fd.Read(buf)
if err != nil {
return
}
contents = new(string)
*contents = string(buf[:length])
return
}
func readDir(path string) (files, dirs []string, err error) {
fd, err := os.Open(path)
if err != nil {
return
}
direntries, err := fd.Readdir(dirmax)
if err != nil {
return
}
// Not ideal
files = make([]string, 0, len(direntries))
dirs = make([]string, 0, len(direntries))
for _, literal := range direntries {
if literal.IsDir() {
dirs = append(dirs, literal.Name())
} else {
files = append(files, literal.Name())
}
}
return
}
func vetPath(path string) (string, error) {
if strings.Contains(path, "..") {
return "", errors.New("directory traversal attempt detected")
}
finalPath := filepath.Join("/proc", path)
cleanedFinalPath, err := filepath.EvalSymlinks(finalPath)
if err != nil {
return "", err
}
if cleanedFinalPath == "/proc" || strings.HasPrefix(cleanedFinalPath, "/proc/") {
return cleanedFinalPath, nil
}
return "", errors.New(fmt.Sprint("Symlink traversal attempt detected from ", cleanedFinalPath))
}
func readProcPath(path string) (rval *ProcResult) {
cleanedPath, err := vetPath(path)
rval = &ProcResult{Path: cleanedPath}
if err != nil {
rval.Err = err.Error()
return
}
fileinfo, err := os.Stat(cleanedPath)
if err != nil {
rval.Err = err.Error()
return
}
rval.Mode = fileinfo.Mode().String()
if fileinfo.Mode().IsRegular() {
rval.Contents, err = readFile(cleanedPath)
} else if fileinfo.Mode().IsDir() {
rval.Files, rval.Dirs, err = readDir(cleanedPath)
}
if err != nil {
rval.Err = err.Error()
}
return
}
func jsonHandler(w http.ResponseWriter, r *http.Request) {
b := readProcPath(r.URL.Path)
bStr, err := json.Marshal(*b)
if err != nil {
log.Println("marshalling error", err, b)
}
if err != nil || b.Err != "" {
w.WriteHeader(http.StatusInternalServerError)
}
fmt.Fprintf(w, string(bStr))
}
func main() {
listen := flag.String("listen", ":9234", "What to listen on- you should prefer to bind to a local interface, like 10.0.1.3:9234")
flag.IntVar(&bufmax, "file-limit", bufmax, "Maximum amount of files to read")
flag.IntVar(&dirmax, "dir-limit", dirmax, "Maximum number of directory entries to read")
flag.Parse()
http.HandleFunc("/", jsonHandler)
log.Fatal(http.ListenAndServe(*listen, nil))
}