-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
websites-add-content.go
130 lines (116 loc) · 3.11 KB
/
websites-add-content.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
package websites
import (
"context"
"errors"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"github.com/AlecAivazis/survey/v2"
"github.com/bishopfox/sliver/client/console"
"github.com/bishopfox/sliver/protobuf/clientpb"
"github.com/desertbit/grumble"
)
// WebsitesAddContentCmd - Add static content to a website
func WebsitesAddContentCmd(ctx *grumble.Context, con *console.SliverConsoleClient) {
websiteName := ctx.Flags.String("website")
if websiteName == "" {
con.PrintErrorf("Must specify a website name via --website, see --help\n")
return
}
webPath := ctx.Flags.String("web-path")
if webPath == "" {
con.PrintErrorf("Must specify a web path via --web-path, see --help\n")
return
}
contentPath := ctx.Flags.String("content")
if contentPath == "" {
con.PrintErrorf("Must specify some --content\n")
return
}
contentPath, _ = filepath.Abs(contentPath)
contentType := ctx.Flags.String("content-type")
recursive := ctx.Flags.Bool("recursive")
fileInfo, err := os.Stat(contentPath)
if err != nil {
con.PrintErrorf("Error adding content %s\n", err)
return
}
addWeb := &clientpb.WebsiteAddContent{
Name: websiteName,
Contents: map[string]*clientpb.WebContent{},
}
if fileInfo.IsDir() {
if !recursive && !confirmAddDirectory() {
return
}
webAddDirectory(addWeb, webPath, contentPath)
} else {
webAddFile(addWeb, webPath, contentType, contentPath)
}
web, err := con.Rpc.WebsiteAddContent(context.Background(), addWeb)
if err != nil {
con.PrintErrorf("%s", err)
return
}
PrintWebsite(web, con)
}
func webAddDirectory(web *clientpb.WebsiteAddContent, webpath string, contentPath string) {
fullLocalPath, _ := filepath.Abs(contentPath)
filepath.Walk(contentPath, func(localPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
// localPath is the full absolute path to the file, so we cut it down
fullWebpath := path.Join(webpath, localPath[len(fullLocalPath):])
webAddFile(web, fullWebpath, "", localPath)
}
return nil
})
}
func webAddFile(web *clientpb.WebsiteAddContent, webpath string, contentType string, contentPath string) error {
fileInfo, err := os.Stat(contentPath)
if os.IsNotExist(err) {
return err // contentPath does not exist
}
if fileInfo.IsDir() {
return errors.New("file content path is directory")
}
file, err := os.Open(contentPath)
if err != nil {
return err
}
defer file.Close()
data, err := ioutil.ReadAll(file)
if err != nil {
return err
}
if contentType == "" {
contentType = sniffContentType(file)
}
web.Contents[webpath] = &clientpb.WebContent{
Path: webpath,
ContentType: contentType,
Content: data,
}
return nil
}
func confirmAddDirectory() bool {
confirm := false
prompt := &survey.Confirm{Message: "Recursively add entire directory?"}
survey.AskOne(prompt, &confirm, nil)
return confirm
}
func sniffContentType(out *os.File) string {
out.Seek(0, io.SeekStart)
buffer := make([]byte, fileSampleSize)
_, err := out.Read(buffer)
if err != nil {
return defaultMimeType
}
contentType := http.DetectContentType(buffer)
return contentType
}