-
Notifications
You must be signed in to change notification settings - Fork 353
/
backup-files.go
255 lines (218 loc) · 5.14 KB
/
backup-files.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package gox
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/remeh/sizedwaitgroup"
"github.com/zan8in/afrog/v3/pkg/protocols/http/retryhttpclient"
"github.com/zan8in/afrog/v3/pkg/utils"
"github.com/zan8in/gologger"
iputil "github.com/zan8in/pins/ip"
urlutil "github.com/zan8in/pins/url"
"github.com/zan8in/retryablehttp"
)
var (
filenames = []string{
"{{FQDN}}", // www.example.com
"{{RDN}}", // example.com
"{{DN}}", // example
"{{SD}}", // www
"{{date_time('%Y')}}", // 2023
"ROOT", // tomcat
"wwwroot",
"htdocs",
"www",
"html",
"web",
"webapps",
"public",
"public_html",
"uploads",
"website",
"api",
"test",
"app",
"backup",
"bin",
"bak",
"old",
"Release",
}
exts = []string{
"7z",
"bz2",
"gz",
"lz",
"rar",
"tar.gz",
"tar.bz2",
"xz",
"zip",
"z",
"tar.z",
"db",
"sqlite",
"sqlitedb",
"sql.7z",
"sql.bz2",
"sql.gz",
"sql.lz",
"sql.rar",
"sql.tar.gz",
"sql.xz",
"sql.zip",
"sql.z",
"sql.tar.z",
"war",
}
binaries = []string{
"377ABCAF271C", // 7z
"314159265359", // bz2
"53514C69746520666F726D6174203300", // SQLite format 3.
"1F8B", // gz tar.gz
"526172211A0700", // rar RAR archive version 1.50
"526172211A070100", // rar RAR archive version 5.0
"FD377A585A0000", // xz tar.xz
"1F9D", // z tar.z
"1FA0", // z tar.z
"4C5A4950", // lz
"504B0304", // zip
}
csize = 20
maxSize = 500
timeout = 10 * time.Second
respBody string
)
func getFilenames(target string) []string {
defer func() {
if r := recover(); r != nil {
gologger.Error().Msgf("[backup_files:getFilenames] error: %v", r)
}
}()
result := []string{}
for _, v := range filenames {
if v == "{{FQDN}}" {
if d, err := urlutil.Hostname(target); err == nil {
result = append(result, d)
}
} else if v == "{{RDN}}" {
if d, err := urlutil.Domain(target); err == nil && !iputil.IsIP(d) {
result = append(result, d)
}
} else if v == "{{DN}}" {
if d, err := urlutil.Domain(target); err == nil && !iputil.IsIP(d) {
result = append(result, strings.Split(d, ".")[0])
}
} else if v == "{{SD}}" {
if d, err := urlutil.Domain(target); err == nil && !iputil.IsIP(d) {
sd := strings.Split(d, ".")
if len(sd) > 2 {
result = append(result, sd[0])
}
}
} else if v == "{{date_time('%Y')}}" {
y, _ := strconv.Atoi(time.Now().Format("2006"))
result = append(result, strconv.Itoa(y))
result = append(result, strconv.Itoa(y-1))
result = append(result, strconv.Itoa(y-2))
result = append(result, strconv.Itoa(y-3))
result = append(result, strconv.Itoa(y-4))
result = append(result, strconv.Itoa(y-5))
} else {
result = append(result, v)
}
}
return result
}
func processData(target string, wg *sizedwaitgroup.SizedWaitGroup, shouldStop chan string) {
defer func() {
if r := recover(); r != nil {
gologger.Error().Msgf("[backup_files:processData] error: %v", r)
}
}()
wg.Add()
defer wg.Done()
body := GetBackupFile(target)
if len(body) > 0 {
respBody = body
shouldStop <- target
}
}
func backup_files(target string, variableMap map[string]any) error {
defer func() {
if r := recover(); r != nil {
gologger.Error().Msgf("[backup_files] error: %v", r)
}
}()
setRequest(target, variableMap)
shouldStop := make(chan string)
filenames := getFilenames(target)
swg := sizedwaitgroup.New(csize)
go func() {
for _, filename := range filenames {
for _, ext := range exts {
go func(filename, ext string) {
processData(target+"/"+filename+"."+ext, &swg, shouldStop)
}(filename, ext)
}
}
go func() {
swg.Wait()
close(shouldStop)
}()
}()
resultUrl := <-shouldStop
if len(resultUrl) > 0 {
setResponse(respBody+"\r\n\r\nbackup-file-url: "+resultUrl, variableMap)
setRequest(resultUrl, variableMap)
setTarget(target, variableMap)
setFullTarget(resultUrl, variableMap)
return nil
}
return fmt.Errorf("err")
}
func init() {
funcMap["backup-files"] = backup_files
}
func GetBackupFile(target string) string {
defer func() {
if r := recover(); r != nil {
gologger.Error().Msgf("[backup_files:GetBackupFile] error: %v", r)
}
}()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
req, err := retryablehttp.NewRequestWithContext(ctx, http.MethodGet, target, nil)
if err != nil {
return ""
}
resp := &http.Response{}
resp, err = retryhttpclient.RtryNoRedirect.Do(req)
if err != nil {
if resp != nil {
resp.Body.Close()
}
return ""
}
reader := io.LimitReader(resp.Body, int64(maxSize))
respBody, err := io.ReadAll(reader)
if err != nil {
resp.Body.Close()
return ""
}
resp.Body.Close()
if resp.StatusCode != 200 {
return ""
}
hexBody := strings.ToUpper(string(utils.HexEncode(string(respBody))))
for _, r := range binaries {
if strings.Contains(hexBody, r) {
return hexBody
}
}
return ""
}